WeeklyCadencePickerView.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. 'bi/sharecommon/utils/simpledoT',
  18. 'bi/commons/utils/ContentFormatter',
  19. 'text!bi/schedule/templates/WeeklyCadencePicker.html',
  20. 'underscore',
  21. 'moment-timezone',
  22. "bi/schedule/views/DailyIntervalCadencePickerView"
  23. ],
  24. function (View, controler, $, t, Q, dot, stringFormatter, template, _, moment, DailyIntervalCadencePicker) {
  25. 'use strict';
  26. var weeklyCadence = View.extend({
  27. isEditMode: false,
  28. buttonClassName: [
  29. 'weeklySunday',
  30. 'weeklyMonday',
  31. 'weeklyTuesday',
  32. 'weeklyWednesday',
  33. 'weeklyThursday',
  34. 'weeklyFriday',
  35. 'weeklySaturday'
  36. ],
  37. dayNumberMap: {
  38. 'weeklySunday': 0,
  39. 'weeklyMonday': 1,
  40. 'weeklyTuesday': 2,
  41. 'weeklyWednesday': 3,
  42. 'weeklyThursday': 4,
  43. 'weeklyFriday': 5,
  44. 'weeklySaturday': 6
  45. },
  46. /**
  47. * @constructor
  48. */
  49. init: function(options) {
  50. weeklyCadence.inherited('init', this, arguments);
  51. $.extend(this, options);
  52. if(typeof this.objectInformation.descriptor !== "undefined") {
  53. this.scheduleInfo = this.objectInformation.descriptor.scheduleInfo;
  54. this.isEditMode = true;
  55. }
  56. this.readOnly = false;
  57. this.timezone = this.glassContext.services.userProfile.preferences.timeZoneID;
  58. },
  59. /**
  60. * Render the new schedule view day picker section
  61. *
  62. */
  63. render: function() {
  64. var deferred = Q.defer();
  65. var htmlGenerator = dot.simpleTemplate(template);
  66. var defaultPeriod = 1;
  67. if (typeof this.scheduleInfo !== 'undefined' && this.scheduleInfo.everyNPeriods > 1) {
  68. defaultPeriod = this.scheduleInfo.everyNPeriods;
  69. }
  70. this.readOnly = this.isEditMode && this.hasPermission && !this.hasPermission.write && this.hasPermission.read;
  71. var attributes = {
  72. weekly_cadence_buttons_description: t.translate('weekly_cadence_buttons_description'),
  73. schedule_run_every_label: t.translate("schedule_run_every_label"),
  74. schedule_run_every_week_label: t.translate("schedule_run_every_week_label"),
  75. schedule_weekly_on_days_label: t.translate("schedule_weekly_on_days_label"),
  76. schedule_every_weeks_value: defaultPeriod,
  77. schedule_weekday_Sunday: t.translate("schedule_run_on_the_sun_label"),
  78. schedule_weekday_Saturday: t.translate("schedule_run_on_the_sat_label"),
  79. schedule_weekday_Friday: t.translate("schedule_run_on_the_fri_label"),
  80. schedule_weekday_Thursday: t.translate("schedule_run_on_the_thu_label"),
  81. schedule_weekday_Wednesday: t.translate("schedule_run_on_the_wed_label"),
  82. schedule_weekday_Tuesday: t.translate("schedule_run_on_the_tue_label"),
  83. schedule_weekday_Monday: t.translate("schedule_run_on_the_mon_label"),
  84. schedule_weekday_button_Sunday: t.translate("schedule_weekday_button_Sunday_t"),
  85. schedule_weekday_button_Saturday: t.translate("schedule_weekday_button_Saturday_t"),
  86. schedule_weekday_button_Friday: t.translate("schedule_weekday_button_Friday_t"),
  87. schedule_weekday_button_Thursday: t.translate("schedule_weekday_button_Thursday_t"),
  88. schedule_weekday_button_Wednesday: t.translate("schedule_weekday_button_Wednesday_t"),
  89. schedule_weekday_button_Tuesday: t.translate("schedule_weekday_button_Tuesday_t"),
  90. schedule_weekday_button_Monday: t.translate("schedule_weekday_button_Monday_t"),
  91. run_everyid: _.uniqueId(),
  92. showRunEvery: this.objectInformation.showRunEvery
  93. };
  94. this.$el.append(htmlGenerator(attributes));
  95. this._setDay();
  96. if(this.objectInformation.showDailyInterval) {
  97. this.dailyInterval = new DailyIntervalCadencePicker({
  98. $el: this.$el,
  99. objectInformation: {
  100. descriptor: this.objectInformation.descriptor,
  101. showDailyIntervalCheckbox: true
  102. },
  103. glassContext: this.glassContext,
  104. readOnly: this.readOnly
  105. });
  106. this.dailyInterval.render();
  107. }
  108. // in update mode the permissions are passed
  109. if ( this.readOnly ) {
  110. for(var i=0; i<this.buttonClassName.length; i++) {
  111. this.$el.find('.' + this.buttonClassName[i]).prop('disabled', true);
  112. }
  113. }
  114. stringFormatter.middleShortenString(this.$el.find('.weekly_on_days_label'));
  115. this._setEvents();
  116. deferred.resolve(this);
  117. return deferred.promise;
  118. },
  119. /** All views should overwrite this function.
  120. * It takes a partially populated json schedule descriptor and adds to it
  121. * based on the properties of this view.
  122. * @param desc the partial JSON schedule descriptor
  123. * @returns the descriptor passed in, with added attributes.
  124. */
  125. toDescriptor: function(descriptor) {
  126. // compute the day of the week
  127. var weekly = {
  128. weeklySunday: false,
  129. weeklyMonday: false,
  130. weeklyTuesday: false,
  131. weeklyWednesday: false,
  132. weeklyThursday: false,
  133. weeklyFriday: false,
  134. weeklySaturday: false
  135. };
  136. for (var key in weekly) {
  137. if (this.$el.find("." + key).hasClass("selectedButton")) {
  138. weekly[key] = true;
  139. }
  140. }
  141. descriptor.weekly = weekly;
  142. var everyNPeriods = (this.objectInformation.showRunEvery) ? parseInt(this.$el.find('.schedule_every_weeks_input').val(), 10) : 1;
  143. descriptor.everyNPeriods = everyNPeriods;
  144. // set type
  145. descriptor.type = "weekly";
  146. // set time or daily interval
  147. if(this.dailyInterval) {
  148. descriptor = this.dailyInterval.toDescriptor(descriptor);
  149. }
  150. return descriptor;
  151. },
  152. validate: function(msgs) {
  153. if(this.dailyInterval) {
  154. msgs = this.dailyInterval.validate(msgs);
  155. // One warning at a time
  156. if ( msgs.length > 0) {
  157. return msgs;
  158. }
  159. }
  160. var parameters = {};
  161. var $everyNPeriodDiv = this.$el.find(".schedule_frequency_input_container");
  162. $everyNPeriodDiv.removeAttr('aria-invalid aria-describedby');
  163. var everyNPeriods = (this.objectInformation.showRunEvery) ? parseInt(this.$el.find('.schedule_every_weeks_input').val(), 10) : 1;
  164. if ( isNaN(everyNPeriods) || everyNPeriods === 0 ) {
  165. parameters.param1 = t.translate("schedule_run_every_label");
  166. parameters.param2 = t.translate("schedule_run_every_week_label");
  167. msgs.push(t.translate("schedule_invalid_form_input", parameters));
  168. $everyNPeriodDiv.attr({
  169. 'aria-invalid': 'true',
  170. 'aria-describedby': msgs[0]
  171. });
  172. }
  173. return msgs;
  174. },
  175. _setEvents: function() {
  176. var btnHandler = function(e) {
  177. /* Handle button clicked event
  178. When button clicked, the background color should change
  179. */
  180. var btnClassNames = e.currentTarget.className.split(/\s+/);
  181. for (var i = 0; i < btnClassNames.length; i++) {
  182. if (btnClassNames[i].indexOf('weekly')===0) {
  183. this._toggleWeekday(btnClassNames[i]);
  184. }
  185. }
  186. }.bind(this);
  187. for(var i=0; i<this.buttonClassName.length; i++) {
  188. this.$el.find('.' + this.buttonClassName[i]).on('clicktap', btnHandler);
  189. }
  190. $('.schedule_every_weeks_input').on('input', function (event) {
  191. this.value = this.value.replace(/[^0-9]/g, '');
  192. });
  193. },
  194. _setDay: function() {
  195. var hasDays = false;
  196. if(typeof this.scheduleInfo !== 'undefined' && this.scheduleInfo.weekly) {
  197. for(var day in this.scheduleInfo.weekly) {
  198. if (this.scheduleInfo.weekly[day] === true) {
  199. var converted = this._convertTimezone(this.dayNumberMap[day]);
  200. this._toggleWeekday(this.buttonClassName[converted]);
  201. hasDays = true;
  202. }
  203. }
  204. }
  205. // Select the current day if no other days were selected
  206. if(!hasDays) {
  207. var currentDayNumber = moment().tz(this.timezone).day();
  208. this._toggleWeekday(this.buttonClassName[currentDayNumber]);
  209. }
  210. },
  211. // takes a day number in the original timezone, and returning the day number
  212. // in the new timezone.
  213. _convertTimezone: function(day) {
  214. // calculate the difference in days between source and target timezones
  215. var sourceDay = moment(this.scheduleInfo.startDate).tz(this.scheduleInfo.timezone).day();
  216. var targetDay = moment(this.scheduleInfo.startDate).tz(this.timezone).day();
  217. var newDay = day + (targetDay - sourceDay);
  218. if(newDay < 0) {
  219. newDay = newDay + 7;
  220. }
  221. else if(newDay > 6) {
  222. newDay = newDay - 7;
  223. }
  224. return newDay;
  225. },
  226. /*highlight the button when a button is selected or it is the day of the week
  227. */
  228. _toggleWeekday: function(buttonclassName) {
  229. var aButtonclassName = "." + buttonclassName;
  230. var $button = this.$el.find(aButtonclassName);
  231. if ($button.hasClass('selectedButton')) {
  232. if (this.$el.find(".selectedButton").length > 1 ) {
  233. $button.removeClass('selectedButton');
  234. $button.attr("aria-pressed", "false");
  235. }
  236. } else {
  237. $button.addClass('selectedButton');
  238. $button.attr("aria-pressed", "true");
  239. }
  240. }
  241. });
  242. return weeklyCadence;
  243. });