custom_date_popup.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. module.exports = Em.Object.create({
  20. // Fields values from Select Custom Dates form
  21. customDateFormFields: Ember.Object.create({
  22. startDate: null,
  23. hoursForStart: null,
  24. minutesForStart: null,
  25. middayPeriodForStart: null,
  26. endDate: null,
  27. hoursForEnd: null,
  28. minutesForEnd: null,
  29. middayPeriodForEnd: null
  30. }),
  31. errors: Ember.Object.create({
  32. isStartDateError: false,
  33. isEndDateError: false
  34. }),
  35. errorMessages: Ember.Object.create({
  36. startDate: '',
  37. endDate: ''
  38. }),
  39. showCustomDatePopup: function (context) {
  40. var self = this;
  41. return App.ModalPopup.show({
  42. header: Em.I18n.t('jobs.table.custom.date.header'),
  43. onPrimary: function () {
  44. self.validate();
  45. if(self.get('errors.isStartDateError') || self.get('errors.isEndDateError')) {
  46. return false;
  47. }
  48. var windowStart = self.createCustomStartDate();
  49. var windowEnd = self.createCustomEndDate();
  50. context.set('actualValues', {
  51. endTime: windowEnd.getTime(),
  52. startTime: windowStart.getTime()
  53. });
  54. this.hide();
  55. },
  56. onSecondary: function () {
  57. context.cancel();
  58. this.hide();
  59. },
  60. bodyClass: App.JobsCustomDatesSelectView.extend({
  61. controller: self,
  62. validationErrors: self.get('errorMessages'),
  63. isValid: self.get('errors')
  64. })
  65. });
  66. },
  67. createCustomStartDate : function () {
  68. var startDate = this.get('customDateFormFields.startDate');
  69. var hoursForStart = this.get('customDateFormFields.hoursForStart');
  70. var minutesForStart = this.get('customDateFormFields.minutesForStart');
  71. var middayPeriodForStart = this.get('customDateFormFields.middayPeriodForStart');
  72. if (startDate && hoursForStart && minutesForStart && middayPeriodForStart) {
  73. return new Date(startDate + ' ' + hoursForStart + ':' + minutesForStart + ' ' + middayPeriodForStart);
  74. }
  75. return null;
  76. },
  77. createCustomEndDate : function () {
  78. var endDate = this.get('customDateFormFields.endDate');
  79. var hoursForEnd = this.get('customDateFormFields.hoursForEnd');
  80. var minutesForEnd = this.get('customDateFormFields.minutesForEnd');
  81. var middayPeriodForEnd = this.get('customDateFormFields.middayPeriodForEnd');
  82. if (endDate && hoursForEnd && minutesForEnd && middayPeriodForEnd) {
  83. return new Date(endDate + ' ' + hoursForEnd + ':' + minutesForEnd + ' ' + middayPeriodForEnd);
  84. }
  85. return null;
  86. },
  87. clearErrors: function () {
  88. var errorMessages = this.get('errorMessages');
  89. Em.keys(errorMessages).forEach(function (key) {
  90. errorMessages.set(key, '');
  91. }, this);
  92. var errors = this.get('errors');
  93. Em.keys(errors).forEach(function (key) {
  94. errors.set(key, false);
  95. }, this);
  96. },
  97. // Validation for every field in customDateFormFields
  98. validate: function () {
  99. var formFields = this.get('customDateFormFields');
  100. var errors = this.get('errors');
  101. var errorMessages = this.get('errorMessages');
  102. this.clearErrors();
  103. // Check if feild is empty
  104. Em.keys(errorMessages).forEach(function (key) {
  105. if (!formFields.get(key)) {
  106. errors.set('is' + key.capitalize() + 'Error', true);
  107. errorMessages.set(key, Em.I18n.t('jobs.customDateFilter.error.required'));
  108. }
  109. }, this);
  110. // Check that endDate is after startDate
  111. var startDate = this.createCustomStartDate();
  112. var endDate = this.createCustomEndDate();
  113. if (startDate && endDate && (startDate > endDate)) {
  114. errors.set('isEndDateError', true);
  115. errorMessages.set('endDate', Em.I18n.t('jobs.customDateFilter.error.date.order'));
  116. }
  117. }
  118. });