nginx-proxy-manager/src/frontend/js/lib/marionette.js

119 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-06-19 18:48:14 -04:00
'use strict';
const _ = require('underscore');
const Mn = require('backbone.marionette');
const moment = require('moment');
const numeral = require('numeral');
2018-07-18 00:28:41 -04:00
const i18n = require('../app/i18n');
2018-06-19 18:48:14 -04:00
let render = Mn.Renderer.render;
Mn.Renderer.render = function (template, data, view) {
data = _.clone(data);
2018-07-18 00:28:41 -04:00
data.i18n = i18n;
2018-06-19 18:48:14 -04:00
/**
* @param {Integer} number
* @returns {String}
*/
data.niceNumber = function (number) {
return numeral(number).format('0,0');
};
/**
* @param {Integer} seconds
* @returns {String}
*/
data.secondsToTime = function (seconds) {
let sec_num = parseInt(seconds, 10);
let minutes = Math.floor(sec_num / 60);
let sec = sec_num - (minutes * 60);
if (sec < 10) {
sec = '0' + sec;
}
return minutes + ':' + sec;
};
/**
* @param {String} date
* @returns {String}
*/
2018-06-25 02:56:13 -04:00
data.formatDbDate = function (date, format) {
2018-06-19 18:48:14 -04:00
if (typeof date === 'number') {
2018-06-25 02:56:13 -04:00
return moment.unix(date).format(format);
2018-06-19 18:48:14 -04:00
}
2018-06-25 02:56:13 -04:00
return moment(date).format(format);
2018-06-19 18:48:14 -04:00
};
/**
* @param {String} date
* @returns {String}
*/
2018-06-25 02:56:13 -04:00
data.shortDate = function (date) {
let shortdate = data.formatDbDate(date, 'YYYY-MM-DD');
2018-06-19 18:48:14 -04:00
2018-06-25 02:56:13 -04:00
return moment().format('YYYY-MM-DD') === shortdate ? 'Today' : shortdate;
};
2018-06-19 18:48:14 -04:00
2018-06-25 02:56:13 -04:00
/**
* @param {String} date
* @returns {String}
*/
data.shortTime = function (date) {
return data.formatDbDate(date, 'H:mm A');
2018-06-19 18:48:14 -04:00
};
/**
* @param {String} string
* @returns {String}
*/
data.escape = function (string) {
let entityMap = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
'\'': '&#39;',
'/': '&#x2F;'
};
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
};
/**
* @param {String} string
* @param {Integer} length
* @returns {String}
*/
data.trim = function (string, length) {
if (string.length > length) {
let trimmedString = string.substr(0, length);
return trimmedString.substr(0, Math.min(trimmedString.length, trimmedString.lastIndexOf(' '))) + '...';
}
return string;
};
/**
* @param {String} name
* @returns {String}
*/
data.niceVarName = function (name) {
return name.replace('_', ' ')
.replace(/^(.)|\s+(.)/g, function ($1) {
return $1.toUpperCase();
});
};
return render.call(this, template, data, view);
};
module.exports = Mn;