TargetSelectWidget.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*##############################################################################
  2. # Copyright (C) 2011 HPCC Systems.
  3. #
  4. # All rights reserved. This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as
  6. # published by the Free Software Foundation, either version 3 of the
  7. # License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU Affero General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Affero General Public License
  15. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ############################################################################## */
  17. require([
  18. "dojo/_base/declare",
  19. "dojo/_base/xhr",
  20. "dojo/dom",
  21. "dijit/layout/_LayoutWidget",
  22. "dijit/_TemplatedMixin",
  23. "dijit/_WidgetsInTemplateMixin",
  24. "dijit/form/Select",
  25. "dijit/registry",
  26. "hpcc/ESPBase",
  27. "dojo/text!./templates/TargetSelectWidget.html"
  28. ], function (declare, xhr, dom,
  29. _LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin, Select, registry,
  30. ESPBase, template) {
  31. return declare("TargetSelectWidget", [_LayoutWidget, _TemplatedMixin, _WidgetsInTemplateMixin], {
  32. templateString: template,
  33. baseClass: "TargetSelectWidget",
  34. targetSelectControl: null,
  35. _value: "",
  36. postCreate: function (args) {
  37. this.inherited(arguments);
  38. this._initControls();
  39. },
  40. resize: function (args) {
  41. this.inherited(arguments);
  42. },
  43. layout: function (args) {
  44. this.inherited(arguments);
  45. },
  46. // Implementation ---
  47. _initControls: function () {
  48. var context = this;
  49. this.targetSelectControl = registry.byId(this.id + "TargetSelect");
  50. this.targetSelectControl.onChange = function () {
  51. context.onChange(this.get("value"));
  52. };
  53. this.loadTargets();
  54. },
  55. onChange: function (target) {
  56. this._value = target;
  57. },
  58. setValue: function (target) {
  59. if (target && this._value != target) {
  60. this._value = target;
  61. this.targetSelectControl.set("value", target);
  62. }
  63. },
  64. getValue: function () {
  65. return this._value;
  66. },
  67. loadTargets: function () {
  68. var base = new ESPBase({
  69. });
  70. var request = {
  71. rawxml_: true
  72. };
  73. var context = this;
  74. xhr.post({
  75. url: base.getBaseURL("WsTopology") + "/TpTargetClusterQuery",
  76. handleAs: "xml",
  77. content: request,
  78. load: function (xmlDom) {
  79. var targetData = base.getValues(xmlDom, "TpTargetCluster");
  80. context.targetSelectControl.options = [];
  81. var has_hthor = false;
  82. for (var i = 0; i < targetData.length; ++i) {
  83. context.targetSelectControl.options.push({
  84. label: targetData[i].Name,
  85. value: targetData[i].Name
  86. });
  87. if (targetData[i].Name == "hthor") {
  88. has_hthor = true;
  89. }
  90. }
  91. if (context._value == "") {
  92. if (has_hthor) {
  93. context.setValue("hthor");
  94. } else {
  95. context._value = context.targetSelectControl.options[0].value;
  96. }
  97. } else {
  98. context.targetSelectControl.set("value", context._value);
  99. }
  100. },
  101. error: function () {
  102. }
  103. });
  104. }
  105. });
  106. });