|
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.AutoUpdateTime = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
|
"use strict"; |
|
|
|
var _interopRequire = function (obj) { return obj && obj.__esModule ? obj["default"] : obj; }; |
|
|
|
var _createClass = (function () { function defineProperties(target, props) { for (var key in props) { var prop = props[key]; prop.configurable = true; if (prop.value) prop.writable = true; } Object.defineProperties(target, props); } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })(); |
|
|
|
var _classCallCheck = function (instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }; |
|
|
|
var diff = _interopRequire(require("./diff.js")); |
|
|
|
if (!Date.now) { |
|
Date.now = function now() { |
|
return new Date().getTime(); |
|
}; |
|
} |
|
|
|
if (!Function.prototype.bind) { |
|
Function.prototype.bind = function (oThis) { |
|
if (typeof this !== "function") { |
|
// closest thing possible to the ECMAScript 5 |
|
// internal IsCallable function |
|
throw new TypeError("Function.prototype.bind - what is trying to be bound is not callable"); |
|
} |
|
|
|
var aArgs = Array.prototype.slice.call(arguments, 1), |
|
fToBind = this, |
|
fNOP = function fNOP() {}, |
|
fBound = function fBound() { |
|
return fToBind.apply(this instanceof fNOP ? this : oThis, aArgs.concat(Array.prototype.slice.call(arguments))); |
|
}; |
|
|
|
fNOP.prototype = this.prototype; |
|
fBound.prototype = new fNOP(); |
|
|
|
return fBound; |
|
}; |
|
} |
|
|
|
var AutoUpdateTime = (function () { |
|
function AutoUpdateTime(value, callback, options) { |
|
_classCallCheck(this, AutoUpdateTime); |
|
|
|
options = options || {}; |
|
|
|
this.value = value ? new Date(value).getTime() : Date.now(); |
|
this.callback = callback; |
|
|
|
this.units = options.units; |
|
|
|
this._intervalHandler = this._intervalHandler.bind(this); |
|
this._intervalHandler(); |
|
} |
|
|
|
_createClass(AutoUpdateTime, { |
|
_calcUnits: { |
|
value: function _calcUnits(now, date) { |
|
var diffReport = diff(now, date); |
|
return this.units || AutoUpdateTime._selectUnits(diffReport); |
|
} |
|
}, |
|
_intervalHandler: { |
|
value: function _intervalHandler() { |
|
if (this.callback && this.intervalTime) this.callback(this.intervalTime); |
|
|
|
var intervalTime = AutoUpdateTime._calcInterval(this._calcUnits(Date.now(), this.value)); |
|
if (this.intervalTime !== intervalTime) { |
|
clearInterval(this.intervalId); |
|
this.intervalId = setInterval(this._intervalHandler, intervalTime); |
|
this.intervalTime = intervalTime; |
|
} |
|
} |
|
}, |
|
clear: { |
|
value: function clear() { |
|
clearInterval(this.intervalId); |
|
} |
|
} |
|
}, { |
|
_selectUnits: { |
|
value: function _selectUnits(diffReport) { |
|
var i, l, units; |
|
|
|
for (i = 0, l = AutoUpdateTime.FIELDS.length; i < l; i += 1) { |
|
units = AutoUpdateTime.FIELDS[i]; |
|
|
|
if (Math.abs(diffReport[units]) < AutoUpdateTime.thresholds[units]) { |
|
break; |
|
} |
|
} |
|
|
|
return units; |
|
} |
|
}, |
|
_calcInterval: { |
|
value: function _calcInterval(units) { |
|
return 1000 * AutoUpdateTime.intervals[units]; |
|
} |
|
} |
|
}); |
|
|
|
return AutoUpdateTime; |
|
})(); |
|
|
|
AutoUpdateTime.FIELDS = ["second", "minute", "hour"]; |
|
|
|
AutoUpdateTime.thresholds = { |
|
second: 60, |
|
minute: 60, |
|
hour: 24 |
|
}; |
|
|
|
AutoUpdateTime.intervals = { |
|
second: 1, |
|
minute: 30, // no more than 30 sec delay |
|
hour: 15 * 60 // no more than 15 min delay |
|
}; |
|
|
|
module.exports = AutoUpdateTime; |
|
|
|
},{"./diff.js":2}],2:[function(require,module,exports){ |
|
/* |
|
Copyright (c) 2014, Yahoo! Inc. All rights reserved. |
|
Copyrights licensed under the New BSD License. |
|
See the accompanying LICENSE file for terms. |
|
*/ |
|
|
|
/* jslint esnext: true */ |
|
|
|
"use strict"; |
|
|
|
var round = Math.round; |
|
|
|
function daysToYears(days) { |
|
// 400 years have 146097 days (taking into account leap year rules) |
|
return days * 400 / 146097; |
|
} |
|
|
|
module.exports = function (from, to) { |
|
// Convert to ms timestamps. |
|
from = +from; |
|
to = +to; |
|
|
|
var millisecond = round(to - from), |
|
second = round(millisecond / 1000), |
|
minute = round(second / 60), |
|
hour = round(minute / 60), |
|
day = round(hour / 24), |
|
week = round(day / 7); |
|
|
|
var rawYears = daysToYears(day), |
|
month = round(rawYears * 12), |
|
year = round(rawYears); |
|
|
|
return { |
|
millisecond: millisecond, |
|
second: second, |
|
minute: minute, |
|
hour: hour, |
|
day: day, |
|
week: week, |
|
month: month, |
|
year: year |
|
}; |
|
}; |
|
|
|
},{}]},{},[1])(1) |
|
}); |