cron.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import $ from 'jquery';
  2. import '../../utils/cron-ui';
  3. import { translations } from 'grav-config';
  4. export default class CronField {
  5. constructor() {
  6. this.items = $();
  7. $('[data-grav-field="cron"]').each((index, cron) => this.addCron(cron));
  8. $('body').on('mutation._grav', this._onAddedNodes.bind(this));
  9. }
  10. addCron(cron) {
  11. cron = $(cron);
  12. this.items = this.items.add(cron);
  13. cron.find('.cron-selector').each((index, container) => {
  14. container = $(container);
  15. const input = container.closest('[data-grav-field]').find('input');
  16. container.jqCron({
  17. numeric_zero_pad: true,
  18. enabled_minute: true,
  19. multiple_dom: true,
  20. multiple_month: true,
  21. multiple_mins: true,
  22. multiple_dow: true,
  23. multiple_time_hours: true,
  24. multiple_time_minutes: true,
  25. default_period: 'hour',
  26. default_value: input.val() || '* * * * *',
  27. no_reset_button: false,
  28. bind_to: input,
  29. bind_method: {
  30. set: function($element, value) {
  31. $element.val(value);
  32. }
  33. },
  34. texts: {
  35. en: {
  36. empty: translations.GRAV_CORE['CRON.EVERY'],
  37. empty_minutes: translations.GRAV_CORE['CRON.EVERY'],
  38. empty_time_hours: translations.GRAV_CORE['CRON.EVERY_HOUR'],
  39. empty_time_minutes: translations.GRAV_CORE['CRON.EVERY_MINUTE'],
  40. empty_day_of_week: translations.GRAV_CORE['CRON.EVERY_DAY_OF_WEEK'],
  41. empty_day_of_month: translations.GRAV_CORE['CRON.EVERY_DAY_OF_MONTH'],
  42. empty_month: translations.GRAV_CORE['CRON.EVERY_MONTH'],
  43. name_minute: translations.GRAV_CORE['NICETIME.MINUTE'],
  44. name_hour: translations.GRAV_CORE['NICETIME.HOUR'],
  45. name_day: translations.GRAV_CORE['NICETIME.DAY'],
  46. name_week: translations.GRAV_CORE['NICETIME.WEEK'],
  47. name_month: translations.GRAV_CORE['NICETIME.MONTH'],
  48. name_year: translations.GRAV_CORE['NICETIME.YEAR'],
  49. text_period: translations.GRAV_CORE['CRON.TEXT_PERIOD'],
  50. text_mins: translations.GRAV_CORE['CRON.TEXT_MINS'],
  51. text_time: translations.GRAV_CORE['CRON.TEXT_TIME'],
  52. text_dow: translations.GRAV_CORE['CRON.TEXT_DOW'],
  53. text_month: translations.GRAV_CORE['CRON.TEXT_MONTH'],
  54. text_dom: translations.GRAV_CORE['CRON.TEXT_DOM'],
  55. error1: translations.GRAV_CORE['CRON.ERROR1'],
  56. error2: translations.GRAV_CORE['CRON.ERROR2'],
  57. error3: translations.GRAV_CORE['CRON.ERROR3'],
  58. error4: translations.GRAV_CORE['CRON.ERROR4'],
  59. weekdays: translations.GRAV_CORE['DAYS_OF_THE_WEEK'],
  60. months: translations.GRAV_CORE['MONTHS_OF_THE_YEAR']
  61. }
  62. }
  63. });
  64. });
  65. }
  66. _onAddedNodes(event, target/* , record, instance */) {
  67. let crons = $(target).find('[data-grav-field="cron"]');
  68. if (!crons.length) { return; }
  69. crons.each((index, list) => {
  70. list = $(list);
  71. if (!~this.items.index(list)) {
  72. this.addCron(list);
  73. }
  74. });
  75. }
  76. }
  77. export let Instance = new CronField();
  78. /*
  79. // cron-selector
  80. $(document).ready(function() {
  81. $('.cron-selector').each(function(index, wrapper) {
  82. wrapper = $(wrapper);
  83. const input = wrapper.closest('[data-grav-field]').find('input');
  84. wrapper.jqCron({
  85. enabled_minute: true,
  86. multiple_dom: true,
  87. multiple_month: true,
  88. multiple_mins: true,
  89. multiple_dow: true,
  90. multiple_time_hours: true,
  91. multiple_time_minutes: true,
  92. default_period: 'week',
  93. default_value: input.val() || '3 * * * *',
  94. no_reset_button: false,
  95. bind_to: input,
  96. bind_method: {
  97. set: function($element, value) {
  98. $element.val(value);
  99. }
  100. },
  101. texts: {
  102. en: {
  103. empty: 'every',
  104. empty_minutes: 'every',
  105. empty_time_hours: 'every hour',
  106. empty_time_minutes: 'every minute',
  107. empty_day_of_week: 'every day of the week',
  108. empty_day_of_month: 'every day of the month',
  109. empty_month: 'every month',
  110. name_minute: 'minute',
  111. name_hour: 'hour',
  112. name_day: 'day',
  113. name_week: 'week',
  114. name_month: 'month',
  115. name_year: 'year',
  116. text_period: 'Every <b />',
  117. text_mins: ' at <b /> minute(s) past the hour',
  118. text_time: ' at <b />:<b />',
  119. text_dow: ' on <b />',
  120. text_month: ' of <b />',
  121. text_dom: ' on <b />',
  122. error1: 'The tag %s is not supported !',
  123. error2: 'Bad number of elements',
  124. error3: 'The jquery_element should be set into jqCron settings',
  125. error4: 'Unrecognized expression',
  126. weekdays: ['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'],
  127. months: ['january', 'february', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december']
  128. }
  129. }
  130. });
  131. });
  132. });
  133. */