alert_definitions_actions_controller.js 6.9 KB

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