manage_alert_groups_controller.js 23 KB

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