manage_alert_groups_controller.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  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. var numberUtils = require('utils/number_utils');
  21. App.ManageAlertGroupsController = Em.Controller.extend({
  22. name: 'manageAlertGroupsController',
  23. isLoaded: false,
  24. alertGroups: [],
  25. originalAlertGroups: [],
  26. selectedAlertGroup: null,
  27. selectedDefinitions: [],
  28. /**
  29. * List of all Alert Notifications
  30. * @type {App.AlertNotification[]}
  31. */
  32. alertNotifications: function () {
  33. return this.get('isLoaded') ? App.AlertNotification.find().map (function (target) {
  34. return Em.Object.create ({
  35. name: target.get('name'),
  36. id: target.get('id'),
  37. description: target.get('description'),
  38. type: target.get('type'),
  39. global: target.get('global')
  40. });
  41. }) : [];
  42. }.property('isLoaded'),
  43. /**
  44. * List of all global Alert Notifications
  45. * @type {App.AlertNotification[]}
  46. */
  47. alertGlobalNotifications: function () {
  48. return this.get('alertNotifications').filterProperty('global');
  49. }.property('alertNotifications'),
  50. /**
  51. * Load all Alert Notifications from server
  52. * @returns {$.ajax|null}
  53. */
  54. loadAlertNotifications: function () {
  55. this.set('isLoaded', false);
  56. this.set('alertGroups', []);
  57. this.set('originalAlertGroups', []);
  58. this.set('selectedAlertGroup', null);
  59. this.set('isRemoveButtonDisabled', true);
  60. this.set('isRenameButtonDisabled', true);
  61. this.set('isDuplicateButtonDisabled', true);
  62. return App.ajax.send({
  63. name: 'alerts.notifications',
  64. sender: this,
  65. success: 'getAlertNotificationsSuccessCallback',
  66. error: 'getAlertNotificationsErrorCallback'
  67. });
  68. },
  69. /**
  70. * Success-callback for load alert notifications request
  71. * @param {object} json
  72. * @method getAlertNotificationsSuccessCallback
  73. */
  74. getAlertNotificationsSuccessCallback: function (json) {
  75. App.alertNotificationMapper.map(json);
  76. this.loadAlertGroups();
  77. },
  78. /**
  79. * Error-callback for load alert notifications request
  80. * @method getAlertNotificationsErrorCallback
  81. */
  82. getAlertNotificationsErrorCallback: function () {
  83. this.set('isLoaded', true);
  84. },
  85. /**
  86. * Load all alert groups from alert group model
  87. */
  88. loadAlertGroups: function () {
  89. var alertGroups = App.AlertGroup.find().map(function (group) {
  90. var definitions = group.get('definitions').map (function (def) {
  91. return Em.Object.create ({
  92. name: def.get('name'),
  93. serviceName: def.get('serviceName'),
  94. componentName: def.get('componentName'),
  95. serviceNameDisplay: def.get('service.displayName'),
  96. componentNameDisplay: def.get('componentNameFormatted'),
  97. label: def.get('label'),
  98. id: def.get('id')
  99. });
  100. });
  101. var targets = group.get('targets').map (function (target) {
  102. return Em.Object.create ({
  103. name: target.get('name'),
  104. id: target.get('id'),
  105. description: target.get('description'),
  106. type: target.get('type'),
  107. global: target.get('global')
  108. });
  109. });
  110. return Em.Object.create({
  111. id: group.get('id'),
  112. name: group.get('name'),
  113. default: group.get('default'),
  114. displayName: function () {
  115. var name = this.get('name');
  116. if (name && name.length > App.config.CONFIG_GROUP_NAME_MAX_LENGTH) {
  117. var middle = Math.floor(App.config.CONFIG_GROUP_NAME_MAX_LENGTH / 2);
  118. name = name.substring(0, middle) + "..." + name.substring(name.length - middle);
  119. }
  120. return this.get('default') ? (name + ' Default') : name;
  121. }.property('name', 'default'),
  122. label: function () {
  123. return this.get('displayName') + ' (' + this.get('definitions.length') + ')';
  124. }.property('displayName', 'definitions.length'),
  125. definitions: definitions,
  126. isAddDefinitionsDisabled: group.get('isAddDefinitionsDisabled'),
  127. notifications: targets
  128. });
  129. });
  130. this.set('alertGroups', alertGroups);
  131. this.set('isLoaded', true);
  132. this.set('originalAlertGroups', this.copyAlertGroups(this.get('alertGroups')));
  133. this.set('selectedAlertGroup', this.get('alertGroups')[0]);
  134. },
  135. /**
  136. * @type {boolean}
  137. */
  138. isRemoveButtonDisabled: true,
  139. /**
  140. * @type {boolean}
  141. */
  142. isRenameButtonDisabled: true,
  143. /**
  144. * @type {boolean}
  145. */
  146. isDuplicateButtonDisabled: true,
  147. /**
  148. * Enable/disable "Remove"/"Rename"/"Duplicate" buttons basing on <code>controller.selectedAlertGroup</code>
  149. * @method buttonObserver
  150. */
  151. buttonObserver: function () {
  152. var selectedAlertGroup = this.get('selectedAlertGroup');
  153. var flag = selectedAlertGroup && selectedAlertGroup.get('default');
  154. this.set('isRemoveButtonDisabled', flag);
  155. this.set('isRenameButtonDisabled', flag);
  156. this.set('isDuplicateButtonDisabled', false);
  157. }.observes('selectedAlertGroup'),
  158. resortAlertGroup: function() {
  159. var alertGroups = Ember.copy(this.get('alertGroups'));
  160. if(alertGroups.length < 2){
  161. return;
  162. }
  163. var defaultGroups = alertGroups.filterProperty('default');
  164. defaultGroups.forEach( function(defaultGroup) {
  165. alertGroups.removeObject(defaultGroup);
  166. });
  167. var sorted = defaultGroups.sortProperty('name').concat(alertGroups.sortProperty('name'));
  168. this.removeObserver('alertGroups.@each.name', this, 'resortAlertGroup');
  169. this.set('alertGroups', sorted);
  170. this.addObserver('alertGroups.@each.name', this, 'resortAlertGroup');
  171. }.observes('alertGroups.@each.name'),
  172. /**
  173. * remove definitions from group
  174. */
  175. deleteDefinitions: function () {
  176. if (this.get('isDeleteDefinitionsDisabled')) {
  177. return;
  178. }
  179. var groupDefinitions = this.get('selectedAlertGroup.definitions');
  180. this.get('selectedDefinitions').slice().forEach(function (defObj) {
  181. groupDefinitions.removeObject(defObj);
  182. }, this);
  183. this.set('selectedDefinitions', []);
  184. },
  185. isDeleteDefinitionsDisabled: function () {
  186. var selectedGroup = this.get('selectedAlertGroup');
  187. if (selectedGroup) {
  188. return selectedGroup.default || this.get('selectedDefinitions').length === 0;
  189. }
  190. return true;
  191. }.property('selectedAlertGroup', 'selectedAlertGroup.definitions.length', 'selectedDefinitions.length'),
  192. /**
  193. * Provides alert definitions which are available for inclusion in
  194. * non-default alert groups.
  195. */
  196. getAvailableDefinitions: function (selectedAlertGroup) {
  197. if (selectedAlertGroup.get('default')) return [];
  198. var usedDefinitionsMap = {};
  199. var availableDefinitions = [];
  200. var sharedDefinitions = App.AlertDefinition.getAllDefinitions();
  201. selectedAlertGroup.get('definitions').forEach(function (def) {
  202. usedDefinitionsMap[def.name] = true;
  203. });
  204. sharedDefinitions.forEach(function (shared_def) {
  205. if (!usedDefinitionsMap[shared_def.get('name')]) {
  206. availableDefinitions.pushObject(shared_def);
  207. }
  208. });
  209. return availableDefinitions.map (function (def) {
  210. return Em.Object.create ({
  211. name: def.get('name'),
  212. serviceName: def.get('serviceName'),
  213. componentName: def.get('componentName'),
  214. serviceNameDisplay: def.get('service.displayName'),
  215. componentNameDisplay: def.get('componentNameFormatted'),
  216. label: def.get('label'),
  217. id: def.get('id')
  218. });
  219. });
  220. },
  221. /**
  222. * add alert definitions to a group
  223. */
  224. addDefinitions: function () {
  225. if (this.get('selectedAlertGroup.isAddDefinitionsDisabled')){
  226. return false;
  227. }
  228. var availableDefinitions = this.getAvailableDefinitions(this.get('selectedAlertGroup'));
  229. var popupDescription = {
  230. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.title'),
  231. dialogMessage: Em.I18n.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.message').format(this.get('selectedAlertGroup.displayName'))
  232. };
  233. var validComponents = App.StackServiceComponent.find().map(function (component) {
  234. return Em.Object.create({
  235. componentName: component.get('componentName'),
  236. displayName: App.format.role(component.get('componentName')),
  237. selected: false
  238. });
  239. });
  240. var validServices = App.Service.find().map(function (service) {
  241. return Em.Object.create({
  242. serviceName: service.get('serviceName'),
  243. displayName: App.format.role(service.get('serviceName')),
  244. selected: false
  245. });
  246. });
  247. this.launchDefsSelectionDialog (availableDefinitions, [], validServices, validComponents, this.addDefinitionsCallback.bind(this), popupDescription);
  248. },
  249. /**
  250. * Launch a table view of all available definitions to choose
  251. */
  252. launchDefsSelectionDialog : function(initialDefs, selectedDefs, validServices, validComponents, callback, popupDescription) {
  253. App.ModalPopup.show({
  254. classNames: [ 'sixty-percent-width-modal' ],
  255. header: popupDescription.header,
  256. dialogMessage: popupDescription.dialogMessage,
  257. warningMessage: null,
  258. availableDefs: [],
  259. onPrimary: function () {
  260. this.set('warningMessage', null);
  261. var arrayOfSelectedDefs = this.get('availableDefs').filterProperty('selected', true);
  262. if (arrayOfSelectedDefs.length < 1) {
  263. this.set('warningMessage', Em.I18n.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.message.warning'));
  264. return;
  265. }
  266. callback(arrayOfSelectedDefs);
  267. console.debug('(new-selectedDefs)=', arrayOfSelectedDefs);
  268. this.hide();
  269. },
  270. disablePrimary: function () {
  271. return !this.get('isLoaded');
  272. }.property('isLoaded'),
  273. onSecondary: function () {
  274. callback(null);
  275. this.hide();
  276. },
  277. bodyClass: App.TableView.extend({
  278. templateName: require('templates/main/alerts/add_definition_to_group_popup'),
  279. controllerBinding: 'App.router.manageAlertGroupsController',
  280. isPaginate: true,
  281. filteredContent: function() {
  282. return this.get('parentView.availableDefs').filterProperty('filtered') || [];
  283. }.property('parentView.availableDefs.@each.filtered'),
  284. showOnlySelectedDefs: false,
  285. filterComponents: validComponents,
  286. filterServices: validServices,
  287. filterComponent: null,
  288. filterService: null,
  289. isDisabled: function () {
  290. return !this.get('parentView.isLoaded');
  291. }.property('parentView.isLoaded'),
  292. didInsertElement: function(){
  293. initialDefs.setEach('filtered', true);
  294. this.set('parentView.availableDefs', initialDefs);
  295. this.set('parentView.isLoaded', true);
  296. },
  297. filterDefs: function () {
  298. var showOnlySelectedDefs = this.get('showOnlySelectedDefs');
  299. var filterComponent = this.get('filterComponent');
  300. var filterService = this.get('filterService');
  301. this.get('parentView.availableDefs').forEach(function (defObj) {
  302. var componentOnObj = true;
  303. var serviceOnObj = true;
  304. if (filterComponent) {
  305. componentOnObj = (defObj.componentName == filterComponent.get('componentName'));
  306. }
  307. if (defObj.serviceName && filterService) {
  308. serviceOnObj = (defObj.serviceName == filterService.get('serviceName'));
  309. }
  310. if (showOnlySelectedDefs) {
  311. defObj.set('filtered', componentOnObj && serviceOnObj && defObj.get('selected'));
  312. } else {
  313. defObj.set('filtered', componentOnObj && serviceOnObj);
  314. }
  315. }, this);
  316. this.set('startIndex', 1);
  317. }.observes('parentView.availableDefs', 'filterService', 'filterService.serviceName', 'filterComponent', 'filterComponent.componentName', 'showOnlySelectedDefs'),
  318. defSelectMessage: function () {
  319. var defs = this.get('parentView.availableDefs');
  320. var selectedDefs = defs.filterProperty('selected', true);
  321. return this.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.selectedDefsLink').format(selectedDefs.get('length'), defs.get('length'));
  322. }.property('parentView.availableDefs.@each.selected'),
  323. selectFilterComponent: function (event) {
  324. if (event != null && event.context != null && event.context.componentName != null) {
  325. var currentFilter = this.get('filterComponent');
  326. if (currentFilter != null) {
  327. currentFilter.set('selected', false);
  328. }
  329. if (currentFilter != null && currentFilter.componentName === event.context.componentName) {
  330. // selecting the same filter deselects it.
  331. this.set('filterComponent', null);
  332. } else {
  333. this.set('filterComponent', event.context);
  334. event.context.set('selected', true);
  335. }
  336. }
  337. },
  338. selectFilterService: function (event) {
  339. if (event != null && event.context != null && event.context.serviceName != null) {
  340. var currentFilter = this.get('filterService');
  341. if (currentFilter != null) {
  342. currentFilter.set('selected', false);
  343. }
  344. if (currentFilter != null && currentFilter.serviceName === event.context.serviceName) {
  345. // selecting the same filter deselects it.
  346. this.set('filterService', null);
  347. } else {
  348. this.set('filterService', event.context);
  349. event.context.set('selected', true);
  350. }
  351. }
  352. },
  353. allDefsSelected: false,
  354. toggleSelectAllDefs: function (event) {
  355. this.get('parentView.availableDefs').filterProperty('filtered').setEach('selected', this.get('allDefsSelected'));
  356. }.observes('allDefsSelected'),
  357. toggleShowSelectedDefs: function () {
  358. var filter1 = this.get('filterComponent');
  359. if (filter1 != null) {
  360. filter1.set('selected', false);
  361. }
  362. var filter2 = this.get('filterService');
  363. if (filter2 != null) {
  364. filter2.set('selected', false);
  365. }
  366. this.set('filterComponent', null);
  367. this.set('filterService', null);
  368. this.set('showOnlySelectedDefs', !this.get('showOnlySelectedDefs'));
  369. }
  370. })
  371. });
  372. },
  373. /**
  374. * add alert definitions callback
  375. */
  376. addDefinitionsCallback: function (selectedDefs) {
  377. var group = this.get('selectedAlertGroup');
  378. if (selectedDefs) {
  379. var alertGroupDefs = group.get('definitions');
  380. selectedDefs.forEach(function (defObj) {
  381. alertGroupDefs.pushObject(defObj);
  382. }, this);
  383. }
  384. },
  385. /**
  386. * observes if any group changed including: group name, newly created group, deleted group, group with definitions/notifications changed
  387. */
  388. defsModifiedAlertGroups: function () {
  389. if (!this.get('isLoaded')) {
  390. return false;
  391. }
  392. var groupsToDelete = [];
  393. var groupsToSet = [];
  394. var groupsToCreate = [];
  395. var groups = this.get('alertGroups'); //current alert groups
  396. var originalGroups = this.get('originalAlertGroups'); // original alert groups
  397. var originalGroupsIds = originalGroups.mapProperty('id');
  398. groups.forEach(function (group) {
  399. var originalGroup = originalGroups.findProperty('id', group.get('id'));
  400. if (originalGroup) {
  401. // should update definitions or nitifications
  402. if (!(JSON.stringify(group.get('definitions').slice().sort()) === JSON.stringify(originalGroup.get('definitions').slice().sort()))
  403. || !(JSON.stringify(group.get('notifications').slice().sort()) === JSON.stringify(originalGroup.get('notifications').slice().sort()))) {
  404. groupsToSet.push(group.set('id', originalGroup.get('id')));
  405. } else if (group.get('name') !== originalGroup.get('name') ) {
  406. // should update name
  407. groupsToSet.push(group.set('id', originalGroup.get('id')));
  408. }
  409. originalGroupsIds = originalGroupsIds.without(group.get('id'));
  410. } else {
  411. // should add new group
  412. groupsToCreate.push(group);
  413. }
  414. });
  415. // should delete groups
  416. originalGroupsIds.forEach(function (id) {
  417. groupsToDelete.push(originalGroups.findProperty('id', id));
  418. }, this);
  419. return {
  420. toDelete: groupsToDelete,
  421. toSet: groupsToSet,
  422. toCreate: groupsToCreate
  423. };
  424. }.property('selectedAlertGroup.definitions.@each', 'selectedAlertGroup.definitions.length', 'selectedAlertGroup.notifications.@each', 'selectedAlertGroup.notifications.length', 'alertGroups', 'isLoaded'),
  425. isDefsModified: function () {
  426. var modifiedGroups = this.get('defsModifiedAlertGroups');
  427. if (!this.get('isLoaded')) {
  428. return false;
  429. }
  430. return !!(modifiedGroups.toSet.length || modifiedGroups.toCreate.length || modifiedGroups.toDelete.length);
  431. }.property('defsModifiedAlertGroups'),
  432. /**
  433. * copy alert groups for backup, to compare with current alert groups, so will know if some groups changed/added/deleted
  434. * @param originGroups
  435. * @return {Array}
  436. */
  437. copyAlertGroups: function (originGroups) {
  438. var alertGroups = [];
  439. originGroups.forEach(function (alertGroup) {
  440. var copiedGroup = Em.Object.create($.extend(true, {}, alertGroup));
  441. alertGroups.pushObject(copiedGroup);
  442. });
  443. return alertGroups;
  444. },
  445. /**
  446. * ==============on API side: following are four functions to an alert group: create, delete, update definitions/notificationss/label of a group ===========
  447. */
  448. /**
  449. * Create a new alert group
  450. * @param newAlertGroupData
  451. * @param callback Callback function for Success or Error handling
  452. * @return Returns the created alert group
  453. */
  454. postNewAlertGroup: function (newAlertGroupData, callback) {
  455. // create a new group with name , definition and notifications
  456. var data = {
  457. 'name': newAlertGroupData.get('name')
  458. };
  459. if (newAlertGroupData.get('definitions').length > 0) {
  460. data.definitions = newAlertGroupData.get('definitions').mapProperty('id');
  461. }
  462. if (newAlertGroupData.get('notifications').length > 0) {
  463. data.targets = newAlertGroupData.get('notifications').mapProperty('id');
  464. }
  465. var sendData = {
  466. name: 'alert_groups.create',
  467. data: data,
  468. success: 'successFunction',
  469. error: 'errorFunction',
  470. successFunction: function () {
  471. if (callback) {
  472. callback();
  473. }
  474. },
  475. errorFunction: function (xhr, text, errorThrown) {
  476. if (callback) {
  477. callback(xhr, text, errorThrown);
  478. }
  479. console.error('Error in creating new Alert Group');
  480. }
  481. };
  482. sendData.sender = sendData;
  483. App.ajax.send(sendData);
  484. return newAlertGroupData;
  485. },
  486. /**
  487. * PUTs the new alert group information on the server.
  488. * Changes possible here are the name, definitions, notifications
  489. *
  490. * @param {App.AlertGroup} alertGroup
  491. * @param {Function} successCallback
  492. * @param {Function} errorCallback
  493. */
  494. updateAlertGroup: function (alertGroup, successCallback, errorCallback) {
  495. var sendData = {
  496. name: 'alert_groups.update',
  497. data: {
  498. "group_id": alertGroup.id,
  499. 'name': alertGroup.get('name'),
  500. 'definitions': alertGroup.get('definitions').mapProperty('id'),
  501. 'targets': alertGroup.get('notifications').mapProperty('id')
  502. },
  503. success: 'successFunction',
  504. error: 'errorFunction',
  505. successFunction: function () {
  506. if (successCallback) {
  507. successCallback();
  508. }
  509. },
  510. errorFunction: function (xhr, text, errorThrown) {
  511. if (errorCallback) {
  512. errorCallback(xhr, text, errorThrown);
  513. }
  514. }
  515. };
  516. sendData.sender = sendData;
  517. App.ajax.send(sendData);
  518. },
  519. removeAlertGroup: function (alertGroup, successCallback, errorCallback) {
  520. var sendData = {
  521. name: 'alert_groups.delete',
  522. data: {
  523. "group_id": alertGroup.id
  524. },
  525. success: 'successFunction',
  526. error: 'errorFunction',
  527. successFunction: function () {
  528. if (successCallback) {
  529. successCallback();
  530. }
  531. },
  532. errorFunction: function (xhr, text, errorThrown) {
  533. if (errorCallback) {
  534. errorCallback(xhr, text, errorThrown);
  535. }
  536. }
  537. };
  538. sendData.sender = sendData;
  539. App.ajax.send(sendData);
  540. },
  541. /**
  542. * =============on UI side: following are four operations to an alert group: add, remove, rename and duplicate==================
  543. */
  544. /**
  545. * confirm delete alert group
  546. */
  547. confirmDelete : function () {
  548. if (this.get('isRemoveButtonDisabled')) return;
  549. var self = this;
  550. App.showConfirmationPopup(function() {
  551. self.deleteAlertGroup();
  552. });
  553. },
  554. /**
  555. * delete selected alert group
  556. */
  557. deleteAlertGroup: function () {
  558. var selectedAlertGroup = this.get('selectedAlertGroup');
  559. if (this.get('isDeleteAlertDisabled')) {
  560. return;
  561. }
  562. this.get('alertGroups').removeObject(selectedAlertGroup);
  563. this.set('selectedAlertGroup', this.get('alertGroups')[0]);
  564. },
  565. /**
  566. * rename non-default alert group
  567. */
  568. renameAlertGroup: function () {
  569. if(this.get('selectedAlertGroup.default')) {
  570. return;
  571. }
  572. var self = this;
  573. this.renameGroupPopup = App.ModalPopup.show({
  574. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.renameButton'),
  575. bodyClass: Ember.View.extend({
  576. templateName: require('templates/main/alerts/create_new_alert_group')
  577. }),
  578. alertGroupName: self.get('selectedAlertGroup.name'),
  579. warningMessage: null,
  580. validate: function () {
  581. var warningMessage = '';
  582. var originalGroup = self.get('selectedAlertGroup');
  583. var groupName = this.get('alertGroupName').trim();
  584. if (originalGroup.get('name').trim() === groupName) {
  585. warningMessage = Em.I18n.t("alerts.actions.manage_alert_groups_popup.addGroup.exist");
  586. } else {
  587. if (self.get('alertGroups').mapProperty('displayName').contains(groupName)) {
  588. warningMessage = Em.I18n.t("alerts.actions.manage_alert_groups_popup.addGroup.exist");
  589. }
  590. else if (groupName && !validator.isValidAlertGroupName(groupName)) {
  591. warningMessage = Em.I18n.t("form.validator.alertGroupName");
  592. }
  593. }
  594. this.set('warningMessage', warningMessage);
  595. }.observes('alertGroupName'),
  596. disablePrimary: function () {
  597. return !(this.get('alertGroupName').trim().length > 0 && (this.get('warningMessage') !== null && !this.get('warningMessage')));
  598. }.property('warningMessage', 'alertGroupName'),
  599. onPrimary: function () {
  600. self.set('selectedAlertGroup.name', this.get('alertGroupName'));
  601. this.hide();
  602. }
  603. });
  604. },
  605. /**
  606. * create new alert group
  607. */
  608. addAlertGroup: function (duplicated) {
  609. duplicated = (duplicated === true);
  610. var self = this;
  611. this.addGroupPopup = App.ModalPopup.show({
  612. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.addButton'),
  613. bodyClass: Ember.View.extend({
  614. templateName: require('templates/main/alerts/create_new_alert_group')
  615. }),
  616. alertGroupName: duplicated ? self.get('selectedAlertGroup.name') + ' Copy' : "",
  617. warningMessage: '',
  618. didInsertElement: function(){
  619. this.validate();
  620. this.$('input').focus();
  621. },
  622. validate: function () {
  623. var warningMessage = '';
  624. var groupName = this.get('alertGroupName').trim();
  625. if (self.get('alertGroups').mapProperty('displayName').contains(groupName)) {
  626. warningMessage = Em.I18n.t("alerts.actions.manage_alert_groups_popup.addGroup.exist");
  627. }
  628. else if (groupName && !validator.isValidAlertGroupName(groupName)) {
  629. warningMessage = Em.I18n.t("form.validator.alertGroupName");
  630. }
  631. this.set('warningMessage', warningMessage);
  632. }.observes('alertGroupName'),
  633. disablePrimary: function () {
  634. return !(this.get('alertGroupName').trim().length > 0 && !this.get('warningMessage'));
  635. }.property('warningMessage', 'alertGroupName'),
  636. onPrimary: function () {
  637. var newAlertGroup = Em.Object.create({
  638. name: this.get('alertGroupName').trim(),
  639. default: false,
  640. displayName: function () {
  641. var name = this.get('name');
  642. if (name && name.length > App.config.CONFIG_GROUP_NAME_MAX_LENGTH) {
  643. var middle = Math.floor(App.config.CONFIG_GROUP_NAME_MAX_LENGTH / 2);
  644. name = name.substring(0, middle) + "..." + name.substring(name.length - middle);
  645. }
  646. return this.get('default') ? (name + ' Default') : name;
  647. }.property('name', 'default'),
  648. label: function () {
  649. return this.get('displayName') + ' (' + this.get('definitions.length') + ')';
  650. }.property('displayName', 'definitions.length'),
  651. definitions: [],
  652. notifications: self.get('alertGlobalNotifications'),
  653. isAddDefinitionsDisabled: false
  654. });
  655. self.get('alertGroups').pushObject(newAlertGroup);
  656. self.set('selectedAlertGroup', newAlertGroup);
  657. this.hide();
  658. }
  659. });
  660. },
  661. duplicateAlertGroup: function() {
  662. this.addAlertGroup(true);
  663. }
  664. });