summaryrefslogtreecommitdiffstats
path: root/pyload/web/app/scripts/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'pyload/web/app/scripts/helpers')
-rw-r--r--pyload/web/app/scripts/helpers/fileHelper.js69
-rw-r--r--pyload/web/app/scripts/helpers/formatSize.js20
-rw-r--r--pyload/web/app/scripts/helpers/formatTime.js20
-rw-r--r--pyload/web/app/scripts/helpers/formatTimeLeft.js17
-rw-r--r--pyload/web/app/scripts/helpers/gettext.js16
-rw-r--r--pyload/web/app/scripts/helpers/ifEq.js14
-rw-r--r--pyload/web/app/scripts/helpers/linkStatus.js18
-rw-r--r--pyload/web/app/scripts/helpers/pluginIcon.js14
-rw-r--r--pyload/web/app/scripts/helpers/truncate.js25
9 files changed, 213 insertions, 0 deletions
diff --git a/pyload/web/app/scripts/helpers/fileHelper.js b/pyload/web/app/scripts/helpers/fileHelper.js
new file mode 100644
index 000000000..2e14f939f
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/fileHelper.js
@@ -0,0 +1,69 @@
+// Helpers to render the file view
+define('helpers/fileHelper', ['handlebars', 'utils/apitypes', 'helpers/formatTimeLeft'],
+ function(Handlebars, Api, formatTime) {
+ 'use strict';
+
+ function fileClass(file, options) {
+ if (file.finished)
+ return 'finished';
+ else if (file.failed)
+ return 'failed';
+ else if (file.offline)
+ return 'offline';
+ else if (file.online)
+ return 'online';
+ else if (file.waiting)
+ return 'waiting';
+ else if (file.downloading)
+ return 'downloading';
+
+ return '';
+ }
+
+ function fileIcon(media, options) {
+ switch (media) {
+ case Api.MediaType.Audio:
+ return 'icon-music';
+ case Api.MediaType.Image:
+ return 'icon-picture';
+ case Api.MediaType.Video:
+ return 'icon-film';
+ case Api.MediaType.Document:
+ return 'icon-file-text';
+ case Api.MediaType.Archive:
+ return 'icon-archive';
+ case Api.MediaType.Executable:
+ return 'icon-cog';
+ default:
+ return 'icon-file-alt';
+ }
+ }
+
+ // TODO rest of the states
+ function fileStatus(file, options) {
+ var s;
+ var msg = file.download.statusmsg;
+
+ if (file.failed) {
+ s = '<i class="icon-remove"></i>&nbsp;';
+ if (file.download.error)
+ s += file.download.error;
+ else s += msg;
+ } else if (file.finished)
+ s = '<i class="icon-ok"></i>&nbsp;' + msg;
+ else if (file.downloading)
+ s = '<div class="progress"><div class="bar" style="width: ' + file.progress + '%">&nbsp;&nbsp;' +
+ formatTime(file.eta) + '</div></div>';
+ else if (file.waiting)
+ s = '<i class="icon-time"></i>&nbsp;' + formatTime(file.eta);
+ else
+ s = msg;
+
+ return new Handlebars.SafeString(s);
+ }
+
+ Handlebars.registerHelper('fileClass', fileClass);
+ Handlebars.registerHelper('fileIcon', fileIcon);
+ Handlebars.registerHelper('fileStatus', fileStatus);
+ return fileClass;
+ }); \ No newline at end of file
diff --git a/pyload/web/app/scripts/helpers/formatSize.js b/pyload/web/app/scripts/helpers/formatSize.js
new file mode 100644
index 000000000..f72d62158
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/formatSize.js
@@ -0,0 +1,20 @@
+// Format bytes in human readable format
+define('helpers/formatSize', ['handlebars', 'utils/i18n'], function(Handlebars, i18n) {
+ 'use strict';
+
+ var sizes = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB'];
+ function formatSize(bytes, options) {
+ if (!bytes || bytes === 0) return '0 B';
+ if (bytes === -1)
+ return i18n.gettext('not available');
+ if (bytes === -2)
+ return i18n.gettext('unlimited');
+
+ var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)), 10);
+ // round to two digits
+ return (bytes / Math.pow(1024, i)).toFixed(2) + ' ' + sizes[i];
+ }
+
+ Handlebars.registerHelper('formatSize', formatSize);
+ return formatSize;
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/helpers/formatTime.js b/pyload/web/app/scripts/helpers/formatTime.js
new file mode 100644
index 000000000..750ce58fe
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/formatTime.js
@@ -0,0 +1,20 @@
+// Formats a timestamp
+define('helpers/formatTime', ['underscore','handlebars', 'moment', 'utils/i18n'],
+ function(_, Handlebars, moment, i18n) {
+ 'use strict';
+
+ function formatTime(time, format) {
+ if (time === -1)
+ return i18n.gettext('unknown');
+ else if (time === -2)
+ return i18n.gettext('unlimited');
+
+ if (!_.isString(format))
+ format = 'lll';
+
+ return moment(time).format(format);
+ }
+
+ Handlebars.registerHelper('formatTime', formatTime);
+ return formatTime;
+});
diff --git a/pyload/web/app/scripts/helpers/formatTimeLeft.js b/pyload/web/app/scripts/helpers/formatTimeLeft.js
new file mode 100644
index 000000000..dafeda3e2
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/formatTimeLeft.js
@@ -0,0 +1,17 @@
+// Format seconds in human readable format
+define('helpers/formatTimeLeft', ['handlebars', 'vendor/remaining'], function(Handlebars, Remaining) {
+ 'use strict';
+
+ function formatTimeLeft(seconds, options) {
+ if (seconds === Infinity)
+ return '∞';
+ else if (!seconds || seconds <= 0)
+ return '-';
+
+ // TODO: digital or written string
+ return Remaining.getStringDigital(seconds, window.dates);
+ }
+
+ Handlebars.registerHelper('formatTimeLeft', formatTimeLeft);
+ return formatTimeLeft;
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/helpers/gettext.js b/pyload/web/app/scripts/helpers/gettext.js
new file mode 100644
index 000000000..d73b5e378
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/gettext.js
@@ -0,0 +1,16 @@
+require(['underscore', 'handlebars', 'utils/i18n'], function(_, Handlebars, i18n) {
+ 'use strict';
+ // These methods binds additional content directly to translated message
+ function ngettext(single, plural, n) {
+ return i18n.sprintf(i18n.ngettext(single, plural, n), n);
+ }
+
+ function gettext(key, message) {
+ return i18n.sprintf(i18n.gettext(key), message);
+ }
+
+ Handlebars.registerHelper('_', gettext);
+ Handlebars.registerHelper('gettext', gettext);
+ Handlebars.registerHelper('ngettext', ngettext);
+ return gettext;
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/helpers/ifEq.js b/pyload/web/app/scripts/helpers/ifEq.js
new file mode 100644
index 000000000..1c8a71b61
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/ifEq.js
@@ -0,0 +1,14 @@
+define('helpers/ifEq', ['underscore', 'handlebars'],
+ function(_, Handlebars) {
+ /*jshint validthis:true */
+ 'use strict';
+ function ifEq(v1, v2, options) {
+ if (v1 === v2) {
+ return options.fn(this);
+ }
+ return options.inverse(this);
+ }
+
+ Handlebars.registerHelper('ifEq', ifEq);
+ return ifEq;
+ });
diff --git a/pyload/web/app/scripts/helpers/linkStatus.js b/pyload/web/app/scripts/helpers/linkStatus.js
new file mode 100644
index 000000000..448d63691
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/linkStatus.js
@@ -0,0 +1,18 @@
+define('helpers/linkStatus', ['underscore', 'handlebars', 'utils/apitypes', 'utils/i18n'],
+ function(_, Handlebars, Api, i18n) {
+ 'use strict';
+ function linkStatus(status) {
+ var s;
+ if (status === Api.DownloadStatus.Online)
+ s = '<span class="text-success">' + i18n.gettext('online') + '</span>';
+ else if (status === Api.DownloadStatus.Offline)
+ s = '<span class="text-error">' + i18n.gettext('offline') + '</span>';
+ else
+ s = '<span class="text-info">' + i18n.gettext('unknown') + '</span>';
+
+ return new Handlebars.SafeString(s);
+ }
+
+ Handlebars.registerHelper('linkStatus', linkStatus);
+ return linkStatus;
+ });
diff --git a/pyload/web/app/scripts/helpers/pluginIcon.js b/pyload/web/app/scripts/helpers/pluginIcon.js
new file mode 100644
index 000000000..1004c2487
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/pluginIcon.js
@@ -0,0 +1,14 @@
+// Resolves name of plugin to icon path
+define('helpers/pluginIcon', ['handlebars', 'app'], function(Handlebars, App) {
+ 'use strict';
+
+ function pluginIcon(name) {
+ if (name && typeof name === 'object' && typeof name.get === 'function')
+ name = name.get('plugin');
+
+ return App.apiUrl('icons/' + name);
+ }
+
+ Handlebars.registerHelper('pluginIcon', pluginIcon);
+ return pluginIcon;
+}); \ No newline at end of file
diff --git a/pyload/web/app/scripts/helpers/truncate.js b/pyload/web/app/scripts/helpers/truncate.js
new file mode 100644
index 000000000..fb351b776
--- /dev/null
+++ b/pyload/web/app/scripts/helpers/truncate.js
@@ -0,0 +1,25 @@
+require(['underscore','handlebars'], function(_, Handlebars) {
+ 'use strict';
+
+ function truncate(fullStr, options) {
+ var strLen = 30;
+ if (_.isNumber(options))
+ strLen = options;
+
+ if (fullStr.length <= strLen) return fullStr;
+
+ var separator = options.separator || '…';
+
+ var sepLen = separator.length,
+ charsToShow = strLen - sepLen,
+ frontChars = Math.ceil(charsToShow / 2),
+ backChars = Math.floor(charsToShow / 2);
+
+ return fullStr.substr(0, frontChars) +
+ separator +
+ fullStr.substr(fullStr.length - backChars);
+ }
+
+ Handlebars.registerHelper('truncate', truncate);
+ return truncate;
+}); \ No newline at end of file