forked from sachinchoolur/angular-flash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathangular-flash.js
More file actions
87 lines (78 loc) · 3 KB
/
angular-flash.js
File metadata and controls
87 lines (78 loc) · 3 KB
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
(function() {
'use strict';
var app = angular.module('flash', []);
app.run(['$rootScope', function($rootScope) {
// initialize variables
$rootScope.flash = {};
$rootScope.flash.text = '';
$rootScope.flash.type = '';
$rootScope.flash.timeout = 5000;
$rootScope.hasFlash = false;
}]);
// Directive for compiling dynamic html
app.directive('dynamic', ['$compile', function($compile) {
return {
restrict: 'A',
replace: true,
link: function(scope, ele, attrs) {
scope.$watch(attrs.dynamic, function(html) {
ele.html(html);
$compile(ele.contents())(scope);
});
}
};
}]);
// Directive for closing the flash message
app.directive('closeFlash', ['$compile', 'Flash', function($compile, Flash) {
return {
link: function(scope, ele) {
ele.on('click', function() {
Flash.dismiss();
});
}
};
}]);
// Create flashMessage directive
app.directive('flashMessage', ['$compile', '$rootScope', function($compile, $rootScope) {
return {
restrict: 'A',
template: '<div role="alert" ng-show="hasFlash" class="alert tw-ng-animate {{flash.addClass}} alert-{{flash.type}} alert-dismissible ng-hide alertIn alertOut "> <span dynamic="flash.text"></span> <button type="button" class="close" close-flash><span aria-hidden="true">×</span><span class="sr-only">Close</span></button> </div>',
link: function(scope, ele, attrs) {
// get timeout value from directive attribute and set to flash timeout
$rootScope.flash.timeout = parseInt(attrs.flashMessage, 10);
}
};
}]);
app.factory('Flash', ['$rootScope', '$timeout',
function($rootScope, $timeout) {
var dataFactory = {},
timeOut;
// Create flash message
dataFactory.create = function(type, text, addClass) {
var $this = this;
$timeout.cancel(timeOut);
$rootScope.flash.type = type;
$rootScope.flash.text = text;
$rootScope.flash.addClass = addClass;
$timeout(function() {
$rootScope.hasFlash = true;
}, 100);
timeOut = $timeout(function() {
$this.dismiss();
}, $rootScope.flash.timeout);
};
// Cancel flashmessage timeout function
dataFactory.pause = function() {
$timeout.cancel(timeOut);
};
// Dismiss flash message
dataFactory.dismiss = function() {
$timeout.cancel(timeOut);
$timeout(function() {
$rootScope.hasFlash = false;
});
};
return dataFactory;
}
]);
}());