123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428 |
- define([
- 'jquery',
- 'underscore',
- 'moment-timezone',
- 'rave2',
- 'q',
- 'bi/commons/utils/BidiUtil',
- 'text!bi/schedule/templates/SubscriptionManagementLoading.html',
- 'text!bi/schedule/templates/SubscriptionManagement.html',
- 'text!bi/notifications/templates/emptyMessageList.html',
- 'bi/glass/app/ContentView',
- 'bi/sharecommon/utils/simpledoT',
- 'bi/sharecommon/utils/translator',
- 'bi/schedule/app/appControler',
- 'bi/schedule/utils/CadenceLabelGenerator',
- 'bi/commons/ui/properties/CheckBox',
- 'bi/commons/utils/Utils',
- 'bi/schedule/utils/charts/BarChart',
- 'datatables'
- ],
- function($, _, moment, rave, Q, BidiUtil,
- loadingViewTemplate, viewTemplate, emptyListTemplate,
- ContentView, dot, t, controler, cadence, CheckBox, Utils, BarChart) {
- var DEFAULT_DELAY = 500;
- var __STANDARD_DATE_TIME_FORMAT = 'lll';
- var view = ContentView.extend({
- init: function(options) {
- view.inherited('init', this, arguments);
-
- this.subscriptionsCount = 0;
- this.enabledCount = 0;
- this.disabledCount = 0;
- this.windowWidth = 0;
- this.timezone = options.glassContext.services.userProfile.preferences.timeZoneID;
- this.contentLocale = this.glassContext.services.userProfile.preferences.contentLocale || "en-us";
- $.extend(this, options);
- this._id = _.uniqueId("smv_");
- this.$subscriptionsListDiv = null;
- this.$noSubscriptionsDiv = null;
- },
- activate: function() {
- return this.updateView();
- },
-
- deactivate: function() {
-
- },
- getContent: function() {
- return {};
- },
- getTitle: function() {
- return t.translate("subscribe_mgmt_label");
- },
- getIcon: function() {
- return 'common-subscribe_icon';
- },
- render: function() {
- var deferred = $.Deferred();
- var subscribe_graph_title = t.translate("subscribe_graph_title");
- var subscribe_last_refresh_label = t.translate("subscribe_last_refresh_label");
-
-
- var loadingHtmlGenerator = dot.simpleTemplate(loadingViewTemplate);
-
- var currentTime = moment(new Date()).locale(this.contentLocale).tz(this.timezone).format(__STANDARD_DATE_TIME_FORMAT);
-
- var attributes = {
- subscribe_graph_title: subscribe_graph_title,
- subscribe_last_refresh_label: subscribe_last_refresh_label,
- current_time: currentTime
- };
- this.$el.html(loadingHtmlGenerator(attributes));
- var loadingAnimation = Utils.getLoadingAnimation(2);
- this.$el.find('div.s10n_loading').html(loadingAnimation);
- deferred.resolve(this);
- controler.getSubscriptions(this.glassContext).then(function(data) {
- var subscriptions = [];
- this.enabledCount = 0;
- var dataLength = data.length;
- for(var i = 0; i < dataLength; i++) {
- var name = _.escape(data[i].name.substr(0, 3) + BidiUtil.enforceTextDirection(data[i].name.substr(3))) || "";
- var nameInAttr = _.escape(data[i].name)|| "";
- var cadenceLabel = cadence.getLabelFor(data[i].scheduleInfo, this.timezone, this.contentLocale);
- var ownerName = data[i].scheduleInfo ? _.escape(data[i].scheduleInfo.ownerName) || "" : "";
- var lastModified = data[i].scheduleInfo ? data[i].scheduleInfo.modificationTime || "" : "";
- lastModified = moment(lastModified).locale(this.contentLocale).tz(this.timezone).format(__STANDARD_DATE_TIME_FORMAT);
- data[i].checkbox_aria_label = t.translate("subscribe_mgmt_checkbox_description", {
- 'subscription_name': nameInAttr
- });
- var isEnabled = data[i].scheduleInfo.active || false;
- if(isEnabled) {
- this.enabledCount++;
- }
- subscriptions.push({
- id: data[i].id,
- name: name,
- owner: ownerName,
- active: isEnabled,
- modifiedTime: lastModified,
- cadence_label: cadenceLabel,
- context_menu_description: t.translate("subs_mgmt_context_menu_description", {
- 'subscription_name': nameInAttr
- })
- });
- }
- this.subscriptionsCount = subscriptions.length;
- this.disabledCount = subscriptions.length - this.enabledCount;
- var htmlGenerator = dot.simpleTemplate(viewTemplate);
-
- var currentTime = moment(new Date()).locale(this.contentLocale).tz(this.timezone).format(__STANDARD_DATE_TIME_FORMAT);
-
- var attributes = {
- id: this._id,
- subscribe_graph_title: subscribe_graph_title,
- subscribe_last_refresh_label: subscribe_last_refresh_label,
- current_time: currentTime,
- ariaLabel: t.translate("subscriptions_table_accessible_label"),
- chartDescription: t.translate("subscribe_graph_description", {
- enabled: this.enabledCount, disabled: this.disabledCount
- }),
- columns: this._tableColumnHeaders(),
- subs: subscriptions
- };
- this.$el.html(htmlGenerator(attributes));
-
- this.$subscriptionsListDiv = this.$el.find('.subscriptions_list');
- this.$noSubscriptionsDiv = this.$el.find('.no_subscriptions_message');
- var emptyTemplate = dot.template(emptyListTemplate);
- this.$noSubscriptionsDiv.html(emptyTemplate({
- 'text': t.translate("subscription_list_is_empty")
- }));
- this._setSvg();
- this._renderChart();
- this._renderCheckBoxes(data);
- if (this.subscriptionsCount > 0) {
- this.$subscriptionsListDiv.find('.subscriptions_list_table').dataTable({
- paging: false,
- searching: false,
- info: false,
- aaSorting: [[1, 'asc']],
- scrollY: '60vh',
- scrollX: 'auto',
- width: '100%'
- });
- this.$noSubscriptionsDiv.hide();
- }
- else {
- this.$subscriptionsListDiv.hide();
- }
- this._fixTableBodyHeight();
- this._setEvents();
- }.bind(this));
- return deferred.promise();
- },
- updateView: function() {
- this.$el.empty();
- return this.render();
- },
- _renderChart: function() {
- var chartData = [
- {
- name: 'enabled',
- value: this.enabledCount
- },
- {
- name: 'disabled',
- value: this.disabledCount
- }
- ];
-
-
- this._chart = new BarChart({
- $el: this.$el.find('svg.chart'),
- data: chartData
- });
- },
- _updateChart: function(data) {
- this._chart.redraw(data);
- },
- _renderCheckBoxes: function(data) {
- var _self = this;
- for(var i = 0; i < data.length; i++) {
- var stateToggler = new CheckBox({
- 'id': 'subscription_' + data[i].id,
- 'el': _self.$el.find("[data-subscription-id='"+ data[i].id +"'] > td:first"),
- 'name': data[i].id,
- 'label': '',
- 'ariaLabel': data[i].checkbox_aria_label,
- 'checked': data[i].scheduleInfo.active,
- 'onChange': function(name, value) {
- _self._handleCheckBoxes({
- subscriptionId: name,
- state: value,
- $checkbox: this.getPropertyNode()
- });
- }
- });
- stateToggler.doRender();
- }
- },
- _handleCheckBoxes: function(properties) {
- var subscriptionId = properties.subscriptionId;
- var state = properties.state;
- var $checkbox = properties.$checkbox;
- $checkbox.addClass('disabled');
- controler.getSubscriptionDetails(subscriptionId, this.glassContext).then(function(data) {
- subscriptionDescriptor = data;
- subscriptionDescriptor.scheduleInfo.active = state;
- this._toggleSingleSubscriptionStatus(subscriptionDescriptor).then(function() {
- $checkbox.removeClass('disabled');
- });
- }.bind(this));
- },
- _toggleSingleSubscriptionStatus: function(descriptor) {
- var deferred = Q.defer();
- controler.updateSubscription(descriptor, this.glassContext).done(function(data) {
- this._updateSubscriptionList().then(function() {
- deferred.resolve();
- });
- }.bind(this));
- return deferred.promise;
- },
- _fixTableBodyHeight: function() {
- var contentHeight = this.$el.find('div.s10n_main').height();
- var topHeight = this.$el.find('div.s10n_header').height() + this.$el.find('svg.chart').height();
-
- this.$el.find('div.dataTables_scrollBody').height(contentHeight - topHeight - 40);
- },
- _setEvents: function() {
- var _self = this;
- window.onresize = function() {
- if(_self.$el.find('svg.chart').css('visibility') !== 'hidden' && $(window).width() !== _self.windowWidth) {
- _self.windowWidth = $(window).width();
- _self._updateChart();
- }
-
- _self._fixTableBodyHeight();
- };
- $('button.btnContextMenu').on('clicktap', function(event) {
- var menuProperties = {
- 'menuId': 'com.ibm.bi.subscribe_mgmt.contextMenu',
- 'position': {
- pageX: event.pageX,
- pageY: event.pageY
- },
- 'activeObject': {
- currentTarget: event.currentTarget,
- subscriptionId: $(event.currentTarget).closest('tr').data('subscription-id'),
- updateCallback: _self.updateView.bind(_self),
- deleteCallbacks: {
- hideRow: _self._hideDeleteRow.bind(_self),
- undoDelete: _self._undoDeleteAndShowRow.bind(_self),
- complete: _self._completeSubscriptionDelete.bind(_self)
- }
- }
- };
- _self.glassContext.appController.showContextMenu(menuProperties);
- });
- },
- _hideDeleteRow: function($subscriptionRow) {
- var deferred = Q.defer();
- var _self = this;
- $subscriptionRow.hide(DEFAULT_DELAY, function() {
- if(_self.subscriptionsCount == 1) {
- _self.$noSubscriptionsDiv.show();
- _self.$subscriptionsListDiv.hide();
- }
- deferred.resolve();
- });
- return deferred.promise;
- },
- _undoDeleteAndShowRow: function($subscriptionRow) {
- var _self = this;
-
- $subscriptionRow.show(0, function() {
- if(_self.subscriptionsCount == 1) {
- _self.$noSubscriptionsDiv.hide();
- _self.$subscriptionsListDiv.show();
- }
- });
- },
- _completeSubscriptionDelete: function(options) {
- if(options.pending) {
- controler.deleteSubscription(options.subscriptionId, this.glassContext).done(function() {
- this._updateSubscriptionList();
- }.bind(this));
- }
- },
- _updateSubscriptionList: function() {
- var deferred = Q.defer();
- controler.getSubscriptions(this.glassContext).done(function(data) {
- this.subscriptionsCount = data.length;
- var enabled = _.countBy(data, function(s) {
- if(s.scheduleInfo.active)
- return "enabled";
- });
- if(!enabled.enabled) {
- enabled.enabled = 0;
- }
- this.enabledCount = enabled.enabled;
- this.disabledCount = this.subscriptionsCount - enabled.enabled;
- if(this.subscriptionsCount === 0) {
- this.$noSubscriptionsDiv.show();
- this.$subscriptionsListDiv.hide();
- }
- var chartData = [
- {
- name: 'enabled',
- value: this.enabledCount
- },
- {
- name: 'disabled',
- value: this.disabledCount
- }
- ];
- this._updateChart(chartData);
- deferred.resolve();
- }.bind(this));
- return deferred.promise;
- },
- _setSvg: function() {
- var $icons = this.$el.find('.common_icon');
- for(var i = 0; i < $icons.length; i++) {
- var $icon = $($icons[i]);
- Utils.setIcon($icon, 'common-' + $icon.data('icon'), t.translate('svg_menu_icon'));
- }
- },
- _tableColumnHeaders: function() {
- return [
- {
- 'name': 'enable',
- 'label': t.translate("subscribe_status_header"),
- 'class': 's10n_status_col'
- },
-
- {
- 'name': 'name',
- 'label': t.translate("subscribe_header"),
- 'sort': true
- },
-
- {
- 'name': 'owner',
- 'label': t.translate("subscribe_owner_header"),
- 'sort': true
- },
-
- {
- 'name': 'modified',
- 'label': t.translate("subscribe_modified_header"),
- 'sort': true
- },
-
- {
- 'name': 'context'
- }
- ];
- }
- });
- return view;
- });
|