manage_alert_notifications_controller.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  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. });
  242. Em.keys(inputFields).forEach(function (key) {
  243. inputFields.set(key + '.value', inputFields.get(key + '.defaultValue'));
  244. });
  245. inputFields.set('severityFilter.value', ['OK', 'WARNING', 'CRITICAL', 'UNKNOWN']);
  246. this.showCreateEditPopup(false);
  247. },
  248. /**
  249. * Edit Notification button handler
  250. * @method editAlertNotification
  251. */
  252. editAlertNotification: function () {
  253. this.fillEditCreateInputs();
  254. this.showCreateEditPopup(true);
  255. },
  256. /**
  257. * Fill inputs of Create/Edit popup form
  258. * @param addCopyToName define whether add 'Copy of ' to name
  259. * @method fillEditCreateInputs
  260. */
  261. fillEditCreateInputs: function (addCopyToName) {
  262. var inputFields = this.get('inputFields');
  263. var selectedAlertNotification = this.get('selectedAlertNotification');
  264. inputFields.set('name.value', (addCopyToName ? 'Copy of ' : '') + selectedAlertNotification.get('name'));
  265. inputFields.set('groups.value', selectedAlertNotification.get('groups').toArray());
  266. inputFields.set('email.value', selectedAlertNotification.get('properties')['ambari.dispatch.recipients'] ?
  267. selectedAlertNotification.get('properties')['ambari.dispatch.recipients'].join(', ') : '');
  268. inputFields.set('SMTPServer.value', selectedAlertNotification.get('properties')['mail.smtp.host']);
  269. inputFields.set('SMTPPort.value', selectedAlertNotification.get('properties')['mail.smtp.port']);
  270. inputFields.set('SMTPUseAuthentication.value', selectedAlertNotification.get('properties')['mail.smtp.auth']);
  271. inputFields.set('SMTPUsername.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.username']);
  272. inputFields.set('SMTPPassword.value', selectedAlertNotification.get('properties')['ambari.dispatch.credential.password']);
  273. inputFields.set('SMTPSTARTTLS.value', selectedAlertNotification.get('properties')['mail.smtp.starttls.enable']);
  274. inputFields.set('emailFrom.value', selectedAlertNotification.get('properties')['mail.smtp.from']);
  275. inputFields.set('version.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.version']);
  276. inputFields.set('OIDs.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.oids.trap']);
  277. inputFields.set('community.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.community']);
  278. inputFields.set('port.value', selectedAlertNotification.get('properties')['ambari.dispatch.snmp.port']);
  279. inputFields.set('severityFilter.value', selectedAlertNotification.get('alertStates'));
  280. inputFields.set('global.value', selectedAlertNotification.get('global'));
  281. inputFields.set('allGroups.value', selectedAlertNotification.get('global') ? 'all' : 'custom');
  282. // not allow to edit global field
  283. inputFields.set('global.disabled', true);
  284. inputFields.set('description.value', selectedAlertNotification.get('description'));
  285. inputFields.set('method.value', selectedAlertNotification.get('type'));
  286. inputFields.get('customProperties').clear();
  287. var properties = selectedAlertNotification.get('properties');
  288. var ignoredCustomProperties = this.get('ignoredCustomProperties');
  289. Em.keys(properties).forEach(function (k) {
  290. if (ignoredCustomProperties.contains(k)) return;
  291. inputFields.get('customProperties').pushObject({
  292. name: k,
  293. value: properties[k],
  294. defaultValue: properties[k]
  295. });
  296. });
  297. },
  298. /**
  299. * Show Edit or Create Notification popup
  300. * @param {boolean} isEdit true - edit, false - create
  301. * @returns {App.ModalPopup}
  302. * @method showCreateEditPopup
  303. */
  304. showCreateEditPopup: function (isEdit) {
  305. var self = this;
  306. var createEditPopup = App.ModalPopup.show({
  307. header: isEdit ? Em.I18n.t('alerts.actions.manage_alert_notifications_popup.editHeader') : Em.I18n.t('alerts.actions.manage_alert_notifications_popup.addHeader'),
  308. classNames: ['create-edit-alert-notification-popup'],
  309. bodyClass: Em.View.extend({
  310. controller: this,
  311. templateName: require('templates/main/alerts/create_alert_notification'),
  312. didInsertElement: function () {
  313. App.tooltip($('.checkbox-tooltip'));
  314. this.nameValidation();
  315. this.emailToValidation();
  316. this.emailFromValidation();
  317. this.smtpPortValidation();
  318. this.portValidation();
  319. },
  320. isEmailMethodSelected: function () {
  321. return this.get('controller.inputFields.method.value') === 'EMAIL';
  322. }.property('controller.inputFields.method.value'),
  323. nameValidation: function () {
  324. this.set('parentView.hasErrors', !this.get('controller.inputFields.name.value').trim());
  325. }.observes('controller.inputFields.name.value'),
  326. emailToValidation: function () {
  327. var emailTo = this.get('controller.inputFields.email.value');
  328. if (emailTo && !validator.isValidEmail(emailTo)) {
  329. this.set('parentView.hasErrors', true);
  330. this.set('controller.inputFields.email.errorMsg', Em.I18n.t('alerts.notifications.error.email'));
  331. } else {
  332. this.set('parentView.hasErrors', false);
  333. this.set('controller.inputFields.email.errorMsg', null);
  334. }
  335. }.observes('controller.inputFields.email.value'),
  336. emailFromValidation: function () {
  337. var emailFrom = this.get('controller.inputFields.emailFrom.value');
  338. if (emailFrom && !validator.isValidEmail(emailFrom)) {
  339. this.set('parentView.hasErrors', true);
  340. this.set('controller.inputFields.emailFrom.errorMsg', Em.I18n.t('alerts.notifications.error.email'));
  341. } else {
  342. this.set('parentView.hasErrors', false);
  343. this.set('controller.inputFields.emailFrom.errorMsg', null);
  344. }
  345. }.observes('controller.inputFields.emailFrom.value'),
  346. smtpPortValidation: function () {
  347. var value = this.get('controller.inputFields.SMTPPort.value');
  348. if (value && (!validator.isValidInt(value) || value < 0)) {
  349. this.set('parentView.hasErrors', true);
  350. this.set('controller.inputFields.SMTPPort.errorMsg', Em.I18n.t('alerts.notifications.error.integer'));
  351. } else {
  352. this.set('parentView.hasErrors', false);
  353. this.set('controller.inputFields.SMTPPort.errorMsg', null);
  354. }
  355. }.observes('controller.inputFields.SMTPPort.value'),
  356. portValidation: function () {
  357. var value = this.get('controller.inputFields.port.value');
  358. if (value && (!validator.isValidInt(value) || value < 0)) {
  359. this.set('parentView.hasErrors', true);
  360. this.set('controller.inputFields.port.errorMsg', Em.I18n.t('alerts.notifications.error.integer'));
  361. } else {
  362. this.set('parentView.hasErrors', false);
  363. this.set('controller.inputFields.port.errorMsg', null);
  364. }
  365. }.observes('controller.inputFields.port.value'),
  366. groupsSelectView: Em.Select.extend({
  367. attributeBindings: ['disabled'],
  368. init: function () {
  369. this._super();
  370. this.set('parentView.groupSelect', this);
  371. }
  372. }),
  373. groupSelect: null,
  374. /**
  375. * Select all alert-groups if <code>allGroups.value</code> is 'custom'
  376. * @method selectAllGroups
  377. */
  378. selectAllGroups: function () {
  379. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  380. this.set('groupSelect.selection', this.get('groupSelect.content').slice());
  381. }
  382. },
  383. /**
  384. * Deselect all alert-groups if <code>allGroups.value</code> is 'custom'
  385. * @method clearAllGroups
  386. */
  387. clearAllGroups: function () {
  388. if (this.get('controller.inputFields.allGroups.value') == 'custom') {
  389. this.set('groupSelect.selection', []);
  390. }
  391. },
  392. severitySelectView: Em.Select.extend({
  393. init: function () {
  394. this._super();
  395. this.set('parentView.severitySelect', this);
  396. }
  397. }),
  398. severitySelect: null,
  399. /**
  400. * Determines if all alert-groups are selected
  401. * @type {boolean}
  402. */
  403. allGroupsSelected: function () {
  404. return this.get('groupSelect.selection.length') === this.get('groupSelect.content.length');
  405. }.property('groupSelect.selection.length', 'groupSelect.content.length', 'groupSelect.disabled'),
  406. /**
  407. * Determines if no one alert-group is selected
  408. * @type {boolean}
  409. */
  410. noneGroupsSelected: function () {
  411. return this.get('groupSelect.selection.length') === 0;
  412. }.property('groupSelect.selection.length', 'groupSelect.content.length', 'groupSelect.disabled'),
  413. /**
  414. * Determines if all severities are selected
  415. * @type {boolean}
  416. */
  417. allSeveritySelected: function () {
  418. return this.get('severitySelect.selection.length') === this.get('severitySelect.content.length');
  419. }.property('severitySelect.selection.length', 'severitySelect.content.length'),
  420. /**
  421. * Determines if no one severity is selected
  422. * @type {boolean}
  423. */
  424. noneSeveritySelected: function () {
  425. return this.get('severitySelect.selection.length') === 0;
  426. }.property('severitySelect.selection.length', 'severitySelect.content.length'),
  427. /**
  428. * Select all severities
  429. * @method selectAllSeverity
  430. */
  431. selectAllSeverity: function () {
  432. this.set('severitySelect.selection', this.get('severitySelect.content').slice());
  433. },
  434. /**
  435. * Deselect all severities
  436. * @method clearAllSeverity
  437. */
  438. clearAllSeverity: function () {
  439. this.set('severitySelect.selection', []);
  440. }
  441. }),
  442. isSaving: false,
  443. hasErrors: false,
  444. primary: Em.I18n.t('common.save'),
  445. disablePrimary: function () {
  446. return this.get('isSaving') || this.get('hasErrors');
  447. }.property('isSaving', 'hasErrors'),
  448. onPrimary: function () {
  449. this.set('isSaving', true);
  450. var apiObject = self.formatNotificationAPIObject();
  451. if (isEdit) {
  452. self.updateAlertNotification(apiObject);
  453. } else {
  454. self.createAlertNotification(apiObject);
  455. }
  456. }
  457. });
  458. this.set('createEditPopup', createEditPopup);
  459. return createEditPopup;
  460. },
  461. /**
  462. * Create API-formatted object from data populate by user
  463. * @returns {Object}
  464. * @method formatNotificationAPIObject
  465. */
  466. formatNotificationAPIObject: function () {
  467. var inputFields = this.get('inputFields');
  468. var properties = {};
  469. if (inputFields.get('method.value') === 'EMAIL') {
  470. properties['ambari.dispatch.recipients'] = inputFields.get('email.value').replace(/\s/g, '').split(',');
  471. properties['mail.smtp.host'] = inputFields.get('SMTPServer.value');
  472. properties['mail.smtp.port'] = inputFields.get('SMTPPort.value');
  473. properties['mail.smtp.from'] = inputFields.get('emailFrom.value');
  474. properties['mail.smtp.auth'] = inputFields.get('SMTPUseAuthentication.value');
  475. if (inputFields.get('SMTPUseAuthentication.value')) {
  476. properties['ambari.dispatch.credential.username'] = inputFields.get('SMTPUsername.value');
  477. properties['ambari.dispatch.credential.password'] = inputFields.get('SMTPPassword.value');
  478. properties['mail.smtp.starttls.enable'] = inputFields.get('SMTPSTARTTLS.value');
  479. }
  480. } else {
  481. properties['ambari.dispatch.snmp.version'] = inputFields.get('version.value');
  482. properties['ambari.dispatch.snmp.oids.trap'] = inputFields.get('OIDs.value');
  483. properties['ambari.dispatch.snmp.community'] = inputFields.get('community.value');
  484. properties['ambari.dispatch.snmp.port'] = inputFields.get('port.value');
  485. }
  486. inputFields.get('customProperties').forEach(function (customProperty) {
  487. properties[customProperty.name] = customProperty.value;
  488. });
  489. var apiObject = {
  490. AlertTarget: {
  491. name: inputFields.get('name.value'),
  492. description: inputFields.get('description.value'),
  493. global: inputFields.get('allGroups.value') === 'all',
  494. notification_type: inputFields.get('method.value'),
  495. alert_states: inputFields.get('severityFilter.value'),
  496. properties: properties
  497. }
  498. };
  499. if (inputFields.get('allGroups.value') == 'custom') {
  500. apiObject.AlertTarget.groups = inputFields.get('groups.value').mapProperty('id');
  501. }
  502. return apiObject;
  503. },
  504. /**
  505. * Send request to server to create Alert Notification
  506. * @param {object} apiObject (@see formatNotificationAPIObject)
  507. * @returns {$.ajax}
  508. * @method createAlertNotification
  509. */
  510. createAlertNotification: function (apiObject) {
  511. return App.ajax.send({
  512. name: 'alerts.create_alert_notification',
  513. sender: this,
  514. data: {
  515. data: apiObject
  516. },
  517. success: 'createAlertNotificationSuccessCallback',
  518. error: 'saveErrorCallback'
  519. });
  520. },
  521. /**
  522. * Success callback for <code>createAlertNotification</code>
  523. * @method createAlertNotificationSuccessCallback
  524. */
  525. createAlertNotificationSuccessCallback: function () {
  526. this.loadAlertNotifications();
  527. var createEditPopup = this.get('createEditPopup');
  528. if (createEditPopup) {
  529. createEditPopup.hide();
  530. }
  531. },
  532. /**
  533. * Send request to server to update Alert Notification
  534. * @param {object} apiObject (@see formatNotificationAPIObject)
  535. * @returns {$.ajax}
  536. * @method updateAlertNotification
  537. */
  538. updateAlertNotification: function (apiObject) {
  539. return App.ajax.send({
  540. name: 'alerts.update_alert_notification',
  541. sender: this,
  542. data: {
  543. data: apiObject,
  544. id: this.get('selectedAlertNotification.id')
  545. },
  546. success: 'updateAlertNotificationSuccessCallback',
  547. error: 'saveErrorCallback'
  548. });
  549. },
  550. /**
  551. * Success callback for <code>updateAlertNotification</code>
  552. * @method updateAlertNotificationSuccessCallback
  553. */
  554. updateAlertNotificationSuccessCallback: function () {
  555. this.loadAlertNotifications();
  556. var createEditPopup = this.get('createEditPopup');
  557. if (createEditPopup) {
  558. createEditPopup.hide();
  559. }
  560. },
  561. /**
  562. * Error callback for <code>createAlertNotification</code> and <code>updateAlertNotification</code>
  563. * @method saveErrorCallback
  564. */
  565. saveErrorCallback: function () {
  566. this.set('createEditPopup.isSaving', false);
  567. },
  568. /**
  569. * Delete Notification button handler
  570. * @return {App.ModalPopup}
  571. * @method deleteAlertNotification
  572. */
  573. deleteAlertNotification: function () {
  574. var self = this;
  575. return App.showConfirmationPopup(function () {
  576. App.ajax.send({
  577. name: 'alerts.delete_alert_notification',
  578. sender: self,
  579. data: {
  580. id: self.get('selectedAlertNotification.id')
  581. },
  582. success: 'deleteAlertNotificationSuccessCallback'
  583. });
  584. }, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteBody').format(this.get('selectedAlertNotification.name')),
  585. null, Em.I18n.t('alerts.actions.manage_alert_notifications_popup.confirmDeleteHeader'), Em.I18n.t('common.delete'));
  586. },
  587. /**
  588. * Success callback for <code>deleteAlertNotification</code>
  589. * @method deleteAlertNotificationSuccessCallback
  590. */
  591. deleteAlertNotificationSuccessCallback: function () {
  592. this.loadAlertNotifications();
  593. var selectedAlertNotification = this.get('selectedAlertNotification');
  594. selectedAlertNotification.deleteRecord();
  595. this.set('selectedAlertNotification', null);
  596. },
  597. /**
  598. * Duplicate Notification button handler
  599. * @method duplicateAlertNotification
  600. */
  601. duplicateAlertNotification: function () {
  602. this.fillEditCreateInputs(true);
  603. this.showCreateEditPopup();
  604. },
  605. /**
  606. * Show popup with form for new custom property
  607. * @method addCustomPropertyHandler
  608. * @return {App.ModalPopup}
  609. */
  610. addCustomPropertyHandler: function () {
  611. var self = this;
  612. return App.ModalPopup.show({
  613. header: Em.I18n.t('alerts.notifications.addCustomPropertyPopup.header'),
  614. primary: Em.I18n.t('common.add'),
  615. bodyClass: Em.View.extend({
  616. /**
  617. * If some error with new custom property
  618. * @type {boolean}
  619. */
  620. isError: false,
  621. controller: this,
  622. /**
  623. * Error message for new custom property (invalid name, existed name etc)
  624. * @type {string}
  625. */
  626. errorMessage: '',
  627. /**
  628. * Check new custom property for errors with its name
  629. * @method errorHandler
  630. */
  631. errorsHandler: function () {
  632. var name = this.get('controller.newCustomProperty.name');
  633. var flag = validator.isValidConfigKey(name);
  634. if (flag) {
  635. if (this.get('controller.inputFields.customProperties').mapProperty('name').contains(name) ||
  636. this.get('controller.ignoredCustomProperties').contains(name)) {
  637. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.propertyExists'));
  638. flag = false;
  639. }
  640. }
  641. else {
  642. this.set('errorMessage', Em.I18n.t('alerts.notifications.addCustomPropertyPopup.error.invalidPropertyName'));
  643. }
  644. this.set('isError', !flag);
  645. this.set('parentView.disablePrimary', !flag);
  646. }.observes('controller.newCustomProperty.name'),
  647. templateName: require('templates/main/alerts/add_custom_config_to_alert_notification_popup')
  648. }),
  649. disablePrimary: true,
  650. onPrimary: function () {
  651. self.addCustomProperty();
  652. self.set('newCustomProperty', {name: '', value: ''}); // cleanup
  653. this.hide();
  654. }
  655. });
  656. },
  657. /**
  658. * Add Custom Property to <code>selectedAlertNotification</code> (push it to <code>properties</code>-field)
  659. * @method addCustomProperty
  660. */
  661. addCustomProperty: function () {
  662. var newCustomProperty = this.get('newCustomProperty');
  663. this.get('inputFields.customProperties').pushObject({
  664. name: newCustomProperty.name,
  665. value: newCustomProperty.value,
  666. defaultValue: newCustomProperty.value
  667. });
  668. },
  669. /**
  670. * "Remove"-button click handler
  671. * Delete customProperty from <code>inputFields.customProperties</code>
  672. * @param {object} e
  673. * @method removeCustomProperty
  674. */
  675. removeCustomPropertyHandler: function (e) {
  676. var customProperties = this.get('inputFields.customProperties');
  677. this.set('inputFields.customProperties', customProperties.without(e.context));
  678. }
  679. });