TargetSelectWidget.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. "dojo/text!./templates/TargetSelectWidget.html"
  28. ], function (declare, lang, arrayUtil, dom,
  29. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, Select, registry,
  30. WsTopology,
  31. template) {
  32. return declare("TargetSelectWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  33. templateString: template,
  34. baseClass: "TargetSelectWidget",
  35. targetSelectControl: null,
  36. _value: "",
  37. postCreate: function (args) {
  38. this.inherited(arguments);
  39. this._initControls();
  40. },
  41. resize: function (args) {
  42. this.inherited(arguments);
  43. },
  44. layout: function (args) {
  45. this.inherited(arguments);
  46. },
  47. // Implementation ---
  48. _initControls: function () {
  49. var context = this;
  50. this.targetSelectControl = registry.byId(this.id + "TargetSelect");
  51. this.targetSelectControl.onChange = function () {
  52. context.onChange(this.get("value"));
  53. };
  54. },
  55. init: function (params) {
  56. if (this.initalized)
  57. return;
  58. this.initalized = true;
  59. if (params.Target) {
  60. this._value = params.Target;
  61. }
  62. if (params.includeBlank) {
  63. this.includeBlank = params.includeBlank;
  64. }
  65. if (params.Groups === true) {
  66. this.loadGroups();
  67. } else if (params.DropZones === true) {
  68. this.loadDropZones();
  69. } else {
  70. this.loadTargets();
  71. }
  72. if (params.callback) {
  73. this.callback = params.callback;
  74. }
  75. if (params.callback) {
  76. this.callback = params.callback;
  77. }
  78. if (params.includeBlank) {
  79. }
  80. },
  81. onChange: function (target) {
  82. this._value = target;
  83. this._valueItem = null;
  84. var context = this;
  85. var idx = arrayUtil.forEach(this.targetSelectControl.options, function(item, idx) {
  86. if (item.value === context._value) {
  87. context._valueItem = item;
  88. }
  89. });
  90. if (this.callback) {
  91. this.callback(this._value, this._valueItem);
  92. }
  93. },
  94. setValue: function (target) {
  95. if (target !== null && this._value != target) {
  96. this._value = target;
  97. this.targetSelectControl.set("value", target);
  98. }
  99. },
  100. getValue: function () {
  101. return this._value;
  102. },
  103. loadDropZones: function () {
  104. var context = this;
  105. WsTopology.TpServiceQuery({
  106. load: function (response) {
  107. if (lang.exists("TpServiceQueryResponse.ServiceList.TpDropZones.TpDropZone", response)) {
  108. var targetData = response.TpServiceQueryResponse.ServiceList.TpDropZones.TpDropZone;
  109. context.targetSelectControl.options = [];
  110. if (context.includeBlank) {
  111. context.targetSelectControl.options.push({
  112. label: "",
  113. value: ""
  114. });
  115. }
  116. for (var i = 0; i < targetData.length; ++i) {
  117. context.targetSelectControl.options.push({
  118. label: targetData[i].Name,
  119. value: targetData[i].Name,
  120. machine: targetData[i].TpMachines.TpMachine[0]
  121. });
  122. }
  123. if (context._value == "") {
  124. context._value = context.targetSelectControl.options[0].value;
  125. }
  126. context.targetSelectControl.set("value", context._value);
  127. }
  128. }
  129. });
  130. },
  131. loadGroups: function () {
  132. var context = this;
  133. WsTopology.TpGroupQuery({
  134. load: function (response) {
  135. if (lang.exists("TpGroupQueryResponse.TpGroups.TpGroup", response)) {
  136. var targetData = response.TpGroupQueryResponse.TpGroups.TpGroup;
  137. context.targetSelectControl.options = [];
  138. if (context.includeBlank) {
  139. context.targetSelectControl.options.push({
  140. label: "",
  141. value: ""
  142. });
  143. }
  144. for (var i = 0; i < targetData.length; ++i) {
  145. context.targetSelectControl.options.push({
  146. label: targetData[i].Name,
  147. value: targetData[i].Name
  148. });
  149. }
  150. if (context._value == "") {
  151. context._value = context.targetSelectControl.options[0].value;
  152. }
  153. context.targetSelectControl.set("value", context._value);
  154. }
  155. }
  156. });
  157. },
  158. loadTargets: function () {
  159. var context = this;
  160. WsTopology.TpTargetClusterQuery({
  161. load: function (response) {
  162. if (lang.exists("TpTargetClusterQueryResponse.TpTargetClusters.TpTargetCluster", response)) {
  163. var targetData = response.TpTargetClusterQueryResponse.TpTargetClusters.TpTargetCluster;
  164. context.targetSelectControl.options = [];
  165. if (context.includeBlank) {
  166. context.targetSelectControl.options.push({
  167. label: "",
  168. value: ""
  169. });
  170. }
  171. var has_hthor = false;
  172. for (var i = 0; i < targetData.length; ++i) {
  173. context.targetSelectControl.options.push({
  174. label: targetData[i].Name,
  175. value: targetData[i].Name
  176. });
  177. if (targetData[i].Name == "hthor") {
  178. has_hthor = true;
  179. }
  180. }
  181. if (!context.includeBlank && context._value == "") {
  182. if (has_hthor) {
  183. context.setValue("hthor");
  184. } else {
  185. context._value = context.targetSelectControl.options[0].value;
  186. }
  187. } else {
  188. context.targetSelectControl.set("value", context._value);
  189. }
  190. }
  191. }
  192. });
  193. }
  194. });
  195. });