1
0

requesttimeplugin.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /**
  2. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  3. *
  4. * @license GNU AGPL version 3 or any later version
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License as
  8. * published by the Free Software Foundation, either version 3 of the
  9. * License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. (function() {
  21. OCA.WorkflowEngine = OCA.WorkflowEngine || {};
  22. OCA.WorkflowEngine.Plugins = OCA.WorkflowEngine.Plugins || {};
  23. OCA.WorkflowEngine.Plugins.RequestTimePlugin = {
  24. timezones: [
  25. "Europe/Berlin",
  26. "Europe/London"
  27. ],
  28. _$element: null,
  29. getCheck: function() {
  30. return {
  31. 'class': 'OCA\\WorkflowEngine\\Check\\RequestTime',
  32. 'name': t('workflowengine', 'Request time'),
  33. 'operators': [
  34. {'operator': 'in', 'name': t('workflowengine', 'between')},
  35. {'operator': '!in', 'name': t('workflowengine', 'not between')}
  36. ]
  37. };
  38. },
  39. render: function(element, check) {
  40. if (check['class'] !== 'OCA\\WorkflowEngine\\Check\\RequestTime') {
  41. return;
  42. }
  43. var startTime = '09:00',
  44. endTime = '18:00',
  45. timezone = jstz.determine().name(),
  46. $element = $(element);
  47. if (_.isString(check['value']) && check['value'] !== '') {
  48. var value = JSON.parse(check['value']),
  49. splittedStart = value[0].split(' ', 2),
  50. splittedEnd = value[1].split(' ', 2);
  51. startTime = splittedStart[0];
  52. endTime = splittedEnd[0];
  53. timezone = splittedStart[1];
  54. }
  55. var valueJSON = JSON.stringify([startTime + ' ' + timezone, endTime + ' ' + timezone]);
  56. if (check['value'] !== valueJSON) {
  57. check['value'] = valueJSON;
  58. $element.val(valueJSON);
  59. }
  60. $element.css('display', 'none');
  61. $('<input>')
  62. .attr('type', 'text')
  63. .attr('placeholder', t('workflowengine', 'Start'))
  64. .attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: '16:00'}))
  65. .addClass('has-tooltip')
  66. .tooltip({
  67. placement: 'bottom'
  68. })
  69. .addClass('start')
  70. .val(startTime)
  71. .insertBefore($element);
  72. $('<input>')
  73. .attr('type', 'text')
  74. .attr('placeholder', t('workflowengine', 'End'))
  75. .attr('title', t('workflowengine', 'Example: {placeholder}', {placeholder: '16:00'}))
  76. .addClass('has-tooltip')
  77. .tooltip({
  78. placement: 'bottom'
  79. })
  80. .addClass('end')
  81. .val(endTime)
  82. .insertBefore($element);
  83. var timezoneInput = $('<input>')
  84. .attr('type', 'hidden')
  85. .css('width', '250px')
  86. .insertBefore($element)
  87. .val(timezone);
  88. timezoneInput.select2({
  89. allowClear: false,
  90. multiple: false,
  91. placeholder: t('workflowengine', 'Select timezone…'),
  92. ajax: {
  93. url: OC.generateUrl('apps/workflowengine/timezones'),
  94. dataType: 'json',
  95. quietMillis: 100,
  96. data: function (term) {
  97. if (term === '') {
  98. // Default search in the same continent...
  99. term = jstz.determine().name().split('/');
  100. term = term[0];
  101. }
  102. return {
  103. search: term
  104. };
  105. },
  106. results: function (response) {
  107. var results = [];
  108. $.each(response, function(timezone) {
  109. results.push({ id: timezone });
  110. });
  111. return {
  112. results: results,
  113. more: false
  114. };
  115. }
  116. },
  117. initSelection: function (element, callback) {
  118. callback(element.val());
  119. },
  120. formatResult: function (element) {
  121. return '<span>' + element.id + '</span>';
  122. },
  123. formatSelection: function (element) {
  124. if (!_.isUndefined(element.id)) {
  125. element = element.id;
  126. }
  127. return '<span>' + element + '</span>';
  128. }
  129. });
  130. // Has to be added after select2 for `event.target.classList`
  131. timezoneInput.addClass('timezone');
  132. $element.parent()
  133. .on('change', '.start', _.bind(this.update, this))
  134. .on('change', '.end', _.bind(this.update, this))
  135. .on('change', '.timezone', _.bind(this.update, this));
  136. this._$element = $element;
  137. },
  138. update: function(event) {
  139. var value = event.target.value,
  140. key = null;
  141. for (var i = 0; i < event.target.classList.length; i++) {
  142. key = event.target.classList[i];
  143. }
  144. if (key === null) {
  145. console.warn('update triggered but element doesn\'t have any class');
  146. return;
  147. }
  148. var data = JSON.parse(this._$element.val()),
  149. startTime = moment(data[0].split(' ', 2)[0], 'H:m Z'),
  150. endTime = moment(data[1].split(' ', 2)[0], 'H:m Z'),
  151. timezone = data[0].split(' ', 2)[1];
  152. if (key === 'start' || key === 'end') {
  153. var parsedDate = moment(value, ['H:m', 'h:m a'], true).format('HH:mm');
  154. if (parsedDate === 'Invalid date') {
  155. return;
  156. }
  157. var indexValue = 0;
  158. if (key === 'end') {
  159. indexValue = 1;
  160. }
  161. data[indexValue] = parsedDate + ' ' + timezone;
  162. }
  163. if (key === 'timezone') {
  164. data[0] = startTime.format('HH:mm') + ' ' + value;
  165. data[1] = endTime.format('HH:mm') + ' ' + value;
  166. }
  167. this._$element.val(JSON.stringify(data));
  168. this._$element.trigger('change');
  169. }
  170. };
  171. })();
  172. OC.Plugins.register('OCA.WorkflowEngine.CheckPlugins', OCA.WorkflowEngine.Plugins.RequestTimePlugin);