all files / app/components/toastr/ toastrHelper.service.js

100% Statements 26/26
100% Branches 6/6
100% Functions 7/7
100% Lines 26/26
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74                          198×                                   203× 203× 203× 203× 203×   203×   203×     33× 33×   33× 33×     13× 13× 13×     309× 309×                  
/**
 * @ngdoc service
 * @name app.components.toastrHelper
 * @description
 * Adds popup toast functionality to a controller
 *
 */
(function () {
  'use strict';
 
  angular
    .module('app.components')
    .factory('toastrHelper', factory);
 
  /** @ngInject */
  function factory(toastr) {
    return activate;
 
    /**
     * @ngdoc function
     * @name activate
     * @methodOf app.components.toastrHelper
     * @description
     * Adds members to a controller:
     * * showToast()
     * * showHttpErrorToast()
     * * clearToast()
     * * lastToast
     *
     * @param {Object} _vm_
     * Controller instance
     * @param {Object} _scope_
     * Controller scope
     */
    function activate(_vm_, _scope_) {
      var vm = _vm_;
      vm.showToast = showToast;
      vm.showHttpErrorToast = showHttpErrorToast;
      vm.clearToast = clearToast;
      vm.lastToast = null;
 
      _scope_.$on('$destroy', function () {
        // Remove current toasts when switch views
        clearToast();
      });
 
      function showToast(message, caption, kind) {
        kind = kind || 'info';
        if (angular.isUndefined(caption))
          caption = kind.charAt(0).toUpperCase() + kind.slice(1);
 
        toastr.clear();
        vm.lastToast = toastr[kind](message, caption);
      }
 
      function showHttpErrorToast(status) {
        var result = status == 401;
        if (result)
          vm.showToast('Please login again.', "Authorization is no longer valid");
        return result;
      }
 
      function clearToast() {
        toastr.clear();
        vm.lastToast = null;
      }
    }
  }
})();