assign_master_components_view.js 6.1 KB

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