-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathangular.snackbar.js
More file actions
67 lines (45 loc) · 1.74 KB
/
angular.snackbar.js
File metadata and controls
67 lines (45 loc) · 1.74 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
/*
@license Angular Snackbar version 1.0.1
ⓒ 2015 AHN JAE-HA http://github.com/eu81273/angular.snackbar
License: MIT
<div class="snackbar-container" data-snackbar="true" data-snackbar-duration="3000" data-snackbar-remove-delay="200"></div>
*/
(function ( angular ) {
"use strict";
var module = angular.module( "angular.snackbar", [] );
//snackbar service
module.factory("snackbar", ['$rootScope', function ($rootScope) {
return {
create : function ( content, duration ) {
$rootScope.$broadcast('createSnackbar', { 'content': content, 'duration': duration });
}
}
}]);
//snackbar directive
module.directive( "snackbar", ["$rootScope", "$compile", "$timeout", function($rootScope, $compile, $timeout) {
return function ( scope, element, attrs ) {
//snackbar container
var snackbarContainer = angular.element(element);
//snackbar duration time (ms)
var snackbarDuration = attrs.snackbarDuration || 3000;
//delay time to remove from DOM after hide (ms)
var snackbarRemoveDelay = attrs.snackbarRemoveDelay || 200;
//receive broadcating
$rootScope.$on('createSnackbar', function(event, received) {
//snackbar template
var template = "<div class=\"snackbar snackbar-opened\"><span class=\"snackbar-content\">" + received.content + "</span></div>";
//template compile
var snackbar = angular.element($compile(template)(scope));
//add snackbar
snackbarContainer.append(snackbar);
//snackbar duration time
$timeout(function () {
//hide snackbar
snackbar.removeClass("snackbar-opened");
//remove snackbar
$timeout(function () { snackbar.remove(); }, snackbarRemoveDelay, false);
}, received.duration || snackbarDuration, false);
});
};
}]);
})(angular);