VizWidget.js 15 KB

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