-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathionic.icon.js
131 lines (111 loc) · 3.46 KB
/
ionic.icon.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
(function(ionic) {
// Define local variables
var defaults = {
type: 'icon',
map: {}
};
/**
* isEmpty() checks if the given
* object is empty.
*
* @param {Object} obj
*
* @return {Boolean}
*/
function isEmpty(obj) {
var obj = Object(obj);
if (Object.keys(obj).length === 0) {
return true
}
return false;
}
angular.module('ionic.contrib.icon', ['ionic'])
/**
* @ngdoc object
* @name $ionicIconConfig
* @module ionic.contrib.icon
* @description
* Sets default behavior of the icon directive.
*
* @param {String} type
* @param {Object} map
*
* @usage
* ```js
* var app = angular.module('app', ['ionic', 'ionic.contrib.icon'])
* app.constant('$ionicIconConfig', {
* type: 'icon',
* map: {
* 'ion-heart': {
* ios: 'ion-ios7-heart',
* android: 'ion-android-heart'
* }
* }
* });
* ```
*/
.constant('$ionicIconConfig', defaults)
/**
* @ngdoc directive
* @name icon
* @module ionic.contrib.icon
* @restrict E
*
* @description
* The icon directive automatically displays platform-specific icons based on passed options
* defined by the $ionicIconConfig constant.
*
* @param {string=} ios
* @param {string=} ipad
* @param {string=} android
* @param {string=} windows
* @param {string=} default Required default icon to use.
*/
.directive('icon', function($ionicIconConfig) {
return {
restrict: 'E',
scope: {
ios: '@',
ipad: '@',
android: '@',
windows: '@',
default: '@'
},
link: function($scope, $element, $attrs) {
var options = $ionicIconConfig;
function setIcon(icon) {
// Check if icon is empty
if (isEmpty(icon)) {
icon = {};
}
// Set icon depending on device's platform
if (ionic.Platform.isIPad()) {
$scope.platform = ($scope.ipad || icon.ipad);
} else if (ionic.Platform.isIOS()) {
$scope.platform = ($scope.ios || icon.ios);
} else if (ionic.Platform.isAndroid()) {
$scope.platform = ($scope.android || icon.android);
} else if (ionic.Platform.isWindowsPhone()) {
$scope.platform = ($scope.windows || icon.windows);
}
// By default use the following icon (if iOS or Android not set)
if (!$scope.platform) {
$scope.platform = $scope.default;
}
}
// Check if using icon mapping
if (!isEmpty(options.map) && options.map[$scope.default]) {
setIcon(options.map[$scope.default]);
} else {
setIcon();
}
// Set icon type (library to use)
$scope.type = $attrs['class'];
if (!$scope.type) {
$scope.type = (options.type || defaults.type);
}
},
template: '<i class="{{ type }} {{ platform }}"></i>'
};
});
})(window.ionic);