manage_alert_groups_controller.js 24 KB

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