alert_definitions_actions_controller.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. /**
  61. * Start "Create Alert Definition" wizard
  62. * @method createNewAlertDefinition
  63. */
  64. createNewAlertDefinition: function() {
  65. App.router.transitionTo('alertAdd');
  66. },
  67. /**
  68. * Handler when clicking on "Manage Alert Groups", a popup will show up
  69. * @method manageAlertGroups
  70. * @return {App.ModalPopup}
  71. */
  72. manageAlertGroups: function () {
  73. return App.ModalPopup.show({
  74. header: Em.I18n.t('alerts.actions.manage_alert_groups_popup.header'),
  75. bodyClass: App.MainAlertsManageAlertGroupView.extend({
  76. controllerBinding: 'App.router.manageAlertGroupsController'
  77. }),
  78. classNames: ['sixty-percent-width-modal', 'manage-alert-group-popup'],
  79. primary: Em.I18n.t('common.save'),
  80. onPrimary: function () {
  81. var modifiedAlertGroups = this.get('subViewController.defsModifiedAlertGroups');
  82. // Save modified Alert-groups
  83. console.log("manageAlertGroups(): Saving modified Alert groups: ", modifiedAlertGroups);
  84. var self = this;
  85. var errors = [];
  86. var deleteQueriesCounter = modifiedAlertGroups.toDelete.length;
  87. var createQueriesCounter = modifiedAlertGroups.toCreate.length;
  88. var deleteQueriesRun = false;
  89. var createQueriesRun = false;
  90. var runNextQuery = function () {
  91. if (!deleteQueriesRun && deleteQueriesCounter > 0) {
  92. deleteQueriesRun = true;
  93. modifiedAlertGroups.toDelete.forEach(function (group) {
  94. self.get('subViewController').removeAlertGroup(group, finishFunction, finishFunction);
  95. }, this);
  96. } else if (!createQueriesRun && deleteQueriesCounter < 1) {
  97. createQueriesRun = true;
  98. modifiedAlertGroups.toSet.forEach(function (group) {
  99. self.get('subViewController').updateAlertGroup(group, finishFunction, finishFunction);
  100. }, this);
  101. modifiedAlertGroups.toCreate.forEach(function (group) {
  102. self.get('subViewController').postNewAlertGroup(group, finishFunction);
  103. }, this);
  104. }
  105. };
  106. var finishFunction = function (xhr, text, errorThrown) {
  107. if (xhr && errorThrown) {
  108. var error = xhr.status + "(" + errorThrown + ") ";
  109. try {
  110. var json = $.parseJSON(xhr.responseText);
  111. error += json.message;
  112. } catch (err) {
  113. }
  114. console.error('Error updating Alert Group:', error);
  115. errors.push(error);
  116. }
  117. if (createQueriesRun) {
  118. createQueriesCounter--;
  119. } else {
  120. deleteQueriesCounter--;
  121. }
  122. if (deleteQueriesCounter + createQueriesCounter < 1) {
  123. if (errors.length > 0) {
  124. console.log(errors);
  125. self.get('subViewController').set('errorMessage', errors.join(". "));
  126. } else {
  127. self.hide();
  128. }
  129. } else {
  130. runNextQuery();
  131. }
  132. };
  133. runNextQuery();
  134. },
  135. subViewController: function () {
  136. return App.router.get('manageAlertGroupsController');
  137. }.property('App.router.manageAlertGroupsController'),
  138. updateButtons: function () {
  139. var modified = this.get('subViewController.isDefsModified');
  140. this.set('disablePrimary', !modified);
  141. }.observes('subViewController.isDefsModified'),
  142. didInsertElement: Em.K
  143. });
  144. },
  145. /**
  146. * "Manage Alert Notifications" handler
  147. * @method manageNotifications
  148. * @return {App.ModalPopup}
  149. */
  150. manageNotifications: function () {
  151. return App.ModalPopup.show({
  152. header: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.header'),
  153. bodyClass: App.ManageAlertNotificationsView.extend({
  154. controllerBinding: 'App.router.manageAlertNotificationsController'
  155. }),
  156. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  157. secondary: null,
  158. primary: Em.I18n.t('common.close'),
  159. didInsertElement: Em.K
  160. });
  161. }
  162. });