manage_alert_groups_controller.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 = this.get('name');
  230. if (name && name.length > App.config.CONFIG_GROUP_NAME_MAX_LENGTH) {
  231. var middle = Math.floor(App.config.CONFIG_GROUP_NAME_MAX_LENGTH / 2);
  232. name = name.substring(0, middle) + "..." + name.substring(name.length - middle);
  233. }
  234. return this.get('default') ? (name + ' Default') : name;
  235. }.property('name', 'default'),
  236. label: function () {
  237. return this.get('displayName') + ' (' + this.get('definitions.length') + ')';
  238. }.property('displayName', 'definitions.length'),
  239. definitions: definitions,
  240. isAddDefinitionsDisabled: group.get('isAddDefinitionsDisabled'),
  241. notifications: targets
  242. });
  243. });
  244. this.setProperties({
  245. alertGroups: alertGroups,
  246. isLoaded: true,
  247. originalAlertGroups: this.copyAlertGroups(alertGroups),
  248. selectedAlertGroup: this.get('alertGroups')[0]
  249. });
  250. },
  251. /**
  252. * Enable/disable "Remove"/"Rename"/"Duplicate" buttons basing on <code>controller.selectedAlertGroup</code>
  253. * @method buttonObserver
  254. */
  255. buttonObserver: function () {
  256. var selectedAlertGroup = this.get('selectedAlertGroup');
  257. var flag = selectedAlertGroup && selectedAlertGroup.get('default');
  258. this.setProperties({
  259. isRemoveButtonDisabled: flag,
  260. isRenameButtonDisabled: flag,
  261. isDuplicateButtonDisabled: false
  262. });
  263. }.observes('selectedAlertGroup'),
  264. /**
  265. * @method resortAlertGroup
  266. */
  267. resortAlertGroup: function () {
  268. var alertGroups = Em.copy(this.get('alertGroups'));
  269. if (alertGroups.length < 2) {
  270. return;
  271. }
  272. var defaultGroups = alertGroups.filterProperty('default');
  273. defaultGroups.forEach(function (defaultGroup) {
  274. alertGroups.removeObject(defaultGroup);
  275. });
  276. var sorted = defaultGroups.sortProperty('name').concat(alertGroups.sortProperty('name'));
  277. this.removeObserver('alertGroups.@each.name', this, 'resortAlertGroup');
  278. this.set('alertGroups', sorted);
  279. this.addObserver('alertGroups.@each.name', this, 'resortAlertGroup');
  280. }.observes('alertGroups.@each.name'),
  281. /**
  282. * remove definitions from group
  283. * @method deleteDefinitions
  284. */
  285. deleteDefinitions: function () {
  286. if (this.get('isDeleteDefinitionsDisabled')) {
  287. return;
  288. }
  289. var groupDefinitions = this.get('selectedAlertGroup.definitions');
  290. this.get('selectedDefinitions').slice().forEach(function (defObj) {
  291. groupDefinitions.removeObject(defObj);
  292. }, this);
  293. this.set('selectedDefinitions', []);
  294. },
  295. /**
  296. * Provides alert definitions which are available for inclusion in
  297. * non-default alert groups.
  298. * @param {App.AlertGroup} selectedAlertGroup
  299. * @method getAvailableDefinitions
  300. * @return {{name: string, serviceName: string, componentName: string, serviceNameDisplay: string, componentNameDisplay: string, label: string, id: number}[]}
  301. */
  302. getAvailableDefinitions: function (selectedAlertGroup) {
  303. if (selectedAlertGroup.get('default')) return [];
  304. var usedDefinitionsMap = {};
  305. var availableDefinitions = [];
  306. var sharedDefinitions = App.AlertDefinition.find();
  307. usedDefinitionsMap = selectedAlertGroup.get('definitions').toWickMapByProperty('name');
  308. selectedAlertGroup.get('definitions').forEach(function (def) {
  309. usedDefinitionsMap[def.name] = true;
  310. });
  311. sharedDefinitions.forEach(function (shared_def) {
  312. if (!usedDefinitionsMap[shared_def.get('name')]) {
  313. availableDefinitions.pushObject(shared_def);
  314. }
  315. });
  316. return availableDefinitions.map(function (def) {
  317. return Em.Object.create({
  318. name: def.get('name'),
  319. serviceName: def.get('serviceName'),
  320. componentName: def.get('componentName'),
  321. serviceNameDisplay: def.get('service.displayName'),
  322. componentNameDisplay: def.get('componentNameFormatted'),
  323. label: def.get('label'),
  324. id: def.get('id')
  325. });
  326. });
  327. },
  328. /**
  329. * add alert definitions to a group
  330. * @method addDefinitions
  331. */
  332. addDefinitions: function () {
  333. if (this.get('selectedAlertGroup.isAddDefinitionsDisabled')) {
  334. return false;
  335. }
  336. var availableDefinitions = this.getAvailableDefinitions(this.get('selectedAlertGroup'));
  337. var popupDescription = {
  338. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.title'),
  339. dialogMessage: Em.I18n.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.message').format(this.get('selectedAlertGroup.displayName'))
  340. };
  341. var validComponents = App.StackServiceComponent.find().map(function (component) {
  342. return Em.Object.create({
  343. componentName: component.get('componentName'),
  344. displayName: App.format.role(component.get('componentName')),
  345. selected: false
  346. });
  347. });
  348. var validServices = App.Service.find().map(function (service) {
  349. return Em.Object.create({
  350. serviceName: service.get('serviceName'),
  351. displayName: App.format.role(service.get('serviceName')),
  352. selected: false
  353. });
  354. });
  355. this.launchDefsSelectionDialog(availableDefinitions, [], validServices, validComponents, this.addDefinitionsCallback.bind(this), popupDescription);
  356. },
  357. /**
  358. * Launch a table view of all available definitions to choose
  359. * @method launchDefsSelectionDialog
  360. * @return {App.ModalPopup}
  361. */
  362. launchDefsSelectionDialog: function (initialDefs, selectedDefs, validServices, validComponents, callback, popupDescription) {
  363. return App.ModalPopup.show({
  364. classNames: [ 'sixty-percent-width-modal' ],
  365. header: popupDescription.header,
  366. /**
  367. * @type {string}
  368. */
  369. dialogMessage: popupDescription.dialogMessage,
  370. /**
  371. * @type {string|null}
  372. */
  373. warningMessage: null,
  374. /**
  375. * @type {App.AlertDefinition[]}
  376. */
  377. availableDefs: [],
  378. onPrimary: function () {
  379. this.set('warningMessage', null);
  380. var arrayOfSelectedDefs = this.get('availableDefs').filterProperty('selected', true);
  381. if (arrayOfSelectedDefs.length < 1) {
  382. this.set('warningMessage', Em.I18n.t('alerts.actions.manage_alert_groups_popup.selectDefsDialog.message.warning'));
  383. return;
  384. }
  385. callback(arrayOfSelectedDefs);
  386. this.hide();
  387. },
  388. /**
  389. * Primary button should be disabled while alert definitions are not loaded
  390. * @type {boolean}
  391. */
  392. disablePrimary: Em.computed.not('isLoaded'),
  393. onSecondary: function () {
  394. callback(null);
  395. this.hide();
  396. },
  397. bodyClass: App.SelectDefinitionsPopupBodyView.extend({
  398. filterComponents: validComponents,
  399. filterServices: validServices,
  400. initialDefs: initialDefs
  401. })
  402. });
  403. },
  404. /**
  405. * add alert definitions callback
  406. * @method addDefinitionsCallback
  407. */
  408. addDefinitionsCallback: function (selectedDefs) {
  409. var group = this.get('selectedAlertGroup');
  410. if (selectedDefs) {
  411. group.get('definitions').pushObjects(selectedDefs);
  412. }
  413. },
  414. /**
  415. * copy alert groups for backup, to compare with current alert groups, so will know if some groups changed/added/deleted
  416. * @param {App.AlertGroup[]} originGroups
  417. * @return {App.AlertGroup[]}
  418. * @method copyAlertGroups
  419. */
  420. copyAlertGroups: function (originGroups) {
  421. var alertGroups = [];
  422. originGroups.forEach(function (alertGroup) {
  423. var copiedGroup = Em.Object.create($.extend(true, {}, alertGroup));
  424. alertGroups.pushObject(copiedGroup);
  425. });
  426. return alertGroups;
  427. },
  428. /**
  429. * Create a new alert group
  430. * @param {Em.Object} newAlertGroupData
  431. * @param {callback} callback Callback function for Success or Error handling
  432. * @return {App.AlertGroup} Returns the created alert group
  433. * @method postNewAlertGroup
  434. */
  435. postNewAlertGroup: function (newAlertGroupData, callback) {
  436. // create a new group with name , definition and notifications
  437. var data = {
  438. 'name': newAlertGroupData.get('name')
  439. };
  440. if (newAlertGroupData.get('definitions').length > 0) {
  441. data.definitions = newAlertGroupData.get('definitions').mapProperty('id');
  442. }
  443. if (newAlertGroupData.get('notifications').length > 0) {
  444. data.targets = newAlertGroupData.get('notifications').mapProperty('id');
  445. }
  446. var sendData = {
  447. name: 'alert_groups.create',
  448. data: data,
  449. success: 'successFunction',
  450. error: 'errorFunction',
  451. successFunction: function () {
  452. if (callback) {
  453. callback();
  454. }
  455. },
  456. errorFunction: function (xhr, text, errorThrown) {
  457. if (callback) {
  458. callback(xhr, text, errorThrown);
  459. }
  460. }
  461. };
  462. sendData.sender = sendData;
  463. App.ajax.send(sendData);
  464. return newAlertGroupData;
  465. },
  466. /**
  467. * PUTs the new alert group information on the server.
  468. * Changes possible here are the name, definitions, notifications
  469. *
  470. * @param {App.AlertGroup} alertGroup
  471. * @param {Function} successCallback
  472. * @param {Function} errorCallback
  473. * @method updateAlertGroup
  474. */
  475. updateAlertGroup: function (alertGroup, successCallback, errorCallback) {
  476. var sendData = {
  477. name: 'alert_groups.update',
  478. data: {
  479. "group_id": alertGroup.id,
  480. 'name': alertGroup.get('name'),
  481. 'definitions': alertGroup.get('definitions').mapProperty('id'),
  482. 'targets': alertGroup.get('notifications').mapProperty('id')
  483. },
  484. success: 'successFunction',
  485. error: 'errorFunction',
  486. successFunction: function () {
  487. if (successCallback) {
  488. successCallback();
  489. }
  490. },
  491. errorFunction: function (xhr, text, errorThrown) {
  492. if (errorCallback) {
  493. errorCallback(xhr, text, errorThrown);
  494. }
  495. }
  496. };
  497. sendData.sender = sendData;
  498. App.ajax.send(sendData);
  499. },
  500. /**
  501. * Request for deleting alert group
  502. * @param {App.AlertGroup} alertGroup
  503. * @param {callback} successCallback
  504. * @param {callback} errorCallback
  505. * @method removeAlertGroup
  506. */
  507. removeAlertGroup: function (alertGroup, successCallback, errorCallback) {
  508. var sendData = {
  509. name: 'alert_groups.delete',
  510. data: {
  511. "group_id": alertGroup.id
  512. },
  513. success: 'successFunction',
  514. error: 'errorFunction',
  515. successFunction: function () {
  516. if (successCallback) {
  517. successCallback();
  518. }
  519. },
  520. errorFunction: function (xhr, text, errorThrown) {
  521. if (errorCallback) {
  522. errorCallback(xhr, text, errorThrown);
  523. }
  524. }
  525. };
  526. sendData.sender = sendData;
  527. App.ajax.send(sendData);
  528. },
  529. /**
  530. * confirm delete alert group
  531. * @method confirmDelete
  532. */
  533. confirmDelete: function () {
  534. if (this.get('isRemoveButtonDisabled')) return;
  535. var self = this;
  536. App.showConfirmationPopup(function () {
  537. self.deleteAlertGroup();
  538. });
  539. },
  540. /**
  541. * delete selected alert group
  542. * @method deleteAlertGroup
  543. */
  544. deleteAlertGroup: function () {
  545. var selectedAlertGroup = this.get('selectedAlertGroup');
  546. if (this.get('isDeleteAlertDisabled')) {
  547. return;
  548. }
  549. this.get('alertGroups').removeObject(selectedAlertGroup);
  550. this.set('selectedAlertGroup', this.get('alertGroups')[0]);
  551. },
  552. /**
  553. * Rename non-default alert group
  554. * @method renameAlertGroup
  555. */
  556. renameAlertGroup: function () {
  557. if (this.get('selectedAlertGroup.default')) {
  558. return;
  559. }
  560. var self = this;
  561. var popup;
  562. popup = App.ModalPopup.show({
  563. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.renameButton'),
  564. bodyClass: Ember.View.extend({
  565. templateName: require('templates/main/alerts/create_new_alert_group')
  566. }),
  567. /**
  568. * @type {string}
  569. */
  570. alertGroupName: self.get('selectedAlertGroup.name'),
  571. /**
  572. * @type {string|null}
  573. */
  574. warningMessage: null,
  575. /**
  576. * New group name should be unique and valid
  577. * @method validate
  578. */
  579. validate: function () {
  580. var warningMessage = '';
  581. var originalGroup = self.get('selectedAlertGroup');
  582. var groupName = this.get('alertGroupName').trim();
  583. if (originalGroup.get('name').trim() === groupName) {
  584. warningMessage = Em.I18n.t("alerts.actions.manage_alert_groups_popup.addGroup.exist");
  585. }
  586. else {
  587. if (self.get('alertGroups').mapProperty('displayName').contains(groupName)) {
  588. warningMessage = Em.I18n.t("alerts.actions.manage_alert_groups_popup.addGroup.exist");
  589. }
  590. else {
  591. if (groupName && !validator.isValidAlertGroupName(groupName)) {
  592. warningMessage = Em.I18n.t("form.validator.alertGroupName");
  593. }
  594. }
  595. }
  596. this.set('warningMessage', warningMessage);
  597. }.observes('alertGroupName'),
  598. /**
  599. * Primary button is disabled while user doesn't input valid group name
  600. * @type {boolean}
  601. */
  602. disablePrimary: function () {
  603. return !(this.get('alertGroupName').trim().length > 0 && (this.get('warningMessage') !== null && !this.get('warningMessage')));
  604. }.property('warningMessage', 'alertGroupName'),
  605. onPrimary: function () {
  606. self.set('selectedAlertGroup.name', this.get('alertGroupName'));
  607. this.hide();
  608. }
  609. });
  610. this.set('renameGroupPopup', popup);
  611. },
  612. /**
  613. * Create new alert group
  614. * @param {boolean} duplicated is new group a copy of the existing group
  615. * @method addAlertGroup
  616. */
  617. addAlertGroup: function (duplicated) {
  618. duplicated = (duplicated === true);
  619. var self = this;
  620. var popup = App.ModalPopup.show({
  621. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.addButton'),
  622. bodyClass: Em.View.extend({
  623. templateName: require('templates/main/alerts/create_new_alert_group')
  624. }),
  625. /**
  626. * Name for new alert group
  627. * @type {string}
  628. */
  629. alertGroupName: duplicated ? self.get('selectedAlertGroup.name') + ' Copy' : "",
  630. /**
  631. * @type {string}
  632. */
  633. warningMessage: '',
  634. didInsertElement: function () {
  635. this._super();
  636. this.validate();
  637. },
  638. /**
  639. * alert group name should be unique and valid
  640. * @method validate
  641. */
  642. validate: function () {
  643. var warningMessage = '';
  644. var groupName = this.get('alertGroupName').trim();
  645. if (self.get('alertGroups').mapProperty('displayName').contains(groupName)) {
  646. warningMessage = Em.I18n.t("alerts.actions.manage_alert_groups_popup.addGroup.exist");
  647. }
  648. else {
  649. if (groupName && !validator.isValidAlertGroupName(groupName)) {
  650. warningMessage = Em.I18n.t("form.validator.alertGroupName");
  651. }
  652. }
  653. this.set('warningMessage', warningMessage);
  654. }.observes('alertGroupName'),
  655. /**
  656. * Primary button is disabled while user doesn't input valid group name
  657. * @type {boolean}
  658. */
  659. disablePrimary: function () {
  660. return !(this.get('alertGroupName').trim().length > 0 && !this.get('warningMessage'));
  661. }.property('warningMessage', 'alertGroupName'),
  662. onPrimary: function () {
  663. var newAlertGroup = Em.Object.create({
  664. name: this.get('alertGroupName').trim(),
  665. default: false,
  666. displayName: function () {
  667. var name = this.get('name');
  668. if (name && name.length > App.config.CONFIG_GROUP_NAME_MAX_LENGTH) {
  669. var middle = Math.floor(App.config.CONFIG_GROUP_NAME_MAX_LENGTH / 2);
  670. name = name.substring(0, middle) + "..." + name.substring(name.length - middle);
  671. }
  672. return this.get('default') ? (name + ' Default') : name;
  673. }.property('name', 'default'),
  674. label: function () {
  675. return this.get('displayName') + ' (' + this.get('definitions.length') + ')';
  676. }.property('displayName', 'definitions.length'),
  677. definitions: duplicated ? self.get('selectedAlertGroup.definitions').slice(0) : [],
  678. notifications: self.get('alertGlobalNotifications'),
  679. isAddDefinitionsDisabled: false
  680. });
  681. self.get('alertGroups').pushObject(newAlertGroup);
  682. self.set('selectedAlertGroup', newAlertGroup);
  683. this.hide();
  684. }
  685. });
  686. this.set('addGroupPopup', popup);
  687. },
  688. /**
  689. * @method duplicateAlertGroup
  690. */
  691. duplicateAlertGroup: function () {
  692. this.addAlertGroup(true);
  693. }
  694. });