PluginProbe
WPIDE – File Manager & Code Editor / 3.5.9
WPIDE – File Manager & Code Editor v3.5.9
3.5.9 3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 All 55 releases
wpide / freemius / assets / scripts / postmessage.js

postmessage.js in WPIDE – File Manager & Code Editor 3.5.9, at freemius/assets/scripts/postmessage.js

143 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 _hasParent = ('' !== _parent_url),
29 $window = $(window),
30 $html = $('html');
31
32 return {
33 init : function (url, iframes)
34 {
35 _base_url = url;
36 _init();
37
38 // Automatically receive forward messages.
39 FS.PostMessage.receiveOnce('forward', function (data) {
40 // Sanitize the URL.
41 if (data.url && (data.url.startsWith('http://') || data.url.startsWith('https://'))) {
42 window.location = data.url;
43 }
44 });
45
46 iframes = iframes || [];
47
48 if (iframes.length > 0) {
49 $window.on('scroll', function () {
50 for (var i = 0; i < iframes.length; i++) {
51 FS.PostMessage.postScroll(iframes[i]);
52 }
53 });
54 }
55 },
56 init_child : function ()
57 {
58 // Don't initialize as children if we cannot detect parent.
59 if (!_hasParent) {
60 return;
61 }
62
63 this.init(_parent_subdomain);
64
65 _is_child = true;
66
67 // Post height of a child right after window is loaded.
68 $(window).bind('load', function () {
69 FS.PostMessage.postHeight();
70
71 // Post message that window was loaded.
72 FS.PostMessage.post('loaded');
73 });
74 },
75 hasParent : function ()
76 {
77 return _hasParent;
78 },
79 postHeight : function (diff, wrapper) {
80 diff = diff || 0;
81 wrapper = wrapper || '#wrap_section';
82 this.post('height', {
83 height: diff + $(wrapper).outerHeight(true)
84 });
85 },
86 postScroll : function (iframe) {
87 this.post('scroll', {
88 top: $window.scrollTop(),
89 height: ($window.height() - parseFloat($html.css('paddingTop')) - parseFloat($html.css('marginTop')))
90 }, iframe);
91 },
92 post : function (type, data, iframe)
93 {
94 console.debug('PostMessage.post', type);
95
96 if (iframe)
97 {
98 // Post to iframe.
99 _postman.postMessage(JSON.stringify({
100 type: type,
101 data: data
102 }), iframe.src, iframe.contentWindow);
103 }
104 else {
105 // Post to parent.
106 _postman.postMessage(JSON.stringify({
107 type: type,
108 data: data
109 }), _parent_url, window.parent);
110 }
111 },
112 receive: function (type, callback)
113 {
114 console.debug('PostMessage.receive', type);
115
116 if (undef === _callbacks[type])
117 _callbacks[type] = [];
118
119 _callbacks[type].push(callback);
120 },
121 receiveOnce: function (type, callback)
122 {
123 if (this.is_set(type))
124 return;
125
126 this.receive(type, callback);
127 },
128 // Check if any callbacks assigned to a specified message type.
129 is_set: function (type)
130 {
131 return (undef != _callbacks[type]);
132 },
133 parent_url: function ()
134 {
135 return _parent_url;
136 },
137 parent_subdomain: function ()
138 {
139 return _parent_subdomain;
140 }
141 };
142 }();
143 })(jQuery);