VizWidget.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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/_base/array",
  20. "dojo/_base/Deferred",
  21. "dojo/dom-construct",
  22. "dojo/dom-form",
  23. "dojo/io-query",
  24. "dojo/promise/all",
  25. "dijit/registry",
  26. "dijit/layout/ContentPane",
  27. "dijit/form/Select",
  28. "dojox/layout/TableContainer",
  29. "hpcc/_Widget",
  30. "hpcc/ESPWorkunit",
  31. "hpcc/WsWorkunits",
  32. "dojo/text!../templates/VizWidget.html",
  33. "dijit/layout/BorderContainer",
  34. "dijit/layout/ContentPane",
  35. "dijit/Toolbar",
  36. "dijit/ToolbarSeparator",
  37. "dijit/TooltipDialog",
  38. "dijit/form/Form",
  39. "dijit/form/Button",
  40. "dijit/form/DropDownButton"
  41. ], function (declare, lang, arrayUtil, Deferred, domConstruct, domForm, ioQuery, all,
  42. registry, ContentPane, Select,
  43. TableContainer,
  44. _Widget, ESPWorkunit, WsWorkunits,
  45. template) {
  46. return declare("VizWidget", [_Widget], {
  47. templateString: template,
  48. borderContainer: null,
  49. grid: null,
  50. foundMatchingViz: false,
  51. foundMatchingFields: false,
  52. loaded: false,
  53. buildRendering: function (args) {
  54. this.inherited(arguments);
  55. },
  56. postCreate: function (args) {
  57. this.inherited(arguments);
  58. this.borderContainer = registry.byId(this.id + "BorderContainer");
  59. this.vizSelect = registry.byId(this.id + "VizSelect");
  60. this.mappingDropDown = registry.byId(this.id + "Mappings");
  61. this.mappingItems = registry.byId(this.id + "MappingItems");
  62. },
  63. startup: function (args) {
  64. this.inherited(arguments);
  65. },
  66. resize: function (args) {
  67. this.inherited(arguments);
  68. this.borderContainer.resize();
  69. if (this.d3Viz) {
  70. this.d3Viz.resize();
  71. }
  72. },
  73. layout: function (args) {
  74. this.inherited(arguments);
  75. },
  76. destroy: function (args) {
  77. this.inherited(arguments);
  78. },
  79. _onRefresh: function (evt) {
  80. this.refreshData();
  81. },
  82. _onRefreshData: function (evt) {
  83. this.refreshData();
  84. },
  85. _onVizSelect: function (value) {
  86. this.vizOnChange(value);
  87. },
  88. _onMappingsApply: function (evt) {
  89. this.refreshData();
  90. this.mappingDropDown.closeDropDown();
  91. },
  92. // Implementation ---
  93. onErrorClick: function (line, col) {
  94. },
  95. reset: function () {
  96. this.initalized = false;
  97. this.params = null;
  98. this.wu = null;
  99. this.vizType = null;
  100. this.vizSelect.set("options", []);
  101. },
  102. init: function (params) {
  103. if (this.inherited(arguments))
  104. return;
  105. this.loading = true;
  106. var context = this;
  107. WsWorkunits.GetVisualisations().then(function (vizResponse) {
  108. context.vizSelect.set("options", vizResponse);
  109. if (params.viz) {
  110. context.vizSelect.set("value", params.viz);
  111. } else {
  112. context.vizSelect.set("value", vizResponse[0].value);
  113. }
  114. if (params.mapping) {
  115. context.defaultSelection = ioQuery.queryToObject(params.mapping);
  116. }
  117. if (params.Wuid) {
  118. context.wu = ESPWorkunit.Get(params.Wuid);
  119. context.wu.fetchResults(function (response) {
  120. var newSel = null;
  121. arrayUtil.forEach(response, function(item, idx) {
  122. arrayUtil.forEach(vizResponse, function(vizItem, idx) {
  123. if (vizItem.value.indexOf(item.Name) >= 0) {
  124. newSel = vizItem.value;
  125. return true;
  126. }
  127. });
  128. if (newSel) {
  129. return true;
  130. }
  131. });
  132. if (newSel) {
  133. context.foundMatchingViz = true;
  134. context.vizSelect.set("value", newSel);
  135. }
  136. context.doFetchAllStructures().then(function (response) {
  137. context.loading = false;
  138. context.vizOnChange(context.vizSelect.get("value"), params.mapping);
  139. });
  140. });
  141. }
  142. });
  143. },
  144. doFetchStructure: function (result) {
  145. var deferred = new Deferred();
  146. result.fetchStructure(function (response) {
  147. deferred.resolve(response);
  148. });
  149. return deferred.promise;
  150. },
  151. doFetchAllStructures: function () {
  152. var promiseArray = [];
  153. var context = this;
  154. this.resultStructures = {};
  155. arrayUtil.forEach(this.wu.results, function (item, idx) {
  156. promiseArray.push(context.doFetchStructure(item).then(function (response) {
  157. context.resultStructures[item.Sequence] = response;
  158. return response;
  159. }));
  160. });
  161. return all(promiseArray);
  162. },
  163. getResultOptions: function() {
  164. var retVal = [];
  165. arrayUtil.forEach(this.wu.results, function (item, idx) {
  166. retVal.push({
  167. label: item.Name,
  168. value: item.Sequence
  169. });
  170. });
  171. return retVal;
  172. },
  173. getFieldOptions: function (sequence) {
  174. var retVal = [];
  175. arrayUtil.forEach(this.resultStructures[sequence], function (item, idx) {
  176. if (item.field.indexOf("_") !== 0) {
  177. retVal.push({
  178. label: item.field,
  179. value: item.field
  180. });
  181. }
  182. });
  183. return retVal;
  184. },
  185. vizOnChange: function (value, autoShow) {
  186. if (this.loading)
  187. return;
  188. var context = this;
  189. return this.refreshVizType(value).then(function (vizWidget) {
  190. context.refreshMappings();
  191. if (autoShow || (context.foundMatchingViz && context.foundMatchingFields)) {
  192. setTimeout(function () {
  193. context.refreshData();
  194. }, 1);
  195. } else {
  196. var isVisible = document.getElementById(context.id).offsetHeight != 0;
  197. if (isVisible) {
  198. context.mappingDropDown.focus();
  199. context.mappingDropDown.loadAndOpenDropDown();
  200. }
  201. }
  202. });
  203. },
  204. datasetOnChange: function (select, sequence) {
  205. if (this.loading)
  206. return;
  207. if (sequence != null) {
  208. select.datasetMapping.sequence = sequence;
  209. select.datasetMapping.result = this.wu.results[sequence];
  210. select.datasetMapping.data = null;
  211. this.foundMatchingFields = false;
  212. var foundMatchingFieldCount = 0;
  213. var fieldMappings = select.datasetMapping.getFieldMappings();
  214. var context = this;
  215. arrayUtil.forEach(fieldMappings, function (fieldMapping, idx) {
  216. var options = context.getFieldOptions(sequence);
  217. fieldMapping.select.set("options", options);
  218. // Auto select field priority:
  219. // 1. "defaultSelection" (typically from URL mapping)
  220. // 2. Match by field name
  221. // 3. Match by field index
  222. // 4. Just use option[0]
  223. var value = options[idx < options.length ? idx : 0].value; // If no default value or matching name, revert to field order and failing that use options[0]
  224. if (lang.exists("defaultSelection." + fieldMapping.select.name, context)) {
  225. value = context.defaultSelection[fieldMapping.select.name];
  226. } else {
  227. arrayUtil.forEach(fieldMapping.select.options, function (optionItem, idx) {
  228. if (optionItem.label === fieldMapping.getID()) {
  229. ++foundMatchingFieldCount;
  230. value = optionItem.value;
  231. return true;
  232. }
  233. });
  234. }
  235. fieldMapping.select.set("value", value);
  236. });
  237. if (foundMatchingFieldCount === fieldMappings.length) {
  238. this.foundMatchingFields = true;
  239. }
  240. }
  241. },
  242. fieldOnChange: function (select, value) {
  243. if (this.loading)
  244. return;
  245. this.d3Viz.setFieldMapping(select.fieldMapping.getID(), value, select.datasetMapping.getID());
  246. },
  247. refreshMappings: function () {
  248. this.datasetMappings = this.d3Viz.cloneDatasetMappings();
  249. var placeHolder = registry.byId(this.id + "MappingItems");
  250. if (this.mappingWidget) {
  251. this.mappingWidget.destroyRecursive();
  252. }
  253. this.mappingWidget = new ContentPane({
  254. });
  255. var context = this;
  256. arrayUtil.forEach(this.datasetMappings, function (datasetMapping, idx) {
  257. var tableContainer = new TableContainer({
  258. cols: 1,
  259. customClass: "labelsAndValues",
  260. "labelWidth": "120"
  261. });
  262. datasetMapping.select = new Select({
  263. id: context.id + datasetMapping.getID(),
  264. label: datasetMapping.getDisplay(),
  265. name: datasetMapping.getID(),
  266. datasetMapping: datasetMapping,
  267. options: context.getResultOptions(),
  268. onChange: function (value) {
  269. context.datasetOnChange(this, value);
  270. }
  271. });
  272. tableContainer.addChild(datasetMapping.select);
  273. arrayUtil.forEach(datasetMapping.getFieldMappings(), function (fieldMapping, idx) {
  274. fieldMapping.select = new Select({
  275. id: context.id + datasetMapping.getID() + ":" + fieldMapping.getID(),
  276. datasetMapping: datasetMapping,
  277. fieldMapping: fieldMapping,
  278. label: "&nbsp;&nbsp;&nbsp;&nbsp;" + fieldMapping.getDisplay(),
  279. name: datasetMapping.getID() + ":" + fieldMapping.getID(),
  280. options: [],
  281. onChange: function (value) {
  282. context.fieldOnChange(this, value);
  283. }
  284. });
  285. tableContainer.addChild(fieldMapping.select);
  286. });
  287. context.mappingWidget.addChild(tableContainer);
  288. // "sequence" auto select priority:
  289. // 1. "defaultSelection" (typically from URL mapping)
  290. // 2. Match by field name
  291. // 3. Just use sequence = 0
  292. var sequence = 0;
  293. if (lang.exists("defaultSelection." + datasetMapping.select.name, context)) {
  294. sequence = context.defaultSelection[datasetMapping.select.name];
  295. } else {
  296. arrayUtil.forEach(datasetMapping.select.options, function (optionItem, idx) {
  297. if (optionItem.label === datasetMapping.getID()) {
  298. sequence = optionItem.value;
  299. return true;
  300. }
  301. });
  302. }
  303. if (datasetMapping.select.get("value") != sequence) {
  304. datasetMapping.select.set("value", sequence);
  305. } else {
  306. context.datasetOnChange(datasetMapping.select, sequence);
  307. }
  308. });
  309. placeHolder.addChild(this.mappingWidget);
  310. },
  311. vizType: "",
  312. refreshVizType: function (value) {
  313. var deferred = new Deferred();
  314. if (this.vizType !== value) {
  315. this.vizType = value;
  316. var context = this;
  317. require(["viz/" + this.vizType], function (D3Viz, d3) {
  318. context.d3Viz = new D3Viz();
  319. domConstruct.empty(context.id + "VizCP");
  320. context.d3Viz.renderTo({
  321. domNodeID: context.id + "VizCP"
  322. });
  323. deferred.resolve(context.vizType);
  324. });
  325. } else {
  326. deferred.resolve(this.vizType);
  327. }
  328. return deferred.promise;
  329. },
  330. getFilter: function() {
  331. var filter = domForm.toObject(this.id + "FilterDialog");
  332. var retVal = {};
  333. for (var key in filter) {
  334. if (filter[key]) {
  335. retVal[key] = filter[key];
  336. }
  337. }
  338. return retVal;
  339. },
  340. refreshData: function () {
  341. if (this.d3Viz) {
  342. var context = this;
  343. var allArray = [];
  344. arrayUtil.forEach(this.datasetMappings, function (datasetMapping, idx) {
  345. allArray.push(datasetMapping.result.fetchContent().then(function (response) {
  346. context.d3Viz.setData(response, datasetMapping.getID());
  347. return response;
  348. }));
  349. });
  350. all(allArray).then(function (response) {
  351. context.params.viz = context.vizSelect.get("value");
  352. context.params.mapping = domForm.toQuery(context.id + "MappingForm");
  353. context.d3Viz.display();
  354. });
  355. }
  356. }
  357. });
  358. });