definition_details_controller.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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.MainAlertDefinitionDetailsController = Em.Controller.extend({
  19. name: 'mainAlertDefinitionDetailsController',
  20. alerts: function () {
  21. return App.AlertInstance.find().toArray()
  22. .filterProperty('definitionId', this.get('content.id'));
  23. }.property('App.router.mainAlertInstancesController.isLoaded'),
  24. // stores object with editing form data (label, description, thresholds)
  25. editing: Em.Object.create({
  26. label: Em.Object.create({
  27. name: 'label',
  28. isEditing: false,
  29. value: '',
  30. originalValue: '',
  31. isError: false,
  32. bindingValue: 'content.label'
  33. }),
  34. description: Em.Object.create({
  35. name: 'description',
  36. isEditing: false,
  37. value: '',
  38. originalValue: '',
  39. isError: false,
  40. bindingValue: 'content.description'
  41. })
  42. }),
  43. /**
  44. * Validation function to define if label field populated correctly
  45. */
  46. labelValidation: function () {
  47. this.set('editing.label.isError', !this.get('editing.label.value').trim());
  48. }.observes('editing.label.value'),
  49. /**
  50. * Validation function to define if description field populated correctly
  51. */
  52. descriptionValidation: function () {
  53. this.set('editing.description.isError', !this.get('editing.description.value').trim());
  54. }.observes('editing.description.value'),
  55. /**
  56. * Load alert instances for current alertDefinition
  57. * Start updating loaded data
  58. * @method loadAlertInstances
  59. */
  60. loadAlertInstances: function() {
  61. App.router.get('mainAlertInstancesController').loadAlertInstancesByAlertDefinition(this.get('content.id'));
  62. App.router.set('mainAlertInstancesController.isUpdating', true);
  63. },
  64. /**
  65. * Edit button handler
  66. * @param event
  67. */
  68. edit: function (event) {
  69. var element = event.context;
  70. var value = this.get(element.get('bindingValue'));
  71. element.set('originalValue', value);
  72. element.set('value', value);
  73. element.set('isEditing', true);
  74. },
  75. /**
  76. * Cancel button handler
  77. * @param event
  78. */
  79. cancelEdit: function (event) {
  80. var element = event.context;
  81. element.set('value', element.get('originalValue'));
  82. element.set('isEditing', false);
  83. },
  84. /**
  85. * Save button handler, could save label/description/thresholds of alert definition
  86. * @param event
  87. * @returns {$.ajax}
  88. * @method saveEdit
  89. */
  90. saveEdit: function (event) {
  91. var element = event.context;
  92. this.set(element.get('bindingValue'), element.get('value'));
  93. element.set('isEditing', false);
  94. var data = Em.Object.create({});
  95. var property_name = "AlertDefinition/" + element.get('name');
  96. data.set(property_name, element.get('value'));
  97. var alertDefinition_id = this.get('content.id');
  98. return App.ajax.send({
  99. name: 'alerts.update_alert_definition',
  100. sender: this,
  101. data: {
  102. id: alertDefinition_id,
  103. data: data
  104. }
  105. });
  106. },
  107. /**
  108. * "Delete" button handler
  109. * @param event
  110. */
  111. deleteAlertDefinition : function(event) {
  112. var alertDefinition = this.get('content');
  113. var self = this;
  114. App.showConfirmationPopup(function() {
  115. App.ajax.send({
  116. name : 'alerts.delete_alert_definition',
  117. sender : self,
  118. success : 'deleteAlertDefinitionSuccess',
  119. error : 'deleteAlertDefinitionError',
  120. data : {
  121. id : alertDefinition.get('id')
  122. }
  123. });
  124. }, null, function() {
  125. });
  126. },
  127. deleteAlertDefinitionSuccess : function() {
  128. App.router.transitionTo('main.alerts.index');
  129. },
  130. deleteAlertDefinitionError : function(xhr, textStatus, errorThrown, opt) {
  131. console.log(textStatus);
  132. console.log(errorThrown);
  133. xhr.responseText = "{\"message\": \"" + xhr.statusText + "\"}";
  134. App.ajax.defaultErrorHandler(xhr, opt.url, 'DELETE', xhr.status);
  135. },
  136. /**
  137. * "Disable / Enable" button handler
  138. * @returns {$.ajax}
  139. * @method toggleState
  140. */
  141. toggleState: function() {
  142. var alertDefinition = this.get('content');
  143. return App.ajax.send({
  144. name: 'alerts.update_alert_definition',
  145. sender: this,
  146. data: {
  147. id: alertDefinition.get('id'),
  148. data: {
  149. "AlertDefinition/enabled": !alertDefinition.get('enabled')
  150. }
  151. }
  152. });
  153. },
  154. /**
  155. * Router transition to host level alerts page
  156. * @param event
  157. */
  158. goToHostAlerts: function (event) {
  159. if (event && event.context) {
  160. App.router.transitionTo('main.hosts.hostDetails.alerts', event.context);
  161. }
  162. }
  163. });