alert-messages.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. import Ember from 'ember';
  19. /**
  20. Shows alert flash and also creates `alert` objects in store. If creation of
  21. `alert` objects in store pass `options.flashOnly` as `true`. The options
  22. required for creating the `alert` objects are:
  23. ```
  24. options.message: message field returned by the API server.
  25. options.status : Status XHR request if the message is a response to XHR request. Defaults to -1.
  26. options.error: Detailed error to be displayed.
  27. ```
  28. Options required for ember-cli-flash can also be passed in the alertOptions to override the
  29. default behaviour.
  30. */
  31. export default Ember.Service.extend({
  32. flashMessages: Ember.inject.service('flash-messages'),
  33. store: Ember.inject.service('store'),
  34. success: function(message, options = {}, alertOptions = {}) {
  35. this._processMessage('success', message, options, alertOptions);
  36. },
  37. warn: function(message, options = {}, alertOptions = {}) {
  38. this._processMessage('warn', message, options, alertOptions);
  39. },
  40. info: function(message, options = {}, alertOptions = {}) {
  41. this._processMessage('info', message, options, alertOptions);
  42. },
  43. danger: function(message, options = {}, alertOptions = {}) {
  44. this._processMessage('danger', message, options, alertOptions);
  45. },
  46. clearMessages: function() {
  47. this.get('flashMessages').clearMessages();
  48. },
  49. _processMessage: function(type, message, options, alertOptions) {
  50. this._clearMessagesIfRequired(alertOptions);
  51. let alertRecord = this._createAlert(message, type, options, alertOptions);
  52. if(alertRecord) {
  53. message = this._addDetailsToMessage(message, alertRecord);
  54. }
  55. switch (type) {
  56. case 'success':
  57. this.get('flashMessages').success(message, this._getOptions(alertOptions));
  58. break;
  59. case 'warn':
  60. this.get('flashMessages').warning(message, this._getOptions(alertOptions));
  61. break;
  62. case 'info':
  63. this.get('flashMessages').info(message, this._getOptions(alertOptions));
  64. break;
  65. case 'danger':
  66. this.get('flashMessages').danger(message, this._getOptions(alertOptions));
  67. }
  68. },
  69. _addDetailsToMessage: function(message, record) {
  70. let id = record.get('id');
  71. let suffix = `<a href="#/messages/${id}">(details)</a>`;
  72. return message + " " + suffix;
  73. },
  74. _createAlert: function(message, type, options, alertOptions) {
  75. var data = {};
  76. data.message = message;
  77. data.responseMessage = options.message || '';
  78. data.id = this._getNextAlertId();
  79. data.type = type;
  80. data.status = options.status || -1;
  81. data.trace = this._getDetailedError(options.trace);
  82. delete options.status;
  83. delete options.error;
  84. if(alertOptions.flashOnly === true) {
  85. return;
  86. }
  87. return this.get('store').createRecord('alert', data);
  88. },
  89. _getDetailedError: function(error) {
  90. return error || '';
  91. },
  92. _getOptions: function(options = {}) {
  93. var defaultOptions = {
  94. priority: 100,
  95. showProgress: true,
  96. timeout: 6000
  97. };
  98. return Ember.merge(defaultOptions, options);
  99. },
  100. _getNextAlertId: function() {
  101. return this.get('store').peekAll('alert').get('length') + 1;
  102. },
  103. _clearMessagesIfRequired: function(options = {}) {
  104. var stackMessages = options.stackMessages || false;
  105. if(stackMessages !== true) {
  106. this.clearMessages();
  107. }
  108. }
  109. });