GridDetailsWidget.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. this.selectChild(tab);
  82. },
  83. // Implementation ---
  84. initGrid: function() {
  85. var context = this;
  86. var store = new Memory({
  87. idProperty: this.idProperty,
  88. data: []
  89. });
  90. this.store = Observable(store);
  91. this.grid = this.createGrid(this.id + "Grid");
  92. this.grid.on(".dgrid-row:dblclick", function (evt) {
  93. if (context._onRowDblClick) {
  94. var row = context.grid.row(evt).data;
  95. context._onRowDblClick(row);
  96. }
  97. });
  98. this.grid.on(".dgrid-row:contextmenu", function (evt) {
  99. if (context._onRowContextMenu) {
  100. }
  101. });
  102. this.grid.onSelectionChanged(function (event) {
  103. context._refreshActionState();
  104. });
  105. this.grid.onContentChanged(function (object, removedFrom, insertedInto) {
  106. context._refreshActionState();
  107. });
  108. if (!this.grid.get("noDataMessage")) {
  109. this.grid.set("noDataMessage", "<span class='dojoxGridNoData'>" + this.i18n.noDataMessage + "</span>");
  110. }
  111. if (!this.grid.get("loadingMessage")) {
  112. this.grid.set("loadingMessage", "<span class='dojoxGridNoData'>" + this.i18n.loadingMessage + "</span>");
  113. }
  114. this.grid.startup();
  115. },
  116. getTitle: function () {
  117. return this.gridTitle;
  118. },
  119. appendMenuItem: function (menu, label, onClick) {
  120. var menuItem = new MenuItem({
  121. label: label,
  122. onClick: onClick
  123. });
  124. menu.addChild(menuItem);
  125. return menuItem;
  126. },
  127. appendContextMenuItem: function (label, onClick) {
  128. return this.appendMenuItem(this.contextMenu, label, onClick);
  129. },
  130. initContextMenu: function () {
  131. var context = this;
  132. this.contextMenu = new Menu({
  133. targetNodeIds: [this.id + "Grid"]
  134. });
  135. this.appendContextMenuItem(this.i18n.Refresh, function () {
  136. context._onRefresh();
  137. });
  138. this.contextMenu.addChild(new MenuSeparator());
  139. this.appendContextMenuItem(this.i18n.Open, function () {
  140. context._onOpen();
  141. });
  142. if (this.appendContextMenu) {
  143. this.appendContextMenu();
  144. }
  145. this.contextMenu.startup();
  146. },
  147. initTab: function () {
  148. var currSel = this.getSelectedChild();
  149. if (currSel) {
  150. if (!currSel.initalized) {
  151. if (currSel.init && currSel.hpcc) {
  152. currSel.init(currSel.hpcc.params);
  153. }
  154. currSel.initalized = true;
  155. } else if (currSel.refresh && !currSel.noRefresh) {
  156. currSel.refresh(currSel.hpcc.refreshParams);
  157. }
  158. }
  159. },
  160. getDetailID: function (row, params) {
  161. return this.id + "_" + "Detail" + row[this.idProperty];
  162. },
  163. ensurePane: function (row, params) {
  164. var id = this.getDetailID(row, params);
  165. var retVal = registry.byId(id);
  166. if (!retVal) {
  167. retVal = this.createDetail(id, row, params);
  168. this.addChild(retVal);
  169. } else {
  170. lang.mixin(retVal.hpcc, {
  171. refreshParams: params
  172. });
  173. }
  174. return retVal;
  175. },
  176. _refreshActionState: function () {
  177. var selection = this.grid.getSelected();
  178. this.refreshActionState(selection);
  179. },
  180. refreshActionState: function (selection) {
  181. registry.byId(this.id + "Open").set("disabled", !selection.length);
  182. }
  183. });
  184. });