123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- "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;
- });
|