TargetSelectWidget.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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/xhr",
  19. "dojo/dom",
  20. "dijit/layout/_LayoutWidget",
  21. "dijit/_TemplatedMixin",
  22. "dijit/_WidgetsInTemplateMixin",
  23. "dijit/form/Select",
  24. "dijit/registry",
  25. "hpcc/ESPBase",
  26. "dojo/text!./templates/TargetSelectWidget.html"
  27. ], function (declare, xhr, dom,
  28. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, Select, registry,
  29. ESPBase, template) {
  30. return declare("TargetSelectWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  31. templateString: template,
  32. baseClass: "TargetSelectWidget",
  33. targetSelectControl: null,
  34. _value: "",
  35. postCreate: function (args) {
  36. this.inherited(arguments);
  37. this._initControls();
  38. },
  39. resize: function (args) {
  40. this.inherited(arguments);
  41. },
  42. layout: function (args) {
  43. this.inherited(arguments);
  44. },
  45. // Implementation ---
  46. _initControls: function () {
  47. var context = this;
  48. this.targetSelectControl = registry.byId(this.id + "TargetSelect");
  49. this.targetSelectControl.onChange = function () {
  50. context.onChange(this.get("value"));
  51. };
  52. this.loadTargets();
  53. },
  54. onChange: function (target) {
  55. this._value = target;
  56. },
  57. setValue: function (target) {
  58. if (target && this._value != target) {
  59. this._value = target;
  60. this.targetSelectControl.set("value", target);
  61. }
  62. },
  63. getValue: function () {
  64. return this._value;
  65. },
  66. loadTargets: function () {
  67. var base = new ESPBase({
  68. });
  69. var request = {
  70. rawxml_: true
  71. };
  72. var context = this;
  73. xhr.post({
  74. url: base.getBaseURL("WsTopology") + "/TpTargetClusterQuery",
  75. handleAs: "xml",
  76. content: request,
  77. load: function (xmlDom) {
  78. var targetData = base.getValues(xmlDom, "TpTargetCluster");
  79. context.targetSelectControl.options = [];
  80. var has_hthor = false;
  81. for (var i = 0; i < targetData.length; ++i) {
  82. context.targetSelectControl.options.push({
  83. label: targetData[i].Name,
  84. value: targetData[i].Name
  85. });
  86. if (targetData[i].Name == "hthor") {
  87. has_hthor = true;
  88. }
  89. }
  90. if (context._value == "") {
  91. if (has_hthor) {
  92. context.setValue("hthor");
  93. } else {
  94. context._value = context.targetSelectControl.options[0].value;
  95. }
  96. } else {
  97. context.targetSelectControl.set("value", context._value);
  98. }
  99. },
  100. error: function () {
  101. }
  102. });
  103. }
  104. });
  105. });