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