summaryrefslogtreecommitdiffstats
path: root/app/common/services/toastService.js
blob: 0718e4ddb4e4538c679682745b11469c164f4873 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * data service
 *
 * @module app/common/services/toastService
 * @exports toastService
 * @name toastService

 */

window.angular && (function(angular) {
  'use strict';

  angular.module('app.common.services').service('toastService', [
    'ngToast', '$sce',
    function(ngToast, $sce) {
      function initToast(
          type = 'create', title = '', message = '', dismissOnTimeout = false) {
        const iconStatus = type === 'success' ?
            'on' :
            type === 'danger' ? 'error' : type === 'warning' ? 'warn' : null;
        const content = $sce.trustAsHtml(`
          <div role="alert" class="alert-content-container">
            <status-icon ng-if="${iconStatus !== null}"
                         status="${iconStatus}"
                         class="status-icon">
            </status-icon>
            <div class="alert-content">
              <h2 class="alert-content__header">${title}</h2>
              <p class="alert-content__body">${message}</p>
            </div>
          </div>`);
        ngToast[type]({content, dismissOnTimeout, compileContent: true});
      };

      this.error = function(message) {
        initToast('danger', 'Error', message);
      };

      this.success = function(message) {
        initToast('success', 'Success!', message, true);
      };

      this.warn = function(message) {
        initToast('warning', 'Warning', message);
      };

      this.info = function(title, message) {
        initToast('info', title, message);
      };
    }
  ]);
})(window.angular);
OpenPOWER on IntegriCloud