golf 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,439 @@
1
+ /*
2
+ * jQuery Address Plugin v1.0
3
+ * http://www.asual.com/jquery/address/
4
+ *
5
+ * Copyright (c) 2009 Rostislav Hristov
6
+ * Dual licensed under the MIT and GPL licenses.
7
+ * http://docs.jquery.com/License
8
+ *
9
+ * Date: 2009-04-28 16:54:00 +0300 (Tue, 28 Apr 2009)
10
+ * Revision: 399
11
+ */
12
+ (function ($) {
13
+
14
+ $.address = (function () {
15
+
16
+ var _trigger = function(name) {
17
+ $(this).trigger(
18
+ $.extend($.Event(name),
19
+ (function() {
20
+ var event = {
21
+ value: this.value(),
22
+ path: this.path(),
23
+ pathNames: this.pathNames(),
24
+ parameterNames: this.parameterNames(),
25
+ parameters: {},
26
+ queryString: this.queryString()
27
+ };
28
+ for (var i = 0, l = event.parameterNames.length; i < l; i++)
29
+ event.parameters[event.parameterNames[i]] = this.parameter(event.parameterNames[i]);
30
+ return event;
31
+ }).call(this)
32
+ )
33
+ );
34
+ };
35
+
36
+ var _init = function() {
37
+ _trigger.call($.address, 'init');
38
+ };
39
+
40
+ var _change = function() {
41
+ _trigger.call($.address, 'change');
42
+ };
43
+
44
+ var _getHash = function() {
45
+ var index = _l.href.indexOf('#');
46
+ return index != -1 ? _ec(_dc(_l.href.substr(index + 1))) : '';
47
+ };
48
+
49
+ var _getWindow = function() {
50
+ try {
51
+ top.document;
52
+ return top;
53
+ } catch (e) {
54
+ return window;
55
+ }
56
+ };
57
+
58
+ var _strictCheck = function(value, force) {
59
+ if (_opts.strict)
60
+ value = force ? (value.substr(0, 1) != '/' ? '/' + value : value) : (value == '' ? '/' : value);
61
+ return value;
62
+ };
63
+
64
+ var _ieLocal = function(value, direction) {
65
+ return (_msie && _l.protocol == 'file:') ?
66
+ (direction ? _value.replace(/\?/, '%3F') : _value.replace(/%253F/, '?')) : value;
67
+ };
68
+
69
+ var _searchScript = function(el) {
70
+ for (var i = 0, l = el.childNodes.length, s; i < l; i++) {
71
+ if (el.childNodes[i].src)
72
+ _url = String(el.childNodes[i].src);
73
+ if (s = _searchScript(el.childNodes[i]))
74
+ return s;
75
+ }
76
+ };
77
+
78
+ var _listen = function() {
79
+ if (!_silent) {
80
+ var hash = _getHash();
81
+ var diff = !(_value == hash);
82
+ if (_safari && _version < 523) {
83
+ if (_length != _h.length) {
84
+ _length = _h.length;
85
+ if (typeof _stack[_length - 1] != UNDEFINED)
86
+ _value = _stack[_length - 1];
87
+ _update();
88
+ }
89
+ } else if (_msie && diff) {
90
+ if (_version < 7)
91
+ _l.reload();
92
+ else
93
+ _setters.value(hash);
94
+ } else if (diff) {
95
+ _value = hash;
96
+ _update();
97
+ }
98
+ }
99
+ };
100
+
101
+ var _update = function() {
102
+ _change();
103
+ _st(_track, 10);
104
+ };
105
+
106
+ var _track = function() {
107
+ var value = (_l.pathname + (/\/$/.test(_l.pathname) ? '' : '/') + _getters.value()).replace(/\/\//, '/').replace(/^\/$/, '');
108
+ var fn = window[_opts.tracker];
109
+ if (typeof fn == FUNCTION)
110
+ fn(value);
111
+ else if (typeof pageTracker != UNDEFINED && typeof pageTracker._trackPageview == FUNCTION)
112
+ pageTracker._trackPageview(value);
113
+ else if (typeof urchinTracker == FUNCTION)
114
+ urchinTracker(value);
115
+ };
116
+
117
+ var _htmlWrite = function() {
118
+ var doc = _iframe.contentWindow.document;
119
+ doc.open();
120
+ doc.write('<html><head><title>' + _d.title + '</title><script>var ' + ID + ' = "' + _getHash() + '";</script></head></html>');
121
+ doc.close();
122
+ };
123
+
124
+ var _load = function() {
125
+ if (!_loaded) {
126
+ _loaded = true;
127
+ $('a').attr('xref', function() {
128
+ return $(this).attr('href');
129
+ });
130
+ if (_msie && _version < 8) {
131
+ _d.body.innerHTML = '<iframe id="' + ID + '" src="javascript:false;" width="0" height="0"></iframe>' + _d.body.innerHTML;
132
+ _iframe = _d.getElementById(ID);
133
+ _st(function() {
134
+ $(_iframe).bind('load', function() {
135
+ var win = _iframe.contentWindow;
136
+ var src = win.location.href;
137
+ _value = (typeof win[ID] != UNDEFINED ? win[ID] : '');
138
+ if (_value != _getHash()) {
139
+ _update();
140
+ _l.hash = _ieLocal(_value, true);
141
+ }
142
+ });
143
+ if (typeof _iframe.contentWindow[ID] == UNDEFINED)
144
+ _htmlWrite();
145
+ }, 50);
146
+ } else if (_safari) {
147
+ if (_version < 418) {
148
+ $(_d.body).append('<form id="' + ID + '" style="position:absolute;top:-9999px;" method="get"></form>');
149
+ _form = _d.getElementById(ID);
150
+ }
151
+ if (typeof _l[ID] == UNDEFINED) _l[ID] = {};
152
+ if (typeof _l[ID][_l.pathname] != UNDEFINED) _stack = _l[ID][_l.pathname].split(',');
153
+ }
154
+
155
+ _st(function() {
156
+ _init();
157
+ _change();
158
+ _track();
159
+ }, 1);
160
+
161
+ if (_msie && _version >= 8)
162
+ _d.body.onhashchange = _listen;
163
+ else
164
+ _si(_listen, 50);
165
+
166
+ $('a').attr('href', function() {
167
+ return $(this).attr('xref');
168
+ }).removeAttr('xref');
169
+
170
+ $('a[rel*=address:]').address();
171
+ }
172
+ };
173
+
174
+ var _getters = {
175
+ baseURL: function() {
176
+ var url = _l.href;
177
+ if (url.indexOf('#') != -1)
178
+ url = url.substr(0, url.indexOf('#'));
179
+ if (url.substr(url.length - 1) == '/')
180
+ url = url.substr(0, url.length - 1);
181
+ return url;
182
+ },
183
+ strict: function() {
184
+ return _opts.strict;
185
+ },
186
+ history: function() {
187
+ return _opts.history;
188
+ },
189
+ tracker: function() {
190
+ return _opts.tracker;
191
+ },
192
+ title: function() {
193
+ return _d.title;
194
+ },
195
+ value: function() {
196
+ if (!_supported) return null;
197
+ return _dc(_strictCheck(_ieLocal(_value, false), false));
198
+ },
199
+ path: function() {
200
+ var value = this.value();
201
+ return (value.indexOf('?') != -1) ? value.split('?')[0] : value;
202
+ },
203
+ pathNames: function() {
204
+ var path = this.path();
205
+ var names = path.split('/');
206
+ if (path.substr(0, 1) == '/' || path.length == 0)
207
+ names.splice(0, 1);
208
+ if (path.substr(path.length - 1, 1) == '/')
209
+ names.splice(names.length - 1, 1);
210
+ return names;
211
+ },
212
+ queryString: function() {
213
+ var value = this.value();
214
+ var index = value.indexOf('?');
215
+ return (index != -1 && index < value.length) ? value.substr(index + 1) : '';
216
+ },
217
+ parameter: function(param) {
218
+ var value = this.value();
219
+ var index = value.indexOf('?');
220
+ if (index != -1) {
221
+ value = value.substr(index + 1);
222
+ var params = value.split('&');
223
+ var p, i = params.length;
224
+ while(i--) {
225
+ p = params[i].split('=');
226
+ if (p[0] == param)
227
+ return p[1];
228
+ }
229
+ }
230
+ },
231
+ parameterNames: function() {
232
+ var value = this.value();
233
+ var index = value.indexOf('?');
234
+ var names = [];
235
+ if (index != -1) {
236
+ value = value.substr(index + 1);
237
+ if (value != '' && value.indexOf('=') != -1) {
238
+ var params = value.split('&');
239
+ var i = 0;
240
+ while(i < params.length) {
241
+ names.push(params[i].split('=')[0]);
242
+ i++;
243
+ }
244
+ }
245
+ }
246
+ return names;
247
+ }
248
+ };
249
+
250
+ var _setters = {
251
+ strict: function(strict) {
252
+ _opts.strict = strict;
253
+ },
254
+ history: function(history) {
255
+ _opts.history = history;
256
+ },
257
+ tracker: function(tracker) {
258
+ _opts.tracker = tracker;
259
+ },
260
+ title: function(title) {
261
+ _st(function() {
262
+ _title = _d.title = title;
263
+ if (_juststart && _iframe && _iframe.contentWindow && _iframe.contentWindow.document) {
264
+ _iframe.contentWindow.document.title = title;
265
+ _juststart = false;
266
+ }
267
+ if (!_justset && _mozilla)
268
+ _l.replace(_l.href.indexOf('#') != -1 ? _l.href : _l.href + '#');
269
+ _justset = false;
270
+ }, 50);
271
+ },
272
+ value: function(value) {
273
+ value = _ec(_dc(_strictCheck(value, true)));
274
+ if (value == '/') value = '';
275
+ if (_value == value) return;
276
+ _justset = true;
277
+ _value = value;
278
+ _silent = true;
279
+ _update();
280
+ _stack[_h.length] = _value;
281
+ if (_safari) {
282
+ if (_opts.history) {
283
+ _l[ID][_l.pathname] = _stack.toString();
284
+ _length = _h.length + 1;
285
+ if (_version < 418) {
286
+ if (_l.search == '') {
287
+ _form.action = '#' + _value;
288
+ _form.submit();
289
+ }
290
+ } else if (_version < 523 || _value == '') {
291
+ var evt = _d.createEvent('MouseEvents');
292
+ evt.initEvent('click', true, true);
293
+ var anchor = _d.createElement('a');
294
+ anchor.href = '#' + _value;
295
+ anchor.dispatchEvent(evt);
296
+ } else {
297
+ _l.hash = '#' + _value;
298
+ }
299
+ } else {
300
+ _l.replace('#' + _value);
301
+ }
302
+ } else if (_value != _getHash()) {
303
+ if (_opts.history)
304
+ _l.hash = '#' + _ieLocal(_value, true);
305
+ else
306
+ _l.replace('#' + _value);
307
+ }
308
+ if ((_msie && _version < 8) && _opts.history) {
309
+ _st(_htmlWrite, 50);
310
+ }
311
+ if (_safari)
312
+ _st(function(){ _silent = false; }, 1);
313
+ else
314
+ _silent = false;
315
+ }
316
+ };
317
+
318
+ var ID = 'jQueryAddress',
319
+ FUNCTION = 'function',
320
+ UNDEFINED = 'undefined',
321
+ _browser = $.browser,
322
+ _version = parseFloat($.browser.version),
323
+ _mozilla = _browser.mozilla,
324
+ _msie = _browser.msie,
325
+ _opera = _browser.opera,
326
+ _safari = _browser.safari,
327
+ _supported = false,
328
+ _t = _getWindow(),
329
+ _d = _t.document,
330
+ _h = _t.history,
331
+ _l = _t.location,
332
+ _si = setInterval,
333
+ _st = setTimeout,
334
+ _dc = decodeURI,
335
+ _ec = encodeURI,
336
+ _agent = navigator.userAgent,
337
+ _iframe,
338
+ _form,
339
+ _url,
340
+ _title = _d.title,
341
+ _length = _h.length,
342
+ _silent = false,
343
+ _loaded = false,
344
+ _justset = true,
345
+ _juststart = true,
346
+ _stack = [],
347
+ _listeners = {},
348
+ _value = _getHash(),
349
+ _api = {},
350
+ _opts = {history: true, strict: true};
351
+
352
+ if (_msie)
353
+ _version = parseFloat(_agent.substr(_agent.indexOf('MSIE') + 4));
354
+
355
+ _supported =
356
+ (_mozilla && _version >= 1) ||
357
+ (_msie && _version >= 6) ||
358
+ (_opera && _version >= 9.5) ||
359
+ (_safari && _version >= 312);
360
+
361
+ if (_supported) {
362
+
363
+ for (var i = 1; i < _length; i++)
364
+ _stack.push('');
365
+
366
+ _stack.push(_getHash());
367
+
368
+ if (_msie && _l.hash != _getHash())
369
+ _l.hash = '#' + _ieLocal(_getHash(), true);
370
+
371
+ if (_opera)
372
+ history.navigationMode = 'compatible';
373
+
374
+ _searchScript(document);
375
+ var _qi = _url.indexOf('?');
376
+ if (_url && _qi > -1) {
377
+ var param, params = _url.substr(_qi + 1).split('&');
378
+ for (var i = 0, p; p = params[i]; i++) {
379
+ param = p.split('=');
380
+ if (/^(history|strict)$/.test(param[0])) {
381
+ _opts[param[0]] = (isNaN(param[1]) ? /^(true|yes)$/i.test(param[1]) : (parseInt(param[1]) != 0));
382
+ }
383
+ if (/^tracker$/.test(param[0]))
384
+ _opts[param[0]] = param[1];
385
+ }
386
+ }
387
+
388
+ $(_load);
389
+
390
+ } else if ((!_supported && _l.href.indexOf('#') != -1) ||
391
+ (_safari && _version < 418 && _l.href.indexOf('#') != -1 && _l.search != '')){
392
+ _d.open();
393
+ _d.write('<html><head><meta http-equiv="refresh" content="0;url=' +
394
+ _l.href.substr(0, _l.href.indexOf('#')) + '" /></head></html>');
395
+ _d.close();
396
+ } else {
397
+ _track();
398
+ }
399
+
400
+ $.each(('init,change').split(','), function(i, name){
401
+ _api[name] = function(data, fn){
402
+ $($.address).bind(name, fn || data, fn && data);
403
+ return this;
404
+ };
405
+ });
406
+
407
+ $.each(('baseURL,strict,history,tracker,title,value').split(','), function(i, name){
408
+ _api[name] = function(value){
409
+ if (typeof value != 'undefined') {
410
+ if (_supported)
411
+ _setters[name](value);
412
+ return $.address;
413
+ } else {
414
+ return _getters[name]();
415
+ }
416
+ };
417
+ });
418
+
419
+ $.each(('path,pathNames,queryString,parameter,parameterNames').split(','), function(i, name){
420
+ _api[name] = function(value){
421
+ return _getters[name](value);
422
+ };
423
+ });
424
+
425
+ return _api;
426
+
427
+ })();
428
+
429
+ $.fn.address = function (fn) {
430
+ $(this).click(function() {
431
+ var value = fn ? fn.call(this) :
432
+ /address:/.test($(this).attr('rel')) ? $(this).attr('rel').split('address:')[1].split(' ')[0] :
433
+ $(this).attr('href').replace(/^#/, '');
434
+ $.address.value(value);
435
+ return false;
436
+ });
437
+ };
438
+
439
+ }(jQuery));