forked from sechel/please.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
please.min.js
3 lines (3 loc) · 6.18 KB
/
please.min.js
1
2
3
/*! please.js - v0.1.0 - 2014-02-19
* Copyright (c) 2014 Wingify; Licensed MIT */
!function($,window){"use strict";function pathErrorHelper(a,b){var c=a.length,d=a.slice(0,b+1);return d.unshift("window"),{lastPathPart:a[c-1],fullPath:a.slice(0,c-1).join("."),currentPath:d.join(".")}}function Request(){this.init.apply(this,[].slice.call(arguments))}function Response(a){this.init(a)}function UnserializableResponseData(a){this.id=a,this.type="unserializable"}var console=window.console,defaults={targetWindow:window,targetOrigin:"*",sourceOrigin:!1},please=function(a,b){return $.extend(please.bind(),{targetWindow:a,targetOrigin:b,call:please.call,set:please.set,get:please.get,eval:please.eval,$:please.$})},requests={},responses={};window.please=please,please.requests=requests,please.responses=responses,please.defaults=function(a){return $.extend(!0,defaults,a),please},please.init=function(a){return a.addEventListener("message",please_messageHandler),please};var please_request=function(a){return function(){var b=new Request(a);return b.targetWindow=this.targetWindow||defaults.targetWindow,b.targetOrigin=this.targetOrigin||defaults.targetOrigin,b.data=[].slice.call(arguments),b.send(),b}},please_messageHandler=function(a){if(!$.isFunction(defaults.sourceOrigin)||defaults.sourceOrigin(a)){var b;try{b=JSON.parse(a.data)}catch(c){return void console.log("error parsing json data")}if("request"===b.type){var d=new Response(b);responses[d.id]=d.data,d.targetWindow=a.source,d.targetOrigin="null"===a.origin?defaults.targetOrigin:a.origin,d.send()}else"response"===b.type&&(b.data&&"unserializable"===b.data.type&&(b.data=UnserializableResponseData.create(b.data)),b.success?requests[b.id].resolve(b.data):requests[b.id].reject(new please.Error(b.data)),delete requests[b.id])}};please.call=please_request("call"),please.set=please_request("set"),please.get=please_request("get"),please.eval=please_request("eval"),please.$=please_request("$"),please.$=function(){for(var a,b=please_request("$").apply(this,[].slice.call(arguments)),c=Object.keys($.fn),d=function(a){for(var b in a)"function"==typeof a[b]&&(a[b]=a[b].toString())},e=function(a){return a.map(function(a){return"function"==typeof a?a.toString():("object"==typeof a&&($.extend(!0,{},a),d(a)),a)})},f=function(a){return function(){var b=[].slice.call(arguments);b=e(b);var c=new Request("$_fn");return c.targetWindow=this.targetWindow||defaults.targetWindow,c.targetOrigin=this.targetOrigin||defaults.targetOrigin,c.data=[this,a].concat(b),c.send(),c}},g=0,h=c.length;h>g;g++)a=c[g],"constructor"!==a&&"init"!==a&&"promise"!==a&&"function"==typeof $.fn[a]&&(b[a]=f(a));var i=["draggable","sortable"];for(g=0,h=i.length;h>g;g++)a=i[g],b[a]=f(a);return b};var _please={};_please.call=function(a){var b=a.split("."),c=window,d=c,e=[].slice.call(arguments,1);if(b.forEach(function(a,e){e===b.length-1&&(c=d),d=d[a]}),"function"==typeof d)return d.apply(c,e);throw new please.Error({name:"TypeError",message:"'"+a+"' is not a function"})},_please.set=function(a,b){var c=a.split("."),d=window,e=c.length;c.forEach(function(a,f){if(f===e-1)d[a]=b;else if(d=d[a],"undefined"==typeof d||null===d){var g=pathErrorHelper(c,f);throw new please.Error({name:"TypeError",message:"Can not set '"+g.lastPathPart+"' on '"+g.fullPath+"', because path element '"+g.currentPath+"' is null or undefined"})}})},_please.get=function(a){var b=a.split("."),c=window;return b.forEach(function(a,d){if("undefined"==typeof c||null===c){var e=pathErrorHelper(b,d-1);throw new please.Error({name:"TypeError",message:"Can not get '"+e.lastPathPart+"' of '"+e.fullPath+"', because path element '"+e.currentPath+"' is null or undefined"})}c=c[a]}),c},_please.eval=function(a){return $.globalEval(a)},_please.$=function(){return $.apply($,[].slice.call(arguments))},_please.$_fn=function(parentReq,funcName){var $jq=responses[parentReq.id];if(!($jq instanceof $))return null;for(var args=[].slice.call(arguments,2),mFn=function(arg){if("string"==typeof arg)try{var fn;return eval("fn = "+arg),"function"==typeof fn?fn:arg}catch(e){return arg}return"object"==typeof arg&&mapObjectStringsToFunctions(arg),arg},i=0;i<args.length;i++)args[i]=mFn(args[i]);var retval;return retval="length"===funcName?$jq.length:$jq[funcName].apply($jq,args)},Request.prototype={init:function(a){$.extend(this,$.Deferred());for(var b=+new Date;b===+new Date;);b=+new Date,this.id=b,this.name=a,this.data=[].slice.call(arguments),this.type="request",requests[b]=this},send:function(){this.targetWindow=this.targetWindow||defaults.targetWindow,this.targetOrigin=this.targetOrigin||defaults.targetOrigin;try{var a=this.data,b=a instanceof $?a.toArray():a;if(b&&b.length&&b[0]instanceof Node)throw"";this.targetWindow.postMessage(JSON.stringify(this),this.targetOrigin)}catch(c){this.targetWindow.postMessage(new UnserializableResponseData(this.id),this.targetOrigin)}},perform:function(){return _please[this.name].apply(this,this.data)},toJSON:function(){return{id:this.id,name:this.name,type:this.type,data:this.data}}},Request.create=function(a){return $.extend(new Request,a)},Response.prototype={init:function(a){this.id=a.id,this.name=a.name,this.type="response";try{this.data=Request.create(a).perform(),this.success=!0}catch(b){this.data=new please.Error(b),this.success=!1}},send:function(){try{this.targetWindow=this.targetWindow||defaults.targetWindow,this.targetOrigin=this.targetOrigin||defaults.targetOrigin;var a=this.data,b=a instanceof $?a.toArray():a;if(b&&b.length&&b[0]instanceof Node)throw"";var c=JSON.stringify(this);this.targetWindow.postMessage(c,this.targetOrigin)}catch(d){this.data=new UnserializableResponseData(this.id),this.targetWindow.postMessage(JSON.stringify(this),this.targetOrigin)}finally{if(!this.success)throw this.data.error}},toJSON:function(){return{id:this.id,name:this.name,type:this.type,data:this.data,success:this.success}}},UnserializableResponseData.create=function(a){return $.extend(new UnserializableResponseData,a)},please.Error=function(a){this.error=a,$.extend(this,a),this.name=a.name,this.message=a.message,this.number=a.number,this.description=a.description,this.fileName=a.fileName,this.lineNumber=a.lineNumber,this.stack=a.stack},please.Request=Request,please.Response=Response,please.UnserializableResponseData=UnserializableResponseData}(jQuery,window);