diff options
Diffstat (limited to 'web/static/app/diller/global.js')
-rw-r--r-- | web/static/app/diller/global.js | 72 |
1 files changed, 52 insertions, 20 deletions
diff --git a/web/static/app/diller/global.js b/web/static/app/diller/global.js index 0da8da7..866b124 100644 --- a/web/static/app/diller/global.js +++ b/web/static/app/diller/global.js @@ -21,40 +21,72 @@ } } - Diller.Interval = function (from, to) { - var f = toDate(from), - t = toDate(to); + Diller.Interval = function (from, to, level) { + // console.log('Diller.Interval, from', from && from.toISOString(), 'to', to && to.toISOString(), 'level', level); + var f = moment.isMoment(from) && from || moment(from), + t = moment.isMoment(to) && to || moment(to); - if (f.isAfter(t)) { + f = f.isValid() ? f : moment(); + t = t.isValid() ? t : moment(); + + if (f && t && f.isAfter(t)) { var tmp = f; f = t; t = tmp; } - return { - getFrom: function () { - return f || moment(); - }, - getTo: function () { - return t || moment(); - }, - toString: function () { - return 'yo' + var diffMs = t.diff(f); + var diffMinutes = diffMs / (1000 * 60); + var diffHours = diffMinutes / 60; + + var l = level; + + if (!l) { + if (diffMinutes < 100) { + l = 'minute'; + } else if (diffHours < 100) { + l = 'hour'; + } else { + l = 'day'; } - }; - }; + } + + function getFrom() { + return f; + } + + function getTo() { + return t; + } - Diller.Interval.create = function (value) { - if (value instanceof Diller.Interval) { - return value; + function getLevel() { + return l; } - return new Diller.Interval.hours(24); + /** @lends Diller.Interval.prototype */ + return { + getFrom: getFrom, + getTo: getTo, + getLevel: getLevel + }; }; Diller.Interval.hours = function (hours) { var to = moment(); - var from = to.subtract(hours, 'hours'); + var from = moment(to).subtract(hours, 'hours'); + + return new Diller.Interval(from, to); + }; + + Diller.Interval.days = function (days) { + var to = moment(); + var from = moment(to).subtract(hours, 'days'); + return new Diller.Interval(from, to); + }; + + Diller.Interval.months = function (months) { + var to = moment(); + var from = moment(to).subtract(months, 'months'); return new Diller.Interval(from, to); }; })(); |