LoadOptionsView.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. /**
  3. * Licensed Materials - Property of IBM
  4. * IBM Cognos Products: admin
  5. * Copyright IBM Corp. 2016, 2017
  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/properties/PropertyUIControl', 'bi/commons/i18n/Formatter'], function ($, dot, Q, View, _, StringResource, App, PropertyUIControl, Formatter) {
  10. //NOSONAR: needed for amd
  11. 'use strict'; //NOSONAR: meant to be strict
  12. var MAX_ROWS = 10000;
  13. var DEFAULT_ROWS = 1000;
  14. var LoadOptionsView = View.extend({
  15. _formatedMaxRows: null,
  16. _state: {
  17. 'dataStatistics': 'all',
  18. 'dataSamplingSize': DEFAULT_ROWS,
  19. 'advancedOpen': false,
  20. 'importPrimaryForeignKeys': true
  21. },
  22. /**
  23. * @constructor
  24. */
  25. init: function init(options) {
  26. LoadOptionsView.inherited('init', this, arguments);
  27. _.extend(this, options);
  28. this._formatedMaxRows = Formatter.format(MAX_ROWS, {
  29. type: 'number',
  30. locale: this.glassContext.services.userProfile.preferences.contentLocale
  31. });
  32. this.updateState(this.prop, null, true);
  33. },
  34. updateState: function updateState(key, value, withoutRedraw) {
  35. var obj = {};
  36. if ($.type(key) !== 'string') {
  37. obj = key;
  38. } else {
  39. obj[key] = value;
  40. }
  41. $.extend(this._state, obj);
  42. this._updateComputedState(); //update parent states after local ones are in sync
  43. this.parentView.updateState(this._state, null, withoutRedraw);
  44. if (!withoutRedraw) {
  45. this._redraw();
  46. }
  47. },
  48. _updateComputedState: function _updateComputedState() {
  49. this._state.dataSamplingChecked = this._getDataSamplingState();
  50. this._state.dataStatisticsChecked = this._getDataStatisticsState();
  51. },
  52. _getDataSamplingState: function _getDataSamplingState() {
  53. if (this._state.dataSamplingSize > 0) {
  54. return true;
  55. }
  56. return false;
  57. },
  58. _getDataStatisticsState: function _getDataStatisticsState() {
  59. if (this._state.dataStatistics === 'all') {
  60. return true;
  61. }
  62. return false;
  63. },
  64. _redraw: function _redraw() {
  65. this.$el.empty();
  66. return this.render();
  67. },
  68. /**
  69. Main entry point - will render the load option view
  70. **/
  71. render: function render() {
  72. var deferred = Q.defer();
  73. var items = [{
  74. 'type': 'HintText',
  75. 'label': StringResource.get('modelAssistHint'),
  76. 'id': 'modelAssistHint'
  77. }, {
  78. 'type': 'CheckBox',
  79. 'label': StringResource.get('IncludePFKey'),
  80. 'name': 'importPrimaryForeignKeys',
  81. 'checked': this._state.importPrimaryForeignKeys,
  82. 'onChange': this.updateState.bind(this)
  83. }, {
  84. 'type': 'CheckBox',
  85. 'label': StringResource.get('dataSampling'),
  86. 'name': 'dataSamplingChecked',
  87. 'checked': this._state.dataSamplingChecked,
  88. 'onChange': this.dataSamplingOnChange.bind(this)
  89. }, {
  90. 'id': 'admin_load_options_max_rows',
  91. 'type': 'SingleLineValue',
  92. 'label': StringResource.get('maxRows', {
  93. 'maxRows': this._formatedMaxRows
  94. }),
  95. 'name': 'dataSamplingSize',
  96. 'editable': this._state.dataSamplingChecked === true,
  97. 'onChange': this._maxRowsOnChange.bind(this),
  98. 'value': this._state.dataSamplingChecked === true ? this._state.dataSamplingSize : '',
  99. 'disabled': !(this._state.dataSamplingChecked === true)
  100. }];
  101. this._propertyUIControl = new PropertyUIControl({
  102. 'glassContext': this.glassContext,
  103. 'el': this.$el,
  104. 'items': items
  105. });
  106. this._propertyUIControl.render().then(function () {
  107. deferred.resolve();
  108. });
  109. return deferred.promise;
  110. },
  111. _dataStatisticsOnChange: function _dataStatisticsOnChange(key, value) {
  112. this.updateState(key, value ? 'all' : 'none');
  113. },
  114. dataSamplingOnChange: function dataSamplingOnChange(key, value) {
  115. this.updateState({
  116. 'dataSamplingSize': value ? DEFAULT_ROWS : -1
  117. });
  118. },
  119. _maxRowsOnChange: function _maxRowsOnChange(key, value) {
  120. var val = value;
  121. if (!$.isNumeric(value)) {
  122. val = DEFAULT_ROWS;
  123. } else if (value > MAX_ROWS) {
  124. val = MAX_ROWS;
  125. } else if (value < 1) {
  126. val = 1;
  127. }
  128. this.updateState(key, val);
  129. }
  130. });
  131. return LoadOptionsView;
  132. });