///Inzu.net © 2012 - All rights reserved var osFunction_dialogInstance = null; function osFunction(options, callback, nvp){ this.bypass_dialog = false; //Assign options to object properties for (var key in options) { if (options.hasOwnProperty(key)) { this[key] = options[key]; } } //Override standard callback if (callback in osFunction.prototype) { this.callback = osFunction.prototype[callback]; } else if ( callback !== undefined) { this.callback = callback; } //Add optional nvp string for posting to controller this.nvp = ""; for (var key in nvp) { if (nvp.hasOwnProperty(key)) { this.nvp += "&" + key + "=" + nvp[key]; } } if ( this.sector == undefined ) this.sector = this.node; this.html = null; this.apply = null; this.http = getHTTPObject(); //Close open dialog if ( osFunction_dialogInstance !== null ){ document.body.removeChild(osFunction_dialogInstance.dialog); osFunction_dialogInstance = null; } //Log instance of osFunction if ( this.bypass_dialog == false ){ osFunction_dialogInstance = this; } //Default behaviour for enter key this.enterKey = this.action; } ///ACTION osFunction.prototype.action = function(bypassData){ var val = ''; if ( this.direct_callback == true ) { if (typeof this.callback == 'function') this.callback(this); this.closeDialog(); return; } if ( this.bypass_dialog ) { //bypass user input async = false; if ( bypassData != undefined ) val = bypassData; } else { async = true; //Format form input data appended to dialog into a nvp string if ( this.append != undefined ) { var d = document.getElementById('dialog_input').getElementsByTagName('input'); for (var i=0; i < d.length; i++) { if ( ( d[i].type == "checkbox" && d[i].checked) || d[i].type != "checkbox" ) { val += "&" +d[i].name + "=" + encodeURIComponent(d[i].value); } } d = document.getElementById('dialog_input').getElementsByTagName('select'); for (var i=0; i < d.length; i++){ val += "&" + d[i].name + "=" + encodeURIComponent(d[i].options[d[i].selectedIndex].value); } } } var Obj = this; ///Send data via AJAX var params = "CSFR_token=" + CSFR_token + "§or=" + this.sector + "&procedure=" + this.procedure + "&node=" + this.node + "&v=" + this.v + "&task=" + this.task + "&id=" + this.id + val + this.nvp; this.http.open("POST", "/admin/lib/os/action.html" , async); this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); if ( this.direct ) { this.http.send(params); if (typeof this.callback == 'function') this.callback(this); if ( this.feedback !== "undefined" ) Obj.feedbackDialog(); if ( !this.bypass_dialog && this.feedback === undefined ) this.closeDialog(); } else if ( async ) { this.http.onreadystatechange = function ajaxResponse() { if ( Obj.http.readyState == 4 ) { if ( Obj.http.responseText != "" ) { Obj.response = JSON.parse(Obj.http.responseText); if ( Obj.response[0] == "demo" ) { document.getElementById('dialog_main').innerHTML = '

Sorry you can\'t do this in demo mode!

' + document.getElementById('dialog_main').innerHTML; eventClass.addEventListener( document.getElementById(Obj.cancel), "click", Obj.closeDialog, Obj); } else if ( Obj.response[0] == "callback" && typeof Obj.callback == 'function'){ Obj.callback(Obj,Obj.response); } else if ( Obj.response[0] == "complete" || Obj.response.complete == "true" || (Obj.callbackData && Obj.response[0] != "" ) ){ if ( typeof Obj.callback == 'function') Obj.callback(Obj,Obj.response); //Must come before next line as callback used to set feedback text sometimes if ( Obj.feedback !== "undefined" ) Obj.feedbackDialog(); if ( !Obj.bypass_dialog && Obj.feedback == undefined ){ Obj.closeDialog(); } } else if ( Obj.response[0] == "error" || Obj.response[0] === undefined ) { if ( Obj.response[1] === undefined ) { Obj.error("An unknown error occcured!"); } else { Obj.error(Obj.response[1]); } } } else { var d = document.getElementById("dialog_error"); if ( d != undefined ) d.parentNode.removeChild(d); document.getElementById('dialog_main').innerHTML = '

Sorry there was a problem, please try again!

'+document.getElementById('dialog_main').innerHTML; eventClass.addEventListener( document.getElementById(Obj.cancel), "click", Obj.closeDialog, Obj); eventClass.addEventListener( document.getElementById(Obj.apply), "click", Obj.action, Obj); } } } this.http.send(params); } else { this.http.send(params); if ( this.http.responseText != "" ){ console.log(this.http.responseText); Obj.response = JSON.parse(this.http.responseText); if ( Obj.response[0] == "complete" || Obj.response.complete == "true" ||( Obj.callbackData && Obj.response[0] != "" ) ){ if (typeof Obj.callback == 'function') Obj.callback(Obj,Obj.response); } } } } ////DIALOG osFunction.prototype.dialogGen = function(options){ //Assign options to properties for (var key in options) { if (options.hasOwnProperty(key)) { this[key] = options[key]; } } if ( this.dialog_title == undefined ) this.dialog_title = 'Please confirm'; if ( typeof this.dialog_text === "function" ) { //Custom dynamic text this.dialog_text = this.dialog_text(this.task); } else if ( this.dialog_text !== undefined ) { //Custom static text } else if ( this.dialog_text == undefined ) { //Generic dynamic text for entries if ( this.id !== undefined ) { this.entry_title = document.getElementById(this.task + '_' + this.id + '_name').innerHTML; this.dialog_text = 'Are you sure you want to ' + this.procedure + ' the entry "' + this.entry_title + '"?'; } else { this.dialog_text = ""; } } this.dialog_text = '
' + this.dialog_text + '
'; //Create dialog t = new Date().getTime(); n = 'Dialog_' + t; this.cancel = 'Cancel_' + t; this.apply = 'Apply_' + t; d = document.createElement('DIV'); d.id = n; d.className = 'dialog'; this.html = '
'+this.dialog_title+'
'+ '
' + this.dialog_text; if ( this.append != undefined ) this.html += this.append; this.html+='
cancel
confirm
'; d.innerHTML = this.html; f=document.body.firstChild; document.body.insertBefore(d,f); eventClass.addEventListener( document.getElementById(this.cancel), "click", this.closeDialog, this); eventClass.addEventListener( document.getElementById(this.apply), "click", this.action, this); if (document.getElementById("dialog_val") != undefined ) document.getElementById("dialog_val").focus(); //Perform action with "enter" key press eventClass.addEventListener( document, "keydown", this.keyEvent, this); //Optional trigger function when dialog is ready if (typeof this.ready == 'function') { this.ready(this); } this.dialog = d; } ///APPEND FORM TO DIALOG osFunction.prototype.formAppend = function( formObj ){ this.append = '
'; for (var key in formObj) { //This needs development to handle various input types e.g select this.append += '
' + formObj[key].title + '
'; } this.append += '
'; } ///CLOSE DIALOG osFunction.prototype.closeDialog = function() { eventClass.removeEventListener( document, "keydown", this.keyEvent, this); document.body.removeChild(this.dialog); osFunction_dialogInstance = null; } ///FEEDBACK DIALOG osFunction.prototype.feedbackDialog = function() { d = document.getElementById("dialog_main"); while (d.firstChild) { d.removeChild(d.firstChild); } t = new Date().getTime(); this.close_w = 'Close_' + t; html = '
' + this.feedback +'
ok
'; d.innerHTML = html; eventClass.addEventListener( document.getElementById(this.close_w), "click", this.closeDialog, this); this.enterKey = this.closeDialog; } ///CREATE TABLE ROW osFunction.prototype.add=function(obj,id,val,position) { var row= document.getElementById(obj.task+"_"+position); if(position=="table"){ newRow = row.insertRow(0); }else{ newRow = row.parentNode.insertRow(0); } newRow.setAttribute("id",obj.task+'_'+id); structure=window['oS_build_'+obj.task]; for(var i=0; iCreate a new '+task+'

'; }; this.append='
Name
'; this.callback=function(obj, response){ obj.add(obj,response[1],response[2],'table'); if(obj.complete!=undefined){ obj.complete(); } } this.dialogGen(); } ////REMOVE osFunction.prototype.remove=function() { this.dialog_title='Delete entry'; this.callback=function(obj,response){ entry=document.getElementById(obj.task+'_'+obj.id); entry.parentNode.removeChild(entry); if(obj.complete!=undefined){ obj.complete(); } } this.dialogGen(); } ///RENAME osFunction.prototype.rename=function(){ this.dialog_title='Rename entry'; this.append='
Rename
'; this.callback=function(obj,response){ t = document.getElementById(obj.task+'_'+obj.id); t.cells[0].innerHTML=response[2]; if(obj.complete!=undefined){ obj.complete(); } } this.dialogGen(); } ///COPY osFunction.prototype.copy = function() { this.dialog_title = 'Copy entry'; this.append = '
Name
'; this.callback = function(obj, response) { obj.add(obj,response[1],response[2],obj.id); if ( obj.complete != undefined ) obj.complete(); } this.dialogGen(); } ///EDIT osFunction.prototype.edit = function() { osFunction_dialogInstance = null; // Or instance is preserved on browser back window.location = this.task + '.html?'+ this.task + '_id=' + this.id; } ///ERROR osFunction.prototype.error = function(error) { var d = document.getElementById("dialog_error"); if ( d != undefined ) d.parentNode.removeChild(d); var p = document.createElement("p"); var textnode = document.createTextNode(error); p.id = "dialog_error"; p.className = "dialog_error"; p.appendChild(textnode); var d = document.getElementById("dialog_main"); d.insertBefore(p, d.childNodes[0]); } ///KEY EVENT osFunction.prototype.keyEvent = function(e) { var KeyID = e.keyCode ? e.keyCode : e.keyCode; if ( KeyID == 13 ) this.enterKey(); }