2014-09-02 09:36:42 +02:00
|
|
|
var App = App || {};
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
App.Util = (function(jQuery, promise) {
|
2014-09-02 09:36:42 +02:00
|
|
|
|
|
|
|
var templateCache = {};
|
2014-09-07 09:30:48 +02:00
|
|
|
var lastContentPresenterName;
|
|
|
|
var lastContentPresenter;
|
2014-09-02 09:36:42 +02:00
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
function parseComplexRouteArgs(args) {
|
|
|
|
var result = {};
|
|
|
|
args = (args || '').split(/;/);
|
|
|
|
for (var i = 0; i < args.length; i ++) {
|
|
|
|
var arg = args[i];
|
|
|
|
if (!arg)
|
|
|
|
continue;
|
|
|
|
kv = arg.split(/=/);
|
|
|
|
result[kv[0]] = kv[1];
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function compileComplexRouteArgs(baseUri, args) {
|
|
|
|
var result = baseUri + '/';
|
|
|
|
_.each(args, function(v, k) {
|
|
|
|
if (typeof(v) == 'undefined')
|
|
|
|
return;
|
|
|
|
result += k + '=' + v + ';'
|
|
|
|
});
|
|
|
|
result = result.slice(0, -1);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-07 09:30:48 +02:00
|
|
|
function initPresenter(presenterName, args) {
|
|
|
|
var presenter = App.DI.get(presenterName);
|
2014-09-04 18:06:25 +02:00
|
|
|
presenter.init.call(presenter, args);
|
|
|
|
}
|
|
|
|
|
2014-09-07 09:30:48 +02:00
|
|
|
function initContentPresenter(presenterName, args) {
|
|
|
|
if (lastContentPresenterName != presenterName) {
|
|
|
|
var presenter = App.DI.get(presenterName);
|
|
|
|
var initResult = presenter.init.call(presenter, args);
|
|
|
|
lastContentPresenterName = presenterName;
|
|
|
|
lastContentPresenter = presenter;
|
|
|
|
} else {
|
|
|
|
lastContentPresenter.reinit.call(presenter, args);
|
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
2014-09-04 18:06:25 +02:00
|
|
|
|
|
|
|
function promiseTemplate(templateName) {
|
|
|
|
return promiseTemplateFromCache(templateName)
|
|
|
|
|| promiseTemplateFromDOM(templateName)
|
|
|
|
|| promiseTemplateWithAJAX(templateName);
|
2014-09-02 09:36:42 +02:00
|
|
|
}
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
function promiseTemplateFromCache(templateName) {
|
2014-09-02 09:36:42 +02:00
|
|
|
if (templateName in templateCache) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-09-02 09:36:42 +02:00
|
|
|
resolve(templateCache[templateName]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
function promiseTemplateFromDOM(templateName) {
|
2014-09-02 09:36:42 +02:00
|
|
|
var $template = jQuery('#' + templateName + '-template');
|
|
|
|
if ($template.length) {
|
2014-09-04 18:06:25 +02:00
|
|
|
return promise.make(function(resolve, reject) {
|
2014-09-02 09:36:42 +02:00
|
|
|
resolve($template.html());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2014-09-04 18:06:25 +02:00
|
|
|
function promiseTemplateWithAJAX(templateName) {
|
|
|
|
return promise.make(function(resolve, reject) {
|
2014-09-02 09:36:42 +02:00
|
|
|
var templatesDir = '/templates';
|
|
|
|
var templateUrl = templatesDir + '/' + templateName + '.tpl';
|
|
|
|
var templateString;
|
|
|
|
|
|
|
|
$.ajax({
|
|
|
|
url: templateUrl,
|
|
|
|
method: 'GET',
|
|
|
|
success: function(data, textStatus, xhr) {
|
|
|
|
resolve(data);
|
|
|
|
},
|
|
|
|
error: function(xhr, textStatus, errorThrown) {
|
2014-09-07 00:33:46 +02:00
|
|
|
console.log(Error('Error while loading template ' + templateName + ': ' + errorThrown));
|
2014-09-02 09:36:42 +02:00
|
|
|
reject();
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2014-09-04 18:06:25 +02:00
|
|
|
promiseTemplate: promiseTemplate,
|
2014-09-03 09:10:26 +02:00
|
|
|
initPresenter : initPresenter,
|
|
|
|
initContentPresenter: initContentPresenter,
|
2014-09-03 19:07:53 +02:00
|
|
|
parseComplexRouteArgs: parseComplexRouteArgs,
|
|
|
|
compileComplexRouteArgs: compileComplexRouteArgs,
|
2014-09-02 09:36:42 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
App.DI.registerSingleton('util', App.Util);
|