diff --git a/examples/please-dev.html b/examples/please-dev.html new file mode 100644 index 0000000..495a9e7 --- /dev/null +++ b/examples/please-dev.html @@ -0,0 +1,12 @@ + + + + + Title + + + + + + + \ No newline at end of file diff --git a/package.json b/package.json index 77b7aa7..691fa83 100644 --- a/package.json +++ b/package.json @@ -13,13 +13,16 @@ "bugs": { "url": "https://github.com/Wingify/pleasejs/issues" }, - "license":"MIT", + "license": "MIT", "keywords": [], "devDependencies": { "grunt": "~0.4.4", - "grunt-contrib-uglify": "~0.2.2", "grunt-contrib-jshint": "~0.8.0", "grunt-contrib-qunit": "~0.4.0", + "grunt-contrib-uglify": "~0.2.2", "grunt-contrib-watch": "~0.6.1" + }, + "dependencies": { + "q": "^1.5.0" } } diff --git a/src/please.js b/src/please.js index aee7f11..5238f22 100644 --- a/src/please.js +++ b/src/please.js @@ -1,748 +1,857 @@ -(window.__pleaseClosure = function ($, window) { +(window.__pleaseClosure = function (self) { 'use strict'; - var console = window.console; // jshint unused: false - -var defaults = { - targetWindow: window, - targetOrigin: '*', - sourceOrigin: false, - possiblyOverriddenNativeFunctions: [] -}; - -var pristineWindow; -var usePristineFunctionDefinitions = true; - -function getContextAndLastPathPart(context, path) { - var arr = path.split('.'); - for (var i = 0, il = arr.length - 1; i < il; i++) { - context = context && context[arr[i]]; - path = arr[i + 1]; - } - return { - context: context, - lastPathPart: path - }; -} - -function overrideNativeFunctions(fns) { - if (!pristineWindow) { return; } - // not supported below ECMAScript 5.0 - if (!Object.defineProperty) { return; } - return fns.map(function (funcPath) { - var actualObj = getContextAndLastPathPart(window, funcPath); - if (!actualObj.context) { return; } - var actualVal = actualObj.context[actualObj.lastPathPart]; - var pristineObj = getContextAndLastPathPart(pristineWindow, funcPath); - var pristineVal = pristineObj.context[pristineObj.lastPathPart]; - try { - Object.defineProperty(actualObj.context, actualObj.lastPathPart, { - configurable: true, - enumerable: false, - get: function () { - return usePristineFunctionDefinitions ? pristineVal : actualVal; - }, - set: function (val) { - if (this === Object.prototype) { - actualVal = val; - } else { - Object.defineProperty(this, actualObj.lastPathPart, { - writable: true, - configurable: true, - enumerable: false, - value: val - }); - } - } - }); - } catch (e) {} - - return function restoreFunc() { - delete actualObj.context[actualObj.lastPathPart]; - actualObj.context[actualObj.lastPathPart] = actualVal; - }; - }).reduce(function (a, b) { - return function () { - return (a && a()) || (b && b()); - }; - }, null); -} - -var restoreNativeFunctions; - -function isNode(node) { - return node.hasOwnProperty('nodeName') && node.hasOwnProperty('nodeType'); -} - -/** - * The please global object. Can be used both as an object and a function. - * - * @param {String} [targetWindow] The reference to the window to pass messages to. - * @param {String} [targetOrigin] What the target origin of the other window must be. - * @return {Object} A please object instance. - */ -/* jshint evil: true */ -var please = function Please(targetWindow, targetOrigin) { - if (this instanceof Please) { - return; - } - var object = new Please(); - object.targetWindow = targetWindow; - object.targetOrigin = targetOrigin; - return object; -}; - -please.prototype = please; - -var requests = {}, responses = {}; -please.requests = requests; -please.responses = responses; - -/** - * Sets the default targetWindow to send message to, and the targetOrigin of that window. - * - * @param {String} values Params to use as defaults. - * @return {Object} A please object instance. - */ -please.defaults = function (values) { - if (arguments.length === 0) { - // getter - return defaults; - } - $.extend(true, defaults, values); - return please; -}; - -/** - * Initialize please. In both the windows (frames), add the below code: - * - * @param {Window} thisWindow The reference to the current window. - * @return {Object} A please object instance. - */ -please.init = function (thisWindow) { - pristineWindow = $('