manage_alert_groups_controller.js 24 KB

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