ECLPlaygroundWidget.js 12 KB

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