Guest User

Untitled

a guest
Jan 22nd, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.12 KB | None | 0 0
  1. /*
  2. * This file belongs to Hoist, an application development toolkit
  3. * developed by Extremely Heavy Industries (www.xh.io | info@xh.io)
  4. *
  5. * Copyright © 2017 Extremely Heavy Industries Inc.
  6. */
  7.  
  8.  
  9. XH.defineView('XH.cube.DimensionChooser', {
  10. extend: Ext.Panel,
  11. mixins: [XH.cube.BaseDimensionChooser],
  12. xtype: 'xhDimChooser',
  13.  
  14. cls: 'xh-dimchooser-dim',
  15.  
  16. layout: {type: 'vbox', align: 'stretch'},
  17.  
  18. _store: null,
  19. _value: null,
  20.  
  21. bbar: {
  22. items: [
  23. '->',
  24. {
  25. glyph: XH.Glyph.X,
  26. cls: 'red-glyph',
  27. handler: 'up.onClose'
  28. },
  29. {
  30. text: 'Apply',
  31. glyph: XH.Glyph.CHECK,
  32. cls: 'green-glyph',
  33. handler: 'up.onApply'
  34. }
  35. ]
  36. },
  37.  
  38. initComponent() {
  39. this.callParent();
  40.  
  41. const qDims = this._value = this.getQueryDims(),
  42. store = this._store = this.buildDimStore(qDims[0]).store,
  43. levels = this.getMaxDepth();
  44.  
  45. for (let i = 0; i < levels; i++) {
  46. const ct = this.buildDimensionContainer(store, i);
  47.  
  48. if (i > 0) {
  49. this.addLevelIndentation(ct, i);
  50. }
  51.  
  52. }
  53.  
  54. this.syncButtonValues();
  55. },
  56.  
  57. reset() {
  58. this.setValue(this.getQueryDims());
  59. },
  60.  
  61. //------------------------
  62. // Implementation
  63. //------------------------
  64. addLevelIndentation(container, count) {
  65. const indents = [];
  66. for (let i = 0; i < count; i++) {
  67. let cls = 'xh-dimension-indentation';
  68. if (i === count - 1) cls += ' xh-last';
  69.  
  70. indents.push({
  71. xtype: 'component',
  72. width: 8,
  73. cls: cls
  74. });
  75. }
  76.  
  77. container.insert(0, indents);
  78. },
  79.  
  80. buildDimensionContainer(store, idx) {
  81. const combo = Ext.widget({
  82. xtype: 'combobox',
  83. _index: idx,
  84. queryMode: 'local',
  85. valueField: 'name',
  86. displayField: 'displayName',
  87. store: new Ext.data.ChainedStore({
  88. source: store
  89. }),
  90. listeners: {
  91. change: this.onComboChanged,
  92. scope: this
  93. },
  94. flex: 1,
  95. editable: false
  96. }),
  97.  
  98. removeButton = Ext.widget({
  99. xtype: 'button',
  100. glyph: XH.Glyph.REMOVE,
  101. hidden: true,
  102. handler: this.onRemoveClick,
  103. scope: this
  104. }),
  105.  
  106. addButton = Ext.widget({
  107. xtype: 'button',
  108. glyph: XH.Glyph.ADD,
  109. hidden: true,
  110. handler: this.onAddClick,
  111. scope: this
  112. });
  113.  
  114. return this.add({
  115. xtype: 'container',
  116. layout: {type: 'hbox', align: 'stretch'},
  117. defaults: {margin: 2},
  118. flex: 1,
  119. hidden: true,
  120. _buttons: {combo, removeButton, addButton},
  121. items: [combo, removeButton, addButton]
  122. });
  123. },
  124.  
  125. getMaxDepth() {
  126. return this.maxDepth || this._store.getCount() - 1;
  127. },
  128.  
  129. onAddClick(btn) {
  130. const ct = btn.up('container'),
  131. nextCt = ct.nextSibling(),
  132. nextCombo = nextCt._buttons.combo,
  133. store = nextCombo.getStore();
  134.  
  135. this.setValue(this.getValue().concat(store.getAt(0).get('name')));
  136. nextCombo.expand();
  137.  
  138. this.fireEvent('dimensionAdded', nextCombo);
  139. },
  140.  
  141. onRemoveClick(btn) {
  142. const value = this.getValue().slice(0);
  143. value.splice(-1, 1);
  144. this.setValue(value);
  145.  
  146. this.fireEvent('dimensionRemoved', btn.previousNode('combobox'));
  147. },
  148.  
  149. onApply() {
  150. const value = this.getValue();
  151.  
  152. if (!Ext.Array.equals(this.getQueryDims(), value)) {
  153. this.getCubeView().updateQuery({dimensions: value});
  154. this.fireEvent('change', this, value);
  155. }
  156.  
  157. this.getRefOwner().hide();
  158. },
  159.  
  160. onClose() {
  161. this.getRefOwner().hide();
  162. },
  163.  
  164. onComboChanged(combo, value) {
  165. if (!this.getValue()|| !value) return;
  166.  
  167. let values = this.getValue().slice(0),
  168. removed = false;
  169.  
  170. const idx = values.indexOf(value);
  171.  
  172. if (idx !== -1 || this.isLeafDim(value)) {
  173. values = values.slice(0, idx);
  174. removed = true;
  175. }
  176.  
  177. values.splice(combo._index, 1, value);
  178.  
  179. this.setValue(values);
  180.  
  181. if (removed) {
  182. this.fireEvent('dimensionRemoved');
  183. }
  184.  
  185. this.syncButtonValues();
  186. },
  187.  
  188. getValue() {
  189. return this._value;
  190. },
  191.  
  192. setValue(value) {
  193. this._value = value;
  194. this.syncButtonValues();
  195. },
  196.  
  197. syncButtonValues() {
  198. const values = this.getValue(),
  199. items = this.items;
  200.  
  201. items.each((item, idx) => {
  202. const val = values[idx],
  203. {combo, addButton, removeButton} = item._buttons,
  204. hidden = (val == null),
  205. shouldFilter = idx > 0;
  206.  
  207. item.setHidden(hidden);
  208.  
  209. if (shouldFilter) {
  210. const store = combo.getStore();
  211. store.clearFilter(true);
  212.  
  213. if (values[idx - 1]) {
  214. store.filter(rec => !values.slice(0, idx).includes(rec.get('name')));
  215. }
  216. }
  217.  
  218. combo.suspendEvents();
  219. combo.setValue(val);
  220. combo.resumeEvents(true);
  221.  
  222. const record = combo.getSelectedRecord(),
  223. isLeafDim = record && this.isLeafDim(record.get('name'));
  224.  
  225. if (idx) {
  226. const maxLevels = this.getMaxDepth(),
  227. isLastBtn = idx == values.length - 1;
  228.  
  229. addButton.setHidden(
  230. !isLastBtn ||
  231. idx + 1 >= maxLevels ||
  232. !record ||
  233. isLeafDim
  234. );
  235.  
  236. removeButton.setHidden(!isLastBtn);
  237. } else {
  238. addButton.setHidden(values.length > 1 || isLeafDim);
  239. }
  240. });
  241. }
  242. });
Add Comment
Please, Sign In to add comment