alert_definitions_actions_controller.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. App.MainAlertDefinitionActionsController = Em.ArrayController.extend({
  19. name: 'mainAlertDefinitionActionsController',
  20. /**
  21. * List of available actions for alert definitions
  22. * @type {{title: string, icon: string, action: string, showDivider: boolean}[]}
  23. */
  24. content: [
  25. {
  26. title: Em.I18n.t('alerts.actions.create'),
  27. icon: 'icon-plus',
  28. action: 'createNewAlertDefinition',
  29. showDivider: true
  30. },
  31. {
  32. title: Em.I18n.t('alerts.actions.manageGroups'),
  33. icon: 'icon-th-large',
  34. action: 'manageAlertGroups',
  35. showDivider: false
  36. },
  37. {
  38. title: Em.I18n.t('alerts.actions.manageNotifications'),
  39. icon: 'icon-envelope-alt',
  40. action: 'manageNotifications',
  41. showDivider: false
  42. }
  43. ],
  44. /**
  45. * Common handler for menu item click
  46. * Call proper controller's method described in <code>action</code>-field (see <code>content</code>)
  47. * @param {object} event
  48. * @method actionHandler
  49. */
  50. actionHandler: function(event) {
  51. var menuElement = event.context,
  52. action = menuElement.action;
  53. if ('function' === Em.typeOf(Em.get(this, action))) {
  54. this[action]();
  55. }
  56. else {
  57. console.error('Invalid action provided - ', action);
  58. }
  59. },
  60. createNewAlertDefinition: Em.K,
  61. /**
  62. * handler when clicking on "Manage Alert Groups", a popup will show up
  63. */
  64. manageAlertGroups: function () {
  65. return App.ModalPopup.show({
  66. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.header'),
  67. bodyClass: App.MainAlertsManageAlertGroupView.extend({
  68. controllerBinding: 'App.router.manageAlertGroupsController'
  69. }),
  70. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  71. primary: Em.I18n.t('common.save'),
  72. onPrimary: function () {
  73. var modifiedAlertGroups = this.get('subViewController.defsModifiedAlertGroups');
  74. // Save modified Alert-groups
  75. console.log("manageAlertGroups(): Saving modified Alert groups: ", modifiedAlertGroups);
  76. var self = this;
  77. var errors = [];
  78. var deleteQueriesCounter = modifiedAlertGroups.toDelete.length;
  79. var createQueriesCounter = modifiedAlertGroups.toCreate.length;
  80. var deleteQueriesRun = false;
  81. var createQueriesRun = false;
  82. var runNextQuery = function () {
  83. if (!deleteQueriesRun && deleteQueriesCounter > 0) {
  84. deleteQueriesRun = true;
  85. modifiedAlertGroups.toDelete.forEach(function (group) {
  86. self.get('subViewController').removeAlertGroup(group, finishFunction, finishFunction);
  87. }, this);
  88. } else if (!createQueriesRun && deleteQueriesCounter < 1) {
  89. createQueriesRun = true;
  90. modifiedAlertGroups.toSet.forEach(function (group) {
  91. self.get('subViewController').updateAlertGroup(group, finishFunction, finishFunction);
  92. }, this);
  93. modifiedAlertGroups.toCreate.forEach(function (group) {
  94. self.get('subViewController').postNewAlertGroup(group, finishFunction);
  95. }, this);
  96. }
  97. };
  98. var finishFunction = function (xhr, text, errorThrown) {
  99. if (xhr && errorThrown) {
  100. var error = xhr.status + "(" + errorThrown + ") ";
  101. try {
  102. var json = $.parseJSON(xhr.responseText);
  103. error += json.message;
  104. } catch (err) {
  105. }
  106. console.error('Error updating Alert Group:', error);
  107. errors.push(error);
  108. }
  109. if (createQueriesRun) {
  110. createQueriesCounter--;
  111. } else {
  112. deleteQueriesCounter--;
  113. }
  114. if (deleteQueriesCounter + createQueriesCounter < 1) {
  115. if (errors.length > 0) {
  116. console.log(errors);
  117. self.get('subViewController').set('errorMessage', errors.join(". "));
  118. } else {
  119. self.hide();
  120. }
  121. } else {
  122. runNextQuery();
  123. }
  124. };
  125. runNextQuery();
  126. },
  127. onSecondary: function () {
  128. this.hide();
  129. },
  130. onClose: function () {
  131. this.hide();
  132. },
  133. subViewController: function () {
  134. return App.router.get('manageAlertGroupsController');
  135. }.property('App.router.manageAlertGroupsController'),
  136. updateButtons: function () {
  137. var modified = this.get('subViewController.isDefsModified');
  138. this.set('disablePrimary', !modified);
  139. }.observes('subViewController.isDefsModified'),
  140. secondary: Em.I18n.t('common.cancel'),
  141. didInsertElement: Em.K
  142. });
  143. },
  144. manageNotifications: Em.K
  145. });