This repository has been archived by the owner on Dec 1, 2023. It is now read-only.
/
alert.js
90 lines (89 loc) · 2.94 KB
/
alert.js
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
/**
* angular-strap
* @version v2.3.3 - 2015-09-24
* @link http://mgcrea.github.io/angular-strap
* @author Olivier Louvignes <olivier@mg-crea.com> (https://github.com/mgcrea)
* @license MIT License, http://www.opensource.org/licenses/MIT
*/
'use strict';
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,
duration: false,
type: false,
dismissable: true
};
this.$get = [ '$modal', '$timeout', function($modal, $timeout) {
function AlertFactory(config) {
var $alert = {};
var options = angular.extend({}, defaults, config);
$alert = $modal(options);
$alert.$scope.dismissable = !!options.dismissable;
if (options.type) {
$alert.$scope.type = options.type;
}
var show = $alert.show;
if (options.duration) {
$alert.show = function() {
show();
$timeout(function() {
$alert.hide();
}, options.duration * 1e3);
};
}
return $alert;
}
return AlertFactory;
} ];
}).directive('bsAlert', [ '$window', '$sce', '$alert', function($window, $sce, $alert) {
var requestAnimationFrame = $window.requestAnimationFrame || $window.setTimeout;
return {
restrict: 'EAC',
scope: true,
link: function postLink(scope, element, attr, transclusion) {
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];
});
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;
});
if (!scope.hasOwnProperty('title')) {
scope.title = '';
}
angular.forEach([ 'title', 'content', 'type' ], function(key) {
attr[key] && attr.$observe(key, function(newValue, oldValue) {
scope[key] = $sce.trustAsHtml(newValue);
});
});
attr.bsAlert && scope.$watch(attr.bsAlert, function(newValue, oldValue) {
if (angular.isObject(newValue)) {
angular.extend(scope, newValue);
} else {
scope.content = newValue;
}
}, true);
var alert = $alert(options);
element.on(attr.trigger || 'click', alert.toggle);
scope.$on('$destroy', function() {
if (alert) alert.destroy();
options = null;
alert = null;
});
}
};
} ]);