mirror of
https://github.com/apache/cordova-android.git
synced 2025-02-20 23:56:20 +08:00
Formalize document and window event listeners and allow plugins to override eventListeners.
This commit is contained in:
parent
8c7db9aa32
commit
a735a631f6
@ -27,7 +27,7 @@ var Connection = function() {
|
||||
// set a timer if still offline at the end of timer send the offline event
|
||||
me._timer = setTimeout(function(){
|
||||
me.type = type;
|
||||
PhoneGap.fireEvent('offline');
|
||||
PhoneGap.fireDocumentEvent('offline');
|
||||
me._timer = null;
|
||||
}, me.timeout);
|
||||
} else {
|
||||
@ -37,7 +37,7 @@ var Connection = function() {
|
||||
me._timer = null;
|
||||
}
|
||||
me.type = type;
|
||||
PhoneGap.fireEvent('online');
|
||||
PhoneGap.fireDocumentEvent('online');
|
||||
}
|
||||
|
||||
// should only fire this once
|
||||
|
@ -46,7 +46,9 @@ var PhoneGap = {
|
||||
ready: true,
|
||||
commands: [],
|
||||
timer: null
|
||||
}
|
||||
},
|
||||
documentEventHandler: {}, // Collection of custom document event handlers
|
||||
windowEventHandler: {} // Collection of custom window event handlers
|
||||
};
|
||||
|
||||
/**
|
||||
@ -381,6 +383,36 @@ document.addEventListener('DOMContentLoaded', function() {
|
||||
// Intercept calls to document.addEventListener and watch for deviceready
|
||||
PhoneGap.m_document_addEventListener = document.addEventListener;
|
||||
|
||||
// Intercept calls to window.addEventListener
|
||||
PhoneGap.m_window_addEventListener = window.addEventListener;
|
||||
|
||||
/**
|
||||
* Add a custom window event handler.
|
||||
*
|
||||
* @param {String} event The event name that callback handles
|
||||
* @param {Function} callback The event handler
|
||||
*/
|
||||
PhoneGap.addWindowEventHandler = function(event, callback) {
|
||||
PhoneGap.windowEventHandler[event] = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a custom document event handler.
|
||||
*
|
||||
* @param {String} event The event name that callback handles
|
||||
* @param {Function} callback The event handler
|
||||
*/
|
||||
PhoneGap.addDocumentEventHandler = function(event, callback) {
|
||||
PhoneGap.documentEventHandler[event] = callback;
|
||||
}
|
||||
|
||||
/**
|
||||
* Intercept adding document event listeners and handle our own
|
||||
*
|
||||
* @param {Object} evt
|
||||
* @param {Function} handler
|
||||
* @param capture
|
||||
*/
|
||||
document.addEventListener = function(evt, handler, capture) {
|
||||
var e = evt.toLowerCase();
|
||||
if (e === 'deviceready') {
|
||||
@ -398,15 +430,52 @@ document.addEventListener = function(evt, handler, capture) {
|
||||
if (e === 'backbutton') {
|
||||
PhoneGap.exec(null, null, "App", "overrideBackbutton", [true]);
|
||||
}
|
||||
|
||||
|
||||
// If subscribing to an event that is handled by a plugin
|
||||
else if (typeof PhoneGap.documentEventHandler[e] !== "undefined") {
|
||||
if (PhoneGap.documentEventHandler[e](e, handler, true)) {
|
||||
return; // Stop default behavior
|
||||
}
|
||||
}
|
||||
|
||||
PhoneGap.m_document_addEventListener.call(document, evt, handler, capture);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Intercept adding window event listeners and handle our own
|
||||
*
|
||||
* @param {Object} evt
|
||||
* @param {Function} handler
|
||||
* @param capture
|
||||
*/
|
||||
window.addEventListener = function(evt, handler, capture) {
|
||||
var e = evt.toLowerCase();
|
||||
|
||||
// If subscribing to an event that is handled by a plugin
|
||||
if (typeof PhoneGap.windowEventHandler[e] !== "undefined") {
|
||||
if (PhoneGap.windowEventHandler[e](e, handler, true)) {
|
||||
return; // Stop default behavior
|
||||
}
|
||||
}
|
||||
|
||||
PhoneGap.m_window_addEventListener.call(window, evt, handler, capture);
|
||||
};
|
||||
|
||||
// Intercept calls to document.removeEventListener and watch for events that
|
||||
// are generated by PhoneGap native code
|
||||
PhoneGap.m_document_removeEventListener = document.removeEventListener;
|
||||
|
||||
// Intercept calls to window.removeEventListener
|
||||
PhoneGap.m_window_removeEventListener = window.removeEventListener;
|
||||
|
||||
/**
|
||||
* Intercept removing document event listeners and handle our own
|
||||
*
|
||||
* @param {Object} evt
|
||||
* @param {Function} handler
|
||||
* @param capture
|
||||
*/
|
||||
document.removeEventListener = function(evt, handler, capture) {
|
||||
var e = evt.toLowerCase();
|
||||
|
||||
@ -415,18 +484,70 @@ document.removeEventListener = function(evt, handler, capture) {
|
||||
PhoneGap.exec(null, null, "App", "overrideBackbutton", [false]);
|
||||
}
|
||||
|
||||
// If unsubcribing from an event that is handled by a plugin
|
||||
if (typeof PhoneGap.documentEventHandler[e] !== "undefined") {
|
||||
if (PhoneGap.documentEventHandler[e](e, handler, false)) {
|
||||
return; // Stop default behavior
|
||||
}
|
||||
}
|
||||
|
||||
PhoneGap.m_document_removeEventListener.call(document, evt, handler, capture);
|
||||
};
|
||||
|
||||
/**
|
||||
* Method to fire event from native code
|
||||
* Intercept removing window event listeners and handle our own
|
||||
*
|
||||
* @param {Object} evt
|
||||
* @param {Function} handler
|
||||
* @param capture
|
||||
*/
|
||||
PhoneGap.fireEvent = function(type) {
|
||||
window.removeEventListener = function(evt, handler, capture) {
|
||||
var e = evt.toLowerCase();
|
||||
|
||||
// If unsubcribing from an event that is handled by a plugin
|
||||
if (typeof PhoneGap.windowEventHandler[e] !== "undefined") {
|
||||
if (PhoneGap.windowEventHandler[e](e, handler, false)) {
|
||||
return; // Stop default behavior
|
||||
}
|
||||
}
|
||||
|
||||
PhoneGap.m_window_removeEventListener.call(window, evt, handler, capture);
|
||||
};
|
||||
|
||||
/**
|
||||
* Method to fire document event
|
||||
*
|
||||
* @param {String} type The event type to fire
|
||||
* @param {Object} data Data to send with event
|
||||
*/
|
||||
PhoneGap.fireDocumentEvent = function(type, data) {
|
||||
var e = document.createEvent('Events');
|
||||
e.initEvent(type);
|
||||
if (data) {
|
||||
for (var i in data) {
|
||||
e[i] = data[i];
|
||||
}
|
||||
}
|
||||
document.dispatchEvent(e);
|
||||
};
|
||||
|
||||
/**
|
||||
* Method to fire window event
|
||||
*
|
||||
* @param {String} type The event type to fire
|
||||
* @param {Object} data Data to send with event
|
||||
*/
|
||||
PhoneGap.fireWindowEvent = function(type, data) {
|
||||
var e = document.createEvent('Events');
|
||||
e.initEvent(type);
|
||||
if (data) {
|
||||
for (var i in data) {
|
||||
e[i] = data[i];
|
||||
}
|
||||
}
|
||||
window.dispatchEvent(e);
|
||||
};
|
||||
|
||||
/**
|
||||
* If JSON not included, use our own stringify. (Android 1.6)
|
||||
* The restriction on ours is that it must be an array of simple types.
|
||||
|
10
framework/src/com/phonegap/DroidGap.java
Normal file → Executable file
10
framework/src/com/phonegap/DroidGap.java
Normal file → Executable file
@ -1256,7 +1256,7 @@ public class DroidGap extends PhonegapActivity {
|
||||
|
||||
// If back key is bound, then send event to JavaScript
|
||||
if (this.bound) {
|
||||
this.appView.loadUrl("javascript:PhoneGap.fireEvent('backbutton');");
|
||||
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('backbutton');");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1278,13 +1278,13 @@ public class DroidGap extends PhonegapActivity {
|
||||
|
||||
// If menu key
|
||||
else if (keyCode == KeyEvent.KEYCODE_MENU) {
|
||||
this.appView.loadUrl("javascript:PhoneGap.fireEvent('menubutton');");
|
||||
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('menubutton');");
|
||||
return true;
|
||||
}
|
||||
|
||||
// If search key
|
||||
else if (keyCode == KeyEvent.KEYCODE_SEARCH) {
|
||||
this.appView.loadUrl("javascript:PhoneGap.fireEvent('searchbutton');");
|
||||
this.appView.loadUrl("javascript:PhoneGap.fireDocumentEvent('searchbutton');");
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -1478,13 +1478,13 @@ public class DroidGap extends PhonegapActivity {
|
||||
// gone away.
|
||||
else if (height > oldHeight) {
|
||||
Log.d(LOG_TAG, "Throw hide keyboard event");
|
||||
callbackServer.sendJavascript("PhoneGap.fireEvent('hidekeyboard');");
|
||||
callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('hidekeyboard');");
|
||||
}
|
||||
// If the height as gotten smaller then we will assume the soft keyboard has
|
||||
// been displayed.
|
||||
else if (height < oldHeight) {
|
||||
Log.d(LOG_TAG, "Throw show keyboard event");
|
||||
callbackServer.sendJavascript("PhoneGap.fireEvent('showkeyboard');");
|
||||
callbackServer.sendJavascript("PhoneGap.fireDocumentEvent('showkeyboard');");
|
||||
}
|
||||
|
||||
// Update the old height for the next event
|
||||
|
Loading…
Reference in New Issue
Block a user