/* * Licensed Materials - Property of IBM * * IBM Cognos Products: SHARE * * (C) Copyright IBM Corp. 2015, 2017 * * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define([ 'bi/schedule/views/CadencePickerView', 'bi/schedule/app/appControler', 'jquery', 'bi/sharecommon/utils/translator', 'q', 'bi/sharecommon/utils/simpledoT', 'text!bi/schedule/templates/DailyCadencePicker.html', 'underscore', "bi/schedule/views/DailyIntervalCadencePickerView", 'bi/commons/ui/properties/DropDown' ], function (View, controler, $, t, Q, dot, template, _, DailyIntervalCadencePicker, DropDownMenu) { 'use strict'; var dailyCadence = View.extend({ isEditMode: false, /** * @constructor */ init: function(options) { dailyCadence.inherited('init', this, arguments); _.extend(this, options); if(typeof(this.objectInformation.descriptor)!=="undefined"){ this.scheduleInfo = this.objectInformation.descriptor.scheduleInfo; this.isEditMode = true; } this.uniqueId = _.uniqueId(); this.dayFrequencySelector = null; this.readOnly = false; }, /** * Render the new schedule view month picker section * */ render: function() { var deferred = Q.defer(); var htmlGenerator = dot.simpleTemplate(template); var defaultPeriod = 1; if (this.isEditMode && this.scheduleInfo.everyNPeriods > 1) { defaultPeriod = this.scheduleInfo.everyNPeriods; } this.readOnly = this.isEditMode && this.hasPermission && !this.hasPermission.write && this.hasPermission.read; var attributes = { schedule_every_label: t.translate("schedule_every_label"), schedule_time_dropdown_days_t: t.translate("schedule_time_dropdown_days_t"), schedule_time_dropdown_hours_t: t.translate("schedule_time_dropdown_hours_t"), schedule_time_dropdown_minutes_t: t.translate("schedule_time_dropdown_minutes_t"), defaultPeriod: defaultPeriod, uniqueid: this.uniqueId }; this.$el.append(htmlGenerator(attributes)); this._renderSelectors(); if (this.isEditMode){ if ( this.scheduleInfo.daily ) { this.dayFrequencySelector.getHTMLControl().val(this.scheduleInfo.daily.dailyPeriod||"day"); } } if(this.objectInformation.showDailyInterval){ // Handle in edit mode if there is no intra-day recurrence don't render the radio button groups var objectInfo = { descriptor: this.objectInformation.descriptor }; if(this.isEditMode) { if( (this.scheduleInfo && this.scheduleInfo.intradayRecurrence) || ( this.scheduleInfo && this.scheduleInfo.type != 'daily') || (this.scheduleInfo && this.scheduleInfo.daily && this.scheduleInfo.daily.dailyPeriod == 'day')) { objectInfo.showDailyIntervalCheckbox = true; } else { objectInfo.showDailyIntervalCheckbox = false; } } else { objectInfo.showDailyIntervalCheckbox = true; } var $dailyIntervalCadence = this.$el.find('.schedule_daily_interval_container'); this.dailyInterval = new DailyIntervalCadencePicker({ $el: $dailyIntervalCadence, objectInformation: objectInfo, glassContext: this.glassContext, readOnly: this.readOnly }); this.dailyInterval.render(); } this._setEvents(); deferred.resolve(this); return deferred.promise; }, _setEvents: function() { $('.schedule_every_days_input').on('input', function (event) { this.value = this.value.replace(/[^0-9]/g, ''); }); }, /** All views should overwrite this function. * It takes a partially populated json schedule descriptor and adds to it * based on the properties of this view. * @param desc the partial JSON schedule descriptor * @returns the descriptor passed in, with added attributes. */ toDescriptor: function(desc) { desc.daily = { dailyPeriod: this.dayFrequencySelector.getHTMLControl().val() }; desc.type = "daily"; desc.everyNPeriods = parseInt(this.$el.find('.schedule_every_days_input').val(), 10); if(this.dailyInterval) { desc = this.dailyInterval.toDescriptor(desc); } return desc; }, validate: function(msgs) { if(this.dailyInterval) { msgs = this.dailyInterval.validate(msgs); // One warning at a time if ( msgs.length > 0) { return msgs; } } var parameters = {}; var $dailyInputDiv = this.$el.find('.schedule_dailyinterval_controls_container'); $dailyInputDiv.removeAttr('aria-invalid aria-describedby'); var everyNPeriods = parseInt(this.$el.find('.schedule_every_days_input').val(), 10); if ( isNaN(everyNPeriods)) { parameters.param = t.translate("schedule_every_label"); msgs.push(t.translate("schedule_invalid_form_input_every_days_input", parameters)); $dailyInputDiv.attr({ 'aria-invalid': 'true', 'aria-describedby': msgs[0] }); } return msgs; }, _renderSelectors: function() { var dailyOptions = []; // We've come to here to have at least ByDay granted dailyOptions.push( { 'label': t.translate("schedule_time_dropdown_days_t"), 'value': 'day', 'selected': true } ); if (this.glassContext.hasCapability("canUseSchedulingByHour")) { dailyOptions.push( { 'label': t.translate("schedule_time_dropdown_hours_t"), 'value': 'hour', 'selected': false } ); } if (this.glassContext.hasCapability("canUseSchedulingByMinute")) { dailyOptions.push( { 'label': t.translate("schedule_time_dropdown_minutes_t"), 'value': 'minute', 'selected': false } ); } this.dayFrequencySelector = new DropDownMenu({ 'id': 'schedule_cadence_id_' + this.uniqueId, 'el': this.$el.find('#schedule_frequency_container_' + this.uniqueId), 'label': '', 'name' : 'schedule_daily_period', 'responsive': false, 'onChange': function(name, value){this._handleSelectors(name, value);}.bind(this), 'options': dailyOptions, 'readOnly': this.readOnly }); this.dayFrequencySelector.doRender(); }, _handleSelectors: function(name, value) { var $dailyIntervalCadence = this.$el.find('.schedule_daily_interval_container'); var objectInfo = { descriptor: this.objectInformation.descriptor }; if(name === 'schedule_daily_period') { if ((value === "hour") || (value === "minute")) { objectInfo.showDailyIntervalCheckbox = false; } else if (value === "day") { objectInfo.showDailyIntervalCheckbox = true; } // Re-render the Interval Picker to not show the button groups $dailyIntervalCadence.empty(); this.dailyInterval = new DailyIntervalCadencePicker({ $el: $dailyIntervalCadence, objectInformation: objectInfo, glassContext: this.glassContext }); this.dailyInterval.render(); } else { // no selector return; } } }); return dailyCadence; });