all files / src/alert/ alert.js

89.8% Statements 44/49
83.33% Branches 20/24
85.71% Functions 12/14
89.13% Lines 41/46
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 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136                  27×                                 27×     27×     27×   27×     27× 27×       27× 27×                 27×       27×               23×           23× 23× 253×       23× 23× 92×       23× 92× 92× 16×           23× 23×       23× 69× 15× 15×           23× 15× 17× 17×               23×     23×     23× 23× 23× 23×              
'use strict';
 
// @BUG: following snippet won't compile correctly
// @TODO: submit issue to core
// '<span ng-if="title"><strong ng-bind="title"></strong>&nbsp;</span><span ng-bind-html="content"></span>' +
 
angular.module('mgcrea.ngStrap.alert', ['mgcrea.ngStrap.modal'])
 
  .provider('$alert', function () {
 
    var defaults = this.defaults = {
      animation: 'am-fade',
      prefixClass: 'alert',
      prefixEvent: 'alert',
      placement: null,
      templateUrl: 'alert/alert.tpl.html',
      container: false,
      element: null,
      backdrop: false,
      keyboard: true,
      show: true,
      // Specific options
      duration: false,
      type: false,
      dismissable: true
    };
 
    this.$get = function ($modal, $timeout) {
 
      function AlertFactory (config) {
 
        var $alert = {};
 
        // Common vars
        var options = angular.extend({}, defaults, config);
 
        $alert = $modal(options);
 
        // Support scope as string options [/*title, content, */ type, dismissable]
        $alert.$scope.dismissable = !!options.dismissable;
        if (options.type) {
          $alert.$scope.type = options.type;
        }
 
        // Support auto-close duration
        var show = $alert.show;
        Iif (options.duration) {
          $alert.show = function () {
            show();
            $timeout(function () {
              $alert.hide();
            }, options.duration * 1000);
          };
        }
 
        return $alert;
 
      }
 
      return AlertFactory;
 
    };
 
  })
 
  .directive('bsAlert', function ($window, $sce, $alert) {
 
    return {
      restrict: 'EAC',
      scope: true,
      link: function postLink (scope, element, attr, transclusion) {
 
        // Directive options
        var options = {scope: scope, element: element, show: false};
        angular.forEach(['template', 'templateUrl', 'controller', 'controllerAs', 'placement', 'keyboard', 'html', 'container', 'animation', 'duration', 'dismissable'], function (key) {
          if (angular.isDefined(attr[key])) options[key] = attr[key];
        });
 
        // use string regex match boolean attr falsy values, leave truthy values be
        var falseValueRegExp = /^(false|0|)$/i;
        angular.forEach(['keyboard', 'html', 'container', 'dismissable'], function (key) {
          if (angular.isDefined(attr[key]) && falseValueRegExp.test(attr[key])) options[key] = false;
        });
 
        // bind functions from the attrs to the show and hide events
        angular.forEach(['onBeforeShow', 'onShow', 'onBeforeHide', 'onHide'], function (key) {
          var bsKey = 'bs' + key.charAt(0).toUpperCase() + key.slice(1);
          if (angular.isDefined(attr[bsKey])) {
            options[key] = scope.$eval(attr[bsKey]);
          }
        });
 
        // overwrite inherited title value when no value specified
        // fix for angular 1.3.1 531a8de72c439d8ddd064874bf364c00cedabb11
        Eif (!scope.hasOwnProperty('title')) {
          scope.title = '';
        }
 
        // Support scope as data-attrs
        angular.forEach(['title', 'content', 'type'], function (key) {
          if (attr[key]) {
            attr.$observe(key, function (newValue, oldValue) {
              scope[key] = $sce.trustAsHtml(newValue);
            });
          }
        });
 
        // Support scope as an object
        if (attr.bsAlert) {
          scope.$watch(attr.bsAlert, function (newValue, oldValue) {
            Eif (angular.isObject(newValue)) {
              angular.extend(scope, newValue);
            } else {
              scope.content = newValue;
            }
          }, true);
        }
 
        // Initialize alert
        var alert = $alert(options);
 
        // Trigger
        element.on(attr.trigger || 'click', alert.toggle);
 
        // Garbage collection
        scope.$on('$destroy', function () {
          Eif (alert) alert.destroy();
          options = null;
          alert = null;
        });
 
      }
    };
 
  });