forked from sintaxi/phonegap
-
Notifications
You must be signed in to change notification settings - Fork 1
/
orientation.js
67 lines (62 loc) · 2.45 KB
/
orientation.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
/**
* This class provides access to the device orientation.
* @constructor
*/
function Orientation() {
/**
* The current orientation, or null if the orientation hasn't changed yet.
*/
this.currentOrientation = null;
}
/**
* Set the current orientation of the phone. This is called from the device automatically.
*
* When the orientation is changed, the DOMEvent \c orientationChanged is dispatched against
* the document element. The event has the property \c orientation which can be used to retrieve
* the device's current orientation, in addition to the \c Orientation.currentOrientation class property.
*
* @param {Number} orientation The orientation to be set
*/
Orientation.prototype.setOrientation = function(orientation) {
Orientation.currentOrientation = orientation;
var e = document.createEvent('Events');
e.initEvent('orientationChanged', 'false', 'false');
e.orientation = orientation;
document.dispatchEvent(e);
};
/**
* Asynchronously aquires the current orientation.
* @param {Function} successCallback The function to call when the orientation
* is known.
* @param {Function} errorCallback The function to call when there is an error
* getting the orientation.
*/
Orientation.prototype.getCurrentOrientation = function(successCallback, errorCallback) {
// If the position is available then call success
// If the position is not available then call error
};
/**
* Asynchronously aquires the orientation repeatedly at a given interval.
* @param {Function} successCallback The function to call each time the orientation
* data is available.
* @param {Function} errorCallback The function to call when there is an error
* getting the orientation data.
*/
Orientation.prototype.watchOrientation = function(successCallback, errorCallback) {
// Invoke the appropriate callback with a new Position object every time the implementation
// determines that the position of the hosting device has changed.
this.getCurrentPosition(successCallback, errorCallback);
return setInterval(function() {
navigator.orientation.getCurrentOrientation(successCallback, errorCallback);
}, 10000);
};
/**
* Clears the specified orientation watch.
* @param {String} watchId The ID of the watch returned from #watchOrientation.
*/
Orientation.prototype.clearWatch = function(watchId) {
clearInterval(watchId);
};
PhoneGap.addConstructor(function() {
if (typeof navigator.orientation == "undefined") navigator.orientation = new Orientation();
});