TargetSelectWidget.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. },
  53. init: function (params) {
  54. if (this.initalized)
  55. return;
  56. this.initalized = true;
  57. if (params.Target) {
  58. this._value = params.Target;
  59. }
  60. this.loadTargets();
  61. },
  62. onChange: function (target) {
  63. this._value = target;
  64. },
  65. setValue: function (target) {
  66. if (target && this._value != target) {
  67. this._value = target;
  68. this.targetSelectControl.set("value", target);
  69. }
  70. },
  71. getValue: function () {
  72. return this._value;
  73. },
  74. loadTargets: function () {
  75. var base = new ESPBase({
  76. });
  77. var request = {
  78. rawxml_: true
  79. };
  80. var context = this;
  81. xhr.post({
  82. url: base.getBaseURL("WsTopology") + "/TpTargetClusterQuery",
  83. handleAs: "xml",
  84. content: request,
  85. load: function (xmlDom) {
  86. var targetData = base.getValues(xmlDom, "TpTargetCluster");
  87. context.targetSelectControl.options = [];
  88. var has_hthor = false;
  89. for (var i = 0; i < targetData.length; ++i) {
  90. context.targetSelectControl.options.push({
  91. label: targetData[i].Name,
  92. value: targetData[i].Name
  93. });
  94. if (targetData[i].Name == "hthor") {
  95. has_hthor = true;
  96. }
  97. }
  98. if (context._value == "") {
  99. if (has_hthor) {
  100. context.setValue("hthor");
  101. } else {
  102. context._value = context.targetSelectControl.options[0].value;
  103. }
  104. } else {
  105. context.targetSelectControl.set("value", context._value);
  106. }
  107. },
  108. error: function () {
  109. }
  110. });
  111. }
  112. });
  113. });