-
Notifications
You must be signed in to change notification settings - Fork 70
/
ng-device-detector.js
66 lines (61 loc) · 2.98 KB
/
ng-device-detector.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
(function (angular) {
"use strict";
angular.module("ng.deviceDetector", ["reTree", "uaDeviceDetector"])
.service("detectUtils", ["deviceDetector", "uaDeviceDetector",
function (deviceDetector, uaDeviceDetector) {
var deviceInfo = deviceDetector;
this.isMobile = function () {
return deviceInfo.device !== 'unknown';
};
this.isAndroid = function () {
return (deviceInfo.device === uaDeviceDetector.DEVICES.ANDROID || deviceInfo.OS === uaDeviceDetector.OS.ANDROID);
};
this.isIOS = function () {
return (deviceInfo.os === uaDeviceDetector.OS.IOS || deviceInfo.device === uaDeviceDetector.DEVICES.I_POD ||
deviceInfo.device === uaDeviceDetector.DEVICES.IPHONE);
};
}
])
.provider("deviceDetector", function () {
var customDetectors = [];
this.addCustom = function (customDetectorName, customDetectorRE) {
customDetectors.push({ name: customDetectorName, re: customDetectorRE });
};
this.$get = [
"$window",
"uaDeviceDetector",
"reTree",
function (
$window,
uaDeviceDetector,
reTree
) {
var ua = $window.navigator.userAgent;
var platform = $window.navigator.platform;
var maxTouchPoints= $window.navigator.maxTouchPoints;
var deviceInfo = uaDeviceDetector.parseUserAgent(ua, customDetectors, platform, maxTouchPoints );
deviceInfo.parseUserAgent = function (ua, platform, maxTouchPoints) { return uaDeviceDetector.parseUserAgent(ua, customDetectors, platform, maxTouchPoints) };
return deviceInfo;
}];
}
)
.directive('deviceDetector', ["deviceDetector", function (deviceDetector) {
function customClassName(name) {
return 'is-' + name.toLowerCase().replace(/[^0-9a-z]+/g, '-');
}
return {
restrict: "A",
link: function (scope, elm/*, attrs*/) {
elm.addClass('os-' + deviceDetector.os);
elm.addClass('browser-' + deviceDetector.browser);
elm.addClass('device-' + deviceDetector.device);
elm.toggleClass('is-mobile', deviceDetector.isMobile());
elm.toggleClass('is-tablet', deviceDetector.isTablet());
elm.toggleClass('is-desktop', deviceDetector.isDesktop());
Object.keys(deviceDetector.custom).forEach(function (customKey) {
elm.toggleClass(customClassName(customKey), deviceDetector.custom[customKey]);
});
}
};
}]);
})(angular);