日期:2014-05-16 浏览次数:20447 次
// 导入cordova window.cordova = require('cordova'); // 启动处理 require('cordova/init');
// file: src/cordova.js define("cordova", function(require, exports, module) { // 调用通道和平台模块 var channel = require('cordova/channel'); var platform = require('cordova/platform'); // 备份document和window的事件监听器 var m_document_addEventListener = document.addEventListener; var m_document_removeEventListener = document.removeEventListener; var m_window_addEventListener = window.addEventListener; var m_window_removeEventListener = window.removeEventListener; // 保存自定义的document和window的事件监听器 var documentEventHandlers = {}, windowEventHandlers = {}; // 拦截document和window的事件监听器(addEventListener/removeEventListener) // 存在自定义的事件监听器的话,使用自定义的;不存在的话调用备份document和window的事件监听器 document.addEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); if (typeof documentEventHandlers[e] != 'undefined') { documentEventHandlers[e].subscribe(handler); } else { m_document_addEventListener.call(document, evt, handler, capture); } }; window.addEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); if (typeof windowEventHandlers[e] != 'undefined') { windowEventHandlers[e].subscribe(handler); } else { m_window_addEventListener.call(window, evt, handler, capture); } }; document.removeEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); if (typeof documentEventHandlers[e] != "undefined") { documentEventHandlers[e].unsubscribe(handler); } else { m_document_removeEventListener.call(document, evt, handler, capture); } }; window.removeEventListener = function(evt, handler, capture) { var e = evt.toLowerCase(); if (typeof windowEventHandlers[e] != "undefined") { windowEventHandlers[e].unsubscribe(handler); } else { m_window_removeEventListener.call(window, evt, handler, capture); } }; // 创建一个指定type的事件。 // 参考:https://developer.mozilla.org/en-US/docs/Web/API/document.createEvent#Notes function createEvent(type, data) { var event = document.createEvent('Events'); // 指定事件名、不可冒泡、不可取消 event.initEvent(type, false, false); // 自定义数据 if (data) { for (var i in data) { if (data.hasOwnProperty(i)) { event[i] = data[i]; } } } return event; } // 外部访问cordova.js的入口 var cordova = { // 模块系统 define:define, require:require, // 版本号和平台名 version:CORDOVA_JS_BUILD_LABEL, platformId:platform.id, // 为了拦截document和window的事件监听器,添加或删除自定义的事件监听器 addWindowEventHandler:function(event) { return (windowEventHandlers[event] = channel.create(event)); }, // sticky 是指一旦被调用那么它以后都保持被调用的状态,所定义的监听器会被立即执行。 // 比如: deviceready事件只触发一次,以后的所有监听都是立即执行的。 addStickyDocumentEventHandler:function(event) { return (documentEventHandlers[event] = channel.createSticky(event)); }, addDocumentEventHandler:function(event) { return (documentEventHandlers[event] = channel.create(event)); }, removeWindowEventHandler:function(event) { delete windowEventHandlers[event]; }, removeDocumentEventHandler:function(event) { delete documentEventHandlers[event]; }, // 获取拦截前的document和window的事件监听器 getOriginalHandlers: function() { return {'document': {'addEventListener': m_document_addEventListener, 'removeEventListener': m_document_removeEventListener}, 'window': {'addEventListener': m_window_addEventListener, 'removeEventListener': m_window_removeEventListener}}; }, // 调用document的事件 fireDocum