assign_master_components_view.js 6.0 KB

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