jquery.ba-postmessage.js
10 years ago
jquery.ba-postmessage.min.js
10 years ago
nojquery.ba-postmessage.js
10 years ago
nojquery.ba-postmessage.min.js
10 years ago
postmessage.js
10 years ago
postmessage.js
110 lines
| 1 | (function ($, undef) { |
| 2 | var global = this; |
| 3 | |
| 4 | // Namespace. |
| 5 | global.FS = global.FS || {}; |
| 6 | |
| 7 | global.FS.PostMessage = function () |
| 8 | { |
| 9 | var |
| 10 | _is_child = false, |
| 11 | _postman = new NoJQueryPostMessageMixin('postMessage', 'receiveMessage'), |
| 12 | _callbacks = {}, |
| 13 | _base_url, |
| 14 | _parent_url = decodeURIComponent(document.location.hash.replace(/^#/, '')), |
| 15 | _parent_subdomain = _parent_url.substring(0, _parent_url.indexOf('/', ('https://' === _parent_url.substring(0, ('https://').length)) ? 8 : 7)), |
| 16 | _init = function () { |
| 17 | _postman.receiveMessage(function (e) { |
| 18 | var data = JSON.parse(e.data); |
| 19 | |
| 20 | if (_callbacks[data.type]) { |
| 21 | for (var i = 0; i < _callbacks[data.type].length; i++) { |
| 22 | // Execute type callbacks. |
| 23 | _callbacks[data.type][i](data.data); |
| 24 | } |
| 25 | } |
| 26 | }, _base_url); |
| 27 | }; |
| 28 | |
| 29 | return { |
| 30 | init : function (url) |
| 31 | { |
| 32 | _base_url = url; |
| 33 | _init(); |
| 34 | |
| 35 | // Automatically receive forward messages. |
| 36 | FS.PostMessage.receiveOnce('forward', function (data){ |
| 37 | window.location = data.url; |
| 38 | }); |
| 39 | }, |
| 40 | init_child : function () |
| 41 | { |
| 42 | this.init(_parent_subdomain); |
| 43 | |
| 44 | _is_child = true; |
| 45 | |
| 46 | // Post height of a child right after window is loaded. |
| 47 | $(window).bind('load', function () { |
| 48 | FS.PostMessage.postHeight(); |
| 49 | }); |
| 50 | |
| 51 | }, |
| 52 | postHeight : function (diff, wrapper) { |
| 53 | diff = diff || 0; |
| 54 | wrapper = wrapper || '#wrap_section'; |
| 55 | this.post('height', { |
| 56 | height: diff + $(wrapper).outerHeight(true) |
| 57 | }); |
| 58 | }, |
| 59 | post : function (type, data, iframe) |
| 60 | { |
| 61 | console.debug('PostMessage.post', type); |
| 62 | |
| 63 | if (iframe) |
| 64 | { |
| 65 | // Post to iframe. |
| 66 | _postman.postMessage(JSON.stringify({ |
| 67 | type: type, |
| 68 | data: data |
| 69 | }), iframe.src, iframe.contentWindow); |
| 70 | } |
| 71 | else { |
| 72 | // Post to parent. |
| 73 | _postman.postMessage(JSON.stringify({ |
| 74 | type: type, |
| 75 | data: data |
| 76 | }), _parent_url, window.parent); |
| 77 | } |
| 78 | }, |
| 79 | receive: function (type, callback) |
| 80 | { |
| 81 | console.debug('PostMessage.receive', type); |
| 82 | |
| 83 | if (undef === _callbacks[type]) |
| 84 | _callbacks[type] = []; |
| 85 | |
| 86 | _callbacks[type].push(callback); |
| 87 | }, |
| 88 | receiveOnce: function (type, callback) |
| 89 | { |
| 90 | if (this.is_set(type)) |
| 91 | return; |
| 92 | |
| 93 | this.receive(type, callback); |
| 94 | }, |
| 95 | // Check if any callbacks assigned to a specified message type. |
| 96 | is_set: function (type) |
| 97 | { |
| 98 | return (undef != _callbacks[type]); |
| 99 | }, |
| 100 | parent_url: function () |
| 101 | { |
| 102 | return _parent_url; |
| 103 | }, |
| 104 | parent_subdomain: function () |
| 105 | { |
| 106 | return _parent_subdomain; |
| 107 | } |
| 108 | }; |
| 109 | }(); |
| 110 | })(jQuery); |