You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openNG/public_html/static/js/openng.js

173 lines
3.8 KiB
JavaScript

var jsde_creation_hook = function(win)
{
/* This function is a hook that is called after each creation of
* a JSDE window. */
}
var jsde_contents_hook = function(win)
{
/* This function is a hook that is called after each time
* content is set in a JSDE window. */
placeHooksForWindow(win);
}
notification_popups = [];
error_popups = [];
function hookSubmitEvent(form, callback, error)
{
/* Hooks a form to be submitted via AJAX, executing the given
* callback if the request succeeds.
*
* form: a jQuery object containing one or more form elements
* callback: the function to run after a successful request
* error: optionally, a callback function to run when the
* request fails
*/
form.each(function(index){
var element = $(this);
var method = element.attr("method");
var target = element.attr("action");
if(typeof method !== "undefined")
{
method = method.toUpperCase();
}
else
{
method = "GET";
}
element.submit(function(){
var submit_button = element.find("button[type=submit]");
var submit_icon = submit_button.data("submit-icon");
if(typeof submit_icon !== "undefined")
{
/* First we will try to replace an existing icon
* in the button. If there is no icon yet, the
* entire contents of the button will be replaced
* with the submission icon. */
var current_icon = submit_button.find("i");
if(current_icon.length == 0)
{
submit_button.html("<span style='text-align: center;'><i></i></span>");
current_icon = element.find("i");
}
current_icon.removeClass().addClass(submit_icon);
}
var formdata = element.serialize();
console.log(formdata);
$.ajax({
type: method,
url: target,
data: formdata,
dataType: "json",
success: function(data, xhr){ console.log(data); callback(element, data, xhr); },
error: function(data, xhr, err){ error(element, data, xhr, err); }
});
return false;
});
});
}
function placeHooksForWindow(win)
{
console.log();
$(win._outer).find("form").each(function(){
var callback = $(this).data("hook-callback");
if(typeof callback !== "undefined")
{
console.log("Hooking", this, "using callback", callback);
hookSubmitEvent($(this), window[callback]);
}
});
}
function callbackNodeCreated(form, data)
{
if(data.result == "success")
{
spawnNotification(data.message);
var node_id = data.node_id;
form.getWindow().Close();
new JsdeWindow({
width: 480,
height: 300,
x: 100,
y: 100,
title: "Node lookup",
contents: "Loading...",
source_url: "/nodes/" + node_id
});
}
else if(data.result == "error")
{
spawnError(data.message);
}
}
function spawnNotification(message)
{
var popup = spawnPopup(message, "notification");
notification_popups.push(popup);
}
function spawnError(message)
{
var popup = spawnPopup(message, "error");
error_popups.push(popup);
}
function spawnPopup(message, template)
{
var popup = $("#jsde_templates").find(".template_" + template).clone().removeClass("template_notification template_error");
popup.find(".message").html(message.replace("\n", "<br>"));
popup.hide();
popup.prependTo("#notification_area");
popup.fadeIn(300).wait(5000).fadeOut(400, $).remove();
return popup
}
$(function(){
hookSubmitEvent($("#form_search"));
$("#button_toolbar_addnode").click(function(){
new JsdeWindow({
width: 320,
height: 400,
x: 40,
y: 40,
title: "Create new node",
contents: "Loading...",
source_url: "/nodes/create",
noscroll: true
});
});
/* Intro screen */
new JsdeWindow({
width: 640,
height: 480,
x: ($(window).width() / 2) - (640 / 2),
y: ($(window).height() / 2) - (480 / 2),
title: "Welcome!",
contents: "Loading...",
source_url: "/intro"
});
spawnNotification("Test notification");
spawnError("Test error");
});