manage_alert_groups_controller.js 23 KB

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