beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 1 | /** |
| 2 | * data service |
| 3 | * |
| 4 | * @module app/common/services/toastService |
| 5 | * @exports toastService |
| 6 | * @name toastService |
| 7 | |
| 8 | */ |
| 9 | |
| 10 | window.angular && (function(angular) { |
| 11 | 'use strict'; |
| 12 | |
| 13 | angular.module('app.common.services').service('toastService', [ |
| 14 | 'ngToast', '$sce', |
| 15 | function(ngToast, $sce) { |
Yoshie Muranaka | 5e930c0 | 2019-10-16 08:13:53 -0700 | [diff] [blame] | 16 | function initToast( |
| 17 | type = 'create', title = '', message = '', dismissOnTimeout = false) { |
Patrick Williams | 339db9a | 2021-02-22 17:18:14 -0600 | [diff] [blame^] | 18 | const iconStatus = type === 'success' ? 'on' : |
| 19 | type === 'danger' ? 'error' : |
| 20 | type === 'warning' ? 'warn' : |
| 21 | null; |
Yoshie Muranaka | 5e930c0 | 2019-10-16 08:13:53 -0700 | [diff] [blame] | 22 | const content = $sce.trustAsHtml(` |
| 23 | <div role="alert" class="alert-content-container"> |
| 24 | <status-icon ng-if="${iconStatus !== null}" |
| 25 | status="${iconStatus}" |
| 26 | class="status-icon"> |
| 27 | </status-icon> |
| 28 | <div class="alert-content"> |
| 29 | <h2 class="alert-content__header">${title}</h2> |
| 30 | <p class="alert-content__body">${message}</p> |
| 31 | </div> |
| 32 | </div>`); |
| 33 | ngToast[type]({content, dismissOnTimeout, compileContent: true}); |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 34 | }; |
Yoshie Muranaka | 5e930c0 | 2019-10-16 08:13:53 -0700 | [diff] [blame] | 35 | |
| 36 | this.error = function(message) { |
| 37 | initToast('danger', 'Error', message); |
| 38 | }; |
| 39 | |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 40 | this.success = function(message) { |
Yoshie Muranaka | 5e930c0 | 2019-10-16 08:13:53 -0700 | [diff] [blame] | 41 | initToast('success', 'Success!', message, true); |
| 42 | }; |
| 43 | |
| 44 | this.warn = function(message) { |
| 45 | initToast('warning', 'Warning', message); |
| 46 | }; |
| 47 | |
| 48 | this.info = function(title, message) { |
| 49 | initToast('info', title, message); |
beccabroek | 27ce84d | 2019-02-05 15:43:17 -0600 | [diff] [blame] | 50 | }; |
| 51 | } |
| 52 | ]); |
| 53 | })(window.angular); |