alert_definitions_actions_controller.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. var dataForSuccessPopup = {
  83. created: modifiedAlertGroups.toCreate.length,
  84. deleted: modifiedAlertGroups.toDelete.length,
  85. updated: modifiedAlertGroups.toSet.length
  86. };
  87. var showSuccessPopup = dataForSuccessPopup.created + dataForSuccessPopup.deleted + dataForSuccessPopup.updated > 0;
  88. // Save modified Alert-groups
  89. console.log("manageAlertGroups(): Saving modified Alert groups: ", modifiedAlertGroups);
  90. var self = this;
  91. var errors = [];
  92. var deleteQueriesCounter = modifiedAlertGroups.toDelete.length;
  93. var createQueriesCounter = modifiedAlertGroups.toCreate.length;
  94. var deleteQueriesRun = false;
  95. var createQueriesRun = false;
  96. var runNextQuery = function () {
  97. if (!deleteQueriesRun && deleteQueriesCounter > 0) {
  98. deleteQueriesRun = true;
  99. modifiedAlertGroups.toDelete.forEach(function (group) {
  100. self.get('subViewController').removeAlertGroup(group, finishFunction, finishFunction);
  101. }, this);
  102. } else if (!createQueriesRun && deleteQueriesCounter < 1) {
  103. createQueriesRun = true;
  104. modifiedAlertGroups.toSet.forEach(function (group) {
  105. self.get('subViewController').updateAlertGroup(group, finishFunction, finishFunction);
  106. }, this);
  107. modifiedAlertGroups.toCreate.forEach(function (group) {
  108. self.get('subViewController').postNewAlertGroup(group, finishFunction);
  109. }, this);
  110. }
  111. };
  112. var finishFunction = function (xhr, text, errorThrown) {
  113. if (xhr && errorThrown) {
  114. var error = xhr.status + "(" + errorThrown + ") ";
  115. try {
  116. var json = $.parseJSON(xhr.responseText);
  117. error += json.message;
  118. } catch (err) {
  119. }
  120. console.error('Error updating Alert Group:', error);
  121. errors.push(error);
  122. }
  123. if (createQueriesRun) {
  124. createQueriesCounter--;
  125. } else {
  126. deleteQueriesCounter--;
  127. }
  128. if (deleteQueriesCounter + createQueriesCounter < 1) {
  129. if (errors.length > 0) {
  130. console.log(errors);
  131. self.get('subViewController').set('errorMessage', errors.join(". "));
  132. }
  133. else {
  134. self.hide();
  135. if (showSuccessPopup) {
  136. App.ModalPopup.show({
  137. secondary: null,
  138. header: Em.I18n.t('alerts.groups.successPopup.header'),
  139. bodyClass: Em.View.extend({
  140. dataForSuccessPopup: dataForSuccessPopup,
  141. templateName: require('templates/main/alerts/alert_groups/success_popup_body')
  142. })
  143. });
  144. }
  145. App.router.get('updateController').updateAlertGroups(function () {
  146. App.router.get('updateController').updateAlertDefinitions(function() {
  147. App.router.get('updateController').updateAlertNotifications(Em.K);
  148. });
  149. });
  150. }
  151. } else {
  152. runNextQuery();
  153. }
  154. };
  155. runNextQuery();
  156. },
  157. subViewController: function () {
  158. return App.router.get('manageAlertGroupsController');
  159. }.property('App.router.manageAlertGroupsController'),
  160. updateButtons: function () {
  161. var modified = this.get('subViewController.isDefsModified');
  162. this.set('disablePrimary', !modified);
  163. }.observes('subViewController.isDefsModified'),
  164. didInsertElement: Em.K
  165. });
  166. },
  167. /**
  168. * "Manage Alert Notifications" handler
  169. * @method manageNotifications
  170. * @return {App.ModalPopup}
  171. */
  172. manageNotifications: function () {
  173. return App.ModalPopup.show({
  174. header: Em.I18n.t('alerts.actions.manage_alert_notifications_popup.header'),
  175. bodyClass: App.ManageAlertNotificationsView.extend({
  176. controllerBinding: 'App.router.manageAlertNotificationsController'
  177. }),
  178. classNames: ['sixty-percent-width-modal', 'manage-configuration-group-popup'],
  179. secondary: null,
  180. primary: Em.I18n.t('common.close'),
  181. autoHeight: false
  182. });
  183. }
  184. });