selectHost.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. /**
  20. * Mixin for host select view on step 5 install wizard
  21. * Implements base login for select, input types
  22. * Each view which use this mixin should redefine method <code>changeHandler</code> (with
  23. * observing <code>controller.hostNameCheckTrigger</code>)
  24. *
  25. * @type {Ember.Mixin}
  26. */
  27. App.SelectHost = Em.Mixin.create({
  28. /**
  29. * Element of <code>controller.servicesMasters</code>
  30. * Binded from template
  31. * @type {object}
  32. */
  33. component: null,
  34. /**
  35. * List of avaiable host names
  36. * @type {string[]}
  37. */
  38. content: [],
  39. /**
  40. * Host component name
  41. * @type {string}
  42. */
  43. componentName: null,
  44. /**
  45. * Handler for selected value change
  46. * Triggers <code>changeHandler</code> execution
  47. * @method change
  48. */
  49. change: function () {
  50. if ('destroyed' === this.get('state')) return;
  51. this.set('controller.lastChangedComponent', this.get('component.component_name'));
  52. this.get('controller').toggleProperty('hostNameCheckTrigger');
  53. },
  54. /**
  55. * Add or remove <code>error</code> class from parent div-element
  56. * @param {bool} flag true - add class, false - remove
  57. * @method updateErrorStatus
  58. */
  59. updateErrorStatus: function(flag) {
  60. var parentBlock = this.$().parent('div');
  61. /* istanbul ignore next */
  62. if (flag) {
  63. parentBlock.removeClass('error');
  64. }
  65. else {
  66. parentBlock.addClass('error');
  67. }
  68. },
  69. /**
  70. * If component is multiple (defined in <code>controller.multipleComponents</code>),
  71. * should update <code>controller.componentToRebalance</code> and trigger rebalancing
  72. * @method tryTriggerRebalanceForMultipleComponents
  73. */
  74. tryTriggerRebalanceForMultipleComponents: function() {
  75. var componentIsMultiple = this.get('controller.multipleComponents').contains(this.get("component.component_name"));
  76. if(componentIsMultiple) {
  77. this.get('controller').set('componentToRebalance', this.get("component.component_name"));
  78. this.get('controller').incrementProperty('rebalanceComponentHostsCounter');
  79. }
  80. },
  81. /**
  82. * Handler for value changing
  83. * Should be redeclared in child views
  84. * Each redeclarion should contains code:
  85. * <code>
  86. * if (!this.shouldChangeHandlerBeCalled()) return;
  87. * </code>
  88. * @method changeHandler
  89. */
  90. changeHandler: function() {}.observes('controller.hostNameCheckTrigger'),
  91. /**
  92. * If view is destroyed or not current component's host name was changed we should do nothing
  93. * This method should be called from <code>changeHandler</code>:
  94. * <code>
  95. * changeHandler: function() {
  96. * if (!this.shouldChangeHandlerBeCalled()) return;
  97. * // your code
  98. * }.observes(...)
  99. * </code>
  100. * @returns {boolean}
  101. * @method shouldChangeHandlerBeCalled
  102. */
  103. shouldChangeHandlerBeCalled: function() {
  104. return !(('destroyed' === this.get('state')) || (this.get('controller.lastChangedComponent') !== this.get("component.component_name")));
  105. },
  106. /**
  107. * If <code>component.isHostNameValid</code> was changed,
  108. * error status should be updated according to new value
  109. * @method isHostNameValidObs
  110. */
  111. isHostNameValidObs: function() {
  112. this.updateErrorStatus(this.get('component.isHostNameValid'));
  113. }.observes('component.isHostNameValid'),
  114. /**
  115. * Recalculate available hosts
  116. * This should be done only once per Ember loop
  117. * @method rebalanceComponentHosts
  118. */
  119. rebalanceComponentHosts: function () {
  120. Em.run.once(this, 'rebalanceComponentHostsOnce');
  121. }.observes('controller.rebalanceComponentHostsCounter'),
  122. /**
  123. * Recalculate available hosts
  124. * @method rebalanceComponentHostsOnce
  125. */
  126. rebalanceComponentHostsOnce: function() {
  127. if (this.get('component.component_name') === this.get('controller.componentToRebalance')) {
  128. this.initContent();
  129. }
  130. },
  131. /**
  132. * Get available hosts
  133. * multipleComponents component can be assigned to multiple hosts,
  134. * shared hosts among the same component should be filtered out
  135. * @return {string[]}
  136. * @method getAvailableHosts
  137. */
  138. getAvailableHosts: function () {
  139. var hosts = this.get('controller.hosts').slice(),
  140. componentName = this.get('component.component_name'),
  141. multipleComponents = this.get('controller.multipleComponents'),
  142. occupiedHosts = this.get('controller.selectedServicesMasters')
  143. .filterProperty('component_name', componentName)
  144. .mapProperty('selectedHost')
  145. .without(this.get('component.selectedHost'));
  146. if (multipleComponents.contains(componentName)) {
  147. return hosts.filter(function (host) {
  148. return !occupiedHosts.contains(host.get('host_name'));
  149. }, this);
  150. }
  151. return hosts;
  152. },
  153. /**
  154. * Extract hosts from controller,
  155. * filter out available to selection and
  156. * push them into form element content
  157. * @method initContent
  158. */
  159. initContent: function () {
  160. this.set("content", this.getAvailableHosts());
  161. }
  162. });