/* * Licensed Materials - Property of IBM * * IBM Cognos Products: SHARE * * (C) Copyright IBM Corp. 2015, 2018 * * US Government Users Restricted Rights - Use, duplication or disclosure * restricted by GSA ADP Schedule Contract with IBM Corp. */ define([ 'underscore', 'bi/glass/app/ContentView', 'bi/content_apps/utils/GlassContextHelper', 'jquery', 'q', 'bi/sharecommon/utils/simpledoT', 'text!bi/notifications/templates/preview.html', 'bi/sharecommon/utils/translator', 'bi/commons/utils/ContentFormatter', 'bi/commons/utils/Utils', 'bi/notifications/app/n10nController', 'moment-timezone' ], function(_,View, GlassContextHelper, $, Q, dot, previewTemplate, t, stringFormater, Utils, controller, moment) { var DEFAULT_DELAY = 500; var __STANDAR_DATE_TIME_FORMAT = 'lll'; var previewView = View.extend({ init: function(options){ this.events = { 'primaryaction .headerBack': 'close' }; previewView.inherited('init', this, arguments); this.data = {}; $.extend(this, options); this.$currentMessageItem = this.messageList.$el.find('#' + this.data.id); this.timezone = this.glassContext.services.userProfile.preferences.timeZoneID; this.contentLocale = this.glassContext.services.userProfile.preferences.contentLocale || "en-us"; }, setFocus: function() { this.$el.find('.n10n_preview_links .n10n_preview_link:first').focus(); }, /** * Set the events for the notification detail view * * @param links_map a map from unique id to link for each link */ setEvents: function(links_map) { var glassContext = this.glassContext; this.$el.find('.n10n_preview_link').click(function(event) { // on link click, launch saved output viewer var link = links_map[event.target.id]; this.openLink(link); }.bind(this)); // Click event to delete the notification this.$el.find(".n10n_delete_icon:not(.clickBound)").addClass('clickBound').on('primaryaction', function(event) { if(this.messageList) { var deletionRequest = { messageId: this.data.id, pending: true }; if (this.glassContext.services.userProfile.preferences.accessibilityFeatures) { this.glassContext.appController.showMessage(t.translate('delete_notification_confirm_message'), t.translate('delete_confirm'), 'info', ['ok','cancel'], undefined, function(evt) { if (evt.btn==='ok') { this._completeNotificationDelete(deletionRequest); this._focusNextMessage(); } }.bind(this)); } else { this.close(); this.$currentMessageItem.hide(DEFAULT_DELAY); this.glassContext.appController.showToast(t.translate('delete_notification_message'), { 'type': 'info', 'btnLabel' : t.translate('delete_undo'), 'callback' : function() { deletionRequest.pending = false; this.messageList.$el.find('#' + deletionRequest.messageId).show(DEFAULT_DELAY); }.bind(this), 'newestOnTop' : true, 'preventDuplicates' : false, 'timeOut': 6000, 'extendedTimeOut': 1000, 'onHidden' : function() {this._completeNotificationDelete(deletionRequest)}.bind(this) }); this._focusNextMessage(); } } }.bind(this)); }, /** * Well set the keyboard focus to the next message in the notification list * or if deleting the last message set it to the previous message. * If no messages are found then it doesn't set focus to anything. */ _focusNextMessage: function() { // Make sure the notification view has at least one message in it this.messageList.focusNextMessage(this.$currentMessageItem); }, _completeNotificationDelete: function(deleteRequest) { var $pendingDeleteMessage = this.messageList.$el.find('#' + deleteRequest.messageId); var context = { glassContext: this.glassContext }; if(deleteRequest.pending) { controller.deleteNotification(deleteRequest.messageId, context).done(function(){ $pendingDeleteMessage.remove(); }); } }, openLink: function(link) { var deferred = Q.defer(); // We only care about the output object (link.id). Unfortunately getObjectProperties assumes we are dealing // with reports so it looks for reportId instead of id. var properties = { reportId: link.id }; var context = {glassContext: this.glassContext}; controller.getObjectProperties(properties, context).then(function(newProperties) { // user attached an artifact // Turn reportId back into outputId since it is actually the output object // and restore original reportId in case client can make use of it newProperties.outputId = newProperties.reportId; newProperties.reportId = link.reportId; this._openObject(newProperties); deferred.resolve(); }.bind(this)).fail(function(xhr, status, error) { // May want to consider removing this error handling to let client deal with // missing output object. this.glassContext.appController.showToast(t.translate('error_object_not_found'), { 'type': 'error', 'newestOnTop' : true, 'preventDuplicates' : false, 'timeOut': 6000, 'extendedTimeOut': 1000 }); deferred.reject(); }.bind(this)); return deferred.promise; }, _openObject: function(context) { this.glassContext.appController.hideSlideOut(); this.glassContext.appController.openAppView( undefined, { content: { objRef: context.outputId, reportId: context.reportId } }); }, close: function() { this.slideout.hide(); }, render: function() { var deferred = Q.defer(); //clear the existing second panel content this.$el.find('.preview_tab_panel').remove(); GlassContextHelper.getContentLocales(this.glassContext).then(function(contentLocales) { this._renderPreview(contentLocales); deferred.resolve(this); }.bind(this)).fail(function() { console.warn("failed to retrieve the current content locales"); this._renderPreview(); deferred.resolve(this); }.bind(this)); return deferred.promise; }, _renderPreview: function(contentLocales) { var data = this.data || {}; var previewAttrs = { id: data.id, type_tab: t.translate("type_tab"), status_tab: t.translate("status_tab"), owner_tab: t.translate("owner_tab"), type_name: data.type || "", owner_name: _.escape(data.sender) || "", subject: _.escape(data.subject) || "", created: moment(data.created).locale(this.contentLocale).tz(this.timezone).format(__STANDAR_DATE_TIME_FORMAT), content: _.escape(data.body) || "", links: data.links || [], //HARDCODE ALERT: hard core "read" for now. Wait and see the future content UI status_name: t.translate("status_name"), output_links_description: t.translate('preview_output_links_description'), deleteLabel: t.translate('delete_message_label'), back: t.translate('back') }; var links_map=[]; if (data.links) { for (var i = 0; i