mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-09 04:46:00 +10:00
add angular-moment v0.8.2
This commit is contained in:
+416
@@ -0,0 +1,416 @@
|
||||
/* angular-moment.js / v0.8.2 / (c) 2013, 2014 Uri Shaked / MIT Licence */
|
||||
|
||||
/* global define */
|
||||
|
||||
(function () {
|
||||
'use strict';
|
||||
|
||||
function angularMoment(angular, moment) {
|
||||
|
||||
/**
|
||||
* @ngdoc overview
|
||||
* @name angularMoment
|
||||
*
|
||||
* @description
|
||||
* angularMoment module provides moment.js functionality for angular.js apps.
|
||||
*/
|
||||
return angular.module('angularMoment', [])
|
||||
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angularMoment.config:angularMomentConfig
|
||||
*
|
||||
* @description
|
||||
* Common configuration of the angularMoment module
|
||||
*/
|
||||
.constant('angularMomentConfig', {
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angularMoment.config.angularMomentConfig#preprocess
|
||||
* @propertyOf angularMoment.config:angularMomentConfig
|
||||
* @returns {string} The default preprocessor to apply
|
||||
*
|
||||
* @description
|
||||
* Defines a default preprocessor to apply (e.g. 'unix', 'etc', ...). The default value is null,
|
||||
* i.e. no preprocessor will be applied.
|
||||
*/
|
||||
preprocess: null, // e.g. 'unix', 'utc', ...
|
||||
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angularMoment.config.angularMomentConfig#timezone
|
||||
* @propertyOf angularMoment.config:angularMomentConfig
|
||||
* @returns {string} The default timezone
|
||||
*
|
||||
* @description
|
||||
* The default timezone (e.g. 'Europe/London'). Empty string by default (does not apply
|
||||
* any timezone shift).
|
||||
*/
|
||||
timezone: '',
|
||||
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angularMoment.config.angularMomentConfig#format
|
||||
* @propertyOf angularMoment.config:angularMomentConfig
|
||||
* @returns {string} The pre-conversion format of the date
|
||||
*
|
||||
* @description
|
||||
* Specify the format of the input date. Essentially it's a
|
||||
* default and saves you from specifying a format in every
|
||||
* element. Overridden by element attr. Null by default.
|
||||
*/
|
||||
format: null
|
||||
})
|
||||
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angularMoment.object:moment
|
||||
*
|
||||
* @description
|
||||
* moment global (as provided by the moment.js library)
|
||||
*/
|
||||
.constant('moment', moment)
|
||||
|
||||
/**
|
||||
* @ngdoc object
|
||||
* @name angularMoment.config:amTimeAgoConfig
|
||||
* @module angularMoment
|
||||
*
|
||||
* @description
|
||||
* configuration specific to the amTimeAgo directive
|
||||
*/
|
||||
.constant('amTimeAgoConfig', {
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angularMoment.config.amTimeAgoConfig#withoutSuffix
|
||||
* @propertyOf angularMoment.config:amTimeAgoConfig
|
||||
* @returns {boolean} Whether to include a suffix in am-time-ago directive
|
||||
*
|
||||
* @description
|
||||
* Defaults to false.
|
||||
*/
|
||||
withoutSuffix: false,
|
||||
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angularMoment.config.amTimeAgoConfig#serverTime
|
||||
* @propertyOf angularMoment.config:amTimeAgoConfig
|
||||
* @returns {number} Server time in milliseconds since the epoch
|
||||
*
|
||||
* @description
|
||||
* If set, time ago will be calculated relative to the given value.
|
||||
* If null, local time will be used. Defaults to null.
|
||||
*/
|
||||
serverTime: null
|
||||
})
|
||||
|
||||
/**
|
||||
* @ngdoc directive
|
||||
* @name angularMoment.directive:amTimeAgo
|
||||
* @module angularMoment
|
||||
*
|
||||
* @restrict A
|
||||
*/
|
||||
.directive('amTimeAgo', ['$window', 'moment', 'amMoment', 'amTimeAgoConfig', 'angularMomentConfig', function ($window, moment, amMoment, amTimeAgoConfig, angularMomentConfig) {
|
||||
|
||||
return function (scope, element, attr) {
|
||||
var activeTimeout = null;
|
||||
var currentValue;
|
||||
var currentFormat = angularMomentConfig.format;
|
||||
var withoutSuffix = amTimeAgoConfig.withoutSuffix;
|
||||
var localDate = new Date().getTime();
|
||||
var preprocess = angularMomentConfig.preprocess;
|
||||
var modelName = attr.amTimeAgo.replace(/^::/, '');
|
||||
var isBindOnce = (attr.amTimeAgo.indexOf('::') === 0);
|
||||
var isTimeElement = ('TIME' === element[0].nodeName.toUpperCase());
|
||||
var unwatchChanges;
|
||||
|
||||
function getNow() {
|
||||
var now;
|
||||
if (amTimeAgoConfig.serverTime) {
|
||||
var localNow = new Date().getTime();
|
||||
var nowMillis = localNow - localDate + amTimeAgoConfig.serverTime;
|
||||
now = moment(nowMillis);
|
||||
}
|
||||
else {
|
||||
now = moment();
|
||||
}
|
||||
return now;
|
||||
}
|
||||
|
||||
function cancelTimer() {
|
||||
if (activeTimeout) {
|
||||
$window.clearTimeout(activeTimeout);
|
||||
activeTimeout = null;
|
||||
}
|
||||
}
|
||||
|
||||
function updateTime(momentInstance) {
|
||||
element.text(momentInstance.from(getNow(), withoutSuffix));
|
||||
if (!isBindOnce) {
|
||||
|
||||
var howOld = Math.abs(getNow().diff(momentInstance, 'minute'));
|
||||
var secondsUntilUpdate = 3600;
|
||||
if (howOld < 1) {
|
||||
secondsUntilUpdate = 1;
|
||||
} else if (howOld < 60) {
|
||||
secondsUntilUpdate = 30;
|
||||
} else if (howOld < 180) {
|
||||
secondsUntilUpdate = 300;
|
||||
}
|
||||
|
||||
activeTimeout = $window.setTimeout(function () {
|
||||
updateTime(momentInstance);
|
||||
}, secondsUntilUpdate * 1000);
|
||||
}
|
||||
}
|
||||
|
||||
function updateDateTimeAttr(value) {
|
||||
if (isTimeElement) {
|
||||
element.attr('datetime', value);
|
||||
}
|
||||
}
|
||||
|
||||
function updateMoment() {
|
||||
cancelTimer();
|
||||
if (currentValue) {
|
||||
var momentValue = amMoment.preprocessDate(currentValue, preprocess, currentFormat);
|
||||
updateTime(momentValue);
|
||||
updateDateTimeAttr(momentValue.toISOString());
|
||||
}
|
||||
}
|
||||
|
||||
unwatchChanges = scope.$watch(modelName, function (value) {
|
||||
if ((typeof value === 'undefined') || (value === null) || (value === '')) {
|
||||
cancelTimer();
|
||||
if (currentValue) {
|
||||
element.text('');
|
||||
updateDateTimeAttr('');
|
||||
currentValue = null;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
currentValue = value;
|
||||
updateMoment();
|
||||
|
||||
if (value !== undefined && isBindOnce) {
|
||||
unwatchChanges();
|
||||
}
|
||||
});
|
||||
|
||||
if (angular.isDefined(attr.amWithoutSuffix)) {
|
||||
scope.$watch(attr.amWithoutSuffix, function (value) {
|
||||
if (typeof value === 'boolean') {
|
||||
withoutSuffix = value;
|
||||
updateMoment();
|
||||
} else {
|
||||
withoutSuffix = amTimeAgoConfig.withoutSuffix;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
attr.$observe('amFormat', function (format) {
|
||||
if (typeof format !== 'undefined') {
|
||||
currentFormat = format;
|
||||
updateMoment();
|
||||
}
|
||||
});
|
||||
|
||||
attr.$observe('amPreprocess', function (newValue) {
|
||||
preprocess = newValue;
|
||||
updateMoment();
|
||||
});
|
||||
|
||||
scope.$on('$destroy', function () {
|
||||
cancelTimer();
|
||||
});
|
||||
|
||||
scope.$on('amMoment:localeChanged', function () {
|
||||
updateMoment();
|
||||
});
|
||||
};
|
||||
}])
|
||||
|
||||
/**
|
||||
* @ngdoc service
|
||||
* @name angularMoment.service.amMoment
|
||||
* @module angularMoment
|
||||
*/
|
||||
.service('amMoment', ['moment', '$rootScope', '$log', 'angularMomentConfig', function (moment, $rootScope, $log, angularMomentConfig) {
|
||||
var that = this;
|
||||
/**
|
||||
* @ngdoc property
|
||||
* @name angularMoment:amMoment#preprocessors
|
||||
* @module angularMoment
|
||||
*
|
||||
* @description
|
||||
* Defines the preprocessors for the preprocessDate method. By default, the following preprocessors
|
||||
* are defined: utc, unix.
|
||||
*/
|
||||
this.preprocessors = {
|
||||
utc: moment.utc,
|
||||
unix: moment.unix
|
||||
};
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angularMoment.service.amMoment#changeLocale
|
||||
* @methodOf angularMoment.service.amMoment
|
||||
*
|
||||
* @description
|
||||
* Changes the locale for moment.js and updates all the am-time-ago directive instances
|
||||
* with the new locale. Also broadcasts a `amMoment:localeChanged` event on $rootScope.
|
||||
*
|
||||
* @param {string} locale 2-letter language code (e.g. en, es, ru, etc.)
|
||||
*/
|
||||
this.changeLocale = function (locale) {
|
||||
var result = (moment.locale||moment.lang)(locale);
|
||||
if (angular.isDefined(locale)) {
|
||||
$rootScope.$broadcast('amMoment:localeChanged');
|
||||
|
||||
// The following event is deprecated and will be removed in an upcoming
|
||||
// major release.
|
||||
$rootScope.$broadcast('amMoment:languageChange');
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angularMoment.service.amMoment#changeLanguage
|
||||
* @methodOf angularMoment.service.amMoment
|
||||
* @deprecated Please use changeLocale() instead.
|
||||
*
|
||||
* @description
|
||||
* Deprecated. Please use changeLocale() instead.
|
||||
*/
|
||||
this.changeLanguage = function (lang) {
|
||||
$log.warn('angular-moment: Usage of amMoment.changeLanguage() is deprecated. Please use changeLocale()');
|
||||
return that.changeLocale(lang);
|
||||
};
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angularMoment.service.amMoment#preprocessDate
|
||||
* @methodOf angularMoment.service.amMoment
|
||||
*
|
||||
* @description
|
||||
* Preprocess a given value and convert it into a Moment instance appropriate for use in the
|
||||
* am-time-ago directive and the filters.
|
||||
*
|
||||
* @param {*} value The value to be preprocessed
|
||||
* @param {string} preprocess The name of the preprocessor the apply (e.g. utc, unix)
|
||||
* @param {string=} format Specifies how to parse the value (see {@link http://momentjs.com/docs/#/parsing/string-format/})
|
||||
* @return {Moment} A value that can be parsed by the moment library
|
||||
*/
|
||||
this.preprocessDate = function (value, preprocess, format) {
|
||||
if (angular.isUndefined(preprocess)) {
|
||||
preprocess = angularMomentConfig.preprocess;
|
||||
}
|
||||
if (this.preprocessors[preprocess]) {
|
||||
return this.preprocessors[preprocess](value, format);
|
||||
}
|
||||
if (preprocess) {
|
||||
$log.warn('angular-moment: Ignoring unsupported value for preprocess: ' + preprocess);
|
||||
}
|
||||
if (!isNaN(parseFloat(value)) && isFinite(value)) {
|
||||
// Milliseconds since the epoch
|
||||
return moment(parseInt(value, 10));
|
||||
}
|
||||
// else just returns the value as-is.
|
||||
return moment(value, format);
|
||||
};
|
||||
|
||||
/**
|
||||
* @ngdoc function
|
||||
* @name angularMoment.service.amMoment#applyTimezone
|
||||
* @methodOf angularMoment.service.amMoment
|
||||
*
|
||||
* @description
|
||||
* Apply a timezone onto a given moment object - if moment-timezone.js is included
|
||||
* Otherwise, it'll not apply any timezone shift.
|
||||
*
|
||||
* @param {Moment} aMoment a moment() instance to apply the timezone shift to
|
||||
* @returns {Moment} The given moment with the timezone shift applied
|
||||
*/
|
||||
this.applyTimezone = function (aMoment) {
|
||||
var timezone = angularMomentConfig.timezone;
|
||||
if (aMoment && timezone) {
|
||||
if (aMoment.tz) {
|
||||
aMoment = aMoment.tz(timezone);
|
||||
} else {
|
||||
$log.warn('angular-moment: timezone specified but moment.tz() is undefined. Did you forget to include moment-timezone.js?');
|
||||
}
|
||||
}
|
||||
return aMoment;
|
||||
};
|
||||
}])
|
||||
|
||||
/**
|
||||
* @ngdoc filter
|
||||
* @name angularMoment.filter:amCalendar
|
||||
* @module angularMoment
|
||||
*/
|
||||
.filter('amCalendar', ['moment', 'amMoment', function (moment, amMoment) {
|
||||
return function (value, preprocess) {
|
||||
if (typeof value === 'undefined' || value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
value = amMoment.preprocessDate(value, preprocess);
|
||||
var date = moment(value);
|
||||
if (!date.isValid()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return amMoment.applyTimezone(date).calendar();
|
||||
};
|
||||
}])
|
||||
|
||||
/**
|
||||
* @ngdoc filter
|
||||
* @name angularMoment.filter:amDateFormat
|
||||
* @module angularMoment
|
||||
* @function
|
||||
*/
|
||||
.filter('amDateFormat', ['moment', 'amMoment', function (moment, amMoment) {
|
||||
return function (value, format, preprocess) {
|
||||
if (typeof value === 'undefined' || value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
value = amMoment.preprocessDate(value, preprocess);
|
||||
var date = moment(value);
|
||||
if (!date.isValid()) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return amMoment.applyTimezone(date).format(format);
|
||||
};
|
||||
}])
|
||||
|
||||
/**
|
||||
* @ngdoc filter
|
||||
* @name angularMoment.filter:amDurationFormat
|
||||
* @module angularMoment
|
||||
* @function
|
||||
*/
|
||||
.filter('amDurationFormat', ['moment', function (moment) {
|
||||
return function (value, format, suffix) {
|
||||
if (typeof value === 'undefined' || value === null) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return moment.duration(value, format).humanize(suffix);
|
||||
};
|
||||
}]);
|
||||
}
|
||||
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
define('angular-moment', ['angular', 'moment'], angularMoment);
|
||||
} else {
|
||||
angularMoment(angular, window.moment);
|
||||
}
|
||||
})();
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
!function(){"use strict";function a(a,b){return a.module("angularMoment",[]).constant("angularMomentConfig",{preprocess:null,timezone:"",format:null}).constant("moment",b).constant("amTimeAgoConfig",{withoutSuffix:!1,serverTime:null}).directive("amTimeAgo",["$window","moment","amMoment","amTimeAgoConfig","angularMomentConfig",function(b,c,d,e,f){return function(g,h,i){function j(){var a;if(e.serverTime){var b=(new Date).getTime(),d=b-t+e.serverTime;a=c(d)}else a=c();return a}function k(){q&&(b.clearTimeout(q),q=null)}function l(a){if(h.text(a.from(j(),s)),!w){var c=Math.abs(j().diff(a,"minute")),d=3600;1>c?d=1:60>c?d=30:180>c&&(d=300),q=b.setTimeout(function(){l(a)},1e3*d)}}function m(a){x&&h.attr("datetime",a)}function n(){if(k(),o){var a=d.preprocessDate(o,u,r);l(a),m(a.toISOString())}}var o,p,q=null,r=f.format,s=e.withoutSuffix,t=(new Date).getTime(),u=f.preprocess,v=i.amTimeAgo.replace(/^::/,""),w=0===i.amTimeAgo.indexOf("::"),x="TIME"===h[0].nodeName.toUpperCase();p=g.$watch(v,function(a){return"undefined"==typeof a||null===a||""===a?(k(),void(o&&(h.text(""),m(""),o=null))):(o=a,n(),void(void 0!==a&&w&&p()))}),a.isDefined(i.amWithoutSuffix)&&g.$watch(i.amWithoutSuffix,function(a){"boolean"==typeof a?(s=a,n()):s=e.withoutSuffix}),i.$observe("amFormat",function(a){"undefined"!=typeof a&&(r=a,n())}),i.$observe("amPreprocess",function(a){u=a,n()}),g.$on("$destroy",function(){k()}),g.$on("amMoment:localeChanged",function(){n()})}}]).service("amMoment",["moment","$rootScope","$log","angularMomentConfig",function(b,c,d,e){var f=this;this.preprocessors={utc:b.utc,unix:b.unix},this.changeLocale=function(d){var e=(b.locale||b.lang)(d);return a.isDefined(d)&&(c.$broadcast("amMoment:localeChanged"),c.$broadcast("amMoment:languageChange")),e},this.changeLanguage=function(a){return d.warn("angular-moment: Usage of amMoment.changeLanguage() is deprecated. Please use changeLocale()"),f.changeLocale(a)},this.preprocessDate=function(c,f,g){return a.isUndefined(f)&&(f=e.preprocess),this.preprocessors[f]?this.preprocessors[f](c,g):(f&&d.warn("angular-moment: Ignoring unsupported value for preprocess: "+f),!isNaN(parseFloat(c))&&isFinite(c)?b(parseInt(c,10)):b(c,g))},this.applyTimezone=function(a){var b=e.timezone;return a&&b&&(a.tz?a=a.tz(b):d.warn("angular-moment: timezone specified but moment.tz() is undefined. Did you forget to include moment-timezone.js?")),a}}]).filter("amCalendar",["moment","amMoment",function(a,b){return function(c,d){if("undefined"==typeof c||null===c)return"";c=b.preprocessDate(c,d);var e=a(c);return e.isValid()?b.applyTimezone(e).calendar():""}}]).filter("amDateFormat",["moment","amMoment",function(a,b){return function(c,d,e){if("undefined"==typeof c||null===c)return"";c=b.preprocessDate(c,e);var f=a(c);return f.isValid()?b.applyTimezone(f).format(d):""}}]).filter("amDurationFormat",["moment",function(a){return function(b,c,d){return"undefined"==typeof b||null===b?"":a.duration(b,c).humanize(d)}}])}"function"==typeof define&&define.amd?define("angular-moment",["angular","moment"],a):a(angular,window.moment)}();
|
||||
//# sourceMappingURL=angular-moment.min.js.map
|
||||
@@ -2,7 +2,7 @@
|
||||
"name": "angular-moment",
|
||||
"filename": "angular-moment.min.js",
|
||||
"description": "Angular.JS directives for Moment.JS (timeago alternative)",
|
||||
"version": "0.8.1",
|
||||
"version": "0.8.2",
|
||||
"npmName": "angular-moment",
|
||||
"npmFileMap": [
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user