manage_alert_groups_controller.js 23 KB

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