diff options
Diffstat (limited to 'module/web/static/js/helpers')
-rw-r--r-- | module/web/static/js/helpers/fileHelper.js | 54 | ||||
-rw-r--r-- | module/web/static/js/helpers/formatSize.js | 13 | ||||
-rw-r--r-- | module/web/static/js/helpers/formatTime.js | 17 |
3 files changed, 84 insertions, 0 deletions
diff --git a/module/web/static/js/helpers/fileHelper.js b/module/web/static/js/helpers/fileHelper.js new file mode 100644 index 000000000..085c222aa --- /dev/null +++ b/module/web/static/js/helpers/fileHelper.js @@ -0,0 +1,54 @@ +// Helpers to render the file view +define('helpers/fileHelper', ['handlebars', 'utils/apitypes', 'helpers/formatTime'], + function(Handlebars, Api, formatTime) { + + 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 ""; + } + + // TODO + function fileIcon(media, options) { + return 'iconf-music'; + } + + // TODO rest of the states + function fileStatus(file, options) { + var s; + var msg = file.download.statusmsg; + + if (file.failed) { + s = "<i class='iconf-remove'></i> "; + if (file.download.error) + s += file.download.error; + else s += msg; + } else if (file.finished) + s = "<i class='iconf-ok'></i> " + msg; + else if (file.downloading) + s = "<div class='progress'><div class='bar' style='width: " + file.progress + "%'> " + + formatTime(file.eta) + "</div></div>"; + else if (file.waiting) + s = "<i class='iconf-time'></i> " + 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/module/web/static/js/helpers/formatSize.js b/module/web/static/js/helpers/formatSize.js new file mode 100644 index 000000000..a50588bc6 --- /dev/null +++ b/module/web/static/js/helpers/formatSize.js @@ -0,0 +1,13 @@ +// Format bytes in human readable format +define('helpers/formatSize', ['handlebars'], function(Handlebars) { + var sizes = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"]; + function formatSize(bytes, options) { + if (!bytes || bytes === 0) return '0 B'; + var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024))); + // 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/module/web/static/js/helpers/formatTime.js b/module/web/static/js/helpers/formatTime.js new file mode 100644 index 000000000..77d67a39c --- /dev/null +++ b/module/web/static/js/helpers/formatTime.js @@ -0,0 +1,17 @@ +// Format bytes in human readable format +define('helpers/formatTime', ['handlebars', 'utils/remaining'], function(Handlebars, Remaining) { + + + function formatTime(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('formatTime', formatTime); + return formatTime; +});
\ No newline at end of file |