/*
* Licensed Materials - Property of IBM
* IBM Cognos Products: SHARE
* (C) Copyright IBM Corp. 2015, 2019
* US Government Users Restricted Rights - Use, duplication or disclosure
* restricted by GSA ADP Schedule Contract with IBM Corp.
*/
define([
'jquery',
'underscore',
'bi/sharecommon/utils/simpledoT',
'q',
'ckeditor',
'ckeditor/jquery',
'bi/commons/ui/View',
'bi/schedule/app/appControler',
'bi/sharecommon/utils/translator',
'bi/commons/utils/Utils',
'bi/commons/ui/properties/PropertyUIControl',
'bi/commons/utils/ContentFormatter',
'bi/schedule/utils/TextFormatter',
'text!bi/schedule/templates/DeliveryPickerOptions.html',
'text!bi/schedule/templates/RecipientsList.html',
'bi/commons/ui/properties/CheckBox',
'bi/commons/ui/properties/RadioButtonGroup',
'bootstrap'
], function($, _, dot, Q, ckeditor, jckeditor, View, controller, t, Utils, PropertyUIControl, stringFormatter, TextFormatter, optionsTemplate, recipientsListTemplate, CheckBox, RadioButtonGroup) { //NOSONAR
'use strict'; //NOSONAR
/* deliveryOptions look like:
* {
* save: {
* "notify": false
* },
* email: {
* "emailAsAttachment": false,
* "emailAsURL": false,
* "subject": "blah",
* "memoPart": "this is the body",
* "to": "CAMID ()",
* "cc": ,
* "bcc": ,
* },
* mobile: {
* "to":
* },
* print: {
* printer: ""
* },
* archive: {
* filenameStub: "filename",
* location: /configuration/archiveLocation[@name='test'], //Using of "storeID('id')" is deprecated starting Helios R9
* conflictResolution: "fail(keep)"|"replace(default"|"appendDateTime"|"appendSequenceNumber"
* }
* }
*/
var __STANDARD_DELAY = 500;
var __EMPTY_MESSAGE_BODY = "
";
var deliveryPickerView = View.extend({
/*
* Constructor
*
* options: {
* $el: {},
* deliveryOptions: {},
* reportName: string,
* isEditMode: boolean
*/
init: function(options, $outputDiv) { //NOSONAR
deliveryPickerView.inherited('init', this, arguments);
this.reportName = "";
this.isEditMode = false;
this.enableAdvancedSettings = false;
this.deliveryOptions = {};
this.overlay = false;
this.showMobile = true;
this.s3Connection = '';
this.showSaveToFileSystem = true;
var s3Loc = this._initS3Loc(options);
this.s3SaveToCloud = (s3Loc) && (s3Loc !== '');
if (this.s3SaveToCloud) {
this.s3Location = s3Loc.value;
var s3ReportName = this._initS3ReportName(options);
if (s3ReportName) {
this.s3ReportName = s3ReportName.value;
}
}
this.canSave = true;
this._icons = {
email: 'email',
print: 'print_icon',
mobile: 'mobile_icon',
save: 'save',
archive: 'saveExternal',
saveToCloud: 'saveExternal'
};
this._archiveLocations = [];
if (options.reportName === undefined) {
throw "Error: report name parameter must be provided.";
}
if (options.glassContext === undefined) {
throw "Error: glassContext parameter must be provided.";
}
if (options.$toggler === undefined) {
throw "Error: $toggler parameter must be provided.";
}
$.extend(this, options);
this.notifyOnSave = !this.enableAdvancedSettings || false;
if (options.deliveryOptions && options.deliveryOptions.save) {
this.notifyOnSave = options.deliveryOptions.save.notify;
}
this.save = {
notify: this.notifyOnSave
};
this.print = {
name: ""
};
this.archive = {
filenameStub: this.reportName,
location: "",
conflictResolution: "replace"
};
// if no permission passed in, assume full rights (subscribe case)
if (options.hasPermission === undefined) {
this.hasPermission = {
write: true
};
}
this.canSave = (this.hasPermission.write || _.contains(this.hasPermission, "write")) ? true : false;
// Default the deliveryOptions to just Save, unless user does not have Write permission
if ( ( !options.deliveryOptions || $.isEmptyObject(options.deliveryOptions ) && !this._hasSaveToCloudOption())) {
if (this.canSave) {
this.deliveryOptions = {
save: {
notify: this.notifyOnSave
}
};
} else {
this.deliveryOptions = {
print: {
name: ""
}
};
}
}
if (!this.enableAdvancedSettings) {
this.email = {
emailAsAttachment: !this.canSave,
emailAsURL: this.canSave,
subject: "",
memoPart: ""
};
this.mobile = {};
}
else {
this.email = {
emailAsAttachment: !this.canSave,
emailAsURL: this.canSave,
subject: "",
memoPart: "",
to: [],
bcc: [],
cc: []
};
this.mobile = {
to: []
};
}
if (!this.isEditMode) {
this.email.subject = t.translate("subscription_email_subject", {
report_name: this.reportName
});
if (!this.enableAdvancedSettings) {
this.email.memoPart = '' + t.translate("subscription_email_body") + '
';
}
} else if (this.isEditMode && !this.enableAdvancedSettings) {
// Set defaults for subscription delivery
this.email.subject = t.translate("subscription_email_subject", {
report_name: this.reportName
});
this.email.memoPart = '' + t.translate("subscription_email_body") + '
';
if (this.deliveryOptions.email){
this.deliveryOptions.email.subject = t.translate("subscription_email_subject", {
report_name: this.reportName
});
this.deliveryOptions.email.memoPart = '' + t.translate("subscription_email_body") + '
';
}
}
},
_initS3Loc: function(options) {
if (options.deliveryOptions.saveToCloud) {
var s3Details = {
value: options.deliveryOptions.cloudLocation
};
return s3Details;
}
return _.find(options.rawOptions, function(anOpt){ return anOpt.name === 'runOptionEnum#saveToCloud'; });
},
_initS3ReportName: function(options) {
if (options.deliveryOptions.cloudName) {
var s3ReportNameDetails = {
value: options.deliveryOptions.cloudName
};
return s3ReportNameDetails;
}
return _.find(options.rawOptions, function(anOpt){ return anOpt.name === 'runOptionEnum#cloudName'; });
},
close: function() {
$(document).removeData(['setemail','setmobile']);
},
/**
* Render is the main function of the Content View. Content Views should implement (override) the render
* method to populate this.el with the appropriate HTML. Render should always return this as a promise
* to allow chaining of calls.
* @returns (Promise)
*/
render: function() {
return Q();
},
getDeliveryOptions: function() { //NOSONAR
if (this.deliveryOptions.email) {
if (this.emailCheckbox && this.emailCheckbox.isChecked() && this.attachmentCheckbox && this.attachmentCheckbox.isChecked() && typeof this.deliveryOptions.email.memoPart !== 'undefined' && this.deliveryOptions.email.memoPart === __EMPTY_MESSAGE_BODY) { //NOSONAR
delete this.deliveryOptions.email.memoPart;
}
if (this.deliveryOptions.email.to && this.deliveryOptions.email.to.length > 0) {
this.deliveryOptions.email.to = this._cleanRecipientsArray(this.deliveryOptions.email.to.slice());
}
if (this.deliveryOptions.email.cc && this.deliveryOptions.email.cc.length > 0) {
this.deliveryOptions.email.cc = this._cleanRecipientsArray(this.deliveryOptions.email.cc.slice());
}
if (this.deliveryOptions.email.bcc && this.deliveryOptions.email.bcc.length > 0) {
this.deliveryOptions.email.bcc = this._cleanRecipientsArray(this.deliveryOptions.email.bcc.slice());
}
}
if (this.deliveryOptions.mobile && this.deliveryOptions.mobile.to && this.deliveryOptions.mobile.to.length > 0) {
this.deliveryOptions.mobile.to = this._cleanRecipientsArray(this.deliveryOptions.mobile.to.slice());
}
if (this._isSaveToCloudOptionsSelected()) {
var cloudOptions = this.getCloudOptions();
if (cloudOptions.location) {
this.deliveryOptions.cloudLocation = cloudOptions.location;
if (cloudOptions.cloudReportName) {
this.deliveryOptions.cloudName = cloudOptions.cloudReportName;
}
}
} else {
delete this.deliveryOptions.cloudLocation;
delete this.deliveryOptions.cloudName;
}
return this.deliveryOptions;
},
getDeliveryContent: function(allowRecipients) {
var htmlGenerator = dot.simpleTemplate(optionsTemplate);
if (typeof allowRecipients !== "boolean") {
// default to true
allowRecipients = true;
}
this.contentUniqueId = _.uniqueId('id_');
var attributes = {
schedule_delivery_s3ReportName_label: t.translate('schedule_delivery_s3ReportName_label'),
schedule_delivery_s3ConnectionName_label: t.translate('schedule_delivery_s3ConnectionName_label'),
schedule_delivery_s3LocationName_label: t.translate('schedule_delivery_s3LocationName_label'),
schedule_delivery_printer_name_label: t.translate("schedule_delivery_printer_name_label"),
schedule_name_label: t.translate("schedule_name_label"),
schedule_delivery_location_label: t.translate("schedule_delivery_location_label"),
schedule_delivery_conflict_resolution_label: t.translate("schedule_delivery_conflict_resolution_label"),
showSaveToFileSystem: this.showSaveToFileSystem,
advanced: this.enableAdvancedSettings,
schedule_delivery_recipient_label: t.translate("schedule_delivery_recipient_label"),
schedule_delivery_subject_label: t.translate("schedule_delivery_subject_label"),
schedule_delivery_cc_label: t.translate("schedule_delivery_cc_label"),
schedule_delivery_bcc_label: t.translate("schedule_delivery_bcc_label"),
schedule_done_btn: t.translate("schedule_delivery_done_label"),
uniqueid: this.contentUniqueId,
showMobile: this.showMobile,
allowRecipients: allowRecipients
};
return htmlGenerator(attributes);
},
setCloudEvents: function($deliveryList) {
var reportNameEl = $deliveryList.find('#delivery_saveToS3_reportName_' + this.contentUniqueId);
var cloudOptions = this.getCloudOptions();
if (cloudOptions.cloudReportName) {
reportNameEl.val(cloudOptions.cloudReportName);
} else if (cloudOptions.reportName) {
reportNameEl.val(cloudOptions.reportName);
} else {
reportNameEl.val(this.reportName);
}
reportNameEl.on('change', function(){ this.cloudReportName = reportNameEl.val();}.bind(this));
},
_renderOptions: function($selectEl, optionList) {
$selectEl.empty();
$selectEl.append($('