szurubooru/public_html/js/Util.js

164 lines
3.9 KiB
JavaScript
Raw Normal View History

2014-09-02 09:36:42 +02:00
var App = App || {};
2014-09-08 22:02:28 +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
function parseComplexRouteArgs(args) {
var result = {};
args = (args || '').split(/;/);
for (var i = 0; i < args.length; i ++) {
var arg = args[i];
2014-09-08 22:02:28 +02:00
if (!arg) {
continue;
2014-09-08 22:02:28 +02:00
}
var kv = arg.split(/=/);
result[kv[0]] = kv[1];
}
return result;
}
function compileComplexRouteArgs(baseUri, args) {
var result = baseUri + '/';
_.each(args, function(v, k) {
2014-09-08 22:02:28 +02:00
if (typeof(v) === 'undefined') {
return;
2014-09-08 22:02:28 +02:00
}
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) {
2014-09-08 22:02:28 +02:00
if (lastContentPresenterName !== presenterName) {
2014-09-07 09:30:48 +02:00
var presenter = App.DI.get(presenterName);
2014-09-08 22:02:28 +02:00
presenter.init.call(presenter, args);
2014-09-07 09:30:48 +02:00
lastContentPresenterName = presenterName;
lastContentPresenter = presenter;
2014-09-07 19:49:11 +02:00
} else if (lastContentPresenter.reinit) {
2014-09-08 22:02:28 +02:00
lastContentPresenter.reinit.call(lastContentPresenter, args);
2014-09-07 09:30:48 +02:00
}
}
2014-09-04 18:06:25 +02:00
function promiseTemplate(templateName) {
2014-09-08 22:02:28 +02:00
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';
2014-09-08 22:02:28 +02:00
jQuery.ajax({
2014-09-02 09:36:42 +02:00
url: templateUrl,
method: 'GET',
success: function(data, textStatus, xhr) {
resolve(data);
},
error: function(xhr, textStatus, errorThrown) {
2014-09-08 22:02:28 +02:00
console.log(new Error('Error while loading template ' + templateName + ': ' + errorThrown));
2014-09-02 09:36:42 +02:00
reject();
},
});
});
}
2014-09-07 09:57:01 +02:00
function formatRelativeTime(timeString) {
2014-09-08 22:02:28 +02:00
if (!timeString) {
2014-09-07 09:57:01 +02:00
return 'never';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
2014-09-08 22:02:28 +02:00
var then = Date.parse(timeString);
2014-09-07 09:57:01 +02:00
var now = Date.now();
2014-09-08 22:02:28 +02:00
var difference = Math.abs(now - then);
var future = now < then;
2014-09-07 09:57:01 +02:00
var text = (function(difference) {
var mul = 1000;
var prevMul;
mul *= 60;
2014-09-08 22:02:28 +02:00
if (difference < mul) {
2014-09-07 09:57:01 +02:00
return 'a few seconds';
2014-09-08 22:02:28 +02:00
} else if (difference < mul * 2) {
2014-09-07 09:57:01 +02:00
return 'a minute';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
prevMul = mul; mul *= 60;
2014-09-08 22:02:28 +02:00
if (difference < mul) {
2014-09-07 09:57:01 +02:00
return Math.round(difference / prevMul) + ' minutes';
2014-09-08 22:02:28 +02:00
} else if (difference < mul * 2) {
2014-09-07 09:57:01 +02:00
return 'an hour';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
prevMul = mul; mul *= 24;
2014-09-08 22:02:28 +02:00
if (difference < mul) {
2014-09-07 09:57:01 +02:00
return Math.round(difference / prevMul) + ' hours';
2014-09-08 22:02:28 +02:00
} else if (difference < mul * 2) {
2014-09-07 09:57:01 +02:00
return 'a day';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
prevMul = mul; mul *= 30.42;
2014-09-08 22:02:28 +02:00
if (difference < mul) {
2014-09-07 09:57:01 +02:00
return Math.round(difference / prevMul) + ' days';
2014-09-08 22:02:28 +02:00
} else if (difference < mul * 2) {
2014-09-07 09:57:01 +02:00
return 'a month';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
prevMul = mul; mul *= 12;
2014-09-08 22:02:28 +02:00
if (difference < mul) {
2014-09-07 09:57:01 +02:00
return Math.round(difference / prevMul) + ' months';
2014-09-08 22:02:28 +02:00
} else if (difference < mul * 2) {
2014-09-07 09:57:01 +02:00
return 'a year';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
return Math.round(difference / mul) + ' years';
})(difference);
2014-09-08 22:02:28 +02:00
if (text === 'a day') {
2014-09-07 09:57:01 +02:00
return future ? 'tomorrow' : 'yesterday';
2014-09-08 22:02:28 +02:00
}
2014-09-07 09:57:01 +02:00
return future ? 'in ' + text : text + ' ago';
}
2014-09-02 09:36:42 +02:00
return {
2014-09-04 18:06:25 +02:00
promiseTemplate: promiseTemplate,
initPresenter : initPresenter,
initContentPresenter: initContentPresenter,
parseComplexRouteArgs: parseComplexRouteArgs,
compileComplexRouteArgs: compileComplexRouteArgs,
2014-09-07 09:57:01 +02:00
formatRelativeTime: formatRelativeTime,
2014-09-02 09:36:42 +02:00
};
2014-09-08 22:02:28 +02:00
};
2014-09-02 09:36:42 +02:00
App.DI.registerSingleton('util', ['_', 'jQuery', 'promise'], App.Util);