TableListView.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2016, 2021
  6. * US Government Users Restricted Rights
  7. * Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  8. */
  9. define(['jquery', 'doT', 'q', 'bi/glass/app/util/View', 'underscore', 'bi/admin/nls/StringResource', 'bi/admin/datasource/App', 'bi/commons/ui/content/views/GroupListView', 'bi/commons/ui/content/views/SearchView', 'bi/commons/ui/properties/PropertyUIControl', 'text!bi/admin/datasource/templates/TableListViewTemplate.html'], function ($, dot, Q, View, _, StringResource, App, GroupListView, SearchView, PropertyUIControl, TableListViewTemplate) {
  10. //NOSONAR: needed for amd
  11. 'use strict'; //NOSONAR: meant to be strict
  12. var TableListView = View.extend({
  13. items: null,
  14. groupListView: null,
  15. _state: null,
  16. updateState: function updateState(key, value) {
  17. var obj = {};
  18. if ($.type(key) !== 'string') {
  19. obj = key;
  20. } else {
  21. obj[key] = value;
  22. }
  23. $.extend(this._state, obj);
  24. this.parentView.updateState(this._state);
  25. },
  26. /**
  27. * @constructor
  28. */
  29. init: function init(options) {
  30. TableListView.inherited('init', this, arguments);
  31. _.extend(this, options);
  32. this._state = {};
  33. $.extend(this._state, this.prop);
  34. this.$el.html(dot.template(TableListViewTemplate));
  35. this.groupListView = new GroupListView({
  36. 'el': this.$el.find('.schemaTablesSection'),
  37. 'selectable': true,
  38. 'groupSelection': true,
  39. 'navigable': true,
  40. 'showFilter': true,
  41. 'glassContext': this.glassContext,
  42. 'searchView': new SearchView({})
  43. });
  44. this.groupListView.on('checkbox:primaryaction', this._itemOnCheck.bind(this));
  45. this.includeExcludeRadioButtons = [{
  46. 'type': 'HorizontalRadioButtonGroup',
  47. 'name': 'includeExcludeRadioButton',
  48. 'value': this._getInitialIncludeExcludeSelection(),
  49. //Need to check what we get back and set this properly
  50. 'items': [{
  51. 'label': StringResource.get('include'),
  52. 'value': 'include'
  53. }, {
  54. 'label': StringResource.get('exclude'),
  55. 'value': 'exclude'
  56. }],
  57. 'onChange': this._onChangeIncludeExclude.bind(this),
  58. 'ariaLabel': StringResource.get('includeExcludeTables')
  59. }];
  60. },
  61. _onChangeIncludeExclude: function _onChangeIncludeExclude(group, value) {
  62. this.updateState('includeOrExcludeSelection', value);
  63. this._updateLoadButtonState();
  64. },
  65. _updateLoadButtonState: function _updateLoadButtonState() {
  66. if (this._state.includeOrExcludeSelection === 'exclude') {
  67. this.updateState('loadBtnDisabled', this._state.selectedTables.length === this.items.length);
  68. } else if (this._state.includeOrExcludeSelection === 'include') {
  69. this.updateState('loadBtnDisabled', this._state.selectedTables.length === 0);
  70. }
  71. },
  72. _getInitialIncludeExcludeSelection: function _getInitialIncludeExcludeSelection() {
  73. if (this._state.includeOrExcludeSelection === 'exclude') {
  74. return 'exclude';
  75. }
  76. return 'include';
  77. },
  78. //for the best performance, do not change the loop to _.each
  79. _itemOnCheck: function _itemOnCheck(items) {
  80. for (var i = 0; i < items.length; i++) {
  81. this._changeTables(items[i].id, items[i].checked);
  82. }
  83. this.updateState('selectedTables', this._state.selectedTables);
  84. this._updateLoadButtonState();
  85. },
  86. //for the best performance, do not change to _.different or union
  87. _changeTables: function _changeTables(id, isChecked) {
  88. if (isChecked) {
  89. this._state.selectedTables.push(id);
  90. } else {
  91. this._state.selectedTables.splice(this._state.selectedTables.indexOf(id), 1);
  92. }
  93. },
  94. _setHeights: function _setHeights() {
  95. var windowHeight = $(window).height();
  96. var toggleElement = this.$el.find(".listGroupSelection");
  97. var tableCont = this.$el.find(".listbody");
  98. var headerElementsHeight = tableCont.offset().top;
  99. var footerElHeight = $(this.$el.parents(".flyoutPane")[0]).find("footer").height();
  100. var updatedListHeight = windowHeight - toggleElement.height() - headerElementsHeight - footerElHeight - 80;
  101. tableCont.height(updatedListHeight);
  102. },
  103. _getCheckedValue: function _getCheckedValue(table) {
  104. return this._state.selectedTables.indexOf(table) != -1;
  105. },
  106. /**
  107. Main entry point - will render the table list view.
  108. **/
  109. render: function render() {
  110. var deferred = Q.defer();
  111. this._oPropertyUIControl = new PropertyUIControl({
  112. 'glassContext': this.glassContext,
  113. 'el': this.$el.find('.includeExcludeRadioButtons'),
  114. 'items': this.includeExcludeRadioButtons
  115. });
  116. this._oPropertyUIControl.render().then(function () {
  117. var signonInfo;
  118. if (this.parentView && this.parentView.signonAndConnection) {
  119. signonInfo = this.parentView.signonAndConnection;
  120. }
  121. App.getTables(this.schemaId, signonInfo).done(function (tables) {
  122. this.items = _.map(tables.data, function (table) {
  123. return {
  124. 'id': table,
  125. 'value': table,
  126. 'label': table,
  127. 'toolTip': table,
  128. 'checked': this._getCheckedValue(table),
  129. 'svgHref': '#modeller-querySubject'
  130. };
  131. }.bind(this));
  132. this.groupListView.setItems(this.items);
  133. this.groupListView.render().done(function () {
  134. this._setHeights();
  135. deferred.resolve();
  136. }.bind(this));
  137. }.bind(this));
  138. }.bind(this));
  139. return deferred.promise;
  140. }
  141. });
  142. return TableListView;
  143. });