Revert "Nanoui now supports use of multiple templates (#5105)"

This reverts commit a2841ce9d6.
It turns out nanoui already supports multiple templates.
This commit is contained in:
Leshana
2018-04-27 11:36:03 -04:00
parent 4df02c9160
commit b60281e19a
3 changed files with 160 additions and 168 deletions

View File

@@ -519,8 +519,3 @@ nanoui is used to open and update nano browser uis
*/ */
/datum/nanoui/proc/update(var/force_open = 0) /datum/nanoui/proc/update(var/force_open = 0)
src_object.ui_interact(user, ui_key, src, force_open, master_ui, state) src_object.ui_interact(user, ui_key, src, force_open, master_ui, state)
/datum/nanoui/proc/append_template(var/key, var/filename)
add_template(key, filename)
open()
update(1)

View File

@@ -7,7 +7,8 @@ function NanoStateClass() {
return; return;
} }
this.key = this.key.toLowerCase(); this.key = this.key.toLowerCase();
NanoStateManager.addState(this);*/ NanoStateManager.addState(this);*/
} }
@@ -17,99 +18,102 @@ NanoStateClass.prototype.contentRendered = false;
NanoStateClass.prototype.mapInitialised = false; NanoStateClass.prototype.mapInitialised = false;
NanoStateClass.prototype.isCurrent = function () { NanoStateClass.prototype.isCurrent = function () {
return NanoStateManager.getCurrentState() == this; return NanoStateManager.getCurrentState() == this;
}; };
NanoStateClass.prototype.onAdd = function (previousState) { NanoStateClass.prototype.onAdd = function (previousState) {
// Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function // Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function
NanoBaseCallbacks.addCallbacks();
NanoBaseHelpers.addHelpers(); NanoBaseCallbacks.addCallbacks();
NanoBaseHelpers.addHelpers();
}; };
NanoStateClass.prototype.onRemove = function (nextState) { NanoStateClass.prototype.onRemove = function (nextState) {
// Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function // Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function
NanoBaseCallbacks.removeCallbacks();
NanoBaseHelpers.removeHelpers(); NanoBaseCallbacks.removeCallbacks();
NanoBaseHelpers.removeHelpers();
}; };
NanoStateClass.prototype.onBeforeUpdate = function (data) { NanoStateClass.prototype.onBeforeUpdate = function (data) {
// Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function // Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function
data = NanoStateManager.executeBeforeUpdateCallbacks(data);
return data; // Return data to continue, return false to prevent onUpdate and onAfterUpdate data = NanoStateManager.executeBeforeUpdateCallbacks(data);
return data; // Return data to continue, return false to prevent onUpdate and onAfterUpdate
}; };
NanoStateClass.prototype.onUpdate = function (data) { NanoStateClass.prototype.onUpdate = function (data) {
// Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function // Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function
try
{
if (!this.layoutRendered || (data['config'].hasOwnProperty('autoUpdateLayout') && data['config']['autoUpdateLayout']))
{
$("#uiLayout").html(NanoTemplate.parse('layout', data)); // render the 'mail' template to the #mainTemplate div
this.layoutRendered = true;
}
if (!this.contentRendered || (data['config'].hasOwnProperty('autoUpdateContent') && data['config']['autoUpdateContent']))
{
var content = "";
var keys = NanoTemplate.getKeys();
for (i = 0; i < keys.length; i++) {
if(keys[i] == "layout"){ continue; }
content += NanoTemplate.parse(keys[i], data);
}
$("#uiContent").html(content); // render the 'mail' template to the #mainTemplate div
this.contentRendered = true;
}
if (NanoTemplate.templateExists('mapContent'))
{
if (!this.mapInitialised)
{
// Add drag functionality to the map ui
$('#uiMap').draggable();
$('#uiMapTooltip')
.off('click')
.on('click', function (event) {
event.preventDefault();
$(this).fadeOut(400);
});
this.mapInitialised = true;
}
$("#uiMapContent").html(NanoTemplate.parse('mapContent', data)); // render the 'mapContent' template to the #uiMapContent div try
{
if (!this.layoutRendered || (data['config'].hasOwnProperty('autoUpdateLayout') && data['config']['autoUpdateLayout']))
{
$("#uiLayout").html(NanoTemplate.parse('layout', data)); // render the 'mail' template to the #mainTemplate div
this.layoutRendered = true;
}
if (!this.contentRendered || (data['config'].hasOwnProperty('autoUpdateContent') && data['config']['autoUpdateContent']))
{
$("#uiContent").html(NanoTemplate.parse('main', data)); // render the 'mail' template to the #mainTemplate div
this.contentRendered = true;
}
if (NanoTemplate.templateExists('mapContent'))
{
if (!this.mapInitialised)
{
// Add drag functionality to the map ui
$('#uiMap').draggable();
if (data['config'].hasOwnProperty('showMap') && data['config']['showMap']) $('#uiMapTooltip')
{ .off('click')
$('#uiContent').addClass('hidden'); .on('click', function (event) {
$('#uiMapWrapper').removeClass('hidden'); event.preventDefault();
} $(this).fadeOut(400);
else });
{
$('#uiMapWrapper').addClass('hidden'); this.mapInitialised = true;
$('#uiContent').removeClass('hidden'); }
}
} $("#uiMapContent").html(NanoTemplate.parse('mapContent', data)); // render the 'mapContent' template to the #uiMapContent div
if (NanoTemplate.templateExists('mapHeader'))
{ if (data['config'].hasOwnProperty('showMap') && data['config']['showMap'])
$("#uiMapHeader").html(NanoTemplate.parse('mapHeader', data)); // render the 'mapHeader' template to the #uiMapHeader div {
} $('#uiContent').addClass('hidden');
if (NanoTemplate.templateExists('mapFooter')) $('#uiMapWrapper').removeClass('hidden');
{ }
$("#uiMapFooter").html(NanoTemplate.parse('mapFooter', data)); // render the 'mapFooter' template to the #uiMapFooter div else
} {
} $('#uiMapWrapper').addClass('hidden');
catch(error) $('#uiContent').removeClass('hidden');
{ }
alert('ERROR: An error occurred while rendering the UI: ' + error.message); }
return; if (NanoTemplate.templateExists('mapHeader'))
} {
$("#uiMapHeader").html(NanoTemplate.parse('mapHeader', data)); // render the 'mapHeader' template to the #uiMapHeader div
}
if (NanoTemplate.templateExists('mapFooter'))
{
$("#uiMapFooter").html(NanoTemplate.parse('mapFooter', data)); // render the 'mapFooter' template to the #uiMapFooter div
}
}
catch(error)
{
alert('ERROR: An error occurred while rendering the UI: ' + error.message);
return;
}
}; };
NanoStateClass.prototype.onAfterUpdate = function (data) { NanoStateClass.prototype.onAfterUpdate = function (data) {
// Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function // Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function
NanoStateManager.executeAfterUpdateCallbacks(data);
NanoStateManager.executeAfterUpdateCallbacks(data);
}; };
NanoStateClass.prototype.alertText = function (text) { NanoStateClass.prototype.alertText = function (text) {
// Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function // Do not add code here, add it to the 'default' state (nano_state_defaut.js) or create a new state and override this function
alert(text);
alert(text);
}; };

View File

@@ -1,15 +1,15 @@
var NanoTemplate = function () { var NanoTemplate = function () {
var _templateData = {}; var _templateData = {};
var _templates = {}; var _templates = {};
var _compiledTemplates = {}; var _compiledTemplates = {};
var _helpers = {}; var _helpers = {};
var init = function () { var init = function () {
// We store templateData in the body tag, it's as good a place as any // We store templateData in the body tag, it's as good a place as any
_templateData = $('body').data('templateData'); _templateData = $('body').data('templateData');
if (_templateData == null) if (_templateData == null)
@@ -18,101 +18,94 @@ var NanoTemplate = function () {
} }
loadNextTemplate(); loadNextTemplate();
}; };
var loadNextTemplate = function () { var loadNextTemplate = function () {
// we count the number of templates for this ui so that we know when they've all been rendered // we count the number of templates for this ui so that we know when they've all been rendered
var templateCount = Object.size(_templateData); var templateCount = Object.size(_templateData);
if (!templateCount) if (!templateCount)
{ {
$(document).trigger('templatesLoaded'); $(document).trigger('templatesLoaded');
return; return;
} }
// load markup for each template and register it // load markup for each template and register it
for (var key in _templateData) for (var key in _templateData)
{ {
if (!_templateData.hasOwnProperty(key)) if (!_templateData.hasOwnProperty(key))
{ {
continue; continue;
} }
$.when($.ajax({ $.when($.ajax({
url: _templateData[key], url: _templateData[key],
cache: false, cache: false,
dataType: 'text' dataType: 'text'
})) }))
.done(function(templateMarkup) { .done(function(templateMarkup) {
templateMarkup += '<div class="clearBoth"></div>'; templateMarkup += '<div class="clearBoth"></div>';
try try
{ {
NanoTemplate.addTemplate(key, templateMarkup); NanoTemplate.addTemplate(key, templateMarkup);
} }
catch(error) catch(error)
{ {
alert('ERROR: An error occurred while loading the UI: ' + error.message); alert('ERROR: An error occurred while loading the UI: ' + error.message);
return; return;
} }
delete _templateData[key]; delete _templateData[key];
loadNextTemplate(); loadNextTemplate();
}) })
.fail(function () { .fail(function () {
alert('ERROR: Loading template ' + key + '(' + _templateData[key] + ') failed!'); alert('ERROR: Loading template ' + key + '(' + _templateData[key] + ') failed!');
}); });
return; return;
} }
} }
var compileTemplates = function () { var compileTemplates = function () {
for (var key in _templates) { for (var key in _templates) {
try { try {
_compiledTemplates[key] = doT.template(_templates[key], null, _templates) _compiledTemplates[key] = doT.template(_templates[key], null, _templates)
} }
catch (error) { catch (error) {
alert(error.message); alert(error.message);
} }
} }
}; };
return { return {
init: function () { init: function () {
init(); init();
}, },
addTemplate: function (key, templateString) { addTemplate: function (key, templateString) {
_templates[key] = templateString; _templates[key] = templateString;
}, },
templateExists: function (key) { templateExists: function (key) {
return _templates.hasOwnProperty(key); return _templates.hasOwnProperty(key);
}, },
getKeys: function () { parse: function (templateKey, data) {
var _keys = []; if (!_compiledTemplates.hasOwnProperty(templateKey) || !_compiledTemplates[templateKey]) {
for(var key in _templates) { if (!_templates.hasOwnProperty(templateKey)) {
_keys.push(key); alert('ERROR: Template "' + templateKey + '" does not exist in _compiledTemplates!');
} return '<h2>Template error (does not exist)</h2>';
return _keys; }
}, compileTemplates();
parse: function (templateKey, data) { }
if (!_compiledTemplates.hasOwnProperty(templateKey) || !_compiledTemplates[templateKey]) { if (typeof _compiledTemplates[templateKey] != 'function') {
if (!_templates.hasOwnProperty(templateKey)) { alert(_compiledTemplates[templateKey]);
alert('ERROR: Template "' + templateKey + '" does not exist in _compiledTemplates!'); alert('ERROR: Template "' + templateKey + '" failed to compile!');
return '<h2>Templateerror (does not exist)</h2>'; return '<h2>Template error (failed to compile)</h2>';
} }
compileTemplates(); return _compiledTemplates[templateKey].call(this, data['data'], data['config'], _helpers);
} },
if (typeof _compiledTemplates[templateKey] != 'function') {
alert(_compiledTemplates[templateKey]);
alert('ERROR: Template "' + templateKey + '" failed to compile!');
return '<h2>Template error (failed to compile)</h2>';
}
return _compiledTemplates[templateKey].call(this, data['data'], data['config'], _helpers);
},
addHelper: function (helperName, helperFunction) { addHelper: function (helperName, helperFunction) {
if (!jQuery.isFunction(helperFunction)) { if (!jQuery.isFunction(helperFunction)) {
alert('NanoTemplate.addHelper failed to add ' + helperName + ' as it is not a function.'); alert('NanoTemplate.addHelper failed to add ' + helperName + ' as it is not a function.');
@@ -136,7 +129,7 @@ var NanoTemplate = function () {
delete _helpers[helperName]; delete _helpers[helperName];
} }
} }
} }
}(); }();