fix date locale angular scheduler

This commit is contained in:
jlmitch5
2016-08-24 11:39:16 -04:00
committed by GitHub
parent 75f513e591
commit 0baeafa1f1

View File

@@ -1,5 +1,5 @@
/***************************************************************************
* angular-scheruler.js
* angular-scheduler.js
*
* Copyright (c) 2014 Ansible, Inc.
*
@@ -13,16 +13,32 @@
/* global RRule */
'use strict';
angular.module('underscore',[])
.factory('_', [ function() {
return window._;
}]);
angular.module('AngularScheduler', ['underscore'])
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['lodash', 'angular', 'jquery', 'jquery-ui', 'moment'], factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory(require('lodash'), require('angular'), require('jquery'), require('jquery-ui'), require('moment'));
} else {
// Browser globals (root is window)
root.returnExports = factory(root._, root.angular, root.$, root.$.ui, root.moment);
}
}(this, function(_, angular, $, ui, moment) {
return angular.module('AngularScheduler', ['moment'])
.filter('schedulerDate', function() {
return function(input) {
var date;
if(input === null){
return "";
}else {
date = moment(input);
return date.format('l') + input.slice(input.indexOf(" "));
}
};
})
.constant('AngularScheduler.partials', '/lib/')
.constant('AngularScheduler.useTimezone', false)
@@ -30,9 +46,9 @@ angular.module('AngularScheduler', ['underscore'])
// Initialize supporting scope variables and functions. Returns a scheduler object with getString(),
// setString() and inject() methods.
.factory('SchedulerInit', ['$log', '$filter', '$timezones', 'LoadLookupValues', 'SetDefaults', 'CreateObject', '_',
.factory('SchedulerInit', ['$log', '$filter', '$timezones', 'LoadLookupValues', 'SetDefaults', 'CreateObject',
'AngularScheduler.useTimezone', 'AngularScheduler.showUTCField', 'InRange',
function($log, $filter, $timezones, LoadLookupValues, SetDefaults, CreateObject, _, useTimezone, showUTCField, InRange) {
function($log, $filter, $timezones, LoadLookupValues, SetDefaults, CreateObject, useTimezone, showUTCField, InRange) {
return function(params) {
var scope = params.scope,
@@ -63,11 +79,9 @@ angular.module('AngularScheduler', ['underscore'])
if (scope.schedulerStartDt === "" || scope.schedulerStartDt === null || scope.schedulerStartDt === undefined) {
scope.startDateError("Provide a valid start date and time");
scope.schedulerUTCTime = '';
}
else if ( !(InRange(scope.schedulerStartHour, 0, 23, 2) && InRange(scope.schedulerStartMinute, 0, 59, 2) && InRange(scope.schedulerStartSecond, 0, 59, 2)) ) {
} else if (!(InRange(scope.schedulerStartHour, 0, 23, 2) && InRange(scope.schedulerStartMinute, 0, 59, 2) && InRange(scope.schedulerStartSecond, 0, 59, 2))) {
scope.scheduler_startTime_error = true;
}
else {
} else {
if (useTimezone) {
scope.resetStartDate();
try {
@@ -79,12 +93,10 @@ angular.module('AngularScheduler', ['underscore'])
scope.schedulerUTCTime = $filter('schDateStrFix')($timezones.toUTC(dateStr, scope.schedulerTimeZone.name).toISOString());
scope.scheduler_form_schedulerStartDt_error = false;
scope.scheduler_startTime_error = false;
}
catch(e) {
} catch (e) {
scope.startDateError("Provide a valid start date and time");
}
}
else {
} else {
scope.scheduler_startTime_error = false;
scope.scheduler_form_schedulerStartDt_error = false;
scope.schedulerUTCTime = $filter('schDateStrFix')(scope.schedulerStartDt + 'T' + scope.schedulerStartHour + ':' + scope.schedulerStartMinute +
@@ -122,8 +134,7 @@ angular.module('AngularScheduler', ['underscore'])
scope.schedulerInterval = 1;
scope.schedulerShowInterval = true;
scope.schedulerIntervalLabel = scope.schedulerFrequency.intervalLabel;
}
else {
} else {
scope.schedulerShowInterval = false;
scope.schedulerEnd = scope.endOptions[0];
}
@@ -138,8 +149,7 @@ angular.module('AngularScheduler', ['underscore'])
scope.monthlyRepeatChange = function() {
if (scope.monthlyRepeatOption !== 'day') {
$('#monthDay').spinner('disable');
}
else {
} else {
$('#monthDay').spinner('enable');
}
};
@@ -147,8 +157,7 @@ angular.module('AngularScheduler', ['underscore'])
scope.yearlyRepeatChange = function() {
if (scope.yearlyRepeatOption !== 'month') {
$('#yearlyRepeatDay').spinner('disable');
}
else {
} else {
$('#yearlyRepeatDay').spinner('enable');
}
};
@@ -158,8 +167,7 @@ angular.module('AngularScheduler', ['underscore'])
var i = scope.weekDays.indexOf(day);
if (i >= 0) {
scope.weekDays.splice(i, 1);
}
else {
} else {
scope.weekDays.push(day);
}
$(event.target).blur();
@@ -204,15 +212,15 @@ angular.module('AngularScheduler', ['underscore'])
if (useTimezone) {
// Build list of timezone <select> element options
$timezones.getZoneList(scope);
}
else {
} else {
scope.setDefaults();
}
return CreateObject(scope, requireFutureStartTime);
};
}])
}
])
/**
Return an AngularScheduler object we can use to get the RRule result from user input, check if
@@ -247,23 +255,19 @@ angular.module('AngularScheduler', ['underscore'])
}
if (this.scope.schedulerFrequency.value === 'weekly') {
options.weekDays = this.scope.weekDays;
}
else if (this.scope.schedulerFrequency.value === 'yearly') {
} else if (this.scope.schedulerFrequency.value === 'yearly') {
if (this.scope.yearlyRepeatOption === 'month') {
options.month = this.scope.yearlyMonth.value;
options.monthDay = this.scope.yearlyMonthDay;
}
else {
} else {
options.setOccurrence = this.scope.yearlyOccurrence.value;
options.weekDays = this.scope.yearlyWeekDay.value;
options.month = this.scope.yearlyOtherMonth.value;
}
}
else if (this.scope.schedulerFrequency.value === 'monthly') {
} else if (this.scope.schedulerFrequency.value === 'monthly') {
if (this.scope.monthlyRepeatOption === 'day') {
options.monthDay = this.scope.monthDay;
}
else {
} else {
options.setOccurrence = this.scope.monthlyOccurrence.value;
options.weekDays = this.scope.monthlyWeekDay.value;
}
@@ -308,11 +312,10 @@ angular.module('AngularScheduler', ['underscore'])
$filter('schZeroPad')(dt.getMinutes(), 2) + ':' +
$filter('schZeroPad')(dt.getSeconds(), 2) + ' ' +
dt.getTimezoneAbbreviation();
}
else {
} else {
local = $filter('date')(date, 'MM/dd/yyyy HH:mm:ss Z');
}
scope.occurrence_list.push({ utc: $filter('schDateStrFix')(date.toISOString()), local: local });
scope.occurrence_list.push({ utc: $filter('schedulerDate')($filter('schDateStrFix')(date.toISOString())), local: $filter('schedulerDate')(local) });
return true;
}
return false;
@@ -386,26 +389,22 @@ angular.module('AngularScheduler', ['underscore'])
$filter('schZeroPad')(now.getSeconds(), 2) + '.000Z';
adjNow = $timezones.toUTC(dateStr, this.scope.schedulerTimeZone.name); //Adjust to the selected TZ
timeNow = adjNow.getTime();
}
else {
} else {
timeNow = now.getTime();
}
if (this.requireFutureStartTime && timeNow >= timeFuture) {
this.scope.startDateError("Start time must be in the future");
validity = false;
}
}
else {
} else {
this.scope.startDateError("Invalid start time");
validity = false;
}
}
catch(e) {
} catch (e) {
this.scope.startDateError("Invalid start time");
validity = false;
}
}
else {
} else {
this.scope.startDateError("Provide a start time");
validity = false;
}
@@ -491,7 +490,8 @@ angular.module('AngularScheduler', ['underscore'])
};
return new fn();
};
}])
}
])
.factory('InRange', [function() {
return function(x, min, max, length) {
@@ -580,16 +580,15 @@ angular.module('AngularScheduler', ['underscore'])
monthDay = params.monthDay, // integer, optional, between 1 and 31
weekDays = params.weekDays, // integer, optional, valid value from weekdays
setOccurrence = params.setOccurrence, // integer, optional, valid value from occurrences
options = {}, i;
options = {},
i;
if (angular.isDate(startDate)) {
options.dtstart = startDate;
}
else {
} else {
try {
options.dtstart = new Date(startDate);
}
catch(e) {
} catch (e) {
$log.error('Date conversion failed. Attempted to convert ' + startDate + ' to Date. ' + e.message);
}
}
@@ -623,22 +622,18 @@ angular.module('AngularScheduler', ['underscore'])
if (occurrenceCount) {
options.count = occurrenceCount;
}
else if (endDate) {
} else if (endDate) {
if (angular.isDate(endDate)) {
options.until = endDate;
}
else {
} else {
try {
options.until = new Date(endDate);
}
catch(e) {
} catch (e) {
$log.error('Date conversion failed. Attempted to convert ' + endDate + ' to Date. ' + e.message);
}
}
}
}
else {
} else {
// We only want to run 1x
options.freq = RRule.DAILY;
options.interval = 1;
@@ -648,10 +643,11 @@ angular.module('AngularScheduler', ['underscore'])
};
}])
.factory('SetRule', ['AngularScheduler.useTimezone', '_', '$log', '$timezones', '$filter',
function(useTimezone, _, $log, $timezones, $filter) {
.factory('SetRule', ['AngularScheduler.useTimezone', '$log', '$timezones', '$filter',
function(useTimezone, $log, $timezones, $filter) {
return function(rule, scope) {
var set, result = '', i,
var set, result = '',
i,
setStartDate = false;
// Search the set of RRule keys for a particular key, returning its value
@@ -701,8 +697,7 @@ angular.module('AngularScheduler', ['underscore'])
if (parseInt(value, 10)) {
scope.schedulerInterval = parseInt(value, 10);
scope.schedulerShowInterval = true;
}
else {
} else {
result = 'INTERVAL must contain an integer > 0';
}
}
@@ -714,20 +709,17 @@ angular.module('AngularScheduler', ['underscore'])
if (_.contains(['SU', 'MO', 'TU', 'WE', 'TH', 'FR', 'SA'], days[j])) {
scope.weekDays.push(days[j].toLowerCase());
scope['weekDay' + days[j].toUpperCase() + 'Class'] = 'active'; //activate related button
}
else {
} else {
result = 'BYDAY contains unrecognized day value(s)';
}
}
}
else if (getValue(set, 'FREQ') === 'MONTHLY') {
} else if (getValue(set, 'FREQ') === 'MONTHLY') {
scope.monthlyRepeatOption = 'other';
scope.monthlyWeekDay = toWeekDays(value);
if (!scope.monthlyWeekDay) {
result = 'BYDAY contains unrecognized day value(s)';
}
}
else {
} else {
scope.yearlyRepeatOption = 'other';
scope.yearlyWeekDay = toWeekDays(value);
if (!scope.yearlyWeekDay) {
@@ -739,8 +731,7 @@ angular.module('AngularScheduler', ['underscore'])
if (parseInt(value, 10) && parseInt(value, 10) > 0 && parseInt(value, 10) < 32) {
scope.monthDay = parseInt(value, 10);
scope.monhthlyRepeatOption = 'day';
}
else {
} else {
result = 'BYMONTHDAY must contain an integer between 1 and 31';
}
}
@@ -763,8 +754,7 @@ angular.module('AngularScheduler', ['underscore'])
scope.schedulerStartMinute = $filter('schZeroPad')(dt.getMinutes(), 2);
scope.schedulerStartSecond = $filter('schZeroPad')(dt.getSeconds(), 2);
scope.scheduleTimeChange(); // calc UTC
}
else {
} else {
// expects inbound dates to be in ISO format: 2014-04-02T00:00:00.000Z
scope.schedulerStartDt = value.replace(/T.*$/, '').replace(/(\d{4})-(\d{2})-(\d{2})/, function(match, p1, p2, p3) {
return p2 + '/' + p3 + '/' + p1;
@@ -785,8 +775,7 @@ angular.module('AngularScheduler', ['underscore'])
if (!scope.yearlyOccurrence || !scope.yearlyOccurrence.name) {
result = 'BYSETPOS was not in the set of 1,2,3,4,-1';
}
}
else {
} else {
scope.monthlyOccurrence = _.find(scope.occurrences, function(x) {
return (x.value === parseInt(value, 10));
});
@@ -800,8 +789,7 @@ angular.module('AngularScheduler', ['underscore'])
if (parseInt(value, 10)) {
scope.schedulerEnd = scope.endOptions[1];
scope.schedulerOccurrenceCount = parseInt(value, 10);
}
else {
} else {
result = "COUNT must be a valid integer > 0";
}
}
@@ -833,8 +821,7 @@ angular.module('AngularScheduler', ['underscore'])
if (!scope.yearlyOtherMonth || !scope.yearlyOtherMonth.name) {
result = 'BYMONTH must be an integer between 1 and 12';
}
}
else {
} else {
scope.yearlyOption = 'month';
scope.yearlyMonth = _.find(scope.months, function(x) {
return x.value === parseInt(value, 10);
@@ -848,8 +835,7 @@ angular.module('AngularScheduler', ['underscore'])
if (key === 'BYMONTHDAY') {
if (parseInt(value, 10)) {
scope.yearlyMonthDay = parseInt(value, 10);
}
else {
} else {
result = 'BYMONTHDAY must be an integer between 1 and 31';
}
}
@@ -878,12 +864,10 @@ angular.module('AngularScheduler', ['underscore'])
if (!result) {
isValid();
}
}
else {
} else {
result = 'No rule entered. Provide a valid RRule string.';
}
}
else {
} else {
result = 'No rule entered. Provide a valid RRule string.';
}
if (result) {
@@ -891,7 +875,8 @@ angular.module('AngularScheduler', ['underscore'])
}
return result;
};
}])
}
])
.factory('SetDefaults', ['$filter', function($filter) {
return function(scope) {
@@ -1061,7 +1046,7 @@ angular.module('AngularScheduler', ['underscore'])
zeroPad = attr.zeroPad,
min = attr.min || 1,
max = attr.max || 999;
$(element).spinner({
element.spinner({
min: min,
max: max,
stop: function() {
@@ -1069,10 +1054,9 @@ angular.module('AngularScheduler', ['underscore'])
setTimeout(function() {
scope.$apply(function() {
if (zeroPad) {
scope[attr.ngModel] = $filter('schZeroPad')($(element).spinner('value'),zeroPad);
}
else {
scope[attr.ngModel] = $(element).spinner('value');
scope[attr.ngModel] = $filter('schZeroPad')(element.spinner('value'), zeroPad);
} else {
scope[attr.ngModel] = element.spinner('value');
}
if (attr.ngChange) {
scope.$eval(attr.ngChange);
@@ -1096,3 +1080,5 @@ angular.module('AngularScheduler', ['underscore'])
}
};
}]);
}));