manage_alert_notifications_controller.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. App.ManageAlertNotificationsController = Em.Controller.extend({
  21. name: 'manageAlertNotificationsController',
  22. /**
  23. * Are alert notifications loaded
  24. * @type {boolean}
  25. */
  26. isLoaded: false,
  27. /**
  28. * Create/Edit modal popup object
  29. * used to hide popup
  30. * @type {App.ModalPopup}
  31. */
  32. createEditPopup: null,
  33. /**
  34. * Map of edit inputs shown in Create/Edit Notification popup
  35. * @type {Object}
  36. */
  37. inputFields: Em.Object.create({
  38. name: {
  39. label: Em.I18n.t('common.name'),
  40. value: '',
  41. defaultValue: ''
  42. },
  43. groups: {
  44. label: Em.I18n.t('common.groups'),
  45. value: [],
  46. defaultValue: []
  47. },
  48. global: {
  49. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.global'),
  50. value: false,
  51. defaultValue: false,
  52. disabled: false
  53. },
  54. allGroups: Em.Object.create({
  55. value: '',
  56. defaultValue: 'custom',
  57. disabled: false,
  58. isAll: function () {
  59. return this.get('value') == 'all';
  60. }.property('value')
  61. }),
  62. method: {
  63. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.method'),
  64. value: '',
  65. defaultValue: ''
  66. },
  67. email: {
  68. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.email'),
  69. value: '',
  70. defaultValue: ''
  71. },
  72. SMTPServer: {
  73. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPServer'),
  74. value: '',
  75. defaultValue: ''
  76. },
  77. SMTPPort: {
  78. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPort'),
  79. value: '',
  80. defaultValue: ''
  81. },
  82. SMTPUseAuthentication: Em.Object.create({
  83. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPUseAuthentication'),
  84. value: false,
  85. defaultValue: false,
  86. inversedValue: function () {
  87. return !this.get('value');
  88. }.property('value')
  89. }),
  90. SMTPUsername: {
  91. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPUsername'),
  92. value: '',
  93. defaultValue: ''
  94. },
  95. SMTPPassword: {
  96. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPPassword'),
  97. value: '',
  98. defaultValue: ''
  99. },
  100. SMTPSTARTTLS: {
  101. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.SMTPSTARTTLS'),
  102. value: false,
  103. defaultValue: false
  104. },
  105. emailFrom: {
  106. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.emailFrom'),
  107. value: '',
  108. defaultValue: ''
  109. },
  110. version: {
  111. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.version'),
  112. value: '',
  113. defaultValue: ''
  114. },
  115. OIDs: {
  116. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.OIDs'),
  117. value: '',
  118. defaultValue: ''
  119. },
  120. community: {
  121. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.community'),
  122. value: '',
  123. defaultValue: ''
  124. },
  125. port: {
  126. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.port'),
  127. value: '',
  128. defaultValue: ''
  129. },
  130. severityFilter: {
  131. label: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.severityFilter'),
  132. value: [],
  133. defaultValue: []
  134. },
  135. description: {
  136. label: Em.I18n.t('common.description'),
  137. value: '',
  138. defaultValue: ''
  139. },
  140. customProperties: Em.A([])
  141. }),
  142. /**
  143. * List of available Notification types
  144. * used in Type combobox
  145. * @type {Array}
  146. */
  147. methods: ['EMAIL', 'SNMP'],
  148. /**
  149. * List of available value for Severity Filter
  150. * used in Severity Filter combobox
  151. * @type {Array}
  152. */
  153. severities: ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN'],
  154. /**
  155. * List of available SNMP versions
  156. * @type {Array}
  157. */
  158. SNMPVersions: ['SNMPv1', 'SNMPv2c', 'SNMPv3'],
  159. /**
  160. * List of all Alert Notifications
  161. * @type {App.AlertNotification[]}
  162. */
  163. alertNotifications: function () {
  164. return this.get('isLoaded') ? App.AlertNotification.find().toArray() : [];
  165. }.property('isLoaded'),
  166. /**
  167. * List of all Alert Groups
  168. * @type {App.AlertGroup[]}
  169. */
  170. allAlertGroups: function () {
  171. return this.get('isLoaded') ? App.AlertGroup.find().toArray() : [];
  172. }.property('isLoaded'),
  173. /**
  174. * Selected Alert Notification
  175. * @type {App.AlertNotification}
  176. */
  177. selectedAlertNotification: null,
  178. /**
  179. * Addable to <code>selectedAlertNotification.properties</code> custom property
  180. * @type {{name: string, value: string}}
  181. */
  182. newCustomProperty: {name: '', value: ''},
  183. /**
  184. * List custom property names that shouldn't be displayed on Edit page
  185. * @type {string[]}
  186. */
  187. ignoredCustomProperties: [
  188. 'ambari.dispatch.credential.password',
  189. 'ambari.dispatch.credential.username',
  190. 'ambari.dispatch.recipients',
  191. 'ambari.dispatch.snmp.community',
  192. 'ambari.dispatch.snmp.oids.trap',
  193. 'ambari.dispatch.snmp.port',
  194. 'ambari.dispatch.snmp.version',
  195. 'mail.smtp.auth',
  196. 'mail.smtp.from',
  197. 'mail.smtp.host',
  198. 'mail.smtp.port',
  199. 'mail.smtp.starttls.enable'
  200. ],
  201. /**
  202. * Load all Alert Notifications from server
  203. * @method loadAlertNotifications
  204. */
  205. loadAlertNotifications: function () {
  206. var self = this;
  207. this.set('isLoaded', false);
  208. App.router.get('updateController').updateAlertGroups(function () {
  209. App.ajax.send({
  210. name: 'alerts.notifications',
  211. sender: self,
  212. success: 'getAlertNotificationsSuccessCallback',
  213. error: 'getAlertNotificationsErrorCallback'
  214. });
  215. });
  216. },
  217. /**
  218. * Success-callback for load alert notifications request
  219. * @param {object} json
  220. * @method getAlertNotificationsSuccessCallback
  221. */
  222. getAlertNotificationsSuccessCallback: function (json) {
  223. App.alertNotificationMapper.map(json);
  224. this.set('isLoaded', true);
  225. },
  226. /**
  227. * Error-callback for load alert notifications request
  228. * @method getAlertNotificationsErrorCallback
  229. */
  230. getAlertNotificationsErrorCallback: function () {
  231. this.set('isLoaded', true);
  232. },
  233. /**
  234. * Add Notification button handler
  235. * @method addAlertNotification
  236. */
  237. addAlertNotification: function () {
  238. var inputFields = this.get('inputFields');
  239. inputFields.setProperties({
  240. 'global.disabled': false,
  241. 'allGroups.disabled': false
  242. });
  243. Em.keys(inputFields).forEach(function (key) {
  244. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  245. });
  246. inputFields.set('severityFilter.value', ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']);
  247. this.showCreateEditPopup(false);
  248. },
  249. /**
  250. * Edit Notification button handler
  251. * @method editAlertNotification
  252. */
  253. editAlertNotification: function () {
  254. this.fillEditCreateInputs();
  255. this.showCreateEditPopup(true);
  256. },
  257. /**
  258. * Fill inputs of Create/Edit popup form
  259. * @param addCopyToName define whether add 'Copy of ' to name
  260. * @method fillEditCreateInputs
  261. */
  262. fillEditCreateInputs: function (addCopyToName) {
  263. var inputFields = this.get('inputFields');
  264. var selectedAlertNotification = this.get('selectedAlertNotification');
  265. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  266. inputFields.set('groups.value', selectedAlertNotification.get('groups').toArray());
  267. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  268. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  269. inputFields.set('SMTPServer.value', selectedAlertNotification.get('properties')['mail.smtp.host']);
  270. inputFields.set('SMTPPort.value', selectedAlertNotification.get('properties')['mail.smtp.port']);
  271. inputFields.set('SMTPUseAuthentication.value', selectedAlertNotification.get('properties')['mail.smtp.auth']);
  272. inputFields.set('SMTPUsername.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.username']);
  273. inputFields.set('SMTPPassword.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.password']);
  274. inputFields.set('SMTPSTARTTLS.value', selectedAlertNotification.get('properties')['mail.smtp.starttls.enable']);
  275. inputFields.set('emailFrom.value', selectedAlertNotification.get('properties')['mail.smtp.from']);
  276. inputFields.set('version.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.version']);
  277. inputFields.set('OIDs.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.oids.trap']);
  278. inputFields.set('community.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.community']);
  279. inputFields.set('port.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.port']);
  280. inputFields.set('severityFilter.value', selectedAlertNotification.get('alertStates'));
  281. inputFields.set('global.value', selectedAlertNotification.get('global'));
  282. inputFields.set('allGroups.value', selectedAlertNotification.get('global') ? 'all' : 'custom');
  283. inputFields.set('allGroups.disabled', true);
  284. // not allow to edit global field
  285. inputFields.set('global.disabled', true);
  286. inputFields.set('description.value', selectedAlertNotification.get('description'));
  287. inputFields.set('method.value', selectedAlertNotification.get('type'));
  288. inputFields.get('customProperties').clear();
  289. var properties = selectedAlertNotification.get('properties');
  290. var ignoredCustomProperties = this.get('ignoredCustomProperties');
  291. Em.keys(properties).forEach(function (k) {
  292. if (ignoredCustomProperties.contains(k)) return;
  293. inputFields.get('customProperties').pushObject({
  294. name: k,
  295. value: properties[k],
  296. defaultValue: properties[k]
  297. });
  298. });
  299. },
  300. /**
  301. * Show Edit or Create Notification popup
  302. * @param {boolean} isEdit true - edit, false - create
  303. * @returns {App.ModalPopup}
  304. * @method showCreateEditPopup
  305. */
  306. showCreateEditPopup: function (isEdit) {
  307. var self = this;
  308. var createEditPopup = App.ModalPopup.show({
  309. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  310. classNames: ['create-edit-alert-notification-popup'],
  311. bodyClass: Em.View.extend({
  312. controller: this,
  313. templateName: require('templates/main/alerts/create_alert_notification'),
  314. didInsertElement: function () {
  315. App.tooltip($('.checkbox-tooltip'));
  316. this.nameValidation();
  317. this.emailToValidation();
  318. this.emailFromValidation();
  319. this.smtpPortValidation();
  320. this.portValidation();
  321. },
  322. isEmailMethodSelected: function () {
  323. return this.get('controller.inputFields.method.value') === 'EMAIL';
  324. }.property('controller.inputFields.method.value'),
  325. nameValidation: function () {
  326. this.set('parentView.hasErrors', !this.get('controller.inputFields.name.value').trim());
  327. }.observes('controller.inputFields.name.value'),
  328. emailToValidation: function () {
  329. var emailTo = this.get('controller.inputFields.email.value');
  330. if (emailTo && !validator.isValidEmail(emailTo)) {
  331. this.set('parentView.hasErrors', true);
  332. this.set('controller.inputFields.email.errorMsg', 'Must be a valid email address');
  333. } else {
  334. this.set('parentView.hasErrors', false);
  335. this.set('controller.inputFields.email.errorMsg', null);
  336. }
  337. }.observes('controller.inputFields.email.value'),
  338. emailFromValidation: function () {
  339. var emailFrom = this.get('controller.inputFields.emailFrom.value');
  340. if (emailFrom && !validator.isValidEmail(emailFrom)) {
  341. this.set('parentView.hasErrors', true);
  342. this.set('controller.inputFields.emailFrom.errorMsg', 'Must be a valid email address');
  343. } else {
  344. this.set('parentView.hasErrors', false);
  345. this.set('controller.inputFields.emailFrom.errorMsg', null);
  346. }
  347. }.observes('controller.inputFields.emailFrom.value'),
  348. smtpPortValidation: function () {
  349. var value = this.get('controller.inputFields.SMTPPort.value');
  350. if (value && (!validator.isValidInt(value) || value < 0)) {
  351. this.set('parentView.hasErrors', true);
  352. this.set('controller.inputFields.SMTPPort.errorMsg', 'Invalid! Please enter positive integer.');
  353. } else {
  354. this.set('parentView.hasErrors', false);
  355. this.set('controller.inputFields.SMTPPort.errorMsg', null);
  356. }
  357. }.observes('controller.inputFields.SMTPPort.value'),
  358. portValidation: function () {
  359. var value = this.get('controller.inputFields.port.value');
  360. if (value && (!validator.isValidInt(value) || value < 0)) {
  361. this.set('parentView.hasErrors', true);
  362. this.set('controller.inputFields.port.errorMsg', 'Invalid! Please enter positive integer.');
  363. } else {
  364. this.set('parentView.hasErrors', false);
  365. this.set('controller.inputFields.port.errorMsg', null);
  366. }
  367. }.observes('controller.inputFields.port.value'),
  368. groupsSelectView: Em.Select.extend({
  369. attributeBindings: ['disabled'],
  370. init: function () {
  371. this._super();
  372. this.set('parentView.groupSelect', this);
  373. }
  374. }),
  375. groupSelect: null,
  376. /**
  377. * Select all alert-groups if <code>allGroups.value</code> is 'custom'
  378. * @method selectAllGroups
  379. */
  380. selectAllGroups: function () {
  381. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  382. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  383. }
  384. },
  385. /**
  386. * Deselect all alert-groups if <code>allGroups.value</code> is 'custom'
  387. * @method clearAllGroups
  388. */
  389. clearAllGroups: function () {
  390. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  391. this.set('groupSelect.selection', []);
  392. }
  393. },
  394. severitySelectView: Em.Select.extend({
  395. init: function () {
  396. this._super();
  397. this.set('parentView.severitySelect', this);
  398. }
  399. }),
  400. severitySelect: null,
  401. /**
  402. * Determines if all alert-groups are selected
  403. * @type {boolean}
  404. */
  405. allGroupsSelected: function () {
  406. return this.get('groupSelect.selection.length') === this.get('groupSelect.content.length');
  407. }.property('groupSelect.selection.length', 'groupSelect.content.length', 'groupSelect.disabled'),
  408. /**
  409. * Determines if no one alert-group is selected
  410. * @type {boolean}
  411. */
  412. noneGroupsSelected: function () {
  413. return this.get('groupSelect.selection.length') === 0;
  414. }.property('groupSelect.selection.length', 'groupSelect.content.length', 'groupSelect.disabled'),
  415. /**
  416. * Determines if all severities are selected
  417. * @type {boolean}
  418. */
  419. allSeveritySelected: function () {
  420. return this.get('severitySelect.selection.length') === this.get('severitySelect.content.length');
  421. }.property('severitySelect.selection.length', 'severitySelect.content.length'),
  422. /**
  423. * Determines if no one severity is selected
  424. * @type {boolean}
  425. */
  426. noneSeveritySelected: function () {
  427. return this.get('severitySelect.selection.length') === 0;
  428. }.property('severitySelect.selection.length', 'severitySelect.content.length'),
  429. /**
  430. * Select all severities
  431. * @method selectAllSeverity
  432. */
  433. selectAllSeverity: function () {
  434. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  435. },
  436. /**
  437. * Deselect all severities
  438. * @method clearAllSeverity
  439. */
  440. clearAllSeverity: function () {
  441. this.set('severitySelect.selection', []);
  442. }
  443. }),
  444. isSaving: false,
  445. hasErrors: false,
  446. primary: Em.I18n.t('common.save'),
  447. disablePrimary: function () {
  448. return this.get('isSaving') || this.get('hasErrors');
  449. }.property('isSaving', 'hasErrors'),
  450. onPrimary: function () {
  451. this.set('isSaving', true);
  452. var apiObject = self.formatNotificationAPIObject();
  453. if (isEdit) {
  454. self.updateAlertNotification(apiObject);
  455. } else {
  456. self.createAlertNotification(apiObject);
  457. }
  458. }
  459. });
  460. this.set('createEditPopup', createEditPopup);
  461. return createEditPopup;
  462. },
  463. /**
  464. * Create API-formatted object from data populate by user
  465. * @returns {Object}
  466. * @method formatNotificationAPIObject
  467. */
  468. formatNotificationAPIObject: function () {
  469. var inputFields = this.get('inputFields');
  470. var properties = {};
  471. if (inputFields.get('method.value') === 'EMAIL') {
  472. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  473. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  474. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  475. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  476. properties['mail.smtp.auth'] = inputFields.get('SMTPUseAuthentication.value');
  477. if (inputFields.get('SMTPUseAuthentication.value')) {
  478. properties['ambari.dispatch.credential.username'] = inputFields.get('SMTPUsername.value');
  479. properties['ambari.dispatch.credential.password'] = inputFields.get('SMTPPassword.value');
  480. properties['mail.smtp.starttls.enable'] = inputFields.get('SMTPSTARTTLS.value');
  481. }
  482. } else {
  483. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  484. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  485. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  486. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  487. }
  488. inputFields.get('customProperties').forEach(function (customProperty) {
  489. properties[customProperty.name] = customProperty.value;
  490. });
  491. var apiObject = {
  492. AlertTarget: {
  493. name: inputFields.get('name.value'),
  494. description: inputFields.get('description.value'),
  495. global: inputFields.get('allGroups.value') === 'all',
  496. notification_type: inputFields.get('method.value'),
  497. alert_states: inputFields.get('severityFilter.value'),
  498. properties: properties
  499. }
  500. };
  501. if (inputFields.get('allGroups.value') == 'custom') {
  502. apiObject.AlertTarget.groups = inputFields.get('groups.value').mapProperty('id');
  503. }
  504. return apiObject;
  505. },
  506. /**
  507. * Send request to server to create Alert Notification
  508. * @param {object} apiObject (@see formatNotificationAPIObject)
  509. * @returns {$.ajax}
  510. * @method createAlertNotification
  511. */
  512. createAlertNotification: function (apiObject) {
  513. return App.ajax.send({
  514. name: 'alerts.create_alert_notification',
  515. sender: this,
  516. data: {
  517. data: apiObject
  518. },
  519. success: 'createAlertNotificationSuccessCallback',
  520. error: 'saveErrorCallback'
  521. });
  522. },
  523. /**
  524. * Success callback for <code>createAlertNotification</code>
  525. * @method createAlertNotificationSuccessCallback
  526. */
  527. createAlertNotificationSuccessCallback: function () {
  528. this.loadAlertNotifications();
  529. var createEditPopup = this.get('createEditPopup');
  530. if (createEditPopup) {
  531. createEditPopup.hide();
  532. }
  533. },
  534. /**
  535. * Send request to server to update Alert Notification
  536. * @param {object} apiObject (@see formatNotificationAPIObject)
  537. * @returns {$.ajax}
  538. * @method updateAlertNotification
  539. */
  540. updateAlertNotification: function (apiObject) {
  541. return App.ajax.send({
  542. name: 'alerts.update_alert_notification',
  543. sender: this,
  544. data: {
  545. data: apiObject,
  546. id: this.get('selectedAlertNotification.id')
  547. },
  548. success: 'updateAlertNotificationSuccessCallback',
  549. error: 'saveErrorCallback'
  550. });
  551. },
  552. /**
  553. * Success callback for <code>updateAlertNotification</code>
  554. * @method updateAlertNotificationSuccessCallback
  555. */
  556. updateAlertNotificationSuccessCallback: function () {
  557. this.loadAlertNotifications();
  558. var createEditPopup = this.get('createEditPopup');
  559. if (createEditPopup) {
  560. createEditPopup.hide();
  561. }
  562. },
  563. /**
  564. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  565. * @method saveErrorCallback
  566. */
  567. saveErrorCallback: function () {
  568. this.set('createEditPopup.isSaving', false);
  569. },
  570. /**
  571. * Delete Notification button handler
  572. * @return {App.ModalPopup}
  573. * @method deleteAlertNotification
  574. */
  575. deleteAlertNotification: function () {
  576. var self = this;
  577. return App.showConfirmationPopup(function () {
  578. App.ajax.send({
  579. name: 'alerts.delete_alert_notification',
  580. sender: self,
  581. data: {
  582. id: self.get('selectedAlertNotification.id')
  583. },
  584. success: 'deleteAlertNotificationSuccessCallback'
  585. });
  586. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  587. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  588. },
  589. /**
  590. * Success callback for <code>deleteAlertNotification</code>
  591. * @method deleteAlertNotificationSuccessCallback
  592. */
  593. deleteAlertNotificationSuccessCallback: function () {
  594. this.loadAlertNotifications();
  595. var selectedAlertNotification = this.get('selectedAlertNotification');
  596. selectedAlertNotification.deleteRecord();
  597. this.set('selectedAlertNotification', null);
  598. },
  599. /**
  600. * Duplicate Notification button handler
  601. * @method duplicateAlertNotification
  602. */
  603. duplicateAlertNotification: function () {
  604. this.fillEditCreateInputs(true);
  605. this.showCreateEditPopup();
  606. },
  607. /**
  608. * Show popup with form for new custom property
  609. * @method addCustomPropertyHandler
  610. * @return {App.ModalPopup}
  611. */
  612. addCustomPropertyHandler: function () {
  613. var self = this;
  614. return App.ModalPopup.show({
  615. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  616. primary: Em.I18n.t('common.add'),
  617. bodyClass: Em.View.extend({
  618. /**
  619. * If some error with new custom property
  620. * @type {boolean}
  621. */
  622. isError: false,
  623. controller: this,
  624. /**
  625. * Error message for new custom property (invalid name, existed name etc)
  626. * @type {string}
  627. */
  628. errorMessage: '',
  629. /**
  630. * Check new custom property for errors with its name
  631. * @method errorHandler
  632. */
  633. errorsHandler: function () {
  634. var name = this.get('controller.newCustomProperty.name');
  635. var flag = validator.isValidConfigKey(name);
  636. if (flag) {
  637. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  638. this.get('controller.ignoredCustomProperties').contains(name)) {
  639. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  640. flag = false;
  641. }
  642. }
  643. else {
  644. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  645. }
  646. this.set('isError', !flag);
  647. this.set('parentView.disablePrimary', !flag);
  648. }.observes('controller.newCustomProperty.name'),
  649. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  650. }),
  651. disablePrimary: true,
  652. onPrimary: function () {
  653. self.addCustomProperty();
  654. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  655. this.hide();
  656. }
  657. });
  658. },
  659. /**
  660. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  661. * @method addCustomProperty
  662. */
  663. addCustomProperty: function () {
  664. var newCustomProperty = this.get('newCustomProperty');
  665. this.get('inputFields.customProperties').pushObject({
  666. name: newCustomProperty.name,
  667. value: newCustomProperty.value,
  668. defaultValue: newCustomProperty.value
  669. });
  670. },
  671. /**
  672. * "Remove"-button click handler
  673. * Delete customProperty from <code>inputFields.customProperties</code>
  674. * @param {object} e
  675. * @method removeCustomProperty
  676. */
  677. removeCustomPropertyHandler: function (e) {
  678. var customProperties = this.get('inputFields.customProperties');
  679. this.set('inputFields.customProperties', customProperties.without(e.context));
  680. }
  681. });