manage_alert_groups_controller.js 24 KB

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