ECLPlaygroundWidget.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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.onSelectionChanged = function (items) {
  135. context.editorControl.clearHighlightLines();
  136. for (var i = 0; i < items.length; ++i) {
  137. var props = context.graphControl.getProperties(items[i]);
  138. if (props.definition) {
  139. var startPos = props.definition.indexOf("(");
  140. var endPos = props.definition.lastIndexOf(")");
  141. var pos = props.definition.slice(startPos + 1, endPos).split(",");
  142. var lineNo = parseInt(pos[0], 10);
  143. context.editorControl.highlightLine(lineNo);
  144. context.editorControl.setCursor(lineNo, 0);
  145. }
  146. }
  147. };
  148. },
  149. getGraph: function () {
  150. return registry.byId(this.id + "GraphControl");
  151. },
  152. resetPage: function () {
  153. this.editorControl.clearErrors();
  154. this.editorControl.clearHighlightLines();
  155. this.graphControl.clear();
  156. this.resultsWidget.clear();
  157. this.updateInput("State", null, "...");
  158. this.stackContainer.selectChild(this.resultsWidget);
  159. this.errWarnWidget.set("disabled", true);
  160. this.resultsWidget.set("disabled", true);
  161. this.visualizeWidget.set("disabled", true);
  162. },
  163. watchWU: function () {
  164. if (this.watching) {
  165. this.watching.unwatch();
  166. }
  167. var context = this;
  168. this.watching = this.wu.watch(function (name, oldValue, newValue) {
  169. context.updateInput(name, oldValue, newValue);
  170. if (name === "Exceptions" && newValue) {
  171. context.stackContainer.selectChild(context.errWarnWidget);
  172. context.errWarnWidget.set("disabled", false);
  173. context.errWarnWidget.reset();
  174. context.errWarnWidget.init({
  175. Wuid: context.wu.Wuid
  176. });
  177. } else if (name === "Results" && newValue) {
  178. context.stackContainer.selectChild(context.resultsWidget);
  179. context.resultsWidget.set("disabled", false);
  180. if (context.visualizeWidget.supportsSvg()) {
  181. context.visualizeWidget.set("disabled", false);
  182. context.visualizeWidget.reset();
  183. context.visualizeWidget.init({
  184. Wuid: context.wu.Wuid
  185. });
  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. for (var i = 0; i < graphs.length; ++i) {
  241. var context = this;
  242. this.wu.fetchGraphXgmml(i, function (xgmml) {
  243. context.graphControl.loadXGMML(xgmml, i > 0);
  244. });
  245. }
  246. },
  247. displayAll: function (workunit) {
  248. if (lang.exists("Exceptions.ECLException", this.wu)) {
  249. this.editorControl.setErrors(this.wu.Exceptions.ECLException);
  250. }
  251. this.resultsWidget.refresh({
  252. Wuid: this.wu.Wuid
  253. });
  254. },
  255. _onSubmit: function (evt) {
  256. this.resetPage();
  257. var text = this.editorControl.getText();
  258. var espQuery = ESPQuery.GetFromRequestXML(this.targetSelectWidget.get("value"), text);
  259. if (espQuery) {
  260. this.stackContainer.selectChild(this.resultsWidget);
  261. this.resultsWidget.set("disabled", false);
  262. this.resultsWidget.refresh({
  263. QuerySetId: espQuery.QuerySetId,
  264. Id: espQuery.Id,
  265. RequestXml: text
  266. });
  267. } else {
  268. var context = this;
  269. this.wu = ESPWorkunit.Create({
  270. onCreate: function () {
  271. context.wu.update({
  272. QueryText: text
  273. });
  274. context.watchWU();
  275. },
  276. onUpdate: function () {
  277. context.wu.submit(context.targetSelectWidget.getValue());
  278. },
  279. onSubmit: function () {
  280. }
  281. });
  282. }
  283. }
  284. });
  285. });