/** * Licensed Materials - Property of IBM * * IBM Cognos Products: SHARE * * Copyright IBM Corp. 2015, 2020 * * US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define([ 'bi/commons/ui/View', 'jquery', 'bi/sharecommon/utils/simpledoT', 'bi/sharecommon/utils/translator', 'text!bi/schedule/templates/PromptPickerView.html', 'bi/commons/utils/ContentFormatter', 'bi/glass/common/ui/ProgressToast' ], function (View, $, dot, t, viewTemplate, stringFormatter, ProgressToast) { var view = View.extend({ /** Allows for selecting parameter values for a particular report. * Uses the old UI. Parameters: * $el: Location where this View will display selected parameter values, * in the "name: value" format. * $toggler: link/button that launches the 'prompting dance' * reportId: the report to prompt for * values: Initial values to display. Not used to select the defaults in the prompting dance * glassContext: the glassContext */ init: function (options) { view.inherited('init', this, arguments); $.extend(this, options); }, /** * Calls stopPropagation() on an event when clicking on element that does not have '.prompt-overlay' as an ancestor * @param {boolean} ignore - if 'true', does not trigger stopPropagation() even if '.prompt-overlay' isn't an ancestor of the clicked element */ setOverlayEvents: function (ignore) { $(document).on('clicktap', function (e) { var $event = $(e.target); var $p = $event.closest('.prompt-overlay'); if ($p.length == 0 && !ignore) { e.stopPropagation(); this.close(); } }.bind(this)); this.$promptOverlay.find('.prompt-iframe').load(function () { this.$promptOverlay.find('.prompt-iframe').contents().find('.dialogHeader a').on('clicktap', function (_e) { this.close(); }.bind(this)); // in case of error, an 'OK' button appears, whose default behaviour is to do a // history.back() (!) Turn that off and close the window instead. this.$promptOverlay.find('.prompt-iframe').contents().find('#cmdOK').prop('onclick', null).off('click').on('clicktap', function (e) { e.stopPropagation(); this.close(); }.bind(this)); // this.show(); }.bind(this)); }, setEvents: function () { this.$toggler.on('primaryaction', function () { this.showPromptOverlay(); }.bind(this)); }, close: function () { this.$promptOverlay.remove(); }, show: function () { if (this.$promptOverlay.css('visibility') == 'hidden') { // Remove waitForPromptsProgressToast when displaying promptOverlay if (this.waitForPromptsProgressToast) { this.waitForPromptsProgressToast.remove(0); } this.$promptOverlay.css('visibility', 'visible'); } }, // prompting call back - NB* APAR 145884 depends on this function remaining here and not being replaced by promptFnOkCallback. // this is to avoid nested bound function calls in RS's prod/webcontent/bi/js/classicviewer/cvContentView.js // ... which for unknown reasons cause input object lockup/javascript problems in IE11 only. onPromptOK: function() { this.glassContext.getSvc('.Prompting').then(function (promptSvc) { promptSvc.getPromptAnswers(this.id).then(function(values) { this.applyPromptValues(values); }.bind(this)); }.bind(this)); }, getParameterValues: function () { return this.values; }, _createProgressToast: function (options) { return new ProgressToast(options); }, showPromptOverlay: function () { var htmlGenerator = dot.simpleTemplate(viewTemplate); var _self = this; return this.glassContext.getSvc('.Prompting').then(function (promptSvc) { var options = { 'noCancelBtn': true, 'noHideBtn': true, 'noDetailsBtn': true }; this.waitForPromptsProgressToast = this._createProgressToast(options); this.waitForPromptsProgressToast.show(t.translate('parameters_generating_inputs')); this.waitForPromptsProgressToast.indefinite(t.translate('parameters_generating_inputs')); return promptSvc.getPromptPageInfo(this.reportId); }.bind(this)) .then(function (promptInfo) { this.id = promptInfo.id; // Use interactive report viewer to retrieve parameter values window.promptContext = { promptContext: { requestType: 'collectParameterValues', parameters: this.useValues ? this.values : null, promptFnOkCallback: function (values) { this.applyPromptValues(values); }.bind(_self), promptFnOkCallback1: this.onPromptOK.bind(_self), promptFnErrorCallback: function (values) { var errorMessage = values.code + ' ' + values.message; if (this.waitForPromptsProgressToast) { this.waitForPromptsProgressToast.remove(); } _self.glassContext.appController.showToast(errorMessage, { 'type': 'error', 'preventDuplicates': true }); }.bind(_self), promptFnCancelCallback: function (_values) { // Cancel clicked on prompt overlay, remove ProgressToast if present if (this.waitForPromptsProgressToast) { this.waitForPromptsProgressToast.remove(); } }.bind(_self), // promptOpener has to have a function close() promptOpener: _self } }; if (promptInfo && promptInfo._meta) { var isAdvancedViewer = (promptInfo.runInAdvancedViewer === true) || (promptInfo.base && promptInfo.base[0] && (promptInfo.base[0].runInAdvancedViewer === true)); var perspective = '&perspective=' + (isAdvancedViewer ? 'authoring' : 'classicviewer'); // Report output language will override content locale iff only one report output language is selected, otherwise no. var overrideContentLocale = (_self.languageOptions && _self.languageOptions.length === 1) ? '&contentLocale=' + _self.languageOptions[0] : ''; promptInfo.url = promptInfo._meta.links.path.url; promptInfo.url = promptInfo.url.replace('v1/path?path=', '?pathRef=') + perspective + '&format=HTML&a11y=true&Download=false&prompt=true&ui_appbar=false&ui_navbar=false&launchParametersRef=promptContext' + overrideContentLocale; } if (!promptInfo.url) { this.glassContext.appController.showErrorMessage(t.translate('schedule_prompting_wrong_url'), t.translate('error_label_share')); } else { var path = promptInfo.url; var title = t.translate('schedule_prompting_iframe_title'); var attributes = { prompt_path: path, prompt_title: title }; $('body').append(htmlGenerator(attributes)); this.$promptOverlay = $('body').find('.prompt-overlay'); if (this.$promptOverlay) { this.setOverlayEvents(true); } } }.bind(this)) .catch(function () { /* swallow */ }); }, render: function () { this.updateParameterList(); // in update mode the permissions are passed if (this.hasPermission && !this.hasPermission.write && this.hasPermission.read) { this.$toggler.prop('disabled', true); } this.setEvents(); return Promise.resolve(this); }, applyPromptValues: function (values) { this.values = values; this.updateParameterList(); if (this.onFinish && typeof this.onFinish === 'function') { this.onFinish(); } if (this.values.length === 0) { // No prompts to retrieve, set ProgressToast as complete if (this.waitForPromptsProgressToast) { this.waitForPromptsProgressToast.setComplete(100, { isComplete: true, completeMsg: t.translate('parameters_none_in_report') }); } } else { // Prompt values exist, but ProgressToast may not have been removed by show() because report has default values and no prompt overlay if (this.waitForPromptsProgressToast) { this.waitForPromptsProgressToast.setComplete(100, { isComplete: true }); } } }, updateParameterList: function () { var parameters = this.values; var toDisplay = ''; for (var i = 0; i < parameters.length; i++) { var parameter = ''; var displayValues = ''; var name = parameters[i].name; var values = parameters[i].value; if (!name) continue; if (!values) continue; for (var j = 0; j < values.length; j++) { if (!values[j].type) continue; var value = ''; switch (values[j].type) { case 'rangeParmValueItem': case 'boundRangeParmValueItem': case 'unboundedStartRangeParmValueItem': case 'unboundedEndRangeParmValueItem': { if (values[j].start && values[j].end) { value = t.translate('parameter_range_start_end_label', { start: values[j].start.display ? values[j].start.display : values[j].start.use, end: values[j].end.display ? values[j].end.display : values[j].end.use }); } else if (values[j].start) { value = t.translate('parameter_range_start_only_label', { start: values[j].start.display ? values[j].start.display : values[j].start.use }); } else if (values[j].end) { value = t.translate('parameter_range_end_only_label', { end: values[j].end.display ? values[j].end.display : values[j].end.use }); } else { value = t.translate('parameter_range_none_label'); } break; } case 'simpleParmValueItem': { value = values[j].display ? values[j].display : values[j].use; break; } case 'hierarchicalParmValueItem': { break; } } if (value === '') continue; if (displayValues === '') { displayValues = value; } else { displayValues = displayValues + ', ' + value; } } parameter = name + ': ' + displayValues + '; '; toDisplay += parameter; } if (toDisplay === '') { var none = t.translate('parameter_no_values_set'); this.$el.text(none); this.$el.attr('title', none); } else { this.$el.text(toDisplay); this.$el.attr('title', toDisplay); stringFormatter.middleShortenString(this.$el); } } }); return view; });