host.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. var validator = require('utils/validator');
  20. App.MainHostController = Em.ArrayController.extend({
  21. name:'mainHostController',
  22. content:[],
  23. fullContent:App.Host.find(),
  24. clusters:App.Cluster.find(),
  25. //componentsForFilter: App.Component.find(),
  26. isAdmin: function(){
  27. return App.db.getUser().admin;
  28. }.property('App.router.loginController.loginName'),
  29. componentsForFilter:function() {
  30. var components = App.Component.find();
  31. var ret = new Array();
  32. if (!components) {
  33. return ret;
  34. }
  35. components.forEach(function(item) {
  36. var o = Ember.Object.create({
  37. id: item.get('id'),
  38. isMaster: item.get('isMaster'),
  39. isSlave: item.get('isSlave'),
  40. displayName: item.get('displayName'),
  41. componentName: item.get('componentName'),
  42. checkedForHostFilter: item.get('checkedForHostFilter')
  43. });
  44. ret.push(o);
  45. });
  46. return ret;
  47. }.property(),
  48. totalBinding:'fullContent.length',
  49. filters:{components:[]},
  50. pageSize: 25,
  51. pageSizeRange:[10, 25, 50, 100, 'all'],
  52. rangeStart:0,
  53. // allChecked:false,
  54. // selectedHostsIds:[],
  55. selectedRack:null,
  56. // assignHostsToRack:function () {
  57. // var selectedRack = this.get('selectedRack');
  58. // var sureMessage = this.t('hosts.assignToRack.sure');
  59. // var hostsIds = this.get('selectedHostsIds');
  60. //
  61. // var hostString = hostsIds.length + " " + this.t(hostsIds.length > 1 ? "host.plural" : "host.singular");
  62. //
  63. // if (selectedRack.constructor == 'App.Cluster' && hostsIds.length
  64. // && confirm(sureMessage.format(hostString, selectedRack.get('clusterName')))) {
  65. // this.get('content').forEach(function (host) {
  66. // if (host.get('isChecked')) {
  67. // host.set('cluster', selectedRack);
  68. // host.set('isChecked', false);
  69. // }
  70. // })
  71. // this.set('selectedHostsIds', []);
  72. // }
  73. //
  74. // },
  75. sortingAsc:true,
  76. isSort:false,
  77. sortClass:function () {
  78. return this.get('sortingAsc') ? 'icon-arrow-down' : 'icon-arrow-up';
  79. }.property('sortingAsc'),
  80. isDisabled:true,
  81. checkRemoved:function (host_id) {
  82. var hosts = this.get('content');
  83. var selectedHosts = hosts.filterProperty('id', host_id);
  84. this.get('fullContent').removeObjects(selectedHosts);
  85. },
  86. masterComponents:function () {
  87. var components = [];
  88. this.get('componentsForFilter').forEach(function (component) {
  89. if (component.get('isMaster')) {
  90. components.push(component);
  91. }
  92. });
  93. return components;
  94. }.property('componentsForFilter'),
  95. slaveComponents:function () {
  96. var components = [];
  97. this.get('componentsForFilter').forEach(function (component) {
  98. if (component.get('isSlave')) {
  99. components.push(component);
  100. }
  101. });
  102. return components;
  103. }.property('componentsForFilter'),
  104. clientComponents: function() {
  105. var components = [];
  106. this.get('componentsForFilter').forEach(function(component) {
  107. if (!component.get('isMaster') && !component.get('isSlave')) {
  108. components.push(component);
  109. }
  110. });
  111. return components;
  112. }.property('componentsForFilter'),
  113. backgroundOperationsCount:function () {
  114. return 5;
  115. }.property(),
  116. // onAllChecked:function () {
  117. // var hosts = this.get('content');
  118. // hosts.setEach('isChecked', this.get('allChecked'));
  119. // this.set('isDisabled', !this.get('allChecked'));
  120. // var selectedHostsIds = this.get('allChecked') ? hosts.getEach('id') : [];
  121. // this.set('selectedHostsIds', selectedHostsIds);
  122. // }.observes('allChecked'),
  123. //
  124. // onHostChecked:function (host) {
  125. // var selected = this.get('selectedHostsIds');
  126. // host.set('isChecked', !host.get('isChecked'));
  127. // if (host.get('isChecked')) {
  128. // selected.push(host.get('id'));
  129. // } else {
  130. // var index = selected.indexOf(host.get('id'));
  131. // if (index != -1) selected.splice(index, 1);
  132. // }
  133. // this.set('isDisabled', selected.length == 0);
  134. // this.propertyDidChange('selectedHostsIds');
  135. // },
  136. //
  137. // changeSelectedHosts:function () {
  138. // var visibleHosts = this.get('content');
  139. // var selectedHosts = visibleHosts.filterProperty('isChecked', true);
  140. // this.get('fullContent').forEach(function (item) {
  141. // var index = visibleHosts.getEach('id').indexOf(item.get('id'));
  142. // if (index == -1) item.set('isChecked', false);
  143. // });
  144. // this.set('isDisabled', selectedHosts.length == 0);
  145. // this.set('selectedHostsIds', selectedHosts.getEach('id'));
  146. // },
  147. checkedComponentsIds:function () {
  148. var checked = [];
  149. this.get('componentsForFilter').forEach(function (comp) {
  150. if (comp.get('checkedForHostFilter'))
  151. checked.push(comp.get('id'));
  152. });
  153. return checked;
  154. },
  155. filterByComponentsIds:function () {
  156. var componentsIds = this.checkedComponentsIds();
  157. this.set('filters.components', componentsIds);
  158. // component.set('isChecked', component.get('id') != -1);
  159. this.changeContent();
  160. },
  161. filterHostsBy:function (field, value) {
  162. this.set('hostFilter' + field, value);
  163. this.changeContent();
  164. },
  165. filterByComponent:function (component) {
  166. var id = component.get('id');
  167. /*this.get('componentsForFilter').setEach('isChecked', false);
  168. component.set('isChecked', true);*/
  169. this.get('componentsForFilter').setEach('checkedForHostFilter', false);
  170. this.get('componentsForFilter').filterProperty('id', id).setEach('checkedForHostFilter', true);
  171. //component.set('checkedForHostFilter', true);
  172. this.set('filters.components', [component.get('id')]);
  173. console.log(this.get('filters.components').objectAt(0));
  174. this.changeContent();
  175. },
  176. applyHostFilters:function (items) {
  177. var field = 'hostName'; // make this function universal
  178. var value = this.get('hostFilter' + field);
  179. var itemsToDelete = [];
  180. if (value) {
  181. items.forEach(function (host, index) {
  182. if (host) {
  183. var fieldValue = host.get(field);
  184. if (fieldValue) {
  185. if (fieldValue.indexOf(value) == -1) {
  186. itemsToDelete.push(host);
  187. }
  188. }
  189. }
  190. });
  191. }
  192. if (itemsToDelete.length) {
  193. itemsToDelete.forEach(function (hostToDelete) {
  194. var index = items.indexOf(hostToDelete);
  195. items.removeAt(index);
  196. })
  197. }
  198. return items;
  199. },
  200. changeContent:function () {
  201. var items = [];
  202. var filters = this.get('filters.components');
  203. this.get('fullContent').forEach(function (item) {
  204. if (filters.length) {
  205. var inFilters = false;
  206. item.get('components').forEach(function (component) {
  207. if (filters.indexOf(component.get('id')) != -1) {
  208. inFilters = true;
  209. }
  210. });
  211. if (inFilters) {
  212. items.push(item);
  213. }
  214. }
  215. else {
  216. items.push(item);
  217. }
  218. });
  219. items = this.applyHostFilters(items);
  220. this.set('total', items.length);
  221. var content = items.slice(this.get('rangeStart'), this.get('rangeStop'));
  222. this.replace(0, this.get('length'), content);
  223. // this.changeSelectedHosts();
  224. }.observes('rangeStart', 'rangeStop', 'total'),
  225. showNextPage:function () {
  226. this.nextPage();
  227. },
  228. showPreviousPage:function () {
  229. this.previousPage();
  230. },
  231. assignedToRackPopup:function (event) {
  232. var self = this;
  233. App.ModalPopup.show({
  234. header:Em.I18n.t('hosts.assignedToRack.popup.header'),
  235. body:Em.I18n.t('hosts.assignedToRack.popup.body'),
  236. primary:'Yes',
  237. secondary:'No',
  238. onPrimary:function () {
  239. self.assignedToRack(event.context);
  240. this.hide();
  241. },
  242. onSecondary:function () {
  243. this.hide();
  244. }
  245. });
  246. },
  247. assignedToRack:function (rack) {
  248. var hosts = this.get('content');
  249. var selectedHosts = hosts.filterProperty('isChecked', true);
  250. selectedHosts.setEach('cluster', rack);
  251. },
  252. decommissionButtonPopup:function () {
  253. var self = this;
  254. App.ModalPopup.show({
  255. header:Em.I18n.t('hosts.decommission.popup.header'),
  256. body:Em.I18n.t('hosts.decommission.popup.body'),
  257. primary:'Yes',
  258. secondary:'No',
  259. onPrimary:function () {
  260. alert('do');
  261. this.hide();
  262. },
  263. onSecondary:function () {
  264. this.hide();
  265. }
  266. });
  267. },
  268. deleteButtonPopup:function () {
  269. var self = this;
  270. App.ModalPopup.show({
  271. header:Em.I18n.t('hosts.delete.popup.header'),
  272. body:Em.I18n.t('hosts.delete.popup.body'),
  273. primary:'Yes',
  274. secondary:'No',
  275. onPrimary:function () {
  276. self.removeHosts();
  277. this.hide();
  278. },
  279. onSecondary:function () {
  280. this.hide();
  281. }
  282. });
  283. },
  284. removeHosts:function () {
  285. var hosts = this.get('content');
  286. var selectedHosts = hosts.filterProperty('isChecked', true);
  287. selectedHosts.forEach(function (_hostInfo) {
  288. console.log('Removing: ' + _hostInfo.hostName);
  289. });
  290. // App.db.removeHosts(selectedHosts);
  291. this.get('fullContent').removeObjects(selectedHosts);
  292. },
  293. sortByName:function () {
  294. var asc = this.get('sortingAsc');
  295. var objects = this.get('fullContent').toArray().sort(function (a, b) {
  296. var nA = a.get('hostName').toLowerCase();
  297. var nB = b.get('hostName').toLowerCase();
  298. if (nA < nB)
  299. return asc ? -1 : 1;
  300. else if (nA > nB)
  301. return asc ? 1 : -1;
  302. return 0;
  303. });
  304. this.set('fullContent', objects);
  305. this.set('isSort', true);
  306. this.set('sortingAsc', !this.get('sortingAsc'));
  307. this.changeContent();
  308. }
  309. });