YePpHa / UserProxy

/**
  The MIT License (MIT)

  Copyright (c) 2014 Jeppe Rune Mortensen

  Permission is hereby granted, free of charge, to any person obtaining a copy of
  this software and associated documentation files (the "Software"), to deal in
  the Software without restriction, including without limitation the rights to
  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  the Software, and to permit persons to whom the Software is furnished to do so,
  subject to the following conditions:

  The above copyright notice and this permission notice shall be included in all
  copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
// Source code can be found at https://github.com/YePpHa/UserProxy
var UserProxy = (function(){var requirejs,require,define;!function(n){function t(n,t){return m.call(n,t)}function e(n,t){var e,r,i,o,u,f,a,c,s,l,p,d=t&&t.split("/"),v=g.map,y=v&&v["*"]||{};if(n&&"."===n.charAt(0))if(t){for(d=d.slice(0,d.length-1),n=n.split("/"),u=n.length-1,g.nodeIdCompat&&b.test(n[u])&&(n[u]=n[u].replace(b,"")),n=d.concat(n),s=0;s<n.length;s+=1)if(p=n[s],"."===p)n.splice(s,1),s-=1;else if(".."===p){if(1===s&&(".."===n[2]||".."===n[0]))break;s>0&&(n.splice(s-1,2),s-=2)}n=n.join("/")}else 0===n.indexOf("./")&&(n=n.substring(2));if((d||y)&&v){for(e=n.split("/"),s=e.length;s>0;s-=1){if(r=e.slice(0,s).join("/"),d)for(l=d.length;l>0;l-=1)if(i=v[d.slice(0,l).join("/")],i&&(i=i[r])){o=i,f=s;break}if(o)break;!a&&y&&y[r]&&(a=y[r],c=s)}!o&&a&&(o=a,f=c),o&&(e.splice(0,f,o),n=e.join("/"))}return n}function r(t,e){return function(){return s.apply(n,h.call(arguments,0).concat([t,e]))}}function i(n){return function(t){return e(t,n)}}function o(n){return function(t){d[n]=t}}function u(e){if(t(v,e)){var r=v[e];delete v[e],y[e]=!0,c.apply(n,r)}if(!t(d,e)&&!t(y,e))throw new Error("No "+e);return d[e]}function f(n){var t,e=n?n.indexOf("!"):-1;return e>-1&&(t=n.substring(0,e),n=n.substring(e+1,n.length)),[t,n]}function a(n){return function(){return g&&g.config&&g.config[n]||{}}}var c,s,l,p,d={},v={},g={},y={},m=Object.prototype.hasOwnProperty,h=[].slice,b=/\.js$/;l=function(n,t){var r,o=f(n),a=o[0];return n=o[1],a&&(a=e(a,t),r=u(a)),a?n=r&&r.normalize?r.normalize(n,i(t)):e(n,t):(n=e(n,t),o=f(n),a=o[0],n=o[1],a&&(r=u(a))),{f:a?a+"!"+n:n,n:n,pr:a,p:r}},p={require:function(n){return r(n)},exports:function(n){var t=d[n];return"undefined"!=typeof t?t:d[n]={}},module:function(n){return{id:n,uri:"",exports:d[n],config:a(n)}}},c=function(e,i,f,a){var c,s,g,m,h,b,E=[],w=typeof f;if(a=a||e,"undefined"===w||"function"===w){for(i=!i.length&&f.length?["require","exports","module"]:i,h=0;h<i.length;h+=1)if(m=l(i[h],a),s=m.f,"require"===s)E[h]=p.require(e);else if("exports"===s)E[h]=p.exports(e),b=!0;else if("module"===s)c=E[h]=p.module(e);else if(t(d,s)||t(v,s)||t(y,s))E[h]=u(s);else{if(!m.p)throw new Error(e+" missing "+s);m.p.load(m.n,r(a,!0),o(s),{}),E[h]=d[s]}g=f?f.apply(d[e],E):void 0,e&&(c&&c.exports!==n&&c.exports!==d[e]?d[e]=c.exports:g===n&&b||(d[e]=g))}else e&&(d[e]=f)},requirejs=require=s=function(t,e,r,i,o){if("string"==typeof t)return p[t]?p[t](e):u(l(t,e).f);if(!t.splice){if(g=t,g.deps&&s(g.deps,g.callback),!e)return;e.splice?(t=e,e=r,r=null):t=n}return e=e||function(){},"function"==typeof r&&(r=i,i=o),i?c(n,t,e,r):setTimeout(function(){c(n,t,e,r)},4),s},s.config=function(n){return s(n)},requirejs._defined=d,define=function(n,e,r){e.splice||(r=e,e=[]),t(d,n)||t(v,n)||(v[n]=[n,e,r])},define.amd={jQuery:!0}}(),define("../vendor/almond",function(){}),define("utils",[],function(){function n(n,t){var e=Array.prototype.slice.call(arguments,2);return function(){return t.apply(n,e.concat(Array.prototype.slice.call(arguments)))}}function t(n,t){if(r(n))for(var e=0;e<n.length&&t(e,n[e])!==!0;e++);else for(var i in n)if(n.hasOwnProperty(i)&&t(i,n[i])===!0)break}function e(n){var e=[];return t(n,function(n){e.push(n)}),e}function r(n){return"[object Array]"===Object.prototype.toString.call(n)}function i(n,t){for(var e=0,r=t.length;r>e;e++)if(t[e]===n)return!0;return!1}function o(n,t){for(var e=0,r=t.length;r>e;e++)if(t[e]===n)return e;return-1}function u(n,t){return r(n,t)?o(n,t):void 0}function f(n,t){return Math.random()*(t-n)+n}function a(n,t){return Math.floor(Math.random()*(t-n))+n}function c(n,t){"string"!=typeof n&&(n="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"),"number"!=typeof t&&(t=64);for(var e=n.length,r="",i=0;t>i;i++)r+=n[a(0,e)];return r}function s(n,t){for(n=n.replace(/[^0-9a-zA-Z_\$]/g,"");/$[0-9]/g.test(n);){if(""===n)return t;n=n.substring(1)}return n}return{bind:n,each:t,getKeys:e,isArray:r,inArray:i,indexOf:u,indexOfArray:o,getRandomArbitrary:f,getRandomInt:a,generateToken:c,escapeECMAVariable:s}}),define("support",[],function(){function n(){try{var n=document.createEvent("CustomEvent");return n&&"function"==typeof n.initCustomEvent?(n.initCustomEvent(t,!0,!0,{mod:t}),!0):!1}catch(n){return!1}}var t="support.test";return{CustomEvent:n}}),define("CustomEvent",["utils"],function(n){function t(t,r){o[t]||(o[t]=[],window.addEventListener(t,n.bind(null,e,t,o[t]),!1)),o[t].push(r)}function e(n,t,e){e=e||window.event;var r=JSON.parse(e.detail);if("object"==typeof r.detail&&r.token!==i){for(var o=r.detail,u=0,f=t.length;f>u;u++)t[u](o);e&&"function"==typeof e.stopPropagation&&e.stopPropagation()}}function r(n,t){var e=document.createEvent("CustomEvent");e.initCustomEvent(n,!0,!0,JSON.stringify({detail:t,token:i})),document.documentElement.dispatchEvent(e)}var i=n.generateToken(),o={};return{addEventListener:t,fireEvent:r}}),define("Message",["utils"],function(n){function t(t,e){i(),f[t]||(f[t]=[]);var r=n.bind(null,e,t);f[t].push(r)}function e(n,t){window.postMessage(JSON.stringify({token:u,event:n,detail:t}),"*")}function r(t){t=t||window.event;var e=JSON.parse(t.data);if("string"==typeof e.event&&"object"==typeof e.detail&&e.token!==u&&n.isArray(f[e.event])){for(var r=f[e.event],i=e.detail,o=0,a=r.length;a>o;o++)r(i);t&&"function"==typeof t.stopPropagation&&t.stopPropagation()}}function i(){o||window.addEventListener("message",r,!1)}var o=!1,u=n.generateToken(),f={};return{addEventListener:t,fireEvent:e}}),define("memFunction",["utils","CustomEvent","Message","support"],function(n,t,e,r){function i(t,e,r){if("object"==typeof t)n.each(t,function(n,u){if("object"==typeof u)t[n]=i(u,e,r);else if("string"==typeof u)t[n]=o(u);else if("function"==typeof u){var f=s.push(u)-1;t[n]="${"+e+"/"+r+"/"+f+"}"}});else if("string"==typeof value)t=o(t);else if("function"==typeof t){var u=s.push(t)-1;t="${"+e+"/"+r+"/"+u+"}"}return t}function o(n){return/^\$[\\]*\{([0-9a-zA-Z\.\-_\/\\]+)\}$/g.test(n)?"$\\"+n.substring(1):n}function u(t,e,r){if(/^\$\{([0-9a-zA-Z\.\-_]+)\/([0-9a-zA-Z\.\-_]+)\/([0-9]+)\}$/g.test(t)){var i=t.substring(2,t.length-1).split("/"),o=parseInt(i[2],10);return i[0]===e&&i[1]===r?s[o]:n.bind(null,a,i[0]+"-"+i[1],o)}return/^\$[\\]+\{([0-9a-zA-Z\.\-_\/\\]+)\}$/g.test(t)?"$"+t.substring(2):t}function f(t,e,r){if("object"==typeof t)n.each(t,function(n,i){if("object"==typeof i)t[n]=f(i,e,r);else if("string"==typeof i)t[n]=u(i,e,r);else if("function"==typeof i)throw Error("Function was found!")});else{if("string"==typeof value)return u(value,e,r);if("function"==typeof value)throw Error("Function was found!")}return t}function a(n,i){var o=Array.prototype.slice.call(arguments,2);return r.CustomEvent?t.fireEvent(n,{callbackId:i,args:o,mem:!0}):e.fireEvent(n,{callbackId:i,args:o,mem:!0})}function c(n){return s[n]}var s=[];return{parseObject:i,restoreObject:f,getCacheFunction:c}}),define("Connection",["CustomEvent","Message","utils","support","memFunction"],function(n,t,e,r,i){function o(i,o){r.CustomEvent?n.addEventListener(i+"-content",e.bind(null,u,o,i,"content")):t.addEventListener(i+"-content",e.bind(null,u,o,i,"content"))}function u(n,t,r,i){setTimeout(e.bind(null,f,n,t,r,i),4)}function f(o,u,f,a){var c=e.getKeys(o),s=e.indexOfArray(a.method,c);if(!(s>-1))throw"Method "+a.method+" has not been set!";var l=o[c[s]].apply(null,i.restoreObject(a.args,u,f));if("number"==typeof a.id){var p=i.parseObject(l,u,f),a={callbackId:a.id,args:[p]};r.CustomEvent?n.fireEvent(u+"-page",a):t.addEventListener(u+"-page",a)}}function a(n){this.token=e.generateToken(),this.functions={},this.namespace="UserProxy",this.pageProxy=n}return a.prototype.setFunctions=function(n){this.functions=n},a.prototype.setNamespace=function(n){this.namespace=n},a.prototype.inject=function(n){var t=document.body||document.head||document.documentElement;if(!t)throw"Parent was not found!";var r=document.createElement("script");r.setAttribute("type","text/javascript");var i=[";(function(unsafeWindow){var "+e.escapeECMAVariable(this.namespace)+" = ("+this.pageProxy+')("'+this.token+'", '+JSON.stringify(e.getKeys(this.functions))+", this);","})(window);"];"string"==typeof n&&(n="function(){"+n+"}"),o(this.token,this.functions),r.appendChild(document.createTextNode(i[0]+"("+n+")();"+i[1])),t.appendChild(r),t.removeChild(r)},a}),define("main",["utils","support","Connection"],function(n,t,e){function r(n){var t=new e(a);t.setFunctions(u),t.setNamespace(f),t.inject(n)}function i(n){u=n}function o(n){f=n}var u={},f="UserProxy",a=function(n,t,e){var r,i,o;return!function(n){function t(n,t){return E.call(n,t)}function e(n,t){var e,r,i,o,u,f,a,c,s,l,p,d=t&&t.split("/"),v=h.map,g=v&&v["*"]||{};if(n&&"."===n.charAt(0))if(t){for(d=d.slice(0,d.length-1),n=n.split("/"),u=n.length-1,h.nodeIdCompat&&j.test(n[u])&&(n[u]=n[u].replace(j,"")),n=d.concat(n),s=0;s<n.length;s+=1)if(p=n[s],"."===p)n.splice(s,1),s-=1;else if(".."===p){if(1===s&&(".."===n[2]||".."===n[0]))break;s>0&&(n.splice(s-1,2),s-=2)}n=n.join("/")}else 0===n.indexOf("./")&&(n=n.substring(2));if((d||g)&&v){for(e=n.split("/"),s=e.length;s>0;s-=1){if(r=e.slice(0,s).join("/"),d)for(l=d.length;l>0;l-=1)if(i=v[d.slice(0,l).join("/")],i&&(i=i[r])){o=i,f=s;break}if(o)break;!a&&g&&g[r]&&(a=g[r],c=s)}!o&&a&&(o=a,f=c),o&&(e.splice(0,f,o),n=e.join("/"))}return n}function u(t,e){return function(){return d.apply(n,w.call(arguments,0).concat([t,e]))}}function f(n){return function(t){return e(t,n)}}function a(n){return function(t){y[n]=t}}function c(e){if(t(m,e)){var r=m[e];delete m[e],b[e]=!0,p.apply(n,r)}if(!t(y,e)&&!t(b,e))throw new Error("No "+e);return y[e]}function s(n){var t,e=n?n.indexOf("!"):-1;return e>-1&&(t=n.substring(0,e),n=n.substring(e+1,n.length)),[t,n]}function l(n){return function(){return h&&h.config&&h.config[n]||{}}}var p,d,v,g,y={},m={},h={},b={},E=Object.prototype.hasOwnProperty,w=[].slice,j=/\.js$/;v=function(n,t){var r,i=s(n),o=i[0];return n=i[1],o&&(o=e(o,t),r=c(o)),o?n=r&&r.normalize?r.normalize(n,f(t)):e(n,t):(n=e(n,t),i=s(n),o=i[0],n=i[1],o&&(r=c(o))),{f:o?o+"!"+n:n,n:n,pr:o,p:r}},g={require:function(n){return u(n)},exports:function(n){var t=y[n];return"undefined"!=typeof t?t:y[n]={}},module:function(n){return{id:n,uri:"",exports:y[n],config:l(n)}}},p=function(e,r,i,o){var f,s,l,p,d,h,E=[],w=typeof i;if(o=o||e,"undefined"===w||"function"===w){for(r=!r.length&&i.length?["require","exports","module"]:r,d=0;d<r.length;d+=1)if(p=v(r[d],o),s=p.f,"require"===s)E[d]=g.require(e);else if("exports"===s)E[d]=g.exports(e),h=!0;else if("module"===s)f=E[d]=g.module(e);else if(t(y,s)||t(m,s)||t(b,s))E[d]=c(s);else{if(!p.p)throw new Error(e+" missing "+s);p.p.load(p.n,u(o,!0),a(s),{}),E[d]=y[s]}l=i?i.apply(y[e],E):void 0,e&&(f&&f.exports!==n&&f.exports!==y[e]?y[e]=f.exports:l===n&&h||(y[e]=l))}else e&&(y[e]=i)},r=i=d=function(t,e,r,i,o){if("string"==typeof t)return g[t]?g[t](e):c(v(t,e).f);if(!t.splice){if(h=t,h.deps&&d(h.deps,h.callback),!e)return;e.splice?(t=e,e=r,r=null):t=n}return e=e||function(){},"function"==typeof r&&(r=i,i=o),i?p(n,t,e,r):setTimeout(function(){p(n,t,e,r)},4),d},d.config=function(n){return d(n)},r._defined=y,o=function(n,e,r){e.splice||(r=e,e=[]),t(y,n)||t(m,n)||(m[n]=[n,e,r])},o.amd={jQuery:!0}}(),o("../vendor/almond",function(){}),o("support",[],function(){function n(){try{var n=document.createEvent("CustomEvent");return n&&"function"==typeof n.initCustomEvent?(n.initCustomEvent(t,!0,!0,{mod:t}),!0):!1}catch(n){return!1}}var t="support.test";return{CustomEvent:n}}),o("utils",[],function(){function n(n,t){var e=Array.prototype.slice.call(arguments,2);return function(){return t.apply(n,e.concat(Array.prototype.slice.call(arguments)))}}function t(n,t){if(r(n))for(var e=0;e<n.length&&t(e,n[e])!==!0;e++);else for(var i in n)if(n.hasOwnProperty(i)&&t(i,n[i])===!0)break}function e(n){var e=[];return t(n,function(n){e.push(n)}),e}function r(n){return"[object Array]"===Object.prototype.toString.call(n)}function i(n,t){for(var e=0,r=t.length;r>e;e++)if(t[e]===n)return!0;return!1}function o(n,t){for(var e=0,r=t.length;r>e;e++)if(t[e]===n)return e;return-1}function u(n,t){return r(n,t)?o(n,t):void 0}function f(n,t){return Math.random()*(t-n)+n}function a(n,t){return Math.floor(Math.random()*(t-n))+n}function c(n,t){"string"!=typeof n&&(n="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_"),"number"!=typeof t&&(t=64);for(var e=n.length,r="",i=0;t>i;i++)r+=n[a(0,e)];return r}function s(n,t){for(n=n.replace(/[^0-9a-zA-Z_\$]/g,"");/$[0-9]/g.test(n);){if(""===n)return t;n=n.substring(1)}return n}return{bind:n,each:t,getKeys:e,isArray:r,inArray:i,indexOf:u,indexOfArray:o,getRandomArbitrary:f,getRandomInt:a,generateToken:c,escapeECMAVariable:s}}),o("CustomEvent",["utils"],function(n){function t(t,r){o[t]||(o[t]=[],window.addEventListener(t,n.bind(null,e,t,o[t]),!1)),o[t].push(r)}function e(n,t,e){e=e||window.event;var r=JSON.parse(e.detail);if("object"==typeof r.detail&&r.token!==i){for(var o=r.detail,u=0,f=t.length;f>u;u++)t[u](o);e&&"function"==typeof e.stopPropagation&&e.stopPropagation()}}function r(n,t){var e=document.createEvent("CustomEvent");e.initCustomEvent(n,!0,!0,JSON.stringify({detail:t,token:i})),document.documentElement.dispatchEvent(e)}var i=n.generateToken(),o={};return{addEventListener:t,fireEvent:r}}),o("Message",["utils"],function(n){function t(t,e){i(),f[t]||(f[t]=[]);var r=n.bind(null,e,t);f[t].push(r)}function e(n,t){window.postMessage(JSON.stringify({token:u,event:n,detail:t}),"*")}function r(t){t=t||window.event;var e=JSON.parse(t.data);if("string"==typeof e.event&&"object"==typeof e.detail&&e.token!==u&&n.isArray(f[e.event])){for(var r=f[e.event],i=e.detail,o=0,a=r.length;a>o;o++)r(i);t&&"function"==typeof t.stopPropagation&&t.stopPropagation()}}function i(){o||window.addEventListener("message",r,!1)}var o=!1,u=n.generateToken(),f={};return{addEventListener:t,fireEvent:e}}),o("memFunction",["utils","CustomEvent","Message","support"],function(n,t,e,r){function i(t,e,r){if("object"==typeof t)n.each(t,function(n,u){if("object"==typeof u)t[n]=i(u,e,r);else if("string"==typeof u)t[n]=o(u);else if("function"==typeof u){var f=s.push(u)-1;t[n]="${"+e+"/"+r+"/"+f+"}"}});else if("string"==typeof value)t=o(t);else if("function"==typeof t){var u=s.push(t)-1;t="${"+e+"/"+r+"/"+u+"}"}return t}function o(n){return/^\$[\\]*\{([0-9a-zA-Z\.\-_\/\\]+)\}$/g.test(n)?"$\\"+n.substring(1):n}function u(t,e,r){if(/^\$\{([0-9a-zA-Z\.\-_]+)\/([0-9a-zA-Z\.\-_]+)\/([0-9]+)\}$/g.test(t)){var i=t.substring(2,t.length-1).split("/"),o=parseInt(i[2],10);return i[0]===e&&i[1]===r?s[o]:n.bind(null,a,i[0]+"-"+i[1],o)}return/^\$[\\]+\{([0-9a-zA-Z\.\-_\/\\]+)\}$/g.test(t)?"$"+t.substring(2):t}function f(t,e,r){if("object"==typeof t)n.each(t,function(n,i){if("object"==typeof i)t[n]=f(i,e,r);else if("string"==typeof i)t[n]=u(i,e,r);else if("function"==typeof i)throw Error("Function was found!")});else{if("string"==typeof value)return u(value,e,r);if("function"==typeof value)throw Error("Function was found!")}return t}function a(n,i){var o=Array.prototype.slice.call(arguments,2);return r.CustomEvent?t.fireEvent(n,{callbackId:i,args:o,mem:!0}):e.fireEvent(n,{callbackId:i,args:o,mem:!0})}function c(n){return s[n]}var s=[];return{parseObject:i,restoreObject:f,getCacheFunction:c}}),o("proxy",["support","CustomEvent","Message","utils","memFunction"],function(r,i,o,u,f){function a(t){if("number"==typeof t.callbackId&&u.isArray(t.args)&&t.mem){var e=f.restoreObject(t.args,n,"page"),r=f.getCacheFunction(t.callbackId);"function"==typeof r&&r.apply(null,e)}else{if("number"!=typeof t.callbackId||!u.isArray(t.args))throw Error("Malformed detail!",t);var e=f.restoreObject(t.args,n,"page");"function"==typeof v[t.callbackId]&&v[t.callbackId].apply(null,e)}}function c(t,e){if(!l(t))throw Error(t+" is not a defined function!");if("function"!=typeof e)throw Error("The callback is not a function!");var u=v.push(e)-1,a=Array.prototype.slice.call(arguments,2);return function(){a=a.concat(Array.prototype.slice.call(arguments,0)),a=f.parseObject(a,n,"page");var e={method:t,args:a,id:u};r.CustomEvent?i.fireEvent(n+"-content",e):o.fireEvent(n+"-content",e)}}function s(t,e){function u(n){clearTimeout(s),"function"==typeof n&&(c.id=v.push(n)-1),a()}function a(){r.CustomEvent?i.fireEvent(n+"-content",c):o.fireEvent(n+"-content",c)}if(e=Array.prototype.slice.call(arguments,1),!l(t))throw Error(t+" is not a defined function!");e=f.parseObject(e,n,"page");var c={method:t,args:e},s=setTimeout(a,4);return{then:u}}function l(n){return u.indexOfArray(n,t)>-1}function p(n){if(l(n))return u.bind(null,s,n);throw Error(n+" is not defined!")}function d(){return JSON.parse(JSON.stringify(t))}var v=[];r.CustomEvent?i.addEventListener(n+"-page",a):o.addEventListener(n+"-page",a);for(var g=0,y=t.length;y>g;g++)e[t[g]]=u.bind(null,s,t[g]);return{call:s,prepareCall:c,getFunction:p,isDefined:l,listFunctions:d}}),i("proxy")};return{connect:r,setFunctions:i,setNamespace:o}});;return require("main");})();