// setup namespace var Zymonic = { system_name: null, version: null, token: null, last_authenticated: null, username: null, request_logging: false, lookup: {}, ajax_requests: {}, ajax_request_type: { PROCESS: 'process', FILTER: 'filter', PING: 'ping', TOOLKIT: 'toolkit', NOTSET: 'notset', }, ajax_request_type_check: {}, current_editor: '', temp_block_id: 0, notifications_hidden: true, hidden_fap_items: false, menu_search_ident: 'zd_menu_', min_search_length: 2, menu_search_results: 10, menu_search_key: 'zd_menu_request_key', menu_filter: 'zz_admin_area_menu', menu_search_field: 'FoFapSearchField', last_search: '', location_block_orders: {}, script_includes: [], stylesheet_includes: [], BlockRefreshTimers: [], longestBlock: { block_id: 0, width: 0, height: 0 }, relogin_running: false, relogin_sspids: new Array, relogin_timeout: null, session_expiry: null, relogin_window:1800, relogin_trigger: null, otp_available: false, single_block_mode: false, }; for (var request_type in Zymonic.ajax_request_type) { Zymonic.ajax_request_type_check[Zymonic.ajax_request_type[request_type]] = true; } Zymonic.log_includes = function() { // get list of existing scripts $("head > script").each(function() { if ($(this).attr("type") == "text/javascript") { var script_file = $(this).attr("src"); var found = 0 for (var i = 0; i < Zymonic.script_includes.length; i++) { if (Zymonic.script_includes[i] == script_file) { found = 1; } } if (found == 0) { Zymonic.script_includes.push(script_file); } } }); // get list of existing stylesheets $("head > link").each(function() { if ($(this).attr("type") == "text/css" && $(this).attr("rel") == "stylesheet") { var stylesheet_file = $(this).attr("href"); var found = 0; for (var i = 0; i < Zymonic.stylesheet_includes.length; i++) { if (Zymonic.stylesheet_includes[i] == stylesheet_file) { found = 1; } } if (found == 0) { Zymonic.stylesheet_includes.push(stylesheet_file); } } }); }; Zymonic.InitialiseSortable = function(Zlocation, base_url) { var location = "#SLocation_" + Zlocation; var list = ".List_" + Zlocation; $(document).ready(function() { Zymonic.log_includes(); $(location).sortable({ handle: '.DragAction', start: function(event, ui){ // Store current position... ui.item.data("old_pos", $(">*", location).index(ui.item)) }, stop : function (event, ui) { // Store new position... var new_position= $(">*", location).index(ui.item); // Find our
  • id... var temp_string = $(ui.item).attr('id'); var li_split_array = temp_string.split("_"); var block_id = li_split_array[1]; // params we need var form_data = Zymonic.new_form_data(); form_data.append("ZZchangedblocks_"+Zlocation, block_id); form_data.append("ZZblockchange_"+Zlocation+"_"+block_id+"_position", new_position); form_data.append("ZZupdate_page_only", "Y"); // No JS action needs taking after successful receipt server side Zymonic.request("POST", form_data); } }); }); }; Zymonic.InitialiseDesktop = function(system_name, version, last_authenticated) { /* set the system name and version*/ Zymonic.system_name = system_name; Zymonic.version = version; $(document).ready(function() { /* Get the list of existing scripts */ Zymonic.log_includes(); /* Load the Combined XSL */ $.ajax({ url: window.BlockCombinedXSL, type: "GET", async: true, success: function(xsl) { Zymonic.combinedXSL = xsl; }, error: function(xhr, error) { console.log(error); }, }); }); Zymonic.last_authenticated = last_authenticated; Zymonic.desktop_mode = true; // Setup menus $('#main-menu').smartmenus({hideOnClick:false}); // Set-up notification area $('#DesktopNotices').hide(); $('#notification_label').click(function() { Zymonic.notificationsClicked(); }); // Setup searching if($('#ZZsearch').length) { $('#ZZsearch').on('keyup', function(e) { Zymonic.doFapSearch(); }); $('#ZZsearch').on('keyup keypress', function(e) { var keyCode = e.keyCode || e.which; if (keyCode === 13) { e.preventDefault(); return false; } }); } }; Zymonic.authenticated = function() { if(this.last_authenticated == 'Y') { return true; } else { return false; } }; Zymonic.getPreviousBlockOrder = function(location, default_order) { if (!Zymonic.location_block_orders.location) { // add one here as the return value will decrement it Zymonic.location_block_orders[location] = { min: default_order + 1 }; } if (Zymonic.location_block_orders[location].hasOwnProperty('min')) { // add one here as the return value will decrement it Zymonic.location_block_orders[location].min = default_order + 1; } return --Zymonic.location_block_orders[location].min; }; Zymonic.getNextBlockOrder = function(location, default_order) { if (!Zymonic.location_block_orders.location) { // minus one here as the return value will increment it Zymonic.location_block_orders[location] = { max: default_order + 1 }; } if (Zymonic.location_block_orders[location].hasOwnProperty('max')) { // minus one here as the return value will decrement it Zymonic.location_block_orders[location].max = default_order + 1; } return ++Zymonic.location_block_orders[location].max; } Zymonic.addOpenBlock = function(block) { if(!$('#open_block_' + block.block_id ).length) { var open_entry = '
  • ' + block.label + '
  • '; $('#open_block_list').append(open_entry); $('#open_block_' + block.block_id + ' a').click(function() { block.openListEntryClicked(); }); $('#open_block_' + block.block_id + ' a').dblclick(function() { block.openListEntryDblClicked(); }); } }; Zymonic.removeOpenBlock = function(block) { $('#open_block_' + block.block_id).remove(); }; Zymonic.addFakeOpenBlock = function(block_id) { var open_entry = '
  • Opening...
  • '; $('#open_block_list').append(open_entry); }; Zymonic.removeFakeOpenBlock = function(block_id) { if(Zymonic.desktop_mode) { $('#open_block_' + block_id).remove(); } }; Zymonic.getDesktopSize = function() { var size = {}; // Window appears slightly too big - hence the reduction by 10 & 20px size.width = $('#Content').width() - 10; size.height = $(window).height() - 20 - $("#DesktopStatusbar").height() - $("#DesktopNotificationsbar").height(); return size; } Zymonic.getLeftTop = function() { var size = {}; size.top = $("#DesktopStatusbar").height() - 40; size.left = 0; return size; } Zymonic.blockSizeChangeHandler = function(block_id, width, height) { if(Zymonic.longestBlock.height < height) { Zymonic.longestBlock.height = height; Zymonic.longestBlock.block_id = block_id; Zymonic.longestBlock.width = width; // Ensure the desktop is longer than the largest block $("#DesktopLocations").css({ height: (height+300) }); } } Zymonic.getFrontBlockId = function(ignore_id) { var highest = 0; var frontblock = null; $('.DesktopBlock').each(function() { var zind = parseInt($(this).css("zIndex"), 10); var frontblock_id = $(this).attr("id"); var frontblock_arr = frontblock_id.split("_"); if(zind > highest && frontblock_arr[1] !== ignore_id) { highest = zind; frontblock = frontblock_arr[1]; } }); return frontblock; } Zymonic.activateMenuBranch = function(branch) { $('#main-menu').smartmenus("enable",branch); $('#main-menu').smartmenus("itemActivate",branch); }; Zymonic.MenuHideAll = function() { $('#main-menu').smartmenus('menuHideAll'); }; Zymonic.emptySearchResults = function() { $('#searchresults').empty(); $('#search_result_count').html('...'); $('#searchresults').append('
  • No results
  • '); Zymonic.MenuHideAll(); }; Zymonic.doFapSearch = function() { var search = $('#ZZsearch').val().toLowerCase(); var results = []; if(search !== Zymonic.last_search) { Zymonic.emptySearchResults(); } if(search) { // Fire off a request that will return results // or a count if too many results if(search.length > Zymonic.min_search_length) { var form_data = Zymonic.new_form_data(); form_data.append("ZZwebservicemode", "filter"); form_data.append("ZZBzname", Zymonic.menu_filter); form_data.append("ZZFident", Zymonic.menu_search_ident); if(Zymonic.hidden_fap_items) { form_data.append(Zymonic.menu_search_ident + 'zz_search_hidden', 'Y'); } form_data.append(Zymonic.menu_search_ident + 'search','Show Results'); form_data.append(Zymonic.menu_search_ident + 'results_per_page', Zymonic.menu_search_results); form_data.append(Zymonic.menu_search_ident + 'report_only','true'); form_data.append(Zymonic.menu_search_ident + Zymonic.menu_search_field, search); Zymonic.request( "POST", form_data, true, Zymonic.menu_search_key, Zymonic.ajax_request_type.FILTER, function(xml) { $(xml).find("session_errors").each(function() { Zymonic.addNotification($(this).text()); }); var count = $(xml).find("FilterResultCount Count").text(); $('#search_result_count').html(count); if(count > 0) { $('#searchresults').empty(); $(xml).find("result").each(function() { var js = "Zymonic.openBlock(\"" + $(this).find("zname Value").text() + "\",0,\"" + $(this).find("fap_type Value").text() + "\");"; var searchresult = "
  • " + $(this).find("fap_display_name Value").text() + "
  • "; $('#searchresults').append(searchresult); }); Zymonic.activateMenuBranch($('#searchresultmenu')) } else { Zymonic.emptySearchResults(); } }, function(xml) { Zymonic.emptySearchResults(); $(xml).find("error").each(function() { Zymonic.log("Error code: " + $(this).find("code").text() + " - Error text: " + $(this).find("description").text() ); }); } ); } } Zymonic.last_search = search; }; Zymonic.notificationsClicked = function() { if(Zymonic.notifications_hidden) { $('#notificationcount').removeClass('new_notifications'); $('#DesktopNotices').show(); Zymonic.notifications_hidden = false; } else { $('#notificationcount').removeClass('new_notifications'); $('#DesktopNotices').hide(); Zymonic.notifications_hidden = true; } }; Zymonic.addNotification = function(notification) { if(!Zymonic.desktop_mode) { return; } // Get the count of notifications var count = parseInt($('#notificationcount').text()); count = count + 1; // Add a close all on adding the first notification if( count == 1 ) { Zymonic.addCloseAllNotifications(); } // Generate a random id for the notification const now = Date.now(); var random = Math.floor(Math.random() * 1000); var id = now.toString() + '_' + random.toString(); // for sqlite debug message, wrap in link to open debug gui if debug gui is enabled var message = notification; var debug_file = RegExp('^Debugs being written to : (.*)$').exec(message); if (Zymonic.debug_gui_enabled && debug_file && debug_file[1] && debug_file[1].indexOf('.sqlite') != -1) { var version_details = Zymonic.getVersionDetails(); var debug_file_name = RegExp("([^/]+.sqlite)$").exec(message); var url = "/zymonicdebugs" + "?file=" + debug_file_name[1] + "&branch=" + version_details.branch + "&zymonic_rev=" + version_details.zymonic_rev + "&zcps_rev=" + version_details.zcps_rev ; message = "Debugs being written to: " + debug_file[1] + ""; } // Add the notification var close = " X"; $('#notificationcount').text(count.toString()); $('#notificationcount').addClass('new_notifications'); $('#DesktopNotices').prepend("
  • " + message + close + "
  • "); }; Zymonic.removeNotification = function(id) { $('#' + id).remove(); var count = parseInt($('#notificationcount').text()); count = count - 1; $('#notificationcount').text(count.toString()); // Remove close all if there are no notifications left if( count == 0 ) { Zymonic.removeCloseAllNotifications(); } }; Zymonic.closeAllNotifications = function() { $('#DesktopNotices').empty(); $('#notificationcount').text("0"); $('#DesktopNotices').hide(); Zymonic.notifications_hidden = true; } Zymonic.addCloseAllNotifications = function() { close_all = "
  • Dismiss All Notifications
  • "; $('#DesktopNotices').append(close_all); }; Zymonic.removeCloseAllNotifications = function() { $('#close_all_notifications').remove(); }; Zymonic.internalSearch = function(setting) { if(setting == true) { Zymonic.hidden_fap_items = true; $('#_page_menu_item_authentication_status_internal_search').text("No Internal Functions in Search"); $('#_page_menu_item_authentication_status_internal_search').off().click(function() { Zymonic.internalSearch(false); }); } else { Zymonic.hidden_fap_items = false; $('#_page_menu_item_authentication_status_internal_search').text("Search Internal Functions"); $('#_page_menu_item_authentication_status_internal_search').off().click(function() { Zymonic.internalSearch(true); }); } }; Zymonic.enableDebugs = function() { var form_data = Zymonic.new_form_data(); form_data.append("ZZdebug", "true"); form_data.append("ZZwebservicemode", "login"); form_data.append("ZZno_display_attributes", "false"); Zymonic.request( "POST", form_data, true, null, null, function(xml) { $(xml).find("session_errors").each(function() { Zymonic.addNotification($(this).text()); }); // check for the debug gui flag Zymonic.debug_gui_enabled = ( $(xml).find("Zymonic").attr("debug_gui_enabled") == 'true' ); // setup the disable button $('#_page_menu_item_authentication_status_debugs').text("Disable Debugs"); $('#_page_menu_item_authentication_status_debugs').off().click(function() { Zymonic.disableDebugs(); }); } ); }; Zymonic.disableDebugs = function() { var form_data = Zymonic.new_form_data(); form_data.append("ZZdebug", "false"); form_data.append("ZZwebservicemode", "login"); form_data.append("ZZno_display_attributes", "false"); Zymonic.request( "POST", form_data, true, null, null, function(xml) { $('#_page_menu_item_authentication_status_debugs').text("Enable Debugs"); $('#_page_menu_item_authentication_status_debugs').off().click(function() { Zymonic.enableDebugs(); }); Zymonic.addNotification("Debugs disabled."); } ); }; Zymonic.addOpenBlockParam = function(extras, location, key, value) { if(Zymonic.desktop_mode) { location = 'desktop'; } extras["ZZblockchange_" + location + "_new_" + key] = value; }; Zymonic.openBlock = function( process_filter_zname, process_id, type, location, extras, completed_cb ) { // Temp block generation via some simple HTML var temp_block = "temp_loading_id_" + Zymonic.temp_block_id; var line_1 = '
    Loading block...

    '; var br = '
    '; var img_src = ''; var line_2 = '

    '; var result = ""; var message = "Loading..."; var block_html = line_1 + br + img_src + message + line_2; var location_selector = '#SLocation_' + location; // Desktop mode if(Zymonic.desktop_mode) { location = "desktop"; location_selector = '#DesktopLocations'; block_html = '
    '; Zymonic.addFakeOpenBlock( temp_block ); } // Create a temporary 'li' element var tempBlock = document.createElement('li'); tempBlock.setAttribute('id', "block_" + temp_block); tempBlock.innerHTML = block_html; // Add the temporary block // NOTE: this used to prepend to put new blocks at top, however to maintain // block order it is easy to add them at the end $(location_selector).append(tempBlock); var form_data = Zymonic.new_form_data(); // add the extras var webservicemode_set = false; for (var key in extras) { form_data.append(key, extras[key]); if(key == "ZZwebservicemode") { webservicemode_set = true; } } // Add the fixed params if(!webservicemode_set) { form_data.append("ZZwebservicemode", "block"); form_data.append("ZZBlocation", location); form_data.append("ZZno_display_attributes", "false"); form_data.append("ZZchangedblocks_" + location, "new"); form_data.append("ZZblockchange_" + location + "_new_new", "Y"); form_data.append("ZZblockchange_" + location + "_new_position", "1"); form_data.append("ZZblockchange_" + location + "_new_block_type", type); } if(process_id) { form_data.append("ZZblockchange_" + location + "_new_process_id", process_id); } else if(process_filter_zname) { form_data.append("ZZblockchange_" + location + "_new_zname", process_filter_zname); } // make the request to add the block Zymonic.request( "POST", form_data, true, null, null, function(xml) { Zymonic.BlockLoadCB(xml, temp_block); // trigger callback if incoming, lookup block id from xml to send through if (completed_cb) { var new_block_id = $(xml).find("block").attr("blockid"); completed_cb(xml, new_block_id); } }, function(type, error) { tempBlock.remove(); Zymonic.Block.Block.prototype.default_error_handler(type, error, null, true); } ); }; Zymonic.BlockLoadCB = function(xml, block_id, loading_function) { if (is_xml(xml)) { var zt = new Zymonic.Transformer.Transformer(); zt.init({ blockxml: xml, block_id: block_id }) if(zt.XMLErrHandle(function(dxml) { Zymonic.newBlockCB(dxml, block_id); }, loading_function )) { /* Transform the XML */ Zymonic.removeFakeOpenBlock( block_id ); Zymonic.clearCache( block_id ); if (zt.block_deleted) { $('#block_' + block_id).remove(); Zymonic.removeOpenBlock(block_id); } else { $('#block_' + block_id).replaceWith(zt.resultDocument); //Re-run any JS that has not been run Zymonic.Utils.reRunJS($('#block_' + zt.block_id)); // focus the new block if(!Zymonic.desktop_mode) { try { $("#block_"+zt.block_id).get(0).scrollIntoView(); } catch (e) { return null; } } } } else { // an error occurred which we cannot handle, remove the temp block if (!Zymonic.relogin_running) { $('#block_' + block_id).remove(); Zymonic.removeFakeOpenBlock( block_id ); } } } else { $('#block_' + block_id).replaceWith(xml); Zymonic.Utils.reRunJS(xml); Zymonic.removeFakeOpenBlock( block_id ); } }; // Clear Cache mechanism Zymonic.clearCache = function(block_id) { // Need to empty the item _and_ delete // the reference to it for JS to release // the memory. block_id = block_id + '_'; for (var type in Zymonic.lookup ) { for (var key in Zymonic.lookup[type]) { if(key.startsWith(block_id)) { Zymonic.lookup[type][key] = {}; delete Zymonic.lookup[type][key]; } } } // TODO Modify Field.js to use add_to_lookup instead // of the caches below for (var key in Zymonic.FieldLookup ) { if(key.startsWith(block_id)) { Zymonic.FieldLookup[key] = {}; delete Zymonic.FieldLookup[key]; } } for (var key in Zymonic.FieldLookupByWrapperIdent ) { if(key.startsWith(block_id)) { Zymonic.FieldLookupByWrapperIdent[key] = {}; delete Zymonic.FieldLookupByWrapperIdent[key]; } } } // generic function to add lookup Zymonic.add_to_lookup = function(type, key, object) { if (!Zymonic.lookup[type]) { Zymonic.lookup[type] = {}; } Zymonic.lookup[type][key] = object; }; Zymonic.lookup_object = function(type, key) { if (Zymonic.lookup[type]) { return Zymonic.lookup[type][key]; } return null; }; // add some common functions here, eventually may want to move them into Page/Block/Process/Filter JS object, if we have one Zymonic.process_messages = {}; Zymonic.add_process_message = function(process_ident, message) { if (Zymonic.process_messages[process_ident] && Zymonic.process_messages[process_ident][message]) { // message already exists, nothing to do } else { $("
    ", { 'class': "notice", text: message } ).appendTo("#"+process_ident+".ZymonicProcess .js_process_messages"); if (!Zymonic.process_messages[process_ident]) { Zymonic.process_messages[process_ident] = {}; } if (!Zymonic.process_messages[process_ident][message]) { Zymonic.process_messages[process_ident][message] = true; } } }; Zymonic.defaultErrorHandler = function(type, error, xhr) { // Add a notification Zymonic.addNotification("Unhandled error - see your browser console for more details"); Zymonic.log_object("Unhandled error " + type + " " + error, xhr); } // function to make a request to zymonic server Zymonic.request = function(type, form_data, async, request_key, request_type, success_cb, error_cb, expected_errors, loading_function) { // use base url var url_split = split_baseurl(base_url); var outgoingdata = ''; var contentType = false; // Assemble outogoing data form_data = Zymonic.asssemble_form_data(form_data, type); outgoingdata = form_data.getFormData(); contentType = form_data.getContentType(); // error handling needs block_id, but I'm not what for // try setting it as null for now, see if it works var block_id = null; // Make sure we have an error_cb of some sort... if(typeof(error_cb) !== 'function') { error_cb = function(type, error, xhr) { Zymonic.defaultErrorHandler(type, error,xhr); }; } // log the request then make it Zymonic.log_request(type, url_split[0], form_data); var xhr = $.ajax({ url: url_split[0], type: type, data: outgoingdata, async: async, processData: false, contentType: contentType, cache: false, error: function(xhr, type, error) { Zymonic.clear_ajax_request(request_key, request_type); if (Zymonic.request_logging) { Zymonic.log_object("Zymonic Invalid Response", { type: type, error: error }); } error_cb(type, error); }, success: function(xml) { Zymonic.clear_ajax_request(request_key, request_type); if (Zymonic.request_logging) { Zymonic.log_object("Zymonic Valid Response", new XMLSerializer().serializeToString(xml)); } // update token regardless, so we always get latest token Zymonic.update_token(xml); var zt = new Zymonic.Transformer.Transformer(); var can_continue = zt.XMLErrHandle(function(xml) { if (success_cb) { success_cb(xml); } }, loading_function, xml, block_id); if (can_continue) { if (success_cb) { success_cb(xml); } } else { error_cb("Zymonic", "Error in zymonic response", xml); } }, }); xhr.request_id = form_data.request_id; if (request_key && request_type) { Zymonic.store_ajax_request(request_key, request_type, xhr); } }; // assembles all params as a form data object ready for zymonic requests Zymonic.asssemble_form_data = function(form_data, type) { // use base url var url_split = split_baseurl(base_url); var url_params = get_url_params([]).concat(url_split[1].split('&')); // assemble all urls params into form data to send if (!form_data) { form_data = Zymonic.new_form_data(); } for (var i=0; i", { method: type, action: url_split[0], target: "_blank" }); if (form_data.forEach) { form_data.forEach(function(value, key) { $("", { type: 'hidden', name: key, value: value }).appendTo(form); }); } else { throw new Error("Cannot parse params in this browser"); } $("", { type: 'submit', name: 'Submit', value: 'Go' }).appendTo(form); form.hide().appendTo(document.body); // submit the form to send the request in a new page and remove it from this page form.submit(); form.detach(); }; // function to ping the zymonic server // this will trigger relogin of session if it has expired // ident is optional area of the screen to block during request Zymonic.ping = function(ident, success_cb, error_cb) { var unblock; if (ident) { unblock = Zymonic.Utils.block_gui_element($("#ident")); } var form_data = Zymonic.new_form_data(); form_data.append("ZZwebservicemode", "login"); form_data.append("ZZno_display_attributes", "false"); // this will make it send out the auth status Zymonic.request( "POST", form_data, true, "ping", Zymonic.ajax_request_type.PING, function() { unblock(); if (success_cb) { success_cb(); } }, function(type, error, xml) { unblock(); if (error_cb) { error_cb(type, error, xml); } }, null, function() { if (ident) { Zymonic.Utils.block_gui_element($("#ident")) } } ); }; // create a unique id for a request Zymonic.generate_request_id = function() { return Zymonic.Utils.current_timestamp() + "." + (Math.random()+"").replace("0.", ""); }; // converts request type so its always lowercase, and handles missing types Zymonic.convert_request_type = function(type) { if (!type) type = Zymonic.ajax_request_type.NOTSET; return type.toLowerCase(); }; // store ajax request Zymonic.store_ajax_request = function(key, type, xhr) { type = Zymonic.convert_request_type(type) if (!Zymonic.ajax_request_type_check[type]) { Zymonic.log("Unknown AJAX request type: "+type); } // check for any existing requests to be aborted Zymonic.abort_ajax_request(key, type); if (!Zymonic.ajax_requests[type]) { Zymonic.ajax_requests[type] = {}; } Zymonic.ajax_requests[type][key] = xhr; }; // get ajax request Zymonic.get_ajax_request = function(key, type) { type = Zymonic.convert_request_type(type) if (!Zymonic.ajax_request_type_check[type]) { Zymonic.log("Unknown AJAX request type: "+type); } if (Zymonic.ajax_requests[type] && Zymonic.ajax_requests[type][key]) { return Zymonic.ajax_requests[type][key]; } return null; }; // clears an ajax request from the cache Zymonic.clear_ajax_request = function(key, type) { type = Zymonic.convert_request_type(type) if (Zymonic.ajax_requests[type] && Zymonic.ajax_requests[type][key]) { delete Zymonic.ajax_requests[type][key]; } }; // aborts an ajax request Zymonic.abort_ajax_request = function(key, type, skip_cancel) { type = Zymonic.convert_request_type(type) var ajax_request = Zymonic.get_ajax_request(key, type); if (ajax_request && ajax_request.readyState != 4) { ajax_request.abort(); if (ajax_request.request_id && !skip_cancel) { Zymonic.cancel_ajax_requests([ajax_request.request_id]); } Zymonic.clear_ajax_request(key, type); return ajax_request.request_id; } else { return null; } }; // aborts all ajax requests Zymonic.abort_all_ajax_requests = function(type) { var aborted_request_ids = []; for (var request_type in Zymonic.ajax_requests) { // never abort process requests as that can corrupt data if ( (!type || type == request_type) && request_type != Zymonic.ajax_request_type.PROCESS ) { for (var key in Zymonic.ajax_requests[request_type]) { var request_id = Zymonic.abort_ajax_request(key, request_type, true); aborted_request_ids.push(request_id); } } } Zymonic.cancel_ajax_requests(aborted_request_ids); }; // cancels an ajax requests Zymonic.cancel_ajax_requests = function(request_ids) { if (request_ids.length == 0) { return; } var form_data = Zymonic.new_form_data(); form_data.append("ZZwebservicemode", "toolkit"); form_data.append("ZZmodule", "Session"); form_data.append("ZZmethod", "abort_requests"); form_data.append("system", Zymonic.system_name); form_data.append("request_id", request_ids.join(",")); Zymonic.request( "POST", form_data, true, "cancel_ajax_requests", Zymonic.ajax_request_type.TOOLKIT, function() { }, function(type, error) { } ); }; // function to make a filter request to zymonic server Zymonic.filter_request = function(zname, search_fields, async, request_key, success_cb, error_cb) { // assemble params var ident = "filter_request_"+zname+"_"+Date.now().getTime()+"_"; var form_data = Zymonic.new_form_data(); form_data.append( "ZZwebservicemode", "filter" ); form_data.append( "ZZBzname", zname ); form_data.append ("ZZFident", ident ); form_data.append( ident + 'search', 'Show Results' ); form_data.append( ident + 'report_only', 'true' ); for (var search_field in search_fields) { form_data.append( ident + search_field, search_fields[search_field] ); } // make request Zymonic.request( "POST", form_data, async, request_key, Zymonic.ajax_request_type.FILTER, // parse results to pass into success handler function(xml) { success_cb( xml, Zymonic.parse_filter_results(xml) ); }, error_cb ); }; // parses filter results into list Zymonic.parse_filter_results = function(xml) { // recursively parse each report and its results var reports = []; $(xml).find("report").each( function(i, report_xml) { var report = { xml: report_xml, ident: $(report_xml).attr('ident'), results: [] }; $(report_xml).find("result").each( function(i, result_xml) { var result = { xml: result_xml, ident: $(result_xml).attr('ident'), id: {}, fields : {} }; $(result_xml).find("ZZid").each( function(i, id_xml) { // no jquery to iterate over attributes, so use normal js var attributes = $(id_xml).get(0).attributes; for (var i=0; i" + message + "
    "); } } // TODO consider forgotten password / password change buttons too // Unhide the div $('#ZymonicInlineAuth').show(); // Hide the password change div just in case $('#ZymonicInlinePasswordChange').hide(); // Colorbox to display it $.colorbox({ top: "10px", fixed: true, width: "100%", maxWidth: "400px", inline: true, href:"#ZymonicInlineAuth", overlayClose: allowClose, escKey: allowClose, onLoad: function() { if(!allowClose) { $('#cboxClose').remove(); } }, onClosed: function() { $('#ZymonicInlineAuth').hide(); } }); }; Zymonic.addLoginButton = function() { if($('#ZZInlineLoginButton').length) { // delete the old button $('#ZZInlineLoginButton').remove(); } $('#ZZSession_credentials_form__div').append(""); $('#ZZInlineLoginButton').on("click", function() { Zymonic.doLogin(); }); } // Submit an auth request to the server Zymonic.doLogin = function(loading_function) { var credentials = {}; // Grey out the login button and set 'checking' $('#ZZInlineLoginButton').html('Checking...'); $('#ZZInlineLoginButton').attr('disabled','disabled'); // Grab any fields with znames from the credentials form $('#ZZSession_credentials_form__div input').each(function() { if($(this).attr('zname')) { credentials[$(this).attr('zname')] = $(this).val(); } }); // Send an authentication request var form_data = Zymonic.new_form_data(); form_data.append("ZZwebservicemode", "login"); form_data.append("ZZno_display_attributes", "false"); // this will make it send out the auth status for( var credential in credentials ) { form_data.append("ZA" + credential, credentials[credential]); } Zymonic.request( "POST", form_data, true, "ping", Zymonic.ajax_request_type.PING, function(xml) { Zymonic.processLoginResult(xml, loading_function) }, null, function() { if (ident) { Zymonic.Utils.block_gui_element($("#ident")) } } ); }; // Process the login result Zymonic.processLoginResult = function(result, loading_function) { if( $(result).find('Authenticated').text() == 'Y' ) { // User is now logged in if(Zymonic.offer_login_before_state == 'N') { location.reload(); } else { // Clear any errors if($('#ZZInlineLoginError').length) { $('#ZZInlineLoginEror').remove(); } // Remove the login UI $.colorbox.close(); $('#ZymonicInlineAuth').hide(); // cancel the timeout which would reload the page window.clearTimeout(Zymonic.relogin_timeout); // token will have changed on login, so set the new token from within the box on the main page var token = $(result).find('Zymonic').attr('token'); if (token) { Zymonic.set_token(token); } if (loading_function) { loading_function(); } Zymonic.resubmit_requests(); } } else { var error_message = 'Login failed'; if( $(result).find('AuthFailedMessage').length ) { error_message = $(result).find('AuthFailedMessage').text(); } if($('#ZZInlineLoginError').length) { $('#ZZInlineLoginEror').html(error_message); } else { $('#ZZSession_credentials_form__div').append("
    " + error_message + "
    "); } if($('#ZZInlineLoginButton').length) { // delete the old button $('#ZZInlineLoginButton').remove(); } $('#ZZSession_credentials_form__div').append(""); $('#ZZInlineLoginButton').on("click", function() { Zymonic.doLogin(); }); $.colorbox.resize(); } }; // Session expiry Zymonic.set_session_expiry = function(session_expiry, relogin_window) { if(session_expiry && relogin_window && Zymonic.authenticated()) { Zymonic.session_expiry = session_expiry; Zymonic.relogin_window = relogin_window; // Calculate ms until session expires var until_expiry = (Zymonic.session_expiry * 1000) - Date.now(); // Set a timer going until session expiry window.clearTimeout(Zymonic.relogin_trigger); Zymonic.relogin_trigger = window.setTimeout(function() { Zymonic.relogin(); }, Math.floor(until_expiry)); } } // Relogin Zymonic.relogin = function(sspid, twindow, block_id, cont_func, loading_function) { // Add ourselves to list of requests to re-run after relogin if(sspid) { Zymonic.relogin_sspids.push(new Zymonic.rRequest(sspid, block_id, cont_func)); } // Check for running relogin if(Zymonic.relogin_running == true) { return; } Zymonic.relogin_running = true; // Calculate ms until session expires var until_relogin_end = Zymonic.session_expiry * 1 + Zymonic.relogin_window * 1 - ( Date.now() / 1000 ); // Set timeout to do a complete redirect TODO - calculate the timeout based on how long until relogin window ends Zymonic.relogin_timeout = window.setTimeout(function() { if(Zymonic.relogin_running) { window.location = base_url; } }, until_relogin_end * 1000); Zymonic.offer_login_before_state = 'Y'; // Add a message var message = "Your login has expired..."; if($('#ZZInlineLoginMessage').length) { $('#ZZInlineLoginMessage').html(message); } else { $('#ZZSession_credentials_form__div').prepend("
    " + message + "
    "); } // Clear all fields except username! $('#ZZSession_credentials_form__div input').each(function() { if($(this).attr('zname') && $(this).attr('zname') != 'username') { $(this).val(''); } }); // Add a login button Zymonic.addLoginButton(); // Add OTP button Zymonic.addOTPButton(); $('#ZymonicInlineAuth').show(); // Hide the password change div just in case $('#ZymonicInlinePasswordChange').hide(); $.colorbox({ inline: true, top: "10px", fixed: true, width: "100%", maxWidth: "400px", href:"#ZymonicInlineAuth", overlayClose: false, escKey: false, onLoad: function() { $('#cboxClose').remove(); }, }); }; // One Time Password enabled? Zymonic.set_otp_available = function(otp_available) { Zymonic.otp_available = otp_available; }; Zymonic.addOTPButton = function() { if($('#ZZInlineOTPButton').length) { // delete the old button $('#ZZInlineOTPButton').remove(); } $('#ZZSession_credentials_form__div').append(""); $('#ZZInlineOTPButton').on("click", function() { Zymonic.doOTP(); }); }; Zymonic.doOTP = function() { var credentials = {}; // Disable the button $('#ZZInlineOTPButton').html('Sending...'); $('#ZZInlineOTPButton').attr('disabled','disabled'); // Grab any fields with znames from the credentials form $('#ZZSession_credentials_form__div input').each(function() { if($(this).attr('zname')) { credentials[$(this).attr('zname')] = $(this).val(); } }); // Send an authentication request var form_data = Zymonic.new_form_data(); form_data.append("ZZwebservicemode", "temporary_password"); for( var credential in credentials ) { form_data.append("ZA" + credential, credentials[credential]); } Zymonic.request( "POST", form_data, true, "ping", Zymonic.ajax_request_type.PING, function(xml) { Zymonic.processOTPResult(xml) }, null, function() { } ); }; Zymonic.processOTPResult = function(xml) { // Reinstate the button Zymonic.addOTPButton(); // Show the result if($(xml).find('TemporaryPasswordResult').length) { alert($(xml).find('TemporaryPasswordResult').text()); } }; // Password Change Zymonic.passwordChange = function() { allowClose = true; // TODO only continue if logged in // Add a login button Zymonic.addPasswordChangeButton(); // Unhide the div $('#ZymonicInlinePasswordChange').show(); // Hide the auth div just in case $('#ZymonicInlineAuth').hide(); // Colorbox to display it $.colorbox({ top: "10px", fixed: true, width: "100%", maxWidth: "400px", inline: true, href:"#ZymonicInlinePasswordChange", overlayClose: allowClose, escKey: allowClose, onLoad: function() { if(!allowClose) { $('#cboxClose').remove(); } }, onClosed: function() { $('#ZymonicInlinePasswordChange').hide(); } }); }; Zymonic.addPasswordChangeButton = function() { if($('#ZZInlinePasswordChangeButton').length) { // delete the old button $('#ZZInlinePasswordChangeButton').remove(); } $('#ZZSession_password_change_form__div').append(""); $('#ZZInlinePasswordChangeButton').on("click", function() { Zymonic.doPasswordChange(); }); } // Submit an auth request to the server Zymonic.doPasswordChange = function(loading_function) { // TODO must be logged in var credentials = {}; // Grey out the login button and set 'checking' $('#ZZInlinePasswordChangeButton').html('Checking...'); $('#ZZInlinePasswordChangeButton').attr('disabled','disabled'); // Grab any fields with znames from the credentials form $('#ZZSession_password_change_form__div input').each(function() { if($(this).attr('zname')) { credentials[$(this).attr('zname')] = $(this).val(); } }); // JSON Request var form_data = Zymonic.new_form_data(); form_data.appendToHeader("system", this.system_name); form_data.appendToHeader("webservicemode", "process"); form_data.append("ZZBzname", "Authentication"); form_data.append("_Authentication_AuthenticationStatus_fake_reczz_new_password", credentials['zz_new_password']); form_data.append("_Authentication_AuthenticationStatus_fake_reczz_user", this.username); form_data.append("_Authentication_transition_zz_change_password_from_authenticated", "go"); Zymonic.request( "POST", form_data, true, "ping", Zymonic.ajax_request_type.PING, function(xml) { Zymonic.processPasswordChangeResult(xml, loading_function) }, null, null, function() { if (ident) { Zymonic.Utils.block_gui_element($("#ident")) } }, ); }; // Process the login result Zymonic.processPasswordChangeResult = function(result, loading_function) { if( $(result).find("[type='TransitionResult']").attr("success") == 'true' ) { alert("Password Changed OK"); $.colorbox.close(); $('#ZymonicInlinePasswordChange').hide(); } else { error = $(result).find("UserFacingError"); errortext = ''; if(error.length) { errortext = error.text(); } alert("Password not changed... " + errortext); this.addPasswordChangeButton(); } } Zymonic.resubmit_requests = function() { // Submit the requests for(i=0;i