2 * Galleria History Plugin v 1.0
3 * http://galleria.aino.se
5 * Original Copyright (c) 2009-2010 Rostislav Hristov
6 * Modified by Aino (c) 2010
8 * Dual licensed under the MIT or GPL Version 2 licenses.
9 * http://jquery.org/license
14 var G = window.Galleria;
15 if (typeof G == 'undefined') {
19 G.History = (function () {
21 var _trigger = function(name) {
23 $.extend($.Event(name),
26 parameterNames = G.History.parameterNames();
27 for (var i = 0, l = parameterNames.length; i < l; i++) {
28 parameters[parameterNames[i]] = G.History.parameter(parameterNames[i]);
31 value: G.History.value(),
32 path: G.History.path(),
33 pathNames: G.History.pathNames(),
34 parameterNames: parameterNames,
35 parameters: parameters,
36 queryString: G.History.queryString()
42 _bind = function(value, data, fn) {
43 fn = fn || function(){};
44 $(G.History).bind(value, data, fn);
48 var index = _l.href.indexOf('#');
49 return index != -1 ? _ec(_dc(_crawl(_l.href.substr(index + 1), FALSE))) : '';
51 _window = function() {
53 return top.document !== undefined ? top : window;
61 _strict = function(value, force) {
63 value = force ? (value.substr(0, 1) != '/' ? '/' + value : value) : (value == '' ? '/' : value);
67 _local = function(value, direction) {
68 return (_msie && _l.protocol == 'file:') ?
69 (direction ? _value.replace(/\?/, '%3F') : _value.replace(/%253F/, '?')) : value;
71 _crawl = function(value, direction) {
72 if (_opts.crawlable && direction) {
73 return (value != '' ? '!' : '') + value;
75 return value.replace(/^\!/, '');
77 _cssint = function(el, value) {
78 return parseInt(el.css(value), 10);
80 _search = function(el) {
82 for (var i = 0, l = el.childNodes.length; i < l; i++) {
83 if (el.childNodes[i].src) {
84 url = String(el.childNodes[i].src);
86 s = _search(el.childNodes[i]);
93 _listen = function() {
96 diff = _value != hash;
97 if (_safari && _version < 523) {
98 if (_length != _h.length) {
100 if (typeof _stack[_length - 1] != UNDEFINED) {
101 _value = _stack[_length - 1];
105 } else if (_msie && _version < 7 && diff) {
113 _update = function(internal) {
115 _trigger(internal ? INTERNAL_CHANGE : EXTERNAL_CHANGE);
118 _track = function() {
119 var value = (_l.pathname + (/\/$/.test(_l.pathname) ? '' : '/') +
120 (G.History ? G.History.value() : '')).replace(/\/\//, '/').replace(/^\/$/, ''),
121 fn = _t[_opts.tracker];
122 if (typeof fn == FUNCTION) {
124 } else if (typeof urchinTracker == FUNCTION) {
125 urchinTracker(value);
126 } else if (typeof pageTracker != UNDEFINED && typeof pageTracker._trackPageview == FUNCTION) {
127 pageTracker._trackPageview(value);
128 } else if (typeof _gaq != UNDEFINED && typeof _gaq.push == FUNCTION) {
129 _gaq.push(['_trackPageview', value]);
133 var doc = _frame.contentWindow.document;
135 doc.write('<html><head><title>' + _d.title + '</title><script>var ' + ID + ' = "' + _hash() + '";</' + 'script></head></html>');
141 var body = $('body').ajaxComplete(function() {
142 _unescape.call(this);
143 }).trigger('ajaxComplete');
145 var wrap = $('body > *')
146 .wrapAll('<div style="padding:' +
147 (_cssint(body, 'marginTop') + _cssint(body, 'paddingTop')) + 'px ' +
148 (_cssint(body, 'marginRight') + _cssint(body, 'paddingRight')) + 'px ' +
149 (_cssint(body, 'marginBottom') + _cssint(body, 'paddingBottom')) + 'px ' +
150 (_cssint(body, 'marginLeft') + _cssint(body, 'paddingLeft')) + 'px;" />')
152 .wrap('<div id="' + ID + '" style="height:100%; overflow:auto;' +
153 (_safari ? (window.statusbar.visible && !/chrome/i.test(_agent) ? '' : ' resize:both;') : '') + '" />');
162 $('<style type="text/css" />')
164 .text('#' + ID + '::-webkit-resizer { background-color: #fff; }');
167 if (_msie && _version < 8) {
168 var frameset = _d.getElementsByTagName('frameset')[0];
169 _frame = _d.createElement((frameset ? '' : 'i') + 'frame');
171 frameset.insertAdjacentElement('beforeEnd', _frame);
172 frameset[frameset.cols ? 'cols' : 'rows'] += ',0';
173 _frame.src = _js() + ':' + FALSE;
174 _frame.noResize = TRUE;
175 _frame.frameBorder = _frame.frameSpacing = 0;
177 _frame.src = _js() + ':' + FALSE;
178 _frame.style.display = 'none';
179 _d.body.insertAdjacentElement('afterBegin', _frame);
182 $(_frame).bind('load', function() {
183 var win = _frame.contentWindow;
184 var src = win.location.href;
185 _value = (typeof win[ID] != UNDEFINED ? win[ID] : '');
186 if (_value != _hash()) {
188 _l.hash = _local(_crawl(_value, TRUE), TRUE);
191 if (typeof _frame.contentWindow[ID] == UNDEFINED) {
195 } else if (_safari) {
196 if (_version < 418) {
197 $(_d.body).append('<form id="' + ID + '" style="position:absolute;top:-9999px;" method="get"></form>');
198 _form = _d.getElementById(ID);
200 if (typeof _l[ID] == UNDEFINED) {
203 if (typeof _l[ID][_l.pathname] != UNDEFINED) {
204 _stack = _l[ID][_l.pathname].split(',');
213 if ((_msie && _version > 7) || (!_msie && ('on' + HASH_CHANGE) in _t)) {
214 if (_t.addEventListener) {
215 _t.addEventListener(HASH_CHANGE, _listen, false);
216 } else if (_t.attachEvent) {
217 _t.attachEvent('on' + HASH_CHANGE, _listen);
223 //$('a').filter('[rel*=address:]').address();
226 _unload = function() {
227 if (_t.removeEventListener) {
228 _t.removeEventListener(HASH_CHANGE, _listen, false);
229 } else if (_t.detachEvent) {
230 _t.detachEvent('on' + HASH_CHANGE, _listen);
233 _unescape = function() {
234 var base = _l.pathname.replace(/\/$/, ''),
235 fragment = '_escaped_fragment_';
236 if ($('body').html().indexOf(fragment) != -1) {
237 $('a[href]:not([href^=http])', this).each(function() {
238 var href = $(this).attr('href').replace(new RegExp(base + '/?$'), '');
239 if (href == '' || href.indexOf(fragment) != -1) {
240 $(this).attr('href', '#' + decodeURIComponent(href.replace(new RegExp('/(.*)\\?' + fragment + '=(.*)$'), '!$2')));
245 ID = 'jQueryAddress',
246 FUNCTION = 'function',
247 UNDEFINED = 'undefined',
248 HASH_CHANGE = 'hashchange',
251 INTERNAL_CHANGE = 'internalChange',
252 EXTERNAL_CHANGE = 'externalChange',
262 _browser = $.browser,
263 _version = parseFloat($.browser.version),
264 _mozilla = _browser.mozilla,
265 _msie = _browser.msie,
266 _opera = _browser.opera,
267 _safari = _browser.safari,
277 _agent = navigator.userAgent,
280 _url = _search(document),
281 _qi = _url ? _url.indexOf('?') : -1,
294 _version = parseFloat(_agent.substr(_agent.indexOf('MSIE') + 4));
295 if (_d.documentMode && _d.documentMode != _version) {
296 _version = _d.documentMode != 8 ? 7 : 8;
298 $(document).bind('propertychange', function() {
299 if (_d.title != _title && _d.title.indexOf('#' + _hash()) != -1) {
306 (_mozilla && _version >= 1) ||
307 (_msie && _version >= 6) ||
308 (_opera && _version >= 9.5) ||
309 (_safari && _version >= 312);
313 for (var i = 1; i < _length; i++) {
319 if (_msie && _l.hash != _value) {
320 _l.hash = '#' + _local(_crawl(_value, TRUE), TRUE);
324 history.navigationMode = 'compatible';
327 if (_url && _qi != -1) {
328 var param, params = _url.substr(_qi + 1).split('&');
329 for (i = 0; i < params.length; i++) {
330 param = params[i].split('=');
331 if (/^(autoUpdate|crawlable|history|strict|wrap)$/.test(param[0])) {
332 _opts[param[0]] = (isNaN(param[1]) ? /^(true|yes)$/i.test(param[1]) : (parseInt(param[1], 10) !== 0));
334 if (/^tracker$/.test(param[0])) {
335 _opts[param[0]] = param[1];
340 if (document.readyState == 'complete') {
344 $(window).bind('unload', _unload);
346 } else if ((!_supported && _hash() != '') ||
347 (_safari && _version < 418 && _hash() != '' && _l.search != '')) {
349 _d.write('<html><head><meta http-equiv="refresh" content="0;url=' +
350 encodeURI(_l.href.substr(0, _l.href.indexOf('#'))) + '" /></head></html>');
357 bind: function(type, data, fn) {
358 return _bind(type, data, fn);
361 return _bind(INIT, fn);
363 change: function(fn) {
364 return _bind(CHANGE, fn);
366 internalChange: function(fn) {
367 return _bind(INTERNAL_CHANGE, fn);
369 externalChange: function(fn) {
370 return _bind(EXTERNAL_CHANGE, fn);
372 baseURL: function() {
374 if (url.indexOf('#') != -1) {
375 url = url.substr(0, url.indexOf('#'));
377 if (/\/$/.test(url)) {
378 url = url.substr(0, url.length - 1);
382 autoUpdate: function(value) {
383 if (value !== undefined) {
384 _opts.autoUpdate = value;
387 return _opts.autoUpdate;
389 crawlable: function(value) {
390 if (value !== undefined) {
391 _opts.crawlable = value;
394 return _opts.crawlable;
396 history: function(value) {
397 if (value !== undefined) {
398 _opts.history = value;
401 return _opts.history;
403 strict: function(value) {
404 if (value !== undefined) {
405 _opts.strict = value;
410 tracker: function(value) {
411 if (value !== undefined) {
412 _opts.tracker = value;
415 return _opts.tracker;
417 wrap: function(value) {
418 if (value !== undefined) {
430 title: function(value) {
431 if (value !== undefined) {
434 _title = _d.title = value;
435 if (_juststart && _frame && _frame.contentWindow && _frame.contentWindow.document) {
436 _frame.contentWindow.document.title = value;
439 if (!_justset && _mozilla) {
440 _l.replace(_l.href.indexOf('#') != -1 ? _l.href : _l.href + '#');
448 value: function(value) {
449 if (value !== undefined) {
450 value = _ec(_dc(_strict(value, TRUE)));
454 if (_value == value && !_updating) {
459 if (_opts.autoUpdate || _updating) {
462 _stack[_h.length] = _value;
465 _l[ID][_l.pathname] = _stack.toString();
466 _length = _h.length + 1;
467 if (_version < 418) {
468 if (_l.search == '') {
469 _form.action = '#' + _crawl(_value, TRUE);
472 } else if (_version < 523 || _value == '') {
473 var evt = _d.createEvent('MouseEvents');
474 evt.initEvent('click', TRUE, TRUE);
475 var anchor = _d.createElement('a');
476 anchor.href = '#' + _crawl(_value, TRUE);
477 anchor.dispatchEvent(evt);
479 _l.hash = '#' + _crawl(_value, TRUE);
482 _l.replace('#' + _crawl(_value, TRUE));
484 } else if (_value != _hash()) {
486 _l.hash = '#' + _local(_crawl(_value, TRUE), TRUE);
488 _l.replace('#' + _crawl(_value, TRUE));
491 if ((_msie && _version < 8) && _opts.history) {
495 _st(function(){ _silent = FALSE; }, 1);
505 return _dc(_strict(_local(_value, FALSE), FALSE));
507 path: function(value) {
508 if (value !== undefined) {
509 var qs = this.queryString(),
511 this.value(value + (qs ? '?' + qs : '') + (hash ? '#' + hash : ''));
514 return this.value().split('#')[0].split('?')[0];
516 queryString: function(value) {
517 if (value !== undefined) {
518 var hash = this.hash();
519 this.value(this.path() + (value ? '?' + value : '') + (hash ? '#' + hash : ''));
522 var arr = this.value().split('?');
523 return arr.slice(1, arr.length).join('?').split('#')[0];
525 parameter: function(name, value, append) {
527 if (value !== undefined) {
528 var names = this.parameterNames();
530 for (i = 0; i < names.length; i++) {
532 v = this.parameter(n);
533 if (typeof v == 'string') {
537 v = (value === null || value === '') ? [] :
538 (append ? v.concat([value]) : [value]);
540 for (var j = 0; j < v.length; j++) {
541 params.push(n + '=' + v[j]);
544 if ($.inArray(name, names) == -1 && value !== null && value !== '') {
545 params.push(name + '=' + value);
547 this.queryString(params.join('&'));
550 value = this.queryString();
552 params = value.split('&');
554 for (i = 0; i < params.length; i++) {
555 var p = params[i].split('=');
560 if (r.length !== 0) {
561 return r.length != 1 ? r : r[0];
565 pathNames: function() {
566 var path = this.path(),
567 names = path.replace(/\/{2,9}/g, '/').split('/');
568 if (path.substr(0, 1) == '/' || path.length === 0) {
571 if (path.substr(path.length - 1, 1) == '/') {
572 names.splice(names.length - 1, 1);
576 parameterNames: function() {
577 var qs = this.queryString(),
579 if (qs && qs.indexOf('=') != -1) {
580 var params = qs.split('&');
581 for (var i = 0; i < params.length; i++) {
582 var name = params[i].split('=')[0];
583 if ($.inArray(name, names) == -1) {
590 hash: function(value) {
591 if (value !== undefined) {
592 this.value(this.value().split('#')[0] + (value ? '#' + value : ''));
595 var arr = this.value().split('#');
596 return arr.slice(1, arr.length).join('#');