assign_master_components_view.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. var App = require('app');
  19. App.AssignMasterComponentsView = Em.View.extend({
  20. templateName: require('templates/common/assign_master_components'),
  21. /**
  22. * Title to be shown on the page
  23. * @type {String}
  24. */
  25. title: '',
  26. /**
  27. * Alert message to be shown on the page
  28. * @type {String}
  29. */
  30. alertMessage: '',
  31. /**
  32. * If install more than 25 hosts, should use App.InputHostView for hosts selection
  33. * Otherwise - App.SelectHostView
  34. * @type {bool}
  35. */
  36. shouldUseInputs: function() {
  37. return this.get('controller.hosts.length') > 25;
  38. }.property('controller.hosts.length'),
  39. didInsertElement: function () {
  40. this.get('controller').loadStep();
  41. }
  42. });
  43. App.InputHostView = Em.TextField.extend(App.SelectHost, {
  44. attributeBindings: ['disabled'],
  45. /**
  46. * Saved typeahead component
  47. * @type {$}
  48. */
  49. typeahead: null,
  50. /**
  51. * When <code>value</code> (host_info) is changed this method is triggered
  52. * If new hostname is valid, this host is assigned to master component
  53. * @method changeHandler
  54. */
  55. changeHandler: function() {
  56. if (!this.shouldChangeHandlerBeCalled()) return;
  57. var host = this.get('controller.hosts').findProperty('host_name', this.get('value'));
  58. if (Em.isNone(host)) {
  59. this.get('controller').updateIsHostNameValidFlag(this.get("component.component_name"), this.get("component.serviceComponentId"), false);
  60. return;
  61. }
  62. this.get('controller').assignHostToMaster(this.get("component.component_name"), host.get('host_name'), this.get("component.serviceComponentId"));
  63. this.tryTriggerRebalanceForMultipleComponents();
  64. }.observes('controller.hostNameCheckTrigger'),
  65. didInsertElement: function () {
  66. this.initContent();
  67. var value = this.get('content').findProperty('host_name', this.get('component.selectedHost')).get('host_name');
  68. this.set("value", value);
  69. var content = this.get('content').mapProperty('host_info'),
  70. self = this,
  71. updater = function (item) {
  72. return self.get('content').findProperty('host_info', item).get('host_name');
  73. },
  74. typeahead = this.$().typeahead({items: 10, source: content, updater: updater, minLength: 0});
  75. typeahead.on('blur', function() {
  76. self.change();
  77. }).on('keyup', function(e) {
  78. self.set('value', $(e.currentTarget).val());
  79. self.change();
  80. });
  81. this.set('typeahead', typeahead);
  82. },
  83. /**
  84. * Extract hosts from controller,
  85. * filter out available to selection and
  86. * push them into Em.Select content
  87. * @method initContent
  88. */
  89. initContent: function () {
  90. this._super();
  91. this.updateTypeaheadData(this.get('content').mapProperty('host_info'));
  92. },
  93. /**
  94. * Update <code>source</code> property of <code>typeahead</code> with a new list of hosts
  95. * @param {string[]} hosts
  96. * @method updateTypeaheadData
  97. */
  98. updateTypeaheadData: function(hosts) {
  99. if (this.get('typeahead')) {
  100. this.get('typeahead').data('typeahead').source = hosts;
  101. }
  102. }
  103. });
  104. App.SelectHostView = Em.Select.extend(App.SelectHost, {
  105. attributeBindings: ['disabled'],
  106. didInsertElement: function () {
  107. this.initContent();
  108. this.set("value", this.get("component.selectedHost"));
  109. App.popover($("[rel=popover]"), {'placement': 'right', 'trigger': 'hover'});
  110. },
  111. /**
  112. * Handler for selected value change
  113. * @method change
  114. */
  115. changeHandler: function () {
  116. if (!this.shouldChangeHandlerBeCalled()) return;
  117. this.get('controller').assignHostToMaster(this.get("component.component_name"), this.get("value"), this.get("component.serviceComponentId"));
  118. this.tryTriggerRebalanceForMultipleComponents();
  119. }.observes('controller.hostNameCheckTrigger'),
  120. /**
  121. * On change DOM event handler
  122. * @method change
  123. */
  124. change: function () {
  125. this._super();
  126. this.initContent();
  127. }
  128. });
  129. App.AddControlView = Em.View.extend({
  130. /**
  131. * Current component name
  132. * @type {string}
  133. */
  134. componentName: null,
  135. tagName: "span",
  136. classNames: ["badge", "badge-important"],
  137. template: Em.Handlebars.compile('+'),
  138. /**
  139. * Onclick handler
  140. * Add selected component
  141. * @method click
  142. */
  143. click: function () {
  144. this.get('controller').addComponent(this.get('componentName'));
  145. }
  146. });
  147. App.RemoveControlView = Em.View.extend({
  148. /**
  149. * Index for multiple component
  150. * @type {number}
  151. */
  152. serviceComponentId: null,
  153. /**
  154. * Current component name
  155. * @type {string}
  156. */
  157. componentName: null,
  158. tagName: "span",
  159. classNames: ["badge", "badge-important"],
  160. template: Em.Handlebars.compile('-'),
  161. /**
  162. * Onclick handler
  163. * Remove current component
  164. * @method click
  165. */
  166. click: function () {
  167. this.get('controller').removeComponent(this.get('componentName'), this.get("serviceComponentId"));
  168. }
  169. });