select_custom_date_view.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. var stringUtils = require('utils/string_utils');
  20. App.JobsCustomDatesSelectView = Em.View.extend({
  21. name: 'jobsCustomDatesSelectView',
  22. templateName: require('templates/common/custom_date_popup'),
  23. middayPeriodOptions: [Em.I18n.t('jobs.table.custom.date.am'), Em.I18n.t('jobs.table.custom.date.pm')],
  24. hourOptions: ['01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12'],
  25. minuteOptions: ['00', '05', '10', '15', '20', '25', '30', '35', '40', '45', '50', '55'],
  26. customDateFormFields: Ember.Object.create({
  27. startDate: null,
  28. hoursForStart: null,
  29. minutesForStart: null,
  30. middayPeriodForStart: null,
  31. duration: null,
  32. endDate: null,
  33. hoursForEnd: null,
  34. minutesForEnd: null,
  35. middayPeriodForEnd: null
  36. }),
  37. errors: Ember.Object.create({
  38. isStartDateError: false,
  39. isEndDateError: false
  40. }),
  41. errorMessages: Ember.Object.create({
  42. startDate: '',
  43. endDate: ''
  44. }),
  45. durationSelect: Em.Select.extend({
  46. classNames: ['input-medium'],
  47. content: [
  48. {
  49. value: 900000,
  50. label: Em.I18n.t('jobs.customDateFilter.duration.15min')
  51. },
  52. {
  53. value: 1800000,
  54. label: Em.I18n.t('jobs.customDateFilter.duration.30min')
  55. },
  56. {
  57. value: 3600000,
  58. label: Em.I18n.t('jobs.customDateFilter.duration.1hr')
  59. },
  60. {
  61. value: 7200000,
  62. label: Em.I18n.t('jobs.customDateFilter.duration.2hr')
  63. },
  64. {
  65. value: 14400000,
  66. label: Em.I18n.t('jobs.customDateFilter.duration.4hr')
  67. },
  68. {
  69. value: 43200000,
  70. label: Em.I18n.t('jobs.customDateFilter.duration.12hr')
  71. },
  72. {
  73. value: 86400000,
  74. label: Em.I18n.t('jobs.customDateFilter.duration.24hr')
  75. },
  76. {
  77. value: 604800000,
  78. label: Em.I18n.t('jobs.customDateFilter.duration.1w')
  79. },
  80. {
  81. value: 2592000000,
  82. label: Em.I18n.t('jobs.customDateFilter.duration.1m')
  83. },
  84. {
  85. value: 31536000000,
  86. label: Em.I18n.t('jobs.customDateFilter.duration.1yr')
  87. },
  88. {
  89. value: 0,
  90. label: Em.I18n.t('common.custom')
  91. }
  92. ],
  93. optionValuePath: 'content.value',
  94. optionLabelPath: 'content.label',
  95. selectionBinding: 'parentView.customDateFormFields.duration',
  96. willInsertElement: function () {
  97. var duration = this.get('selection'),
  98. initialOption = this.get('content').find(function (item) {
  99. return duration === item.value || duration === item.label;
  100. }, this);
  101. if (!initialOption) {
  102. initialOption = this.get('content').findProperty('value', 0);
  103. }
  104. this.set('selection', initialOption);
  105. }
  106. }),
  107. isCustomEndDate: Em.computed.equal('customDateFormFields.duration.value', 0),
  108. didInsertElement: function () {
  109. $('.datepicker').datepicker({
  110. format: 'mm/dd/yyyy'
  111. }).on('changeDate', function() {
  112. $(this).datepicker('hide');
  113. });
  114. },
  115. createCustomStartDate: function () {
  116. var startDate = this.get('customDateFormFields.startDate');
  117. var hoursForStart = this.get('customDateFormFields.hoursForStart');
  118. var minutesForStart = this.get('customDateFormFields.minutesForStart');
  119. var middayPeriodForStart = this.get('customDateFormFields.middayPeriodForStart');
  120. if (startDate && hoursForStart && minutesForStart && middayPeriodForStart) {
  121. return App.getTimeStampFromLocalTime(new Date(startDate + ' ' + hoursForStart + ':' + minutesForStart + ' ' + middayPeriodForStart));
  122. }
  123. return null;
  124. },
  125. createCustomEndDate: function (startDate) {
  126. var duration = this.get('customDateFormFields.duration.value'),
  127. date;
  128. if (duration === 0) {
  129. var endDate = this.get('customDateFormFields.endDate');
  130. var hoursForEnd = this.get('customDateFormFields.hoursForEnd');
  131. var minutesForEnd = this.get('customDateFormFields.minutesForEnd');
  132. var middayPeriodForEnd = this.get('customDateFormFields.middayPeriodForEnd');
  133. if (endDate && hoursForEnd && minutesForEnd && middayPeriodForEnd) {
  134. date = App.getTimeStampFromLocalTime(new Date(endDate + ' ' + hoursForEnd + ':' + minutesForEnd + ' ' + middayPeriodForEnd));
  135. }
  136. } else if (!Em.isNone(startDate)) {
  137. date = startDate + duration;
  138. }
  139. return date;
  140. },
  141. setErrorMessage: function (key, message) {
  142. var errors = this.get('errors'),
  143. errorMessages = this.get('errorMessages'),
  144. isError = !Em.isNone(message);
  145. message = isError ? message: '';
  146. errors.set('is' + key.capitalize() + 'Error', isError);
  147. errorMessages.set(key, message);
  148. },
  149. validate: function () {
  150. var hasErrors = false,
  151. formFields = this.get('customDateFormFields'),
  152. errors = this.get('errors'),
  153. errorMessages = this.get('errorMessages');
  154. // Check if fields are empty or invalid
  155. Em.keys(errorMessages).forEach(function (key) {
  156. var value = formFields.get(key),
  157. timestamp = App.getTimeStampFromLocalTime(value);
  158. if (key !== 'endDate' || this.get('isCustomEndDate')) {
  159. if (!formFields.get(key)) {
  160. hasErrors = true;
  161. this.setErrorMessage(key);
  162. } else if (isNaN(timestamp)) {
  163. this.setErrorMessage(key, Em.I18n.t('jobs.customDateFilter.error.incorrect'));
  164. hasErrors = true;
  165. } else {
  166. this.setErrorMessage(key);
  167. }
  168. }
  169. }, this);
  170. // Check that endDate is after startDate
  171. if (!hasErrors) {
  172. var startDate = this.createCustomStartDate(),
  173. endDate = this.createCustomEndDate(startDate);
  174. if (startDate && endDate) {
  175. if (startDate > endDate) {
  176. hasErrors = true;
  177. this.setErrorMessage('endDate', Em.I18n.t('jobs.customDateFilter.error.date.order'));
  178. }
  179. if (startDate > new Date().getTime()) {
  180. hasErrors = true;
  181. this.setErrorMessage('startDate', Em.I18n.t('jobs.customDateFilter.error.laterThanNow'));
  182. }
  183. }
  184. }
  185. this.set('parentView.disablePrimary', hasErrors);
  186. // Get customized time range if there are no errors
  187. if (!hasErrors) {
  188. var duration;
  189. if (this.get('isCustomEndDate')) {
  190. var values = [
  191. {
  192. label: 'year',
  193. max: Infinity
  194. },
  195. {
  196. label: 'month',
  197. max: 12
  198. },
  199. {
  200. label: 'week',
  201. max: 4
  202. },
  203. {
  204. label: 'day',
  205. max: 7
  206. },
  207. {
  208. label: 'hour',
  209. max: 24
  210. },
  211. {
  212. label: 'minute',
  213. max: 60
  214. }
  215. ],
  216. start = moment(startDate),
  217. end = moment(endDate);
  218. values.forEach(function (item) {
  219. var diff = end.diff(start, item.label);
  220. item.value = diff >= item.max ? diff % item.max : diff;
  221. });
  222. duration = values.filterProperty('value').map(function (item) {
  223. return item.value + ' ' + stringUtils.pluralize(item.value, item.label);
  224. }).join(' ');
  225. } else {
  226. duration = this.get('customDateFormFields.duration.label');
  227. }
  228. this.get('controller').setProperties({
  229. startTime: startDate,
  230. endTime: endDate,
  231. customDuration: duration
  232. });
  233. }
  234. }.observes('customDateFormFields.startDate', 'customDateFormFields.hoursForStart', 'customDateFormFields.minutesForStart', 'customDateFormFields.middayPeriodForStart', 'customDateFormFields.endDate', 'customDateFormFields.hoursForEnd', 'customDateFormFields.minutesForEnd', 'customDateFormFields.middayPeriodForEnd', 'customDateFormFields.duration.value')
  235. });