TargetSelectWidget.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. require([
  17. "dojo/_base/declare",
  18. "dojo/_base/lang",
  19. "dojo/_base/array",
  20. "dojo/dom",
  21. "dijit/layout/_LayoutWidget",
  22. "dijit/_TemplatedMixin",
  23. "dijit/_WidgetsInTemplateMixin",
  24. "dijit/form/Select",
  25. "dijit/registry",
  26. "hpcc/WsTopology",
  27. "hpcc/WsWorkunits",
  28. "hpcc/FileSpray",
  29. "dojo/text!./templates/TargetSelectWidget.html"
  30. ], function (declare, lang, arrayUtil, dom,
  31. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, Select, registry,
  32. WsTopology, WsWorkunits, FileSpray,
  33. template) {
  34. return declare("TargetSelectWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  35. templateString: template,
  36. baseClass: "TargetSelectWidget",
  37. targetSelectControl: null,
  38. name: "",
  39. _value: "",
  40. postCreate: function (args) {
  41. this.inherited(arguments);
  42. this._initControls();
  43. },
  44. resize: function (args) {
  45. this.inherited(arguments);
  46. },
  47. layout: function (args) {
  48. this.inherited(arguments);
  49. },
  50. // Implementation ---
  51. _initControls: function () {
  52. var context = this;
  53. this.targetSelectControl = registry.byId(this.id + "TargetSelect");
  54. this.targetSelectControl.onChange = function () {
  55. context.onChange(this.get("value"));
  56. };
  57. },
  58. init: function (params) {
  59. if (this.initalized)
  60. return;
  61. this.initalized = true;
  62. this.targetSelectControl.options = [];
  63. if (params.Target) {
  64. this._value = params.Target;
  65. }
  66. if (params.includeBlank) {
  67. this.includeBlank = params.includeBlank;
  68. this.targetSelectControl.options.push({
  69. label: " ",
  70. value: ""
  71. });
  72. }
  73. if (params.Groups === true) {
  74. this.loadGroups();
  75. } else if (params.DropZones === true) {
  76. this.loadDropZones();
  77. } else if (params.WUState === true) {
  78. this.loadWUState();
  79. } else if (params.DFUState === true) {
  80. this.loadDFUState();
  81. } else {
  82. this.loadTargets();
  83. }
  84. if (params.callback) {
  85. this.callback = params.callback;
  86. }
  87. if (params.includeBlank) {
  88. }
  89. },
  90. onChange: function (target) {
  91. this._value = target;
  92. this._valueItem = null;
  93. var context = this;
  94. var idx = arrayUtil.forEach(this.targetSelectControl.options, function(item, idx) {
  95. if (item.value === context._value) {
  96. context._valueItem = item;
  97. }
  98. });
  99. if (this.callback) {
  100. this.callback(this._value, this._valueItem);
  101. }
  102. },
  103. setValue: function (target) {
  104. if (target !== null && this._value != target) {
  105. this._value = target;
  106. this.targetSelectControl.set("value", target);
  107. }
  108. },
  109. _setValueAttr: function (target) {
  110. if (target === null) {
  111. target = "";
  112. }
  113. if (target !== null && this._value != target) {
  114. this._value = target;
  115. this.targetSelectControl.set("value", target);
  116. }
  117. },
  118. getValue: function () {
  119. return this._value;
  120. },
  121. _getValueAttr: function () {
  122. return this._value;
  123. },
  124. resetDefaultSelection: function () {
  125. if (this._value == "") {
  126. this._value = this.targetSelectControl.options[0].value;
  127. }
  128. this.targetSelectControl.set("value", this._value);
  129. },
  130. loadDropZones: function () {
  131. var context = this;
  132. WsTopology.TpServiceQuery({
  133. load: function (response) {
  134. if (lang.exists("TpServiceQueryResponse.ServiceList.TpDropZones.TpDropZone", response)) {
  135. var targetData = response.TpServiceQueryResponse.ServiceList.TpDropZones.TpDropZone;
  136. for (var i = 0; i < targetData.length; ++i) {
  137. context.targetSelectControl.options.push({
  138. label: targetData[i].Name,
  139. value: targetData[i].Name,
  140. machine: targetData[i].TpMachines.TpMachine[0]
  141. });
  142. }
  143. context.resetDefaultSelection();
  144. }
  145. }
  146. });
  147. },
  148. loadGroups: function () {
  149. var context = this;
  150. WsTopology.TpGroupQuery({
  151. load: function (response) {
  152. if (lang.exists("TpGroupQueryResponse.TpGroups.TpGroup", response)) {
  153. var targetData = response.TpGroupQueryResponse.TpGroups.TpGroup;
  154. for (var i = 0; i < targetData.length; ++i) {
  155. context.targetSelectControl.options.push({
  156. label: targetData[i].Name,
  157. value: targetData[i].Name
  158. });
  159. }
  160. context.resetDefaultSelection();
  161. }
  162. }
  163. });
  164. },
  165. loadWUState: function() {
  166. for (var key in WsWorkunits.States) {
  167. this.targetSelectControl.options.push({
  168. label: WsWorkunits.States[key],
  169. value: WsWorkunits.States[key]
  170. });
  171. }
  172. this.resetDefaultSelection();
  173. },
  174. loadDFUState: function () {
  175. for (var key in FileSpray.States) {
  176. this.targetSelectControl.options.push({
  177. label: FileSpray.States[key],
  178. value: FileSpray.States[key]
  179. });
  180. }
  181. this.resetDefaultSelection();
  182. },
  183. LogicalFileSearchType: function() {
  184. this.targetSelectControl.options.push({
  185. label: "Created",
  186. value: "Created"
  187. });
  188. this.targetSelectControl.options.push({
  189. label: "Used",
  190. value: "Referenced"
  191. });
  192. this.resetDefaultSelection();
  193. },
  194. loadTargets: function () {
  195. var context = this;
  196. WsTopology.TpLogicalClusterQuery({
  197. }).then(function (response) {
  198. if (lang.exists("TpLogicalClusterQueryResponse.TpLogicalClusters.TpLogicalCluster", response)) {
  199. var targetData = response.TpLogicalClusterQueryResponse.TpLogicalClusters.TpLogicalCluster;
  200. var has_hthor = false;
  201. for (var i = 0; i < targetData.length; ++i) {
  202. context.targetSelectControl.options.push({
  203. label: targetData[i].Name,
  204. value: targetData[i].Name
  205. });
  206. if (targetData[i].Name == "hthor") {
  207. has_hthor = true;
  208. }
  209. }
  210. if (!context.includeBlank && context._value == "") {
  211. if (has_hthor) {
  212. context._value = "hthor";
  213. } else {
  214. context._value = context.targetSelectControl.options[0].value;
  215. }
  216. }
  217. context.resetDefaultSelection();
  218. }
  219. });
  220. }
  221. });
  222. });