manage_alert_groups_controller.js 24 KB

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