"use strict"; /** * Licensed Materials - Property of IBM * IBM Cognos Products: admin * Copyright IBM Corp. 2017 * US Government Users Restricted Rights * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ define(['bi/glass/app/util/View', 'underscore'], function (View, _) { 'use strict'; //NOSONAR: meant to be strict var GenericListView = View.extend({ selectedRows: [], sortAsc: true, render: function render(refresh) { return this._renderListView(refresh); }, _setHeights: function _setHeights() { var list = this.$el.find(".bi-admin-list"); var windowHeight = $(window).height(); var headerElementsHeight = this.$el.offset().top; var updatedListHeight = windowHeight - headerElementsHeight - 20; list.height(updatedListHeight); }, _getItemById: function _getItemById(id) { return _.find(this.items, function (item) { return _.unescape(id) === _.unescape(item.id); }); }, _getSelectedItems: function _getSelectedItems() { return this.selectedRows.map(function (row) { return this._getItemById(row.id); }.bind(this)); }, _bindEvents: function _bindEvents() { var $items = this.$el.find('.bi-admin-list-item'); $items.off(); this.off('showContextMenu'); $items.on('primaryaction', function (e) { this._handleSelection(e, false); }.bind(this)); $items.on('contextmenu', function (e) { e.preventDefault(); }.bind(this)); this.on('showContextMenu', function (e) { var handler = this._getNewActionHandler(); var args = { position: e.position, menuId: this.menuId, activeObject: { handler: handler, data: e.data, parentView: this } }; this.glassContext.appController.showContextMenu(args); }.bind(this)); // context menu var $contextMenus = this.$el.find('.ellipsesButton'); $contextMenus.on('primaryaction', function (e) { this._handleSelection(e, true); this._handleContextMenuEvent(e); }.bind(this)); $(window).on('resize', this._setHeights.bind(this)); }, _handleContextMenuEvent: function _handleContextMenuEvent(e) { var position = {}; if (e.pageX === undefined || e.gesture === undefined || e.gesture.center === undefined || e.gesture.center.pageX === undefined) { position = $(e.target).offset(); } else { position.left = e.pageX || e.gesture.center.pageX; position.top = e.pageY || e.gesture.center.pageY; } this.trigger('showContextMenu', { data: this._getSelectedItems(), position: { "pageX": position.left, "pageY": position.top } }); e.stopPropagation(); return false; }, _handleNormalSelect: function _handleNormalSelect(id, $item, selectedIndex, dirtySelections, contextMenuAction) { var selections = dirtySelections; // normal click or keypress var contextSelectUnselectedNode = contextMenuAction && !selectedIndex; var singleSelection = !contextMenuAction && (!selectedIndex || this.selectedRows.length > 1); if (contextSelectUnselectedNode || singleSelection) { selections = this._clearAllSelections(); this._selectItem(id, $item, selections); } this.lastSelectionId = id; this.lastShiftSelectionIndex = -1; return selections; }, _handleMetaSelect: function _handleMetaSelect(id, $item, selectedIndex, dirtySelections) { var selections = dirtySelections; // multi-select via ctrl key (or macOS equivalent) if (!selectedIndex) { this._selectItem(id, $item, selections); } else { this._removeItem(id, $item, selections); } this.lastSelectionId = id; this.lastShiftSelectionIndex = -1; return selections; }, _handleShiftSelect: function _handleShiftSelect(selectedItem, dirtySelections) { var selections = dirtySelections; // multi-select items between last selected and target var lastSelectedIndex = _.indexOf(this.items, this._getItemById(this.lastSelectionId)); var targetIndex = _.indexOf(this.items, selectedItem); // account for the indexing of jquery slice method (include last node) if (targetIndex > lastSelectedIndex) { targetIndex++; } if (targetIndex < lastSelectedIndex) { lastSelectedIndex++; } var shiftBetweenLower = targetIndex < lastSelectedIndex && targetIndex > this.lastShiftSelectionIndex; var shiftBetweenHigher = targetIndex > lastSelectedIndex && targetIndex < this.lastShiftSelectionIndex; if (this.lastShiftSelectionIndex !== -1 && (shiftBetweenHigher || shiftBetweenLower)) { // selection is between the last selected and the last shift select target // must remove the difference this._removeShiftSelection(targetIndex, this.lastShiftSelectionIndex, selections); } else { if (this.lastShiftSelectionIndex >= 0) { this._removeShiftSelection(targetIndex, this.lastShiftSelectionIndex, selections); } this._addShiftSelection(lastSelectedIndex, targetIndex, selections); } this.lastShiftSelectionIndex = targetIndex; return selections; }, _selectItem: function _selectItem(id, $item, dirtySelections) { var schemaToSelect = { id: id, item: $item }; var index; if (this._isItemSelectable(this._getItemById(id))) { for (var i = 0; i < this.selectedRows.length; i++) { if (this.selectedRows[i].id === id) { index = i; break; } } if (index > -1) { this.selectedRows[index] = schemaToSelect; } else { this.selectedRows.push(schemaToSelect); } dirtySelections.push({ item: $item, selected: true }); } }, _removeItem: function _removeItem(id, $item, dirtySelections) { var pos; for (var i = 0; i < this.selectedRows.length; i++) { if (this.selectedRows[i].id === id) { pos = i; break; } } this.selectedRows.splice(pos, 1); dirtySelections.push({ item: $item, selected: false }); }, _clearAllSelections: function _clearAllSelections() { var dirtySelections = this.selectedRows.map(function (selectedRow) { return { item: selectedRow.item, selected: false }; }); this.selectedRows = []; return dirtySelections; }, _addShiftSelection: function _addShiftSelection(index0, index1, dirtySelections) { // new selection is last selected -> new target var $items = this._getSelectionsBetweenIndicies(index0, index1); _.forEach($items, function (shiftItem) { var $shiftItem = $(shiftItem); this._selectItem($shiftItem.attr('id').trim(), $shiftItem, dirtySelections); }.bind(this)); }, _removeShiftSelection: function _removeShiftSelection(index0, index1, dirtySelections) { // remove previously shift selected items var $items = this._getSelectionsBetweenIndicies(index0, index1); _.forEach($items, function (shiftItem) { var $shiftItem = $(shiftItem); this._removeItem($shiftItem.attr('id').trim(), $shiftItem, dirtySelections); }.bind(this)); }, _getSelectionsBetweenIndicies: function _getSelectionsBetweenIndicies(id0, id1) { var $items; if (id0 < id1) { $items = this.$el.find('.bi-admin-list-item').slice(id0, id1); } else { $items = this.$el.find('.bi-admin-list-item').slice(id1, id0); } return $items; }, _applySelectionStyling: function _applySelectionStyling(dirtySelections) { _.forEach(dirtySelections, function (dirtySelection) { if (dirtySelection.selected) { dirtySelection.item.addClass('bi-admin-list-table-selected-schema'); } else { dirtySelection.item.removeClass('bi-admin-list-table-selected-schema'); } }); dirtySelections = []; }, selectById: function selectById(id) { var item = this._getItemById(id) || this._getItemByConnId(id); this.trigger('selected', item); }, selectByIndex: function selectByIndex(index) { var item = this.items[index]; this.trigger('selected', item); }, removeSelectedRowById: function removeSelectedRowById(id) { _.find(this.$el.find('tr.list-item'), function (item) { if (item.id === id) { return item; } }).remove(); if ($('#workPane').is(":visible")) { $('#workPane').addClass('hide'); } }, insertCopyRowById: function insertCopyRowById(copyRow, id) { var copySource = _.find(this.$el.find('tr.list-item'), function (item) { if (item.id === id) { return item; } }); var targetSource = _.clone(copySource); targetSource.id = id; targetSource.insertAfter(copySource); }, _sortItems: function _sortItems(field) { var tempSortField; for (var key in this.sortMap) { if (field.indexOf(key) > -1) { tempSortField = key; break; } } // no sort key found, do not sort if (tempSortField) { if (tempSortField !== this.sortField) { // only sort on first press this.sortField = tempSortField; this.items = _.sortBy(this.items, function (item) { return item[this.sortMap[this.sortField]].toUpperCase(); }.bind(this)); this.sortAsc = true; } else { // reverse on subsequent clicks this.items = this.items.reverse(); this.sortAsc = !this.sortAsc; } } }, remove: function remove() { $(window).off("resize", this._setHeights); }, _isItemSelectable: function _isItemSelectable(item) { return true; } }); return GenericListView; });