angular-cron-jobs
Version:
UI Component For Creating Cron Job Syntax To Send To Server
8 lines (7 loc) • 11 kB
JavaScript
/**
* UI Component For Creating Cron Job Syntax To Send To Server
* @version v3.2.1 - 2019-09-18 * @link https://github.com/jacobscarter/angular-cron-jobs
* @author Jacob Carter <jc@jacobcarter.com>
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
angular.module("templates-angularcronjobs",["cronselection.html"]),angular.module("cronselection.html",[]).run(["$templateCache",function(a){a.put("cronselection.html",'<div class="cron-wrap">\n <span>Every: </span>\n <div class="cron-select-wrap">\n <select class="cron-select" ng-model="myFrequency.base" ng-options="item.value as item.label for item in frequency"></select>\n </div>\n <div class="select-options">\n <span ng-show="myFrequency.base == 4">on </span>\n <div ng-show="myFrequency.base == 4" class="cron-select-wrap">\n \x3c!-- If Multiple is Enabled --\x3e\n <select class="cron-select day-value"\n ng-model="myFrequency.dayValues"\n ng-if="allowMultiple" multiple\n ng-options="value as (value | cronDayName: cronStyle) for value in dayValues">\n </select>\n \x3c!-- If Multiple is not Enabled --\x3e\n <select class="cron-select day-value"\n ng-model="myFrequency.dayValues"\n ng-if="!allowMultiple"\n ng-options="value as (value | cronDayName: cronStyle) for value in dayValues">\n </select>\n </div>\n <span ng-show="myFrequency.base >= 5">on the </span>\n <div ng-show="myFrequency.base >= 5" class="cron-select-wrap">\n \x3c!-- If Multiple is Enabled --\x3e\n <select class="cron-select day-of-month-value"\n ng-model="myFrequency.dayOfMonthValues"\n ng-if="allowMultiple" multiple\n ng-options="value as (value | cronNumeral) for value in dayOfMonthValues">\n </select>\n \x3c!-- If Multiple is not Enabled --\x3e\n <select class="cron-select day-of-month-value"\n ng-model="myFrequency.dayOfMonthValues"\n ng-if="!allowMultiple"\n ng-options="value as (value | cronNumeral) for value in dayOfMonthValues">\n </select>\n </div>\n <span ng-show="myFrequency.base == 6">of </span>\n \x3c!-- If Multiple is Enabled --\x3e\n <div ng-show="myFrequency.base == 6" class="cron-select-wrap">\n <select class="cron-select month-value"\n ng-model="myFrequency.monthValues"\n ng-if="allowMultiple" multiple\n ng-options="value as (value | cronMonthName) for value in monthValues">\n </select>\n \x3c!-- If Multiple is not Enabled --\x3e\n <select class="cron-select month-value"\n ng-model="myFrequency.monthValues"\n ng-if="!allowMultiple"\n ng-options="value as (value | cronMonthName) for value in monthValues">\n </select>\n </div>\n <span ng-show="myFrequency.base >= 2">at </span>\n \x3c!-- If Multiple is Enabled --\x3e\n <div ng-show="myFrequency.base >= 3" class="cron-select-wrap">\n <select class="cron-select hour-value"\n ng-model="myFrequency.hourValues"\n ng-if="allowMultiple" multiple\n ng-options="value as value for value in hourValues">\n </select>\n \x3c!-- If Multiple is not Enabled --\x3e\n <select class="cron-select hour-value"\n ng-model="myFrequency.hourValues"\n ng-if="!allowMultiple"\n ng-options="value as value for value in hourValues">\n </select>\n </div>\n <span ng-show="myFrequency.base >= 3"> : </span>\n \x3c!-- If Multiple is Enabled --\x3e\n <div ng-show="myFrequency.base >= 2" class="cron-select-wrap">\n <select class="cron-select minute-value"\n ng-model="myFrequency.minuteValues"\n ng-if="allowMultiple" multiple\n ng-options="value as value for value in minuteValues">\n </select>\n \x3c!-- If Multiple is not Enabled --\x3e\n <select class="cron-select minute-value"\n ng-model="myFrequency.minuteValues"\n ng-if="!allowMultiple"\n ng-options="value as value for value in minuteValues">\n </select>\n </div>\n <span ng-show="myFrequency.base == 2"> past the hour</span>\n </div>\n</div>')}]),angular.module("angular-cron-jobs",["templates-angularcronjobs"]),angular.module("angular-cron-jobs").directive("cronSelection",["cronService","baseFrequency",function(a,b){return{restrict:"EA",replace:!0,transclude:!0,require:"ngModel",scope:{ngModel:"=",config:"=",myFrequency:"=?frequency"},templateUrl:function(a,b){return b.template||"cronselection.html"},link:function(c,d,e,f){function g(a){a.base=parseInt(a.base),a.base>=b.hour&&(a.minuteValues=c.minuteValues[0]),a.base>=b.day&&(a.hourValues=c.hourValues[0]),a.base===b.week&&(a.dayValues=c.dayValues[0]),a.base>=b.month&&(a.dayOfMonthValues=c.dayOfMonthValues[0]),a.base===b.year&&(a.monthValues=c.monthValues[0])}var h=!1;if(c.baseFrequency=b,c.frequency=[{value:1,label:"Minute"},{value:2,label:"Hour"},{value:3,label:"Day"},{value:4,label:"Week"},{value:5,label:"Month"},{value:6,label:"Year"}],c.$watch("ngModel",function(b){angular.isDefined(b)&&b?(h=!0,c.myFrequency=a.fromCron(b,c.allowMultiple,c.cronStyle)):""===b&&(c.myFrequency=void 0)}),"object"==typeof c.config&&!c.config.length){if("object"==typeof c.config.options){var i=Object.keys(c.config.options);for(var j in i){var k=i[j].replace(/^allow/,""),l=i[j];if(!c.config.options[l])for(var m in c.frequency)c.frequency[m].label===k&&c.frequency.splice(m,1)}}angular.isDefined(c.config.allowMultiple)?c.allowMultiple=c.config.allowMultiple:c.allowMultiple=!1,angular.isDefined(c.config.quartz)&&c.config.quartz?c.cronStyle="quartz":c.cronStyle="default"}c.minuteValues=[0,5,10,15,20,25,30,35,40,45,50,55],c.hourValues=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23],c.dayOfMonthValues=[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31],c.dayValues=[0,1,2,3,4,5,6],c.monthValues=[1,2,3,4,5,6,7,8,9,10,11,12],"quartz"===c.cronStyle&&(c.dayValues=[1,2,3,4,5,6,7]),c.$watch("myFrequency",function(b,d){if(void 0!==b){!b||!b.base||d&&b.base===d.base||h?b&&b.base&&d&&d.base&&(h=!1):g(b);var e=a.setCron(b,c.cronStyle);f.$setViewValue(e)}},!0)}}}]).filter("cronNumeral",function(){return function(a){switch(a){case 1:return"1st";case 2:return"2nd";case 3:return"3rd";case 21:return"21st";case 22:return"22nd";case 23:return"23rd";case 31:return"31st";case null:return null;default:return a+"th"}}}).filter("cronMonthName",function(){return function(a){var b={1:"January",2:"February",3:"March",4:"April",5:"May",6:"June",7:"July",8:"August",9:"September",10:"October",11:"November",12:"December"};return null!==a&&angular.isDefined(b[a])?b[a]:null}}).filter("cronDayName",function(){return function(a,b){var c;return c="quartz"===b?{1:"Sunday",2:"Monday",3:"Tuesday",4:"Wednesday",5:"Thursday",6:"Friday",7:"Saturday"}:{0:"Sunday",1:"Monday",2:"Tuesday",3:"Wednesday",4:"Thursday",5:"Friday",6:"Saturday"},null!==a&&angular.isDefined(c[a])?c[a]:null}}).directive("ngMultiple",function(){return{restrict:"A",scope:{ngMultiple:"="},link:function(a,b){a.$watch("ngMultiple",function(a){a?b.attr("multiple","multiple"):b.removeAttr("multiple")})}}}),angular.module("angular-cron-jobs").value("baseFrequency",{minute:1,hour:2,day:3,week:4,month:5,year:6}).factory("cronService",["baseFrequency",function(a){var b={};return b.setCron=function(a,b){return"quartz"===b?this.setQuartzCron(a):this.setDefaultCron(a)},b.setQuartzCron=function(b){var c=["0","*","*","*","*","?"];return b&&b.base&&b.base>=a.hour&&(c[1]=void 0!==b.minuteValues?b.minuteValues:"0"),b&&b.base&&b.base>=a.day&&(c[2]=void 0!==b.hourValues?b.hourValues:"*"),b&&b.base&&b.base===a.week&&(c[3]="?",c[5]=b.dayValues),b&&b.base&&b.base>=a.month&&(c[3]=void 0!==b.dayOfMonthValues?b.dayOfMonthValues:"?"),b&&b.base&&b.base===a.year&&(c[4]=void 0!==b.monthValues?b.monthValues:"*"),c.join(" ")},b.setDefaultCron=function(b){var c=["*","*","*","*","*"];return b&&b.base&&b.base>=a.hour&&(c[0]=void 0!==b.minuteValues?b.minuteValues:"*"),b&&b.base&&b.base>=a.day&&(c[1]=void 0!==b.hourValues?b.hourValues:"*"),b&&b.base&&b.base===a.week&&(c[4]=b.dayValues),b&&b.base&&b.base>=a.month&&(c[2]=void 0!==b.dayOfMonthValues?b.dayOfMonthValues:"*"),b&&b.base&&b.base===a.year&&(c[3]=void 0!==b.monthValues?b.monthValues:"*"),c.join(" ")},b.fromCron=function(a,b,c){return"quartz"===c?this.fromQuartzCron(a,b):this.fromDefaultCron(a,b)},b.fromDefaultCron=function(b,c){var d=b.replace(/\s+/g," ").split(" "),e={base:"1"},f=[];if("*"===d[0]&&"*"===d[1]&&"*"===d[2]&&"*"===d[3]&&"*"===d[4]?e.base=a.minute:"*"===d[1]&&"*"===d[2]&&"*"===d[3]&&"*"===d[4]?e.base=a.hour:"*"===d[2]&&"*"===d[3]&&"*"===d[4]?e.base=a.day:"*"===d[2]&&"*"===d[3]?e.base=a.week:"*"===d[3]&&"*"===d[4]?e.base=a.month:"*"===d[4]&&(e.base=a.year),"*"!==d[0])if(c){f=d[0].split(",");for(var g=0;g<f.length;g++)f[g]=+f[g];e.minuteValues=f}else e.minuteValues=parseInt(d[0]);if("*"!==d[1])if(c){f=d[1].split(",");for(var g=0;g<f.length;g++)f[g]=+f[g];e.hourValues=f}else e.hourValues=parseInt(d[1]);if("*"!==d[2])if(c){f=d[2].split(",");for(var g=0;g<f.length;g++)f[g]=+f[g];e.dayOfMonthValues=f}else e.dayOfMonthValues=parseInt(d[2]);if("*"!==d[3])if(c){f=d[3].split(",");for(var g=0;g<f.length;g++)f[g]=+f[g];e.monthValues=f}else e.monthValues=parseInt(d[3]);if("*"!==d[4])if(c){f=d[4].split(",");for(var g=0;g<f.length;g++)f[g]=+f[g];e.dayValues=f}else e.dayValues=parseInt(d[4]);return e},b.fromQuartzCron=function(a,b){var c=a.replace(/\s+/g," ").split(" "),d={base:"1"},e=[];if("*"===c[1]&&"*"===c[2]&&"*"===c[3]&&"*"===c[4]&&"?"===c[5]?d.base=1:"*"===c[2]&&"*"===c[3]&&"*"===c[4]&&"?"===c[5]?d.base=2:"*"===c[3]&&"*"===c[4]&&"?"===c[5]?d.base=3:"?"===c[3]?d.base=4:"*"===c[4]&&"?"===c[5]?d.base=5:"?"===c[5]&&(d.base=6),"*"!==c[1])if(b){e=c[1].split(",");for(var f=0;f<e.length;f++)e[f]=+e[f];d.minuteValues=e}else d.minuteValues=parseInt(c[1]);if("*"!==c[2])if(b){e=c[2].split(",");for(var f=0;f<e.length;f++)e[f]=+e[f];d.hourValues=e}else d.hourValues=parseInt(c[2]);if("*"!==c[3]&&"?"!==c[3])if(b){e=c[3].split(",");for(var f=0;f<e.length;f++)e[f]=+e[f];d.dayOfMonthValues=e}else d.dayOfMonthValues=parseInt(c[3]);if("*"!==c[4])if(b){e=c[4].split(",");for(var f=0;f<e.length;f++)e[f]=+e[f];d.monthValues=e}else d.monthValues=parseInt(c[4]);if("*"!==c[5]&&"?"!==c[5])if(b){e=c[5].split(",");for(var f=0;f<e.length;f++)e[f]=+e[f];d.dayValues=e}else d.dayValues=parseInt(c[5]);return d},b}]);