DailyCadencePickerView.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. 'text!bi/schedule/templates/DailyCadencePicker.html',
  19. 'underscore',
  20. "bi/schedule/views/DailyIntervalCadencePickerView",
  21. 'bi/commons/ui/properties/DropDown'
  22. ],
  23. function (View, controler, $, t, Q, dot, template, _, DailyIntervalCadencePicker, DropDownMenu) {
  24. 'use strict';
  25. var dailyCadence = View.extend({
  26. isEditMode: false,
  27. /**
  28. * @constructor
  29. */
  30. init: function(options) {
  31. dailyCadence.inherited('init', this, arguments);
  32. _.extend(this, options);
  33. if(typeof(this.objectInformation.descriptor)!=="undefined"){
  34. this.scheduleInfo = this.objectInformation.descriptor.scheduleInfo;
  35. this.isEditMode = true;
  36. }
  37. this.uniqueId = _.uniqueId();
  38. this.dayFrequencySelector = null;
  39. this.readOnly = false;
  40. },
  41. /**
  42. * Render the new schedule view month picker section
  43. *
  44. */
  45. render: function() {
  46. var deferred = Q.defer();
  47. var htmlGenerator = dot.simpleTemplate(template);
  48. var defaultPeriod = 1;
  49. if (this.isEditMode && this.scheduleInfo.everyNPeriods > 1) {
  50. defaultPeriod = this.scheduleInfo.everyNPeriods;
  51. }
  52. this.readOnly = this.isEditMode && this.hasPermission && !this.hasPermission.write && this.hasPermission.read;
  53. var attributes = {
  54. schedule_every_label: t.translate("schedule_every_label"),
  55. schedule_time_dropdown_days_t: t.translate("schedule_time_dropdown_days_t"),
  56. schedule_time_dropdown_hours_t: t.translate("schedule_time_dropdown_hours_t"),
  57. schedule_time_dropdown_minutes_t: t.translate("schedule_time_dropdown_minutes_t"),
  58. defaultPeriod: defaultPeriod,
  59. uniqueid: this.uniqueId
  60. };
  61. this.$el.append(htmlGenerator(attributes));
  62. this._renderSelectors();
  63. if (this.isEditMode){
  64. if ( this.scheduleInfo.daily ) {
  65. this.dayFrequencySelector.getHTMLControl().val(this.scheduleInfo.daily.dailyPeriod||"day");
  66. }
  67. }
  68. if(this.objectInformation.showDailyInterval){
  69. // Handle in edit mode if there is no intra-day recurrence don't render the radio button groups
  70. var objectInfo = { descriptor: this.objectInformation.descriptor };
  71. if(this.isEditMode) {
  72. if( (this.scheduleInfo && this.scheduleInfo.intradayRecurrence) ||
  73. ( this.scheduleInfo && this.scheduleInfo.type != 'daily') ||
  74. (this.scheduleInfo && this.scheduleInfo.daily && this.scheduleInfo.daily.dailyPeriod == 'day')) {
  75. objectInfo.showDailyIntervalCheckbox = true;
  76. }
  77. else {
  78. objectInfo.showDailyIntervalCheckbox = false;
  79. }
  80. }
  81. else {
  82. objectInfo.showDailyIntervalCheckbox = true;
  83. }
  84. var $dailyIntervalCadence = this.$el.find('.schedule_daily_interval_container');
  85. this.dailyInterval = new DailyIntervalCadencePicker({
  86. $el: $dailyIntervalCadence,
  87. objectInformation: objectInfo,
  88. glassContext: this.glassContext,
  89. readOnly: this.readOnly
  90. });
  91. this.dailyInterval.render();
  92. }
  93. this._setEvents();
  94. deferred.resolve(this);
  95. return deferred.promise;
  96. },
  97. _setEvents: function() {
  98. $('.schedule_every_days_input').on('input', function (event) {
  99. this.value = this.value.replace(/[^0-9]/g, '');
  100. });
  101. },
  102. /** All views should overwrite this function.
  103. * It takes a partially populated json schedule descriptor and adds to it
  104. * based on the properties of this view.
  105. * @param desc the partial JSON schedule descriptor
  106. * @returns the descriptor passed in, with added attributes.
  107. */
  108. toDescriptor: function(desc) {
  109. desc.daily = {
  110. dailyPeriod: this.dayFrequencySelector.getHTMLControl().val()
  111. };
  112. desc.type = "daily";
  113. desc.everyNPeriods = parseInt(this.$el.find('.schedule_every_days_input').val(), 10);
  114. if(this.dailyInterval) {
  115. desc = this.dailyInterval.toDescriptor(desc);
  116. }
  117. return desc;
  118. },
  119. validate: function(msgs) {
  120. if(this.dailyInterval) {
  121. msgs = this.dailyInterval.validate(msgs);
  122. // One warning at a time
  123. if ( msgs.length > 0) {
  124. return msgs;
  125. }
  126. }
  127. var parameters = {};
  128. var $dailyInputDiv = this.$el.find('.schedule_dailyinterval_controls_container');
  129. $dailyInputDiv.removeAttr('aria-invalid aria-describedby');
  130. var everyNPeriods = parseInt(this.$el.find('.schedule_every_days_input').val(), 10);
  131. if ( isNaN(everyNPeriods)) {
  132. parameters.param = t.translate("schedule_every_label");
  133. msgs.push(t.translate("schedule_invalid_form_input_every_days_input", parameters));
  134. $dailyInputDiv.attr({
  135. 'aria-invalid': 'true',
  136. 'aria-describedby': msgs[0]
  137. });
  138. }
  139. return msgs;
  140. },
  141. _renderSelectors: function() {
  142. var dailyOptions = [];
  143. // We've come to here to have at least ByDay granted
  144. dailyOptions.push(
  145. {
  146. 'label': t.translate("schedule_time_dropdown_days_t"),
  147. 'value': 'day',
  148. 'selected': true
  149. }
  150. );
  151. if (this.glassContext.hasCapability("canUseSchedulingByHour")) {
  152. dailyOptions.push(
  153. {
  154. 'label': t.translate("schedule_time_dropdown_hours_t"),
  155. 'value': 'hour',
  156. 'selected': false
  157. }
  158. );
  159. }
  160. if (this.glassContext.hasCapability("canUseSchedulingByMinute")) {
  161. dailyOptions.push(
  162. {
  163. 'label': t.translate("schedule_time_dropdown_minutes_t"),
  164. 'value': 'minute',
  165. 'selected': false
  166. }
  167. );
  168. }
  169. this.dayFrequencySelector = new DropDownMenu({
  170. 'id': 'schedule_cadence_id_' + this.uniqueId,
  171. 'el': this.$el.find('#schedule_frequency_container_' + this.uniqueId),
  172. 'label': '',
  173. 'name' : 'schedule_daily_period',
  174. 'responsive': false,
  175. 'onChange': function(name, value){this._handleSelectors(name, value);}.bind(this),
  176. 'options': dailyOptions,
  177. 'readOnly': this.readOnly
  178. });
  179. this.dayFrequencySelector.doRender();
  180. },
  181. _handleSelectors: function(name, value) {
  182. var $dailyIntervalCadence = this.$el.find('.schedule_daily_interval_container');
  183. var objectInfo = { descriptor: this.objectInformation.descriptor };
  184. if(name === 'schedule_daily_period') {
  185. if ((value === "hour") || (value === "minute")) {
  186. objectInfo.showDailyIntervalCheckbox = false;
  187. }
  188. else if (value === "day") {
  189. objectInfo.showDailyIntervalCheckbox = true;
  190. }
  191. // Re-render the Interval Picker to not show the button groups
  192. $dailyIntervalCadence.empty();
  193. this.dailyInterval = new DailyIntervalCadencePicker({
  194. $el: $dailyIntervalCadence,
  195. objectInformation: objectInfo,
  196. glassContext: this.glassContext
  197. });
  198. this.dailyInterval.render();
  199. }
  200. else {
  201. // no selector
  202. return;
  203. }
  204. }
  205. });
  206. return dailyCadence;
  207. });