Code coverage report for app/mixins/wizard/selectHost.js

Statements: 92.31% (24 / 26)      Branches: 75% (9 / 12)      Functions: 75% (9 / 12)      Lines: 92% (23 / 25)      Ignored: 3 statements, 2 branches     

All files » app/mixins/wizard/ » selectHost.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179    1                                     1                   1                                                     3 2 2                     1 1   1                   6 6 6 6                                                                         19                 6               8 7                       11         11 9 10     11                   9        
'use strict';
 
;require.register("mixins/wizard/selectHost", function (exports, require, module) {
  /**
   * Licensed to the Apache Software Foundation (ASF) under one
   * or more contributor license agreements.  See the NOTICE file
   * distributed with this work for additional information
   * regarding copyright ownership.  The ASF licenses this file
   * to you under the Apache License, Version 2.0 (the
   * "License"); you may not use this file except in compliance
   * with the License.  You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
 
  var App = require('app');
 
  /**
   * Mixin for host select view on step 5 install wizard
   * Implements base login for select, input types
   * Each view which use this mixin should redefine method <code>changeHandler</code> (with
   * observing <code>controller.hostNameCheckTrigger</code>)
   *
   * @type {Ember.Mixin}
   */
  App.SelectHost = Em.Mixin.create({
 
    /**
     * Element of <code>controller.servicesMasters</code>
     * Binded from template
     * @type {object}
     */
    component: null,
 
    /**
     * List of avaiable host names
     * @type {string[]}
     */
    content: [],
 
    /**
     * Host component name
     * @type {string}
     */
    componentName: null,
 
    /**
     * Handler for selected value change
     * Triggers <code>changeHandler</code> execution
     * @method change
     */
    change: function change() {
      if ('destroyed' === this.get('state')) return;
      this.set('controller.lastChangedComponent', this.get('component.component_name'));
      this.get('controller').toggleProperty('hostNameCheckTrigger');
    },
 
    /**
     * Add or remove <code>error</code> class from parent div-element
     * @param {bool} flag true - add class, false - remove
     * @method updateErrorStatus
     */
    updateErrorStatus: function updateErrorStatus(flag) {
      var parentBlock = this.$().parent('div');
      /* istanbul ignore next */
      if (flag) {
        parentBlock.removeClass('error');
      } else {
        parentBlock.addClass('error');
      }
    },
 
    /**
     * If component is multiple (defined in <code>controller.multipleComponents</code>),
     * should update <code>controller.componentToRebalance</code> and trigger rebalancing
     * @method tryTriggerRebalanceForMultipleComponents
     */
    tryTriggerRebalanceForMultipleComponents: function tryTriggerRebalanceForMultipleComponents() {
      var componentIsMultiple = this.get('controller.multipleComponents').contains(this.get("component.component_name"));
      Eif (componentIsMultiple) {
        this.get('controller').set('componentToRebalance', this.get("component.component_name"));
        this.get('controller').incrementProperty('rebalanceComponentHostsCounter');
      }
    },
 
    /**
     * Handler for value changing
     * Should be redeclared in child views
     * Each redeclarion should contains code:
     * <code>
     *   if (!this.shouldChangeHandlerBeCalled()) return;
     * </code>
     * @method changeHandler
     */
    changeHandler: function () {}.observes('controller.hostNameCheckTrigger'),
 
    /**
     * If view is destroyed or not current component's host name was changed we should do nothing
     * This method should be called from <code>changeHandler</code>:
     * <code>
     *   changeHandler: function() {
     *     if (!this.shouldChangeHandlerBeCalled()) return;
     *     // your code
     *   }.observes(...)
     * </code>
     * @returns {boolean}
     * @method shouldChangeHandlerBeCalled
     */
    shouldChangeHandlerBeCalled: function shouldChangeHandlerBeCalled() {
      return !('destroyed' === this.get('state') || this.get('controller.lastChangedComponent') !== this.get("component.component_name"));
    },
 
    /**
     * If <code>component.isHostNameValid</code> was changed,
     * error status should be updated according to new value
     * @method isHostNameValidObs
     */
    isHostNameValidObs: function () {
      this.updateErrorStatus(this.get('component.isHostNameValid'));
    }.observes('component.isHostNameValid'),
 
    /**
     * Recalculate available hosts
     * This should be done only once per Ember loop
     * @method rebalanceComponentHosts
     */
    rebalanceComponentHosts: function () {
      Em.run.once(this, 'rebalanceComponentHostsOnce');
    }.observes('controller.rebalanceComponentHostsCounter'),
 
    /**
     * Recalculate available hosts
     * @method rebalanceComponentHostsOnce
     */
    rebalanceComponentHostsOnce: function rebalanceComponentHostsOnce() {
      if (this.get('component.component_name') === this.get('controller.componentToRebalance')) {
        this.initContent();
      }
    },
 
    /**
     * Get available hosts
     * multipleComponents component can be assigned to multiple hosts,
     * shared hosts among the same component should be filtered out
     * @return {string[]}
     * @method getAvailableHosts
     */
    getAvailableHosts: function getAvailableHosts() {
      var hosts = this.get('controller.hosts').slice(),
          componentName = this.get('component.component_name'),
          multipleComponents = this.get('controller.multipleComponents'),
          occupiedHosts = this.get('controller.selectedServicesMasters').filterProperty('component_name', componentName).mapProperty('selectedHost').without(this.get('component.selectedHost'));
 
      if (multipleComponents.contains(componentName)) {
        hosts = hosts.filter(function (host) {
          return !occupiedHosts.contains(host.get('host_name'));
        }, this);
      }
      return hosts.sortProperty('host_name');
    },
 
    /**
     * Extract hosts from controller,
     * filter out available to selection and
     * push them into form element content
     * @method initContent
     */
    initContent: function initContent() {
      this.set("content", this.getAvailableHosts());
    }
 
  });
});