assign_master_components_view.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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: Em.computed.gt('controller.hosts.length', 25),
  37. didInsertElement: function () {
  38. this.get('controller').loadStep();
  39. }
  40. });
  41. App.InputHostView = Em.TextField.extend(App.SelectHost, {
  42. attributeBindings: ['disabled'],
  43. /**
  44. * Saved typeahead component
  45. * @type {$}
  46. */
  47. typeahead: null,
  48. /**
  49. * When <code>value</code> (host_info) is changed this method is triggered
  50. * If new hostname is valid, this host is assigned to master component
  51. * @method changeHandler
  52. */
  53. changeHandler: function() {
  54. if (!this.shouldChangeHandlerBeCalled()) return;
  55. var host = this.get('controller.hosts').findProperty('host_name', this.get('value'));
  56. if (Em.isNone(host)) {
  57. this.get('controller').updateIsHostNameValidFlag(this.get("component.component_name"), this.get("component.serviceComponentId"), false);
  58. return;
  59. }
  60. this.get('controller').assignHostToMaster(this.get("component.component_name"), host.get('host_name'), this.get("component.serviceComponentId"));
  61. this.tryTriggerRebalanceForMultipleComponents();
  62. }.observes('controller.hostNameCheckTrigger'),
  63. didInsertElement: function () {
  64. this.initContent();
  65. var value = this.get('content').findProperty('host_name', this.get('component.selectedHost')).get('host_name');
  66. this.set("value", value);
  67. var content = this.get('content').mapProperty('host_info'),
  68. self = this,
  69. updater = function (item) {
  70. return self.get('content').findProperty('host_info', item).get('host_name');
  71. },
  72. typeahead = this.$().typeahead({items: 10, source: content, updater: updater, minLength: 0});
  73. typeahead.on('blur', function() {
  74. self.change();
  75. }).on('keyup', function(e) {
  76. self.set('value', $(e.currentTarget).val());
  77. self.change();
  78. });
  79. this.set('typeahead', typeahead);
  80. },
  81. /**
  82. * Extract hosts from controller,
  83. * filter out available to selection and
  84. * push them into Em.Select content
  85. * @method initContent
  86. */
  87. initContent: function () {
  88. this._super();
  89. this.updateTypeaheadData(this.get('content').mapProperty('host_info'));
  90. },
  91. /**
  92. * Update <code>source</code> property of <code>typeahead</code> with a new list of hosts
  93. * @param {string[]} hosts
  94. * @method updateTypeaheadData
  95. */
  96. updateTypeaheadData: function(hosts) {
  97. if (this.get('typeahead')) {
  98. this.get('typeahead').data('typeahead').source = hosts;
  99. }
  100. }
  101. });
  102. App.SelectHostView = Em.Select.extend(App.SelectHost, {
  103. attributeBindings: ['disabled'],
  104. didInsertElement: function () {
  105. this.initContent();
  106. this.set("value", this.get("component.selectedHost"));
  107. App.popover($("[rel=popover]"), {'placement': 'right', 'trigger': 'hover'});
  108. },
  109. /**
  110. * Handler for selected value change
  111. * @method change
  112. */
  113. changeHandler: function () {
  114. if (!this.shouldChangeHandlerBeCalled()) return;
  115. this.get('controller').assignHostToMaster(this.get("component.component_name"), this.get("value"), this.get("component.serviceComponentId"));
  116. this.tryTriggerRebalanceForMultipleComponents();
  117. }.observes('controller.hostNameCheckTrigger'),
  118. /**
  119. * On change DOM event handler
  120. * @method change
  121. */
  122. change: function () {
  123. this._super();
  124. this.initContent();
  125. }
  126. });
  127. App.AddControlView = Em.View.extend({
  128. /**
  129. * DOM node class attribute
  130. * @type {string}
  131. */
  132. uniqueId: Em.computed.format('{0}-add', 'componentName'),
  133. /**
  134. * Current component name
  135. * @type {string}
  136. */
  137. componentName: null,
  138. tagName: "span",
  139. classNames: ["badge", "badge-important"],
  140. classNameBindings: ['uniqueId'],
  141. template: Em.Handlebars.compile('+'),
  142. /**
  143. * Onclick handler
  144. * Add selected component
  145. * @method click
  146. */
  147. click: function () {
  148. this.get('controller').addComponent(this.get('componentName'));
  149. }
  150. });
  151. App.RemoveControlView = Em.View.extend({
  152. /**
  153. * DOM node class attribute
  154. * @type {string}
  155. */
  156. uniqueId: Em.computed.format('{0}-{1}-remove', 'componentName', 'serviceComponentId'),
  157. classNameBindings: ['uniqueId'],
  158. /**
  159. * Index for multiple component
  160. * @type {number}
  161. */
  162. serviceComponentId: null,
  163. /**
  164. * Current component name
  165. * @type {string}
  166. */
  167. componentName: null,
  168. tagName: "span",
  169. classNames: ["badge", "badge-important"],
  170. template: Em.Handlebars.compile('-'),
  171. /**
  172. * Onclick handler
  173. * Remove current component
  174. * @method click
  175. */
  176. click: function () {
  177. this.get('controller').removeComponent(this.get('componentName'), this.get("serviceComponentId"));
  178. }
  179. });