-
Notifications
You must be signed in to change notification settings - Fork 13.5k
/
Copy pathpopover.js
209 lines (190 loc) · 6.94 KB
/
popover.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/**
* @ngdoc service
* @name $ionicPopover
* @module ionic
* @description
*
* Related: {@link ionic.controller:ionicPopover ionicPopover controller}.
*
* The Popover is a view that floats above an app’s content. Popovers provide an
* easy way to present or gather information from the user and are
* commonly used in the following situations:
*
* - Show more info about the current view
* - Select a commonly used tool or configuration
* - Present a list of actions to perform inside one of your views
*
* Put the content of the popover inside of an `<ion-popover-view>` element.
*
* @usage
* ```html
* <p>
* <button ng-click="openPopover($event)">Open Popover</button>
* </p>
*
* <script id="my-popover.html" type="text/ng-template">
* <ion-popover-view>
* <ion-header-bar>
* <h1 class="title">My Popover Title</h1>
* </ion-header-bar>
* <ion-content>
* Hello!
* </ion-content>
* </ion-popover-view>
* </script>
* ```
* ```js
* angular.module('testApp', ['ionic'])
* .controller('MyController', function($scope, $ionicPopover) {
* $ionicPopover.fromTemplateUrl('my-popover.html', {
* scope: $scope,
* }).then(function(popover) {
* $scope.popover = popover;
* });
* $scope.openPopover = function($event) {
* $scope.popover.show($event);
* };
* $scope.closePopover = function() {
* $scope.popover.hide();
* };
* //Cleanup the popover when we're done with it!
* $scope.$on('$destroy', function() {
* $scope.popover.remove();
* });
* // Execute action on hide popover
* $scope.$on('popover.hidden', function() {
* // Execute action
* });
* // Execute action on remove popover
* $scope.$on('popover.removed', function() {
* // Execute action
* });
* });
* ```
*/
IonicModule
.factory('$ionicPopover', ['$ionicModal', '$ionicPosition', '$document', '$window',
function($ionicModal, $ionicPosition, $document, $window) {
var POPOVER_BODY_PADDING = 6;
var POPOVER_OPTIONS = {
viewType: 'popover',
hideDelay: 1,
animation: 'none',
positionView: positionView
};
function positionView(target, popoverEle) {
var targetEle = angular.element(target.target || target);
var buttonOffset = $ionicPosition.offset( targetEle );
var popoverWidth = popoverEle.prop('offsetWidth');
var popoverHeight = popoverEle.prop('offsetHeight');
var bodyWidth = $document[0].body.clientWidth;
// clientHeight doesn't work on all platforms for body
var bodyHeight = $window.innerHeight;
var popoverCSS = {
left: buttonOffset.left + buttonOffset.width / 2 - popoverWidth / 2
};
var arrowEle = jqLite(popoverEle[0].querySelector('.popover-arrow'));
if (popoverCSS.left < POPOVER_BODY_PADDING) {
popoverCSS.left = POPOVER_BODY_PADDING;
} else if(popoverCSS.left + popoverWidth + POPOVER_BODY_PADDING > bodyWidth) {
popoverCSS.left = bodyWidth - popoverWidth - POPOVER_BODY_PADDING;
}
// If the popover when popped down stretches past bottom of screen,
// make it pop up
if (buttonOffset.top + buttonOffset.height + popoverHeight > bodyHeight) {
popoverCSS.top = buttonOffset.top - popoverHeight;
popoverEle.removeClass('popover-top').addClass('popover-bottom');
} else {
popoverCSS.top = buttonOffset.top + buttonOffset.height;
popoverEle.removeClass('popover-bottom').addClass('popover-top');
}
arrowEle.css({
left: buttonOffset.left + buttonOffset.width / 2 -
arrowEle.prop('offsetWidth') / 2 - popoverCSS.left + 'px'
});
popoverEle.css({
top: popoverCSS.top + 'px',
left: popoverCSS.left + 'px',
marginLeft: '0',
opacity: '1'
});
}
/**
* @ngdoc controller
* @name ionicPopover
* @module ionic
* @description
* Instantiated by the {@link ionic.service:$ionicPopover} service.
*
* Be sure to call [remove()](#remove) when you are done with each popover
* to clean it up and avoid memory leaks.
*
* Note: a popover will broadcast 'popover.shown', 'popover.hidden', and 'popover.removed' events from its originating
* scope, passing in itself as an event argument. Both the popover.removed and popover.hidden events are
* called when the popover is removed.
*/
/**
* @ngdoc method
* @name ionicPopover#initialize
* @description Creates a new popover controller instance.
* @param {object} options An options object with the following properties:
* - `{object=}` `scope` The scope to be a child of.
* Default: creates a child of $rootScope.
* - `{boolean=}` `focusFirstInput` Whether to autofocus the first input of
* the popover when shown. Default: false.
* - `{boolean=}` `backdropClickToClose` Whether to close the popover on clicking the backdrop.
* Default: true.
* - `{boolean=}` `hardwareBackButtonClose` Whether the popover can be closed using the hardware
* back button on Android and similar devices. Default: true.
*/
/**
* @ngdoc method
* @name ionicPopover#show
* @description Show this popover instance.
* @param {$event} $event The $event or target element which the popover should align
* itself next to.
* @returns {promise} A promise which is resolved when the popover is finished animating in.
*/
/**
* @ngdoc method
* @name ionicPopover#hide
* @description Hide this popover instance.
* @returns {promise} A promise which is resolved when the popover is finished animating out.
*/
/**
* @ngdoc method
* @name ionicPopover#remove
* @description Remove this popover instance from the DOM and clean up.
* @returns {promise} A promise which is resolved when the popover is finished animating out.
*/
/**
* @ngdoc method
* @name ionicPopover#isShown
* @returns boolean Whether this popover is currently shown.
*/
return {
/**
* @ngdoc method
* @name $ionicPopover#fromTemplate
* @param {string} templateString The template string to use as the popovers's
* content.
* @param {object} options Options to be passed to the initialize method.
* @returns {object} An instance of an {@link ionic.controller:ionicPopover}
* controller ($ionicPopover is built on top of $ionicPopover).
*/
fromTemplate: function(templateString, options) {
return $ionicModal.fromTemplate(templateString, ionic.Utils.extend(options || {}, POPOVER_OPTIONS) );
},
/**
* @ngdoc method
* @name $ionicPopover#fromTemplateUrl
* @param {string} templateUrl The url to load the template from.
* @param {object} options Options to be passed to the initialize method.
* @returns {promise} A promise that will be resolved with an instance of
* an {@link ionic.controller:ionicPopover} controller ($ionicPopover is built on top of $ionicPopover).
*/
fromTemplateUrl: function(url, options, _) {
return $ionicModal.fromTemplateUrl(url, options, ionic.Utils.extend(options || {}, POPOVER_OPTIONS) );
}
};
}]);