hosts.js 8.4 KB

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