hosts.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. module.exports = {
  18. /**
  19. * Launches a dialog to select hosts from the provided available hosts.
  20. *
  21. * Once the user clicks OK or Cancel, the callback is called with the
  22. * array of hosts (App.Host[]) selected. If the dialog was cancelled
  23. * or closed, <code>null</code> is provided to the callback. Else
  24. * an array (maybe empty) will be provided to the callback.
  25. *
  26. * @param availableHosts {App.Host[]} List of hosts to pick from
  27. * @param selectedHosts {App.Host[]} List of hosts already selected from the available hosts
  28. * @param selectAtleastOneHost {boolean} If true atleast one host has to be selected
  29. * @param validComponents {App.HostComponent[]} List of host-component types to pick from.
  30. * @param callback Callback function which is invoked when dialog
  31. * @param popupDescription {Object} Consist header and message for popup
  32. * Example: {header: 'header', dialogMessage: 'message'}
  33. * is closed, cancelled or OK is pressed.
  34. */
  35. launchHostsSelectionDialog : function(availableHosts, selectedHosts,
  36. selectAtleastOneHost, validComponents, callback, popupDescription) {
  37. // set default popup description
  38. var defaultPopupDescription = {
  39. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  40. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message')
  41. };
  42. if (popupDescription !== null) {
  43. popupDescription = $.extend(true, defaultPopupDescription, popupDescription);
  44. }
  45. App.ModalPopup.show({
  46. classNames: [ 'sixty-percent-width-modal' ],
  47. header: popupDescription.header,
  48. dialogMessage: popupDescription.dialogMessage,
  49. warningMessage: null,
  50. onPrimary: function () {
  51. console.debug('(old-selectedHosts)=', selectedHosts);
  52. this.set('warningMessage', null);
  53. var arrayOfSelectedHosts = [];
  54. var selectedHosts = availableHosts.filterProperty('selected', true);
  55. selectedHosts.forEach(function (host) {
  56. arrayOfSelectedHosts.push(host.get('host.id'));
  57. });
  58. if (selectAtleastOneHost && arrayOfSelectedHosts.length<1) {
  59. this.set('warningMessage', 'Atleast one host needs to be selected.');
  60. return;
  61. }
  62. callback(arrayOfSelectedHosts);
  63. console.debug('(new-selectedHosts)=', arrayOfSelectedHosts);
  64. this.hide();
  65. },
  66. onSecondary: function () {
  67. callback(null);
  68. this.hide();
  69. },
  70. bodyClass: Ember.View.extend({
  71. templateName: require('templates/common/configs/overrideWindow'),
  72. controllerBinding: 'App.router.mainServiceInfoConfigsController',
  73. filterText: '',
  74. filterTextPlaceholder: Em.I18n.t('hosts.selectHostsDialog.filter.placeHolder'),
  75. availableHosts: availableHosts,
  76. filterColumn: Ember.Object.create({id: 'ip', name: 'IP Address', selected: false}),
  77. filterColumns: Ember.A([
  78. Ember.Object.create({id: 'ip', name: 'IP Address', selected: false}),
  79. Ember.Object.create({id: 'cpu', name: 'CPU', selected: false}),
  80. Ember.Object.create({id: 'memory', name: 'RAM', selected: false}),
  81. Ember.Object.create({id: 'diskUsage', name: 'Disk Usage', selected: false}),
  82. Ember.Object.create({id: 'loadAvg', name: 'Load Average', selected: false}),
  83. Ember.Object.create({id: 'osArch', name: 'OS Architecture', selected: false}),
  84. Ember.Object.create({id: 'osType', name: 'OS Type', selected: false})
  85. ]),
  86. showOnlySelectedHosts: false,
  87. filterComponents: validComponents,
  88. filterComponent: null,
  89. filteredHosts: function () {
  90. var hosts = this.get('availableHosts');
  91. var filterText = this.get('filterText');
  92. var showOnlySelectedHosts = this.get('showOnlySelectedHosts');
  93. var filteredHosts = Ember.A([]);
  94. var self = this;
  95. hosts.forEach(function (host) {
  96. var skip = false;
  97. var ahost = host.get('host');
  98. var filterColumn = self.get('filterColumn');
  99. if (filterColumn == null) {
  100. filterColumn = self.get('filterColumns').objectAt(0);
  101. }
  102. var value = ahost.get(filterColumn.id);
  103. host.set('filterColumnValue', value);
  104. if (filterText != null && filterText.length > 0) {
  105. if ((value == null || !value.match(filterText)) && !host.get('host.publicHostName').match(filterText)) {
  106. skip = true;
  107. }
  108. }
  109. var filterComponent = self.get('filterComponent');
  110. if (!skip && filterComponent != null) {
  111. var componentFound = false;
  112. var fcn = filterComponent.get('componentName');
  113. var hcs = ahost.get('hostComponents');
  114. if (hcs != null) {
  115. hcs.forEach(function (hc) {
  116. if (fcn === hc.get('componentName')) {
  117. componentFound = true;
  118. }
  119. });
  120. }
  121. if (!componentFound) {
  122. skip = true;
  123. }
  124. }
  125. if (!skip && showOnlySelectedHosts && !host.get('selected')) {
  126. skip = true;
  127. }
  128. if (!skip) {
  129. filteredHosts.pushObject(host);
  130. }
  131. });
  132. return filteredHosts;
  133. }.property('availableHosts', 'filterText', 'filterColumn', 'filterComponent', 'filterComponent.componentName', 'showOnlySelectedHosts'),
  134. hostColumnValue: function (host, column) {
  135. return host.get(column.id);
  136. },
  137. hostSelectMessage: function () {
  138. var hosts = this.get('availableHosts');
  139. var selectedHosts = hosts.filterProperty('selected', true);
  140. return this.t('hosts.selectHostsDialog.selectedHostsLink').format(selectedHosts.get('length'), hosts.get('length'))
  141. }.property('availableHosts.@each.selected'),
  142. selectFilterColumn: function (event) {
  143. if (event != null && event.context != null && event.context.id != null) {
  144. var filterColumn = this.get('filterColumn');
  145. if (filterColumn != null) {
  146. filterColumn.set('selected', false);
  147. }
  148. event.context.set('selected', true);
  149. this.set('filterColumn', event.context);
  150. }
  151. },
  152. selectFilterComponent: function (event) {
  153. if (event != null && event.context != null && event.context.componentName != null) {
  154. var currentFilter = this.get('filterComponent');
  155. if (currentFilter != null) {
  156. currentFilter.set('selected', false);
  157. }
  158. if (currentFilter != null && currentFilter.componentName === event.context.componentName) {
  159. // selecting the same filter deselects it.
  160. this.set('filterComponent', null);
  161. } else {
  162. this.set('filterComponent', event.context);
  163. event.context.set('selected', true);
  164. }
  165. }
  166. },
  167. allHostsSelected: false,
  168. toggleSelectAllHosts: function (event) {
  169. if (this.get('allHostsSelected')) {
  170. // Select all hosts
  171. this.get('availableHosts').setEach('selected', true);
  172. } else {
  173. // Deselect all hosts
  174. this.get('availableHosts').setEach('selected', false);
  175. }
  176. }.observes('allHostsSelected'),
  177. toggleShowSelectedHosts: function () {
  178. var currentFilter = this.get('filterComponent');
  179. if (currentFilter != null) {
  180. currentFilter.set('selected', false);
  181. }
  182. this.set('filterComponent', null);
  183. this.set('filterText', null);
  184. this.set('showOnlySelectedHosts', !this.get('showOnlySelectedHosts'));
  185. }
  186. })
  187. });
  188. }
  189. };