DailyIntervalCadencePickerView.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. /*
  2. * Licensed Materials - Property of IBM
  3. *
  4. * IBM Cognos Products: SHARE
  5. *
  6. * (C) Copyright IBM Corp. 2015, 2017
  7. *
  8. * US Government Users Restricted Rights - Use, duplication or disclosure
  9. * restricted by GSA ADP Schedule Contract with IBM Corp.
  10. */
  11. define([
  12. 'bi/schedule/views/CadencePickerView',
  13. 'bi/schedule/app/appControler',
  14. 'jquery',
  15. 'bi/sharecommon/utils/translator',
  16. 'q',
  17. 'momentjs',
  18. 'bi/sharecommon/utils/simpledoT',
  19. 'text!bi/schedule/templates/DailyIntervalCadencePicker.html',
  20. 'underscore',
  21. 'bi/commons/ui/widgets/TimePicker',
  22. 'bi/commons/ui/properties/CheckBox',
  23. 'bi/commons/ui/properties/DropDown',
  24. 'bi/commons/utils/DateTimeUtils',
  25. 'bi/content_apps/utils/GlassContextHelper'
  26. ], function(View, controler, $, t, Q, moment, dot, template, _, TimePicker, CheckBox, DropDown, DateTimeUtils, GlassContextHelper) {
  27. 'use strict';
  28. var dailyInterval = View.extend({
  29. /**
  30. * @constructor
  31. */
  32. init: function(options) {
  33. dailyInterval.inherited('init', this, arguments);
  34. _.extend(this, options);
  35. this.descriptor = this.objectInformation.descriptor;
  36. this.isEditMode = typeof (this.descriptor) !== "undefined";
  37. this.dailyIntervalCheckbox = null;
  38. this.uniqueId = _.uniqueId();
  39. this.frequencyTypeSelector = null;
  40. this.renderDailyIntervalCheckbox = false;
  41. this.timezone = GlassContextHelper.getUserPreference(this.glassContext, 'timeZoneID');
  42. this.locale = this._determineDateLocale();
  43. },
  44. /**
  45. * Render the new schedule view intraday picker section
  46. *
  47. */
  48. render: function() {
  49. var deferred = Q.defer();
  50. var htmlGenerator = dot.simpleTemplate(template);
  51. var defaultPeriod = 1;
  52. if(this.isEditMode && this.descriptor.intradayRecurrence) {
  53. // PT3H
  54. var text = this.descriptor.intradayRecurrence.intradayRecurrenceInterval;
  55. defaultPeriod = text.substring(2, text.length - 1);
  56. }
  57. var attributes = {
  58. daily_time_interval_label: t.translate("daily_time_interval_label"),
  59. schedule_repeat_label: t.translate("schedule_repeat_label"),
  60. schedule_time_dropdown_hours_t: t.translate("schedule_time_dropdown_hours_t"),
  61. schedule_time_dropdown_minutes_t: t.translate("schedule_time_dropdown_minutes_t"),
  62. schedule_timepicker_label: t.translate("schedule_timepicker_label"),
  63. schedule_timepicker_start: t.translate("period_start_time_label"),
  64. schedule_timepicker_end: t.translate("period_end_time_label"),
  65. defaultPeriod: defaultPeriod,
  66. uniqueid: this.uniqueId
  67. };
  68. this.$el.append(htmlGenerator(attributes));
  69. if((this.glassContext.hasCapability("canUseSchedulingByHour") ||
  70. this.glassContext.hasCapability("canUseSchedulingByMinute")) &&
  71. this.objectInformation.showDailyIntervalCheckbox) {
  72. this.renderDailyIntervalCheckbox = true;
  73. }
  74. if(this.renderDailyIntervalCheckbox) {
  75. this._renderDailyIntervalCheckbox();
  76. this._renderDailyIntervalTimePickers();
  77. this._renderSelectors();
  78. }
  79. if(this.isEditMode) {
  80. if(this.descriptor.scheduleInfo && this.descriptor.scheduleInfo.intradayRecurrence &&
  81. this.renderDailyIntervalCheckbox) {
  82. this.$el.find(".schedule_dailyinterval").slideDown();
  83. // repeat value
  84. var rtext = this.descriptor.scheduleInfo.intradayRecurrence.intradayRecurrenceInterval;
  85. var repeat = rtext.replace(/[^0-9]/g, '');
  86. this.$el.find(".schedule_run_every_input").val(repeat);
  87. // recurrence val
  88. var recurrence = rtext.charAt(rtext.length - 1);
  89. this.frequencyTypeSelector.getHTMLControl().val(recurrence);
  90. }
  91. } else {
  92. if (this.renderDailyIntervalCheckbox) {
  93. // We have either ByHour or ByMinute capability to show the radio button group
  94. if(this.glassContext.hasCapability("canUseSchedulingByHour")) {
  95. this.frequencyTypeSelector.getHTMLControl().val('H');
  96. }
  97. else {
  98. this.frequencyTypeSelector.getHTMLControl().val('M');
  99. }
  100. }
  101. }
  102. this._setEvents();
  103. deferred.resolve(this);
  104. return deferred.promise;
  105. },
  106. _setEvents: function() {
  107. $('.schedule_run_every_input').on('input', function(event) {
  108. this.value = this.value.replace(/[^0-9]/g, '');
  109. });
  110. },
  111. /**
  112. * All views should overwrite this function. It takes a partially
  113. * populated json schedule descriptor and adds to it based on the
  114. * properties of this view.
  115. *
  116. * @param desc the partial JSON schedule descriptor
  117. * @returns the descriptor passed in, with added attributes.
  118. */
  119. toDescriptor: function(desc) {
  120. if (this.renderDailyIntervalCheckbox && this.dailyIntervalCheckbox.isChecked()) {
  121. var repeat = this.frequencyTypeSelector.getHTMLControl().val();
  122. var recurrence = this.$el.find(".schedule_run_every_input").val();
  123. var intradayRecurrenceStart = this.intervalStartTime.getTime24H();
  124. var intradayRecurrenceEnd = this.intervalEndTime.getTime24H();
  125. desc.intradayRecurrence = {
  126. intradayRecurrenceInterval: "PT" + recurrence + repeat, // PT3H
  127. intradayRecurrenceStart: intradayRecurrenceStart,
  128. intradayRecurrenceEnd: intradayRecurrenceEnd
  129. };
  130. }
  131. return desc;
  132. },
  133. validate: function(msgs) {
  134. if (this.renderDailyIntervalCheckbox && this.dailyIntervalCheckbox.isChecked()) {
  135. var parameters = {
  136. param1: "",
  137. param2: "",
  138. param3: ""
  139. };
  140. var $everyDiv = this.$el.find(".schedule_dailyinterval_controls_container");
  141. var $iStartTime = this.$el.find(".schedule_time_interval_start");
  142. var $iEndTime = this.$el.find(".schedule_time_interval_end");
  143. $everyDiv.removeAttr('aria-invalid aria-describedby');
  144. $iStartTime.removeAttr('aria-invalid aria-describedby');
  145. $iEndTime.removeAttr('aria-invalid aria-describedby');
  146. var recurrence = parseInt(this.$el.find(".schedule_run_every_input").val(), 10);
  147. if(isNaN(recurrence) || recurrence === 0) {
  148. parameters.param1 = t.translate("daily_time_interval_label");
  149. parameters.param2 = t.translate("schedule_repeat_label");
  150. msgs.push(t.translate("schedule_invalid_form_input", parameters));
  151. $everyDiv.attr({
  152. 'aria-invalid': 'true',
  153. 'aria-describedby': msgs[0]
  154. });
  155. } else if(!this.intervalStartTime.isValidTime()) {
  156. parameters.param1 = t.translate("daily_time_interval_label");
  157. parameters.param2 = t.translate("schedule_timepicker_label");
  158. parameters.param3 = t.translate("period_start_label");
  159. msgs.push(t.translate("schedule_invalid_interval_time", parameters));
  160. $iStartTime.attr({
  161. 'aria-invalid': 'true',
  162. 'aria-describedby': msgs[0]
  163. });
  164. } else if(!this.intervalEndTime.isValidTime()) {
  165. parameters.param1 = t.translate("daily_time_interval_label");
  166. parameters.param2 = t.translate("schedule_timepicker_label");
  167. parameters.param3 = t.translate("period_end_label");
  168. msgs.push(t.translate("schedule_invalid_interval_time", parameters));
  169. $iEndTime.attr({
  170. 'aria-invalid': 'true',
  171. 'aria-describedby': msgs[0]
  172. });
  173. } else if(this.intervalStartTime.isValidTime() && this.intervalEndTime.isValidTime()) {
  174. var startingTime = new Date(this.intervalStartTime.getDateTimeUTC());
  175. var endingTime = new Date(this.intervalEndTime.getDateTimeUTC());
  176. if(startingTime > endingTime) {
  177. parameters.param1 = t.translate("daily_time_interval_label");
  178. parameters.param2 = t.translate("schedule_timepicker_label");
  179. msgs.push(t.translate("schedule_invalid_time_range", parameters));
  180. $iEndTime.attr({
  181. 'aria-invalid': 'true',
  182. 'aria-describedby': msgs[0]
  183. });
  184. }
  185. }
  186. }
  187. return msgs;
  188. },
  189. _renderDailyIntervalCheckbox: function() {
  190. var checked = false;
  191. if(this.descriptor && this.descriptor.scheduleInfo && this.descriptor.scheduleInfo.intradayRecurrence) {
  192. checked = true;
  193. }
  194. this.dailyIntervalCheckbox = new CheckBox({
  195. 'id': 'schedule_daily_interval_cadence_picker',
  196. 'el': this.$el.find('#schedule_daily_interval_cadence_picker_container_' + this.uniqueId),
  197. 'name': 'schedule_daily_pick',
  198. 'label': t.translate("daily_time_interval_label"),
  199. 'ariaLabel': t.translate("daily_time_interval_label"),
  200. 'checked': checked,
  201. 'controlOnLeft': true,
  202. 'readOnly': this.readOnly,
  203. 'onChange': this._toggleOption.bind(this)
  204. });
  205. this.dailyIntervalCheckbox.doRender();
  206. },
  207. _renderDailyIntervalTimePickers: function() {
  208. this.intervalStartTime = new TimePicker({
  209. $el: this.$el.find(".schedule_time_interval_start"),
  210. timezone: this.timezone,
  211. ariaLabel: t.translate("period_start_time_label"),
  212. attributes: {
  213. showMeridian: !DateTimeUtils.is24HrFormat(this.locale)
  214. }
  215. });
  216. this.intervalStartTime.render();
  217. this.intervalEndTime = new TimePicker({
  218. $el: this.$el.find(".schedule_time_interval_end"),
  219. timezone: this.timezone,
  220. ariaLabel: t.translate("period_end_time_label"),
  221. attributes: {
  222. showMeridian: !DateTimeUtils.is24HrFormat(this.locale)
  223. }
  224. });
  225. this.intervalEndTime.render();
  226. if(this.isEditMode) {
  227. if(this.descriptor.scheduleInfo && this.descriptor.scheduleInfo.intradayRecurrence) {
  228. var startingTime = this.descriptor.scheduleInfo.intradayRecurrence.intradayRecurrenceStart;
  229. this.intervalStartTime.setTime24H(startingTime);
  230. var endTime = this.descriptor.scheduleInfo.intradayRecurrence.intradayRecurrenceEnd;
  231. this.intervalEndTime.setTime24H(endTime);
  232. }
  233. }
  234. },
  235. _toggleOption: function(name, value) {
  236. if( value === true ) {
  237. this.$el.find(".schedule_dailyinterval").slideDown();
  238. } else {
  239. this.$el.find(".schedule_dailyinterval").slideUp();
  240. }
  241. },
  242. _renderSelectors: function() {
  243. var mnOptions = [];
  244. if(this.glassContext.hasCapability("canUseSchedulingByHour")) {
  245. mnOptions.push({
  246. 'label': t.translate("schedule_time_dropdown_hours_t"),
  247. 'value': 'H',
  248. 'selected': false
  249. });
  250. }
  251. if(this.glassContext.hasCapability("canUseSchedulingByMinute")) {
  252. mnOptions.push({
  253. 'label': t.translate("schedule_time_dropdown_minutes_t"),
  254. 'value': 'M',
  255. 'selected': false
  256. });
  257. }
  258. this.frequencyTypeSelector = new DropDown({
  259. 'id': 'schedule_cadence_id_' + this.uniqueId,
  260. 'el': this.$el.find('#schedule_daily_time_interval_selector_container_' + this.uniqueId),
  261. 'ariaLabel': t.translate("schedule_repeat_label"),
  262. 'name': 'schedule_interval_repeat_type',
  263. 'responsive': false,
  264. 'onChange': function() {
  265. return;
  266. },
  267. 'options': mnOptions
  268. });
  269. this.frequencyTypeSelector.doRender();
  270. },
  271. _determineDateLocale: function() {
  272. var prodLocale = GlassContextHelper.getUserPreference(this.glassContext, 'productLocale');
  273. var contentLocale = GlassContextHelper.getUserPreference(this.glassContext, 'contentLocale');
  274. if(prodLocale === contentLocale.substring(0,2)) {
  275. return contentLocale;
  276. } else {
  277. return prodLocale;
  278. }
  279. }
  280. });
  281. return dailyInterval;
  282. });