ECLPlaygroundWidget.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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/hpcc",
  21. "dojo/_base/xhr",
  22. "dojo/dom",
  23. "dojo/query",
  24. "dijit/layout/BorderContainer",
  25. "dijit/layout/TabContainer",
  26. "dijit/layout/ContentPane",
  27. "dijit/registry",
  28. "hpcc/_Widget",
  29. "hpcc/ECLSourceWidget",
  30. "hpcc/TargetSelectWidget",
  31. "hpcc/GraphWidget",
  32. "hpcc/ECLPlaygroundResultsWidget",
  33. "hpcc/ESPWorkunit",
  34. "hpcc/ESPQuery",
  35. "dojo/text!../templates/ECLPlaygroundWidget.html"
  36. ], function (declare, lang, i18n, nlsHPCC, xhr, dom, query,
  37. BorderContainer, TabContainer, ContentPane, registry,
  38. _Widget, EclSourceWidget, TargetSelectWidget, GraphWidget, ResultsWidget, ESPWorkunit, ESPQuery,
  39. template) {
  40. return declare("ECLPlaygroundWidget", [_Widget], {
  41. templateString: template,
  42. baseClass: "ECLPlaygroundWidget",
  43. i18n: nlsHPCC,
  44. wu: null,
  45. editorControl: null,
  46. graphControl: null,
  47. resultsWidget: null,
  48. targetSelectWidget: null,
  49. sampleSelectWidget: null,
  50. buildRendering: function (args) {
  51. this.inherited(arguments);
  52. },
  53. postCreate: function (args) {
  54. this.inherited(arguments);
  55. this._initControls();
  56. },
  57. startup: function (args) {
  58. this.inherited(arguments);
  59. },
  60. resize: function (args) {
  61. this.inherited(arguments);
  62. this.borderContainer.resize();
  63. },
  64. layout: function (args) {
  65. this.inherited(arguments);
  66. },
  67. // Implementation ---
  68. getTitle: function () {
  69. return this.i18n.title_ECLPlayground;
  70. },
  71. _initControls: function () {
  72. var context = this;
  73. this.borderContainer = registry.byId(this.id + "BorderContainer");
  74. this.targetSelectWidget = registry.byId(this.id + "TargetSelect");
  75. this.stackController = registry.byId(this.id + "StackController");
  76. this.stackContainer = registry.byId(this.id + "StackContainer");
  77. this.errWarnWidget = registry.byId(this.id + "_ErrWarn");
  78. this.resultsWidget = registry.byId(this.id + "_Results");
  79. this.resultsWidget.onErrorClick = function (line, col) {
  80. context.editorControl.setCursor(line, col);
  81. };
  82. this.visualizeWidget = registry.byId(this.id + "_Visualize");
  83. },
  84. hideTitle: function () {
  85. var topPane = dom.byId(this.id + "TopPane");
  86. dojo.destroy(topPane);
  87. this.borderContainer.resize();
  88. },
  89. init: function (params) {
  90. if (this.inherited(arguments))
  91. return;
  92. if (params.Wuid) {
  93. this.hideTitle();
  94. }
  95. this.Wuid = params.Wuid;
  96. this.targetSelectWidget.init(params);
  97. this.initEditor();
  98. this.editorControl.init(params);
  99. var context = this;
  100. this.initGraph();
  101. if (params.Wuid) {
  102. this.wu = ESPWorkunit.Get(params.Wuid);
  103. var data = this.wu.getData();
  104. for (var key in data) {
  105. this.updateInput(key, null, data[key]);
  106. }
  107. this.watchWU();
  108. } else {
  109. this.initSamples();
  110. this.graphControl.watchSelect(this.sampleSelectWidget);
  111. }
  112. this.graphControl.watchSplitter(this.borderContainer.getSplitter("right"));
  113. this.graphControl.watchSplitter(this.borderContainer.getSplitter("bottom"));
  114. },
  115. initSamples: function () {
  116. var context = this;
  117. this.sampleSelectWidget = registry.byId(this.id + "SampleSelect");
  118. this.sampleSelectWidget.onNewSelection = function (eclText) {
  119. context.resetPage();
  120. context.editorControl.setText(eclText);
  121. };
  122. this.sampleSelectWidget.init({
  123. ECLSamples: true,
  124. Target: "default.ecl"
  125. });
  126. },
  127. initEditor: function () {
  128. this.editorControl = registry.byId(this.id + "Source");
  129. },
  130. initGraph: function () {
  131. var context = this;
  132. this.graphControl = registry.byId(this.id + "GraphControl");
  133. this.graphControl.showNextPrevious(false);
  134. this.graphControl.showDistance(false);
  135. this.graphControl.showSyncSelection(false);
  136. this.graphControl.onSelectionChanged = function (items) {
  137. context.editorControl.clearHighlightLines();
  138. for (var i = 0; i < items.length; ++i) {
  139. var props = context.graphControl.getProperties(items[i]);
  140. if (props.definition) {
  141. var startPos = props.definition.indexOf("(");
  142. var endPos = props.definition.lastIndexOf(")");
  143. var pos = props.definition.slice(startPos + 1, endPos).split(",");
  144. var lineNo = parseInt(pos[0], 10);
  145. context.editorControl.highlightLine(lineNo);
  146. context.editorControl.setCursor(lineNo, 0);
  147. }
  148. }
  149. };
  150. },
  151. getGraph: function () {
  152. return registry.byId(this.id + "GraphControl");
  153. },
  154. resetPage: function () {
  155. this.editorControl.clearErrors();
  156. this.editorControl.clearHighlightLines();
  157. this.graphControl.clear();
  158. this.resultsWidget.clear();
  159. this.updateInput("State", null, "...");
  160. this.stackContainer.selectChild(this.resultsWidget);
  161. this.errWarnWidget.set("disabled", true);
  162. this.resultsWidget.set("disabled", true);
  163. this.visualizeWidget.set("disabled", true);
  164. },
  165. watchWU: function () {
  166. if (this.watching) {
  167. this.watching.unwatch();
  168. }
  169. var context = this;
  170. this.watching = this.wu.watch(function (name, oldValue, newValue) {
  171. context.updateInput(name, oldValue, newValue);
  172. if (name === "Exceptions" && newValue) {
  173. context.stackContainer.selectChild(context.errWarnWidget);
  174. context.errWarnWidget.set("disabled", false);
  175. context.errWarnWidget.reset();
  176. context.errWarnWidget.init({
  177. Wuid: context.wu.Wuid
  178. });
  179. } else if (name === "Results" && newValue) {
  180. context.stackContainer.selectChild(context.resultsWidget);
  181. context.resultsWidget.set("disabled", false);
  182. context.visualizeWidget.set("disabled", false);
  183. context.visualizeWidget.reset();
  184. context.visualizeWidget.init({
  185. Wuid: context.wu.Wuid
  186. });
  187. }
  188. });
  189. this.wu.monitor();
  190. },
  191. updateInput: function (name, oldValue, newValue) {
  192. var input = query("input[id=" + this.id + name + "]", this.summaryForm)[0];
  193. if (input) {
  194. var dijitInput = registry.byId(this.id + name);
  195. if (dijitInput) {
  196. dijitInput.set("value", newValue);
  197. } else {
  198. input.value = newValue;
  199. }
  200. } else {
  201. var a = query("a[id=" + this.id + name + "]", this.summaryForm)[0];
  202. if (a) {
  203. a.innerHTML = newValue;
  204. if (newValue === "...") {
  205. a.style.visibility = "hidden"
  206. } else if (this.wu && this.wu.Wuid) {
  207. a.style.visibility = "visible"
  208. a.href = "/esp/files/stub.htm?Widget=WUDetailsWidget&Wuid=" + this.wu.Wuid;
  209. }
  210. }
  211. }
  212. if (name === "hasCompleted") {
  213. this.checkIfComplete();
  214. }
  215. },
  216. checkIfComplete: function() {
  217. var context = this;
  218. if (this.wu.isComplete()) {
  219. this.wu.getInfo({
  220. onGetWUExceptions: function (exceptions) {
  221. context.displayExceptions(exceptions);
  222. },
  223. onGetResults: function (results) {
  224. context.displayResults(results);
  225. },
  226. onGetGraphs: function (graphs) {
  227. context.displayGraphs(graphs);
  228. },
  229. onAfterSend: function (workunit) {
  230. context.displayAll(workunit);
  231. }
  232. });
  233. }
  234. },
  235. displayExceptions: function (exceptions) {
  236. },
  237. displayResults: function (results) {
  238. },
  239. displayGraphs: function (graphs) {
  240. var fetchedCount = 0;
  241. for (var i = 0; i < graphs.length; ++i) {
  242. var context = this;
  243. this.wu.fetchGraphXgmml(i, function (xgmml) {
  244. ++fetchedCount;
  245. context.graphControl.loadXGMML(xgmml, fetchedCount > 1);
  246. if (fetchedCount === graphs.length) {
  247. context.graphControl.startLayout("dot");
  248. }
  249. });
  250. }
  251. },
  252. displayAll: function (workunit) {
  253. if (lang.exists("Exceptions.ECLException", this.wu)) {
  254. this.editorControl.setErrors(this.wu.Exceptions.ECLException);
  255. }
  256. this.resultsWidget.refresh({
  257. Wuid: this.wu.Wuid
  258. });
  259. },
  260. _onSubmit: function (evt) {
  261. this.resetPage();
  262. var text = this.editorControl.getText();
  263. var espQuery = ESPQuery.GetFromRequestXML(this.targetSelectWidget.get("value"), text);
  264. if (espQuery) {
  265. this.stackContainer.selectChild(this.resultsWidget);
  266. this.resultsWidget.set("disabled", false);
  267. this.resultsWidget.refresh({
  268. QuerySetId: espQuery.QuerySetId,
  269. Id: espQuery.Id,
  270. RequestXml: text
  271. });
  272. } else {
  273. var context = this;
  274. this.wu = ESPWorkunit.Create({
  275. onCreate: function () {
  276. context.wu.update({
  277. QueryText: text
  278. });
  279. context.watchWU();
  280. },
  281. onUpdate: function () {
  282. context.wu.submit(context.targetSelectWidget.getValue());
  283. },
  284. onSubmit: function () {
  285. }
  286. });
  287. }
  288. }
  289. });
  290. });