hosts.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. require('views/common/table_view');
  18. var App = require('app');
  19. var lazyloading = require('utils/lazy_loading');
  20. module.exports = {
  21. /**
  22. * Launches a dialog to select hosts from the provided available hosts.
  23. *
  24. * Once the user clicks OK or Cancel, the callback is called with the
  25. * array of hosts (App.Host[]) selected. If the dialog was cancelled
  26. * or closed, <code>null</code> is provided to the callback. Else
  27. * an array (maybe empty) will be provided to the callback.
  28. *
  29. * @param initialHosts {App.Host[]} List of hosts to pick from
  30. * @param selectedHosts {App.Host[]} List of hosts already selected from the available hosts
  31. * @param selectAtleastOneHost {boolean} If true atleast one host has to be selected
  32. * @param validComponents {App.HostComponent[]} List of host-component types to pick from.
  33. * @param callback Callback function which is invoked when dialog
  34. * @param popupDescription {Object} Consist header and message for popup
  35. * Example: {header: 'header', dialogMessage: 'message'}
  36. * is closed, cancelled or OK is pressed.
  37. */
  38. launchHostsSelectionDialog : function(initialHosts, selectedHosts,
  39. selectAtleastOneHost, validComponents, callback, popupDescription) {
  40. // set default popup description
  41. var defaultPopupDescription = {
  42. header: Em.I18n.t('hosts.selectHostsDialog.title'),
  43. dialogMessage: Em.I18n.t('hosts.selectHostsDialog.message')
  44. };
  45. if (popupDescription !== null) {
  46. popupDescription = $.extend(true, defaultPopupDescription, popupDescription);
  47. }
  48. App.ModalPopup.show({
  49. classNames: [ 'sixty-percent-width-modal' ],
  50. header: popupDescription.header,
  51. dialogMessage: popupDescription.dialogMessage,
  52. warningMessage: null,
  53. availableHosts: [],
  54. onPrimary: function () {
  55. this.set('warningMessage', null);
  56. var arrayOfSelectedHosts = this.get('availableHosts').filterProperty('selected', true).mapProperty('host.id');
  57. if (selectAtleastOneHost && arrayOfSelectedHosts.length < 1) {
  58. this.set('warningMessage', Em.I18n.t('hosts.selectHostsDialog.message.warning'));
  59. return;
  60. }
  61. callback(arrayOfSelectedHosts);
  62. console.debug('(new-selectedHosts)=', arrayOfSelectedHosts);
  63. this.hide();
  64. },
  65. disablePrimary: function () {
  66. return !this.get('isLoaded');
  67. }.property('isLoaded'),
  68. onSecondary: function () {
  69. callback(null);
  70. this.hide();
  71. },
  72. bodyClass: App.TableView.extend({
  73. templateName: require('templates/common/configs/overrideWindow'),
  74. controllerBinding: 'App.router.mainServiceInfoConfigsController',
  75. isPaginate: true,
  76. filteredContent: function() {
  77. return this.get('parentView.availableHosts').filterProperty('filtered') || [];
  78. }.property('parentView.availableHosts.@each.filtered'),
  79. filterText: '',
  80. filterTextPlaceholder: Em.I18n.t('hosts.selectHostsDialog.filter.placeHolder'),
  81. filterColumn: null,
  82. filterColumns: Ember.A([
  83. Ember.Object.create({id: 'ip', name: 'IP Address', selected: true}),
  84. Ember.Object.create({id: 'cpu', name: 'CPU', selected: false}),
  85. Ember.Object.create({id: 'memory', name: 'RAM', selected: false}),
  86. Ember.Object.create({id: 'osArch', name: 'OS Architecture', selected: false}),
  87. Ember.Object.create({id: 'osType', name: 'OS Type', selected: false}),
  88. Ember.Object.create({id: 'diskTotal', name: 'Total Disks Capacity', selected: false}),
  89. Ember.Object.create({id: 'disksMounted', name: '# of Disk Mounts', selected: false})
  90. ]),
  91. showOnlySelectedHosts: false,
  92. filterComponents: validComponents,
  93. filterComponent: null,
  94. isDisabled: function () {
  95. return !this.get('parentView.isLoaded');
  96. }.property('parentView.isLoaded'),
  97. didInsertElement: function(){
  98. var defaultFilterColumn = this.get('filterColumns').findProperty('selected');
  99. this.set('filterColumn', defaultFilterColumn);
  100. initialHosts.setEach('filtered', true);
  101. this.set('parentView.availableHosts', initialHosts);
  102. this.set('parentView.isLoaded', true);
  103. },
  104. filterHosts: function () {
  105. var filterText = this.get('filterText');
  106. var showOnlySelectedHosts = this.get('showOnlySelectedHosts');
  107. var filterComponent = this.get('filterComponent');
  108. var filterColumn = this.get('filterColumn');
  109. this.get('parentView.availableHosts').forEach(function (host) {
  110. var skip = showOnlySelectedHosts && !host.get('selected');
  111. var value = host.get('host').get(filterColumn.id);
  112. var hostComponentNames = host.get('hostComponentNames');
  113. host.set('filterColumnValue', value);
  114. if (!skip && filterText) {
  115. if ((value == null || !value.toString().match(filterText)) && !host.get('host.publicHostName').match(filterText)) {
  116. skip = true;
  117. }
  118. }
  119. if (!skip && filterComponent) {
  120. if (hostComponentNames.length > 0) {
  121. skip = !hostComponentNames.contains(filterComponent.get('componentName'));
  122. }
  123. }
  124. host.set('filtered', !skip);
  125. }, this);
  126. this.set('startIndex', 1);
  127. }.observes('parentView.availableHosts', 'filterColumn', 'filterText', 'filterComponent', 'filterComponent.componentName', 'showOnlySelectedHosts'),
  128. hostSelectMessage: function () {
  129. var hosts = this.get('parentView.availableHosts');
  130. var selectedHosts = hosts.filterProperty('selected', true);
  131. return this.t('hosts.selectHostsDialog.selectedHostsLink').format(selectedHosts.get('length'), hosts.get('length'))
  132. }.property('parentView.availableHosts.@each.selected'),
  133. selectFilterColumn: function (event) {
  134. if (event != null && event.context != null && event.context.id != null) {
  135. var filterColumn = this.get('filterColumn');
  136. if (filterColumn != null) {
  137. filterColumn.set('selected', false);
  138. }
  139. event.context.set('selected', true);
  140. this.set('filterColumn', event.context);
  141. }
  142. },
  143. selectFilterComponent: function (event) {
  144. if (event != null && event.context != null && event.context.componentName != null) {
  145. var currentFilter = this.get('filterComponent');
  146. if (currentFilter != null) {
  147. currentFilter.set('selected', false);
  148. }
  149. if (currentFilter != null && currentFilter.componentName === event.context.componentName) {
  150. // selecting the same filter deselects it.
  151. this.set('filterComponent', null);
  152. } else {
  153. this.set('filterComponent', event.context);
  154. event.context.set('selected', true);
  155. }
  156. }
  157. },
  158. allHostsSelected: false,
  159. toggleSelectAllHosts: function (event) {
  160. this.get('parentView.availableHosts').filterProperty('filtered').setEach('selected', this.get('allHostsSelected'));
  161. }.observes('allHostsSelected'),
  162. toggleShowSelectedHosts: function () {
  163. var currentFilter = this.get('filterComponent');
  164. if (currentFilter != null) {
  165. currentFilter.set('selected', false);
  166. }
  167. this.set('filterComponent', null);
  168. this.set('filterText', null);
  169. this.set('showOnlySelectedHosts', !this.get('showOnlySelectedHosts'));
  170. }
  171. })
  172. });
  173. }
  174. };