-
Notifications
You must be signed in to change notification settings - Fork 3
/
ps-responsive.js
88 lines (77 loc) · 2.89 KB
/
ps-responsive.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
angular.module('psResponsive', [])
.value('psResponsiveConfig', {
sizes: [{
name: 'tiny',
minWidth: 0
}, {
name: 'small',
minWidth: 768
}, {
name: 'medium',
minWidth: 992
}, {
name: 'large',
minWidth: 1200
}]
})
.factory('psResponsive', ['$window', '$filter', '$rootScope', 'psResponsiveConfig',
function($window, $filter, $rootScope, psResponsiveConfig) {
var opRegEx = /[<>]=?\s\w+/,
forEach = angular.forEach,
filter = angular.filter,
sizes = psResponsiveConfig.sizes;
sizes = $filter('orderBy')(sizes, '-minWidth');
var getHeight = function() {
return $window.innerHeight;
},
getWidth = function() {
return $window.innerWidth;
},
getLabel = function() {
var cWidth = getWidth(),
returnVal = false;
for (var i = 0; i < sizes.length; i++) {
if (parseInt(cWidth) >= parseInt(sizes[i].minWidth)) {
return sizes[i].name;
}
}
},
getWidthFromLabel = function(label) {
return $filter('filter')(sizes, {
name: label
}, true)[0]["minWidth"];
},
getTest = function(test) {
var thingy = test.split(' ')[0],
against = test.split(' ')[1];
if (isNaN(against)) {
return eval('(' + getWidth() + ' ' + thingy + ' ' + getWidthFromLabel(against) + ')');
} else {
return eval('(' + getWidth() + thingy + parseInt(against) + ')');
}
},
getOrientation = function(){
if(getHeight() > getWidth()) return 'portrait';
else return 'landscape';
};
angular.element($window).on('resize', function() {
$rootScope.$digest();
});
return function(onwha) {
if (!onwha) {
return getLabel();
} else if (onwha == 'width') {
return getWidth();
} else if (onwha == 'height') {
return getHeight();
} else if (onwha == 'orientation') {
return getOrientation();
} else if (opRegEx.test(onwha)) {
return getTest(onwha);
} else {
return (getLabel() == onwha);
}
return false;
};
}
]);