-
Notifications
You must be signed in to change notification settings - Fork 2
/
jquery.browserEvent.js
328 lines (272 loc) · 6.7 KB
/
jquery.browserEvent.js
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
* jQuery browserEvent - Plugin for passing events to other browser windows running the same page
*
* Authors: Rodney Rehm
* Documentation: http://code.medialize.de/jQuery/store/
*
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
*/
/*
* No, this utility has got nothing in common with window.postMessage() and EventSource
* http://dev.w3.org/html5/postmsg/
* http://dev.w3.org/html5/eventsource/
*/
// TODO: some sort of locking to avoid race-conditions in registry- and event-queue read/writes
(function($,undefined){
var _store = null,
_winStore = null,
browserEvent = {
// window identification in registry
ident: null,
identPattern: /^browserEvent_([0-9]+)$/,
// checking events for this window
pollInterval: 200,
_pollInterval: null,
// locking, some sort of
lock: false,
lockTimeout: 100,
lockInterval: 10,
_lockInterval: null,
// registry of other windows
registry: [],
registryHash: null,
registryTimeout: 600,
// events to send to other windows
queue: [],
queues: {},
queued: false,
sending: true, // wait for ready
// race-condition hack to inexstent locking problem
registerChecks: 3,
registerChecksDone: 0,
bind: function( event, callback )
{
$( window )[ $.fn.bindDetached ? 'bindDetached' : 'bind' ]( event, callback );
},
unbind: function( event )
{
$( window ).unbind( event );
},
trigger: function( event, data, windows )
{
this.queued = true;
var t = { event:event, data:data, origin:this.ident };
if( !windows || !windows.length )
{
this.queue.push( t );
}
else
{
var that = this;
if( typeof windows === 'string' )
windows = [ windows ];
$.each( windows, function( i, win )
{
if( !that.queues[ win ] )
that.queues[ win ] = [];
that.queues[ win ].push( t );
});
}
$( window ).trigger( event, [ data, 'self', windows ] );
},
requestLock: function( callback )
{
try
{
if( this._lockInterval )
window.clearTimeout( this._lockInterval );
}
catch(e){}
var that = this,
lock = _store.get( 'browserEventLock' ),
now = (new Date()).getTime();
// retry if there's a lock that has not timed out
if( lock && lock > now - that.lockTimeout )
{
this._lockInterval = window.setTimeout( function()
{
that.requestLock.call( that );
}, this.lockInterval );
}
_store.set( 'browserEventLock', now );
this.lock = true;
callback.call( this );
},
releaseLock: function()
{
if( !this.lock )
return;
_store.del( 'browserEventLock' );
this.lock = true;
},
poll: function()
{
try
{
if( this._pollInterval )
window.clearTimeout( this._pollInterval );
}
catch(e){}
var that = this;
// TODO: acquire lock
this.dispatchEvents();
this.updateRegistry();
this.send();
this.releaseLock();
// renew poll
this._pollInterval = window.setTimeout( function()
{
that.requestLock.call( that, that.poll );
}, this.pollInterval );
},
send: function()
{
// send queue
var that = this;
if( this.registry && this.queued)
{
$.each( this.registry, function( win, time )
{
// don't send to self
if( win == that.ident )
return true; // continue;
var queue = _store.get( win ) || [];
queue = queue.concat( that.queue, that.queues[ win ] || [] );
_store.set( win, queue );
});
}
// clean queue
this.queue = [];
this.queues = {};
this.queued = false;
this.sending = false;
},
dispatchEvents: function()
{
var events = _store.get( this.ident );
// make sure events is an array
if( events === null || events.length == undefined )
{
events = [];
_store.set( this.ident, [] );
}
// dispatch browser events
if( events && events.length )
{
_store.set( this.ident, [] );
$.each( events, function()
{
$( window ).trigger( this.event, [ this.data, this.origin ] );
});
}
},
updateRegistry: function()
{
// load windows registry
var that = this,
registry = _store.get( 'browserEventRegistry' ) || {},
registryHash = [],
now = (new Date()).getTime(),
altered = false;
$.each( registry, function( win, time )
{
if( !win || time < now - that.registryTimeout )
{
altered = true;
try
{
delete registry[ win ];
}
catch( e )
{
registry[ win ] = undefined;
}
}
registryHash.push( win );
});
registry[ this.ident ] = now;
_store.set( 'browserEventRegistry', registry );
registryHash.sort()
registryHash = registryHash.join( '#' );
// update windows registry
if( registryHash != this.registryHash )
{
this.registry = registry;
this.registryHash = registryHash;
$( window ).trigger( 'browserWindows', [ this.registry, this.ident ] );
}
},
register: function()
{
var registry = _store.get( 'browserEventRegistry' ) || {};
this.registerChecksDone = 0;
registry[ this.ident ] = (new Date()).getTime();
_store.set( 'browserEventRegistry', registry );
this.releaseLock();
this.ready();
},
ready: function()
{
$.browserEvent.ready.call( $.browserEvent );
// run poller
this.requestLock( this.poll );
},
init: function( storage, winStorage )
{
// abort if storage is incapable of inter-window communication
if( !storage || storage.driver.scope != "browser" )
return;
_store = storage;
_winStore = winStorage;
// identify this window
if( !this.ident )
{
// save ident in window.name to keep accross document changes
this.ident = _winStore.get( 'browserEventIdent' );
if( !this.ident || !this.ident.match( this.identPattern ) )
this.ident = 'browserEvent_' + Math.floor((new Date).getTime() + Math.random() * 10000 );
_winStore.set( 'browserEventIdent', this.ident );
}
// register this window
this.requestLock( this.register );
}
};
/**********************************************************************************
* $.browserEvemt API
**********************************************************************************/
$.browserEvent = function( event, callback )
{
if( callback != undefined && !$.isFunction( callback ) )
browserEvent.bind( event, callback );
else
browserEvent.trigger( event, callback );
};
$.extend( $.browserEvent, {
ready: $.noop,
init: function( storage, winStorage ){
browserEvent.init( storage, winStorage );
return $.browserEvent;
},
bind: function( event, callback )
{
browserEvent.bind( event, callback );
return $.browserEvent;
},
unbind: function( event )
{
browserEvent.unbind( event );
return $.browserEvent;
},
trigger: function( event, data, windows )
{
browserEvent.trigger( event, data, windows );
return $.browserEvent;
},
ident: function()
{
return browserEvent.ident
}
});
})(jQuery);