1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
|
/* global Jetpack, JSON */
/**
* Resizeable Iframes.
*
* Start listening to resize postMessage events for selected iframes:
* $( selector ).Jetpack( 'resizeable' );
* - OR -
* Jetpack.resizeable( 'on', context );
*
* Resize selected iframes:
* $( selector ).Jetpack( 'resizeable', 'resize', { width: 100, height: 200 } );
* - OR -
* Jetpack.resizeable( 'resize', { width: 100, height: 200 }, context );
*
* Stop listening to resize postMessage events for selected iframes:
* $( selector ).Jetpack( 'resizeable', 'off' );
* - OR -
* Jetpack.resizeable( 'off', context );
*
* Stop listening to all resize postMessage events:
* Jetpack.resizeable( 'off' );
*/
( function( $ ) {
var listening = false, // Are we listening for resize postMessage events
sourceOrigins = [], // What origins are allowed to send resize postMessage events
$sources = false, // What iframe elements are we tracking resize postMessage events from
URLtoOrigin, // Utility to convert URLs into origins
setupListener, // Binds global resize postMessage event handler
destroyListener, // Unbinds global resize postMessage event handler
methods; // Jetpack.resizeable methods
// Setup the Jetpack global
if ( 'undefined' === typeof window.Jetpack ) {
window.Jetpack = {
/**
* Handles the two different calling methods:
* $( selector ).Jetpack( 'namespace', 'method', context ) // here, context is optional and is used to filter the collection
* - vs. -
* Jetpack.namespace( 'method', context ) // here context defines the collection
*
* @internal
*
* Call as: Jetpack.getTarget.call( this, context )
*
* @param string context: jQuery selector
* @return jQuery|undefined object on which to perform operations or undefined when context cannot be determined
*/
getTarget: function( context ) {
if ( this instanceof jQuery ) {
return context ? this.filter( context ) : this;
}
return context ? $( context ) : context;
},
};
}
// Setup the Jetpack jQuery method
if ( 'undefined' === typeof $.fn.Jetpack ) {
/**
* Dispatches calls to the correct namespace
*
* @param string namespace
* @param ...
* @return mixed|jQuery (chainable)
*/
$.fn.Jetpack = function( namespace ) {
if ( 'function' === typeof Jetpack[ namespace ] ) {
// Send the call to the correct Jetpack.namespace
return Jetpack[ namespace ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
} else {
$.error( 'Namespace "' + namespace + '" does not exist on jQuery.Jetpack' );
}
};
}
// Define Jetpack.resizeable() namespace to just always bail if no postMessage
if ( 'function' !== typeof window.postMessage ) {
$.extend( window.Jetpack, {
/**
* Defines the Jetpack.resizeable() namespace.
* See below for non-trivial definition for browsers with postMessage.
*/
resizeable: function() {
$.error( 'Browser does not support window.postMessage' );
},
} );
return;
}
/**
* Utility to convert URLs into origins
*
* http://example.com:port/path?query#fragment -> http://example.com:port
*
* @param string URL
* @return string origin
*/
URLtoOrigin = function( URL ) {
if ( ! URL.match( /^https?:\/\// ) ) {
URL = document.location.href;
}
return URL.split( '/' )
.slice( 0, 3 )
.join( '/' );
};
/**
* Binds global resize postMessage event handler
*/
setupListener = function() {
listening = true;
$( window ).on( 'message.JetpackResizeableIframe', function( e ) {
var event = e.originalEvent,
data;
// Ensure origin is allowed
if ( -1 === $.inArray( event.origin, sourceOrigins ) ) {
return;
}
// Some browsers send structured data, some send JSON strings
if ( 'object' === typeof event.data ) {
data = event.data.data;
} else {
try {
data = JSON.parse( event.data );
} catch ( err ) {
data = false;
}
}
if ( ! data.data ) {
return;
}
// Un-nest
data = data.data;
// Is it a resize event?
if ( 'undefined' === typeof data.action || 'resize' !== data.action ) {
return;
}
// Find the correct iframe and resize it
$sources
.filter( function() {
if ( 'undefined' !== typeof data.name ) {
return this.name === data.name;
} else {
return event.source === this.contentWindow;
}
} )
.first()
.Jetpack( 'resizeable', 'resize', data );
} );
};
/**
* Unbinds global resize postMessage event handler
*/
destroyListener = function() {
listening = false;
$( window ).off( 'message.JetpackResizeableIframe' );
sourceOrigins = [];
$( '.jetpack-resizeable' ).removeClass( 'jetpack-resizeable' );
$sources = false;
};
// Methods for Jetpack.resizeable() namespace
methods = {
/**
* Start listening for resize postMessage events on the given iframes
*
* Call statically as: Jetpack.resizeable( 'on', context )
* Call as: $( selector ).Jetpack( 'resizeable', 'on', context ) // context optional: used to filter the collectino
*
* @param string context jQuery selector.
* @return jQuery (chainable)
*/
on: function( context ) {
var target = Jetpack.getTarget.call( this, context );
if ( ! listening ) {
setupListener();
}
target
.each( function() {
sourceOrigins.push( URLtoOrigin( $( this ).attr( 'src' ) ) );
} )
.addClass( 'jetpack-resizeable' );
$sources = $( '.jetpack-resizeable' );
return target;
},
/**
* Stop listening for resize postMessage events on the given iframes
*
* Call statically as: Jetpack.resizeable( 'off', context )
* Call as: $( selector ).Jetpack( 'resizeable', 'off', context ) // context optional: used to filter the collectino
*
* @param string context jQuery selector
* @return jQuery (chainable)
*/
off: function( context ) {
var target = Jetpack.getTarget.call( this, context );
if ( 'undefined' === typeof target ) {
destroyListener();
return target;
}
target
.each( function() {
var origin = URLtoOrigin( $( this ).attr( 'src' ) ),
pos = $.inArray( origin, sourceOrigins );
if ( -1 !== pos ) {
sourceOrigins.splice( pos, 1 );
}
} )
.removeClass( 'jetpack-resizeable' );
$sources = $( '.jetpack-resizeable' );
return target;
},
/**
* Resize the given iframes
*
* Call statically as: Jetpack.resizeable( 'resize', dimensions, context )
* Call as: $( selector ).Jetpack( 'resizeable', 'resize', dimensions, context ) // context optional: used to filter the collectino
*
* @param object dimensions in pixels: { width: (int), height: (int) }
* @param string context jQuery selector
* @return jQuery (chainable)
*/
resize: function( dimensions, context ) {
var target = Jetpack.getTarget.call( this, context );
$.each( [ 'width', 'height' ], function( i, variable ) {
var value = 0,
container;
if ( 'undefined' !== typeof dimensions[ variable ] ) {
value = parseInt( dimensions[ variable ], 10 );
}
if ( 0 !== value ) {
target[ variable ]( value );
container = target.parent();
if ( container.hasClass( 'slim-likes-widget' ) ) {
container[ variable ]( value );
}
}
} );
return target;
},
};
// Define Jetpack.resizeable() namespace
$.extend( window.Jetpack, {
/**
* Defines the Jetpack.resizeable() namespace.
* See above for trivial definition for browsers with no postMessage.
*
* @param string method
* @param ...
* @return mixed|jQuery (chainable)
*/
resizeable: function( method ) {
if ( methods[ method ] ) {
// Send the call to the correct Jetpack.resizeable() method
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ) );
} else if ( ! method ) {
// By default, send to Jetpack.resizeable( 'on' ), which isn't useful in that form but is when called as
// jQuery( selector ).Jetpack( 'resizeable' )
return methods.on.apply( this );
} else {
$.error( 'Method ' + method + ' does not exist on Jetpack.resizeable' );
}
},
} );
} )( jQuery );
|