forked from snapjay/angularjs-breakpoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakpoint-0.0.1.js
59 lines (51 loc) · 2.08 KB
/
breakpoint-0.0.1.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
/*!
* angularjs-breakpoint v0.0.1
*
* BREAKPOINT DIRECTIVE FOR RESPONSIVE WEBSITES
*
* http://snapjay.github.com/angularjs-breakpoint/
* Apply as a attribute of the body tag. Set
* breakpoint="{1250:'break1250', 1000:'break1000',1120:'break1120'}
*
* Values are available on scope as
* {{breakpoint.class}} = current set class
* {{breakpoint.windowSize}} = current width of window
*/
var breakpointApp = angular.module('breakpointApp',[]);
breakpointApp.directive('breakpoint', ['$window', '$rootScope', function($window, $rootScope){
return {
restrict:"A",
link:function(scope, element, attr){
scope.breakpoint = {class:'', windowSize:$window.innerWidth }; // Initialise Values
var breakpoints = (scope.$eval(attr.breakpoint));
var firstTime = true;
angular.element($window).bind('resize', setWindowSize);
scope.$watch('breakpoint.windowSize', function(windowWidth, oldValue){
setClass(windowWidth);
});
scope.$watch('breakpoint.class', function(newClass, oldClass) {
if (newClass != oldClass || firstTime) {
broadcastEvent(oldClass);
firstTime = false;
}
});
function broadcastEvent (oldClass) {
$rootScope.$broadcast('breakpointChange', scope.breakpoint, oldClass);
}
function setWindowSize (){
scope.breakpoint.windowSize = $window.innerWidth;
if(!scope.$$phase) scope.$apply();
}
function setClass(windowWidth){
var setClass = breakpoints[Object.keys(breakpoints)[0]];
for (var breakpoint in breakpoints){
if (breakpoint < windowWidth) setClass = breakpoints[breakpoint];
element.removeClass(breakpoints[breakpoint]);
}
element.addClass(setClass);
scope.breakpoint.class = setClass;
if(!scope.$$phase) scope.$apply();
}
}
}
}]);