GridDetailsWidget.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*##############################################################################
  2. # HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. ############################################################################## */
  16. define([
  17. "dojo/_base/declare",
  18. "dojo/_base/lang",
  19. "dojo/i18n",
  20. "dojo/i18n!./nls/common",
  21. "dojo/i18n!./nls/GridDetailsWidget",
  22. "dojo/store/Memory",
  23. "dojo/store/Observable",
  24. "dijit/registry",
  25. "dijit/Menu",
  26. "dijit/MenuItem",
  27. "dijit/MenuSeparator",
  28. "dijit/PopupMenuItem",
  29. "hpcc/_TabContainerWidget",
  30. "dojo/text!../templates/GridDetailsWidget.html",
  31. "dijit/layout/TabContainer",
  32. "dijit/layout/BorderContainer",
  33. "dijit/Toolbar",
  34. "dijit/form/Button",
  35. "dijit/ToolbarSeparator",
  36. "dijit/layout/ContentPane"
  37. ], function (declare, lang, i18n, nlsCommon, nlsSpecific, Memory, Observable,
  38. registry, Menu, MenuItem, MenuSeparator, PopupMenuItem,
  39. _TabContainerWidget,
  40. template) {
  41. return declare("GridDetailsWidget", [_TabContainerWidget], {
  42. templateString: template,
  43. baseClass: "GridDetailsWidget",
  44. i18n: lang.mixin(nlsCommon, nlsSpecific),
  45. gridTitle: "Change Me",
  46. idProperty: "Change Me",
  47. store: null,
  48. toolbar: null,
  49. gridTab: null,
  50. grid: null,
  51. contextMenu: null,
  52. postCreate: function (args) {
  53. this.inherited(arguments);
  54. this.toolbar = registry.byId(this.id + "Toolbar");
  55. this.gridTab = registry.byId(this.id + "_Grid");
  56. },
  57. startup: function (args) {
  58. this.inherited(arguments);
  59. this.initGrid();
  60. this.initContextMenu();
  61. },
  62. // Hitched actions ---
  63. _onRefresh: function (event) {
  64. this.refreshGrid();
  65. },
  66. _onOpen: function (event, params) {
  67. var selections = this.grid.getSelected();
  68. var firstTab = null;
  69. for (var i = 0; i < selections.length; ++i) {
  70. var tab = this.ensurePane(selections[i], params);
  71. if (i == 0) {
  72. firstTab = tab;
  73. }
  74. }
  75. if (firstTab) {
  76. this.selectChild(firstTab);
  77. }
  78. },
  79. _onRowDblClick: function (row) {
  80. var tab = this.ensurePane(row);
  81. if (tab) {
  82. this.selectChild(tab);
  83. }
  84. },
  85. // Implementation ---
  86. initGrid: function() {
  87. var context = this;
  88. this.noDataMessage = this.i18n.noDataMessage;
  89. this.loadingMessage = this.i18n.loadingMessage;
  90. var store = new Memory({
  91. idProperty: this.idProperty,
  92. data: []
  93. });
  94. this.store = Observable(store);
  95. this.grid = this.createGrid(this.id + "Grid");
  96. this.grid.on(".dgrid-row:dblclick", function (evt) {
  97. if (context._onRowDblClick) {
  98. var row = context.grid.row(evt).data;
  99. context._onRowDblClick(row);
  100. }
  101. });
  102. this.grid.on(".dgrid-row:contextmenu", function (evt) {
  103. if (context._onRowContextMenu) {
  104. }
  105. });
  106. this.grid.onSelectionChanged(function (event) {
  107. context._refreshActionState();
  108. });
  109. this.grid.onContentChanged(function (object, removedFrom, insertedInto) {
  110. context._refreshActionState();
  111. });
  112. if (!this.grid.get("noDataMessage")) {
  113. this.grid.set("noDataMessage", "<span class='dojoxGridNoData'>" + this.noDataMessage + "</span>");
  114. }
  115. if (!this.grid.get("loadingMessage")) {
  116. this.grid.set("loadingMessage", "<span class='dojoxGridNoData'>" + this.loadingMessage + "</span>");
  117. }
  118. this.grid.startup();
  119. },
  120. getTitle: function () {
  121. return this.gridTitle;
  122. },
  123. appendMenuItem: function (menu, label, onClick) {
  124. var menuItem = new MenuItem({
  125. label: label,
  126. onClick: onClick
  127. });
  128. menu.addChild(menuItem);
  129. return menuItem;
  130. },
  131. appendContextMenuItem: function (label, onClick) {
  132. return this.appendMenuItem(this.contextMenu, label, onClick);
  133. },
  134. initContextMenu: function () {
  135. var context = this;
  136. this.contextMenu = new Menu({
  137. targetNodeIds: [this.id + "Grid"]
  138. });
  139. this.appendContextMenuItem(this.i18n.Refresh, function () {
  140. context._onRefresh();
  141. });
  142. this.contextMenu.addChild(new MenuSeparator());
  143. this.appendContextMenuItem(this.i18n.Open, function () {
  144. context._onOpen();
  145. });
  146. if (this.appendContextMenu) {
  147. this.appendContextMenu();
  148. }
  149. this.contextMenu.startup();
  150. },
  151. initTab: function () {
  152. var currSel = this.getSelectedChild();
  153. if (currSel) {
  154. if (!currSel.initalized) {
  155. if (currSel.init && currSel.hpcc) {
  156. currSel.init(currSel.hpcc.params);
  157. }
  158. currSel.initalized = true;
  159. } else if (currSel.refresh && !currSel.noRefresh) {
  160. currSel.refresh(currSel.hpcc.refreshParams);
  161. }
  162. }
  163. },
  164. getDetailID: function (row, params) {
  165. return this.id + "_" + "Detail" + row[this.idProperty];
  166. },
  167. ensurePane: function (row, params) {
  168. var id = this.getDetailID(row, params);
  169. var retVal = registry.byId(id);
  170. if (!retVal) {
  171. retVal = this.createDetail(id, row, params);
  172. if (retVal) {
  173. this.addChild(retVal);
  174. }
  175. } else {
  176. lang.mixin(retVal.hpcc, {
  177. refreshParams: params
  178. });
  179. }
  180. return retVal;
  181. },
  182. _refreshActionState: function () {
  183. var selection = this.grid.getSelected();
  184. this.refreshActionState(selection);
  185. },
  186. refreshActionState: function (selection) {
  187. registry.byId(this.id + "Open").set("disabled", !selection.length);
  188. }
  189. });
  190. });