From fc1d61ac981c0234efca9b5e8ba7163ebe44eb80 Mon Sep 17 00:00:00 2001 From: Mark Aherne Date: Mon, 7 Jul 2014 02:05:39 +0100 Subject: [PATCH] Work in progress on a major revision of the NanoUI templating system. --- code/modules/nano/nanoui.dm | 1 + nano/js/nano_base_helpers.js | 6 +----- nano/js/nano_template.js | 2 +- nano/js/nano_update.js | 19 +++++++++++++++---- nano/js/nano_utility.js | 16 +++++++++++----- nano/templates/apc.tmpl | 2 +- 6 files changed, 30 insertions(+), 16 deletions(-) diff --git a/code/modules/nano/nanoui.dm b/code/modules/nano/nanoui.dm index fba9ec9422e..e0d2cb88609 100644 --- a/code/modules/nano/nanoui.dm +++ b/code/modules/nano/nanoui.dm @@ -95,6 +95,7 @@ nanoui is used to open and update nano browser uis /datum/nanoui/proc/add_common_assets() add_script("libraries.min.js") // A JS file comprising of jQuery, doT.js and jQuery Timer libraries (compressed together) add_script("nano_utility.js") // The NanoUtility JS, this is used to store utility functions. + add_script("nano_template.js") // The NanoTemplate JS, this is used to render templates. add_script("nano_update.js") // The NanoUpdate JS, this is used to receive updates and apply them. add_script("nano_base_helpers.js") // The NanoBaseHelpers JS, this is used to set up template helpers which are common to all templates add_stylesheet("shared.css") // this CSS sheet is common to all UIs diff --git a/nano/js/nano_base_helpers.js b/nano/js/nano_base_helpers.js index 9b83dba6ba1..e9c49f5df04 100644 --- a/nano/js/nano_base_helpers.js +++ b/nano/js/nano_base_helpers.js @@ -1,13 +1,9 @@ // NanoBaseHelpers is where the base template helpers (common to all templates) are stored NanoBaseHelpers = function () { - var _urlParameters = {}; // This is populated with the base url parameters (used by all links), which is probaby just the "src" parameter - var init = function () { - var body = $('body'); // We store data in the body tag, it's as good a place as any - - _urlParameters = body.data('urlParameters'); + initHelpers(); }; diff --git a/nano/js/nano_template.js b/nano/js/nano_template.js index 0bb02d762ab..cde883329dc 100644 --- a/nano/js/nano_template.js +++ b/nano/js/nano_template.js @@ -7,7 +7,7 @@ var NanoTemplate = function () { var _helpers = {}; var init = function () { - doT.templateSettings['varname'] = 'data'; + }; var compileTemplates = function () { diff --git a/nano/js/nano_update.js b/nano/js/nano_update.js index e90f1120df1..72d6755fe0e 100644 --- a/nano/js/nano_update.js +++ b/nano/js/nano_update.js @@ -100,7 +100,12 @@ NanoUpdate = function () { if (templateData.hasOwnProperty(key)) { - $.when($.get(templateData[key])) + $.when($.ajax({ + url: templateData[key], + cache: false, + dataType: 'text' + })) + //$.when($.get(templateData[key])) .done(function(templateMarkup) { //templateMarkup = templateMarkup.replace(/ +\) *\}\}/g, ')}}'); @@ -134,7 +139,10 @@ NanoUpdate = function () return; } - }); + }) + .fail(function () { + alert('Loading template ' + key + '(' + templateData[key] + ') failed!'); + });; } } }; @@ -194,9 +202,12 @@ NanoUpdate = function () // Execute all callbacks in the callbacks array/object provided, updateData is passed to them for processing and potential modification var executeCallbacks = function (callbacks, updateData) { - for (var index in callbacks) + for (var key in callbacks) { - updateData = callbacks[index].call(this, updateData); + if (callbacks.hasOwnProperty(key) && jQuery.isFunction(callbacks[key])) + { + updateData = callbacks[key].call(this, updateData); + } } return updateData; diff --git a/nano/js/nano_utility.js b/nano/js/nano_utility.js index aaf3f13ff49..6d93829883e 100644 --- a/nano/js/nano_utility.js +++ b/nano/js/nano_utility.js @@ -1,15 +1,21 @@ // NanoUtility is the place to store utility functions var NanoUtility = function () { - return { + var _urlParameters = {}; // This is populated with the base url parameters (used by all links), which is probaby just the "src" parameter + + return { init: function () { if (typeof jQuery == 'undefined') { - alert('ERROR: jQuery failed to load!'); + alert('ERROR: Javascript library failed to load!'); } - if (typeof $.views == 'undefined') { - alert('ERROR: JSRender failed to load!'); - } + if (typeof doT == 'undefined') { + alert('ERROR: Template engine failed to load!'); + } + + var body = $('body'); // We store data in the body tag, it's as good a place as any + + _urlParameters = body.data('urlParameters'); }, // generate a Byond href, combines _urlParameters with parameters generateHref: function (parameters) diff --git a/nano/templates/apc.tmpl b/nano/templates/apc.tmpl index 574e53cec69..0fbba9fe860 100644 --- a/nano/templates/apc.tmpl +++ b/nano/templates/apc.tmpl @@ -114,7 +114,7 @@ {{else value.status >= 2}} On {{/if}} - {{if value.status == 1 || status == 3}} + {{if value.status == 1 || value.status == 3}} [Auto] {{else}} [Manual]