Mercurial > kallithea
annotate rhodecode/public/js/yui/history/history.js @ 1073:289ff43cc190 beta
update YUI dev libs to latest version
author | Marcin Kuzminski <marcin@python-works.com> |
---|---|
date | Sun, 20 Feb 2011 20:58:07 +0100 |
parents | 1e757ac98988 |
children |
rev | line source |
---|---|
0 | 1 /* |
1073
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
2 Copyright (c) 2010, Yahoo! Inc. All rights reserved. |
0 | 3 Code licensed under the BSD License: |
1073
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
4 http://developer.yahoo.com/yui/license.html |
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
5 version: 2.8.2r1 |
0 | 6 */ |
7 /** | |
8 * The Browser History Manager provides the ability to use the back/forward | |
9 * navigation buttons in a DHTML application. It also allows a DHTML | |
10 * application to be bookmarked in a specific state. | |
11 * | |
12 * This library requires the following static markup: | |
13 * | |
14 * <iframe id="yui-history-iframe" src="path-to-real-asset-in-same-domain"></iframe> | |
15 * <input id="yui-history-field" type="hidden"> | |
16 * | |
17 * @module history | |
18 * @requires yahoo,event | |
19 * @namespace YAHOO.util | |
20 * @title Browser History Manager | |
21 */ | |
22 | |
23 /** | |
24 * The History class provides the ability to use the back/forward navigation | |
25 * buttons in a DHTML application. It also allows a DHTML application to | |
26 * be bookmarked in a specific state. | |
27 * | |
28 * @class History | |
29 * @constructor | |
30 */ | |
31 YAHOO.util.History = (function () { | |
32 | |
33 /** | |
34 * Our hidden IFrame used to store the browsing history. | |
35 * | |
36 * @property _histFrame | |
37 * @type HTMLIFrameElement | |
38 * @default null | |
39 * @private | |
40 */ | |
41 var _histFrame = null; | |
42 | |
43 /** | |
44 * INPUT field (with type="hidden" or type="text") or TEXTAREA. | |
45 * This field keeps the value of the initial state, current state | |
46 * the list of all states across pages within a single browser session. | |
47 * | |
48 * @property _stateField | |
49 * @type HTMLInputElement|HTMLTextAreaElement | |
50 * @default null | |
51 * @private | |
52 */ | |
53 var _stateField = null; | |
54 | |
55 /** | |
56 * Flag used to tell whether YAHOO.util.History.initialize has been called. | |
57 * | |
58 * @property _initialized | |
59 * @type boolean | |
60 * @default false | |
61 * @private | |
62 */ | |
63 var _initialized = false; | |
64 | |
65 /** | |
66 * List of registered modules. | |
67 * | |
68 * @property _modules | |
69 * @type array | |
70 * @default [] | |
71 * @private | |
72 */ | |
73 var _modules = []; | |
74 | |
75 /** | |
76 * List of fully qualified states. This is used only by Safari. | |
77 * | |
78 * @property _fqstates | |
79 * @type array | |
80 * @default [] | |
81 * @private | |
82 */ | |
83 var _fqstates = []; | |
84 | |
85 /** | |
86 * location.hash is a bit buggy on Opera. I have seen instances where | |
87 * navigating the history using the back/forward buttons, and hence | |
88 * changing the URL, would not change location.hash. That's ok, the | |
89 * implementation of an equivalent is trivial. | |
90 * | |
91 * @method _getHash | |
92 * @return {string} The hash portion of the document's location | |
93 * @private | |
94 */ | |
95 function _getHash() { | |
96 var i, href; | |
97 href = top.location.href; | |
98 i = href.indexOf("#"); | |
99 return i >= 0 ? href.substr(i + 1) : null; | |
100 } | |
101 | |
102 /** | |
103 * Stores all the registered modules' initial state and current state. | |
104 * On Safari, we also store all the fully qualified states visited by | |
105 * the application within a single browser session. The storage takes | |
106 * place in the form field specified during initialization. | |
107 * | |
108 * @method _storeStates | |
109 * @private | |
110 */ | |
111 function _storeStates() { | |
112 | |
113 var moduleName, moduleObj, initialStates = [], currentStates = []; | |
114 | |
115 for (moduleName in _modules) { | |
116 if (YAHOO.lang.hasOwnProperty(_modules, moduleName)) { | |
117 moduleObj = _modules[moduleName]; | |
118 initialStates.push(moduleName + "=" + moduleObj.initialState); | |
119 currentStates.push(moduleName + "=" + moduleObj.currentState); | |
120 } | |
121 } | |
122 | |
123 _stateField.value = initialStates.join("&") + "|" + currentStates.join("&"); | |
124 | |
125 if (YAHOO.env.ua.webkit) { | |
126 _stateField.value += "|" + _fqstates.join(","); | |
127 } | |
128 } | |
129 | |
130 /** | |
131 * Sets the new currentState attribute of all modules depending on the new | |
132 * fully qualified state. Also notifies the modules which current state has | |
133 * changed. | |
134 * | |
135 * @method _handleFQStateChange | |
136 * @param {string} fqstate Fully qualified state | |
137 * @private | |
138 */ | |
139 function _handleFQStateChange(fqstate) { | |
140 | |
141 var i, len, moduleName, moduleObj, modules, states, tokens, currentState; | |
142 | |
143 if (!fqstate) { | |
144 // Notifies all modules | |
145 for (moduleName in _modules) { | |
146 if (YAHOO.lang.hasOwnProperty(_modules, moduleName)) { | |
147 moduleObj = _modules[moduleName]; | |
148 moduleObj.currentState = moduleObj.initialState; | |
149 moduleObj.onStateChange(unescape(moduleObj.currentState)); | |
150 } | |
151 } | |
152 return; | |
153 } | |
154 | |
155 modules = []; | |
156 states = fqstate.split("&"); | |
157 for (i = 0, len = states.length; i < len; i++) { | |
158 tokens = states[i].split("="); | |
159 if (tokens.length === 2) { | |
160 moduleName = tokens[0]; | |
161 currentState = tokens[1]; | |
162 modules[moduleName] = currentState; | |
163 } | |
164 } | |
165 | |
166 for (moduleName in _modules) { | |
167 if (YAHOO.lang.hasOwnProperty(_modules, moduleName)) { | |
168 moduleObj = _modules[moduleName]; | |
169 currentState = modules[moduleName]; | |
170 if (!currentState || moduleObj.currentState !== currentState) { | |
171 moduleObj.currentState = currentState || moduleObj.initialState; | |
172 moduleObj.onStateChange(unescape(moduleObj.currentState)); | |
173 } | |
174 } | |
175 } | |
176 } | |
177 | |
178 /** | |
179 * Update the IFrame with our new state. | |
180 * | |
181 * @method _updateIFrame | |
182 * @private | |
183 * @return {boolean} true if successful. false otherwise. | |
184 */ | |
185 function _updateIFrame (fqstate) { | |
186 | |
187 var html, doc; | |
188 | |
1073
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
189 html = '<html><body><div id="state">' + |
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
190 fqstate.replace(/&/g,'&'). |
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
191 replace(/</g,'<'). |
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
192 replace(/>/g,'>'). |
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
193 replace(/"/g,'"') + |
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
194 '</div></body></html>'; |
0 | 195 |
196 try { | |
197 doc = _histFrame.contentWindow.document; | |
198 doc.open(); | |
199 doc.write(html); | |
200 doc.close(); | |
201 return true; | |
202 } catch (e) { | |
203 return false; | |
204 } | |
205 } | |
206 | |
207 /** | |
208 * Periodically checks whether our internal IFrame is ready to be used. | |
209 * | |
210 * @method _checkIframeLoaded | |
211 * @private | |
212 */ | |
213 function _checkIframeLoaded() { | |
214 | |
215 var doc, elem, fqstate, hash; | |
216 | |
217 if (!_histFrame.contentWindow || !_histFrame.contentWindow.document) { | |
218 // Check again in 10 msec... | |
219 setTimeout(_checkIframeLoaded, 10); | |
220 return; | |
221 } | |
222 | |
223 // Start the thread that will have the responsibility to | |
224 // periodically check whether a navigate operation has been | |
225 // requested on the main window. This will happen when | |
226 // YAHOO.util.History.navigate has been called or after | |
227 // the user has hit the back/forward button. | |
228 | |
229 doc = _histFrame.contentWindow.document; | |
230 elem = doc.getElementById("state"); | |
231 // We must use innerText, and not innerHTML because our string contains | |
232 // the "&" character (which would end up being escaped as "&") and | |
233 // the string comparison would fail... | |
234 fqstate = elem ? elem.innerText : null; | |
235 | |
236 hash = _getHash(); | |
237 | |
238 setInterval(function () { | |
239 | |
240 var newfqstate, states, moduleName, moduleObj, newHash, historyLength; | |
241 | |
242 doc = _histFrame.contentWindow.document; | |
243 elem = doc.getElementById("state"); | |
244 // See my comment above about using innerText instead of innerHTML... | |
245 newfqstate = elem ? elem.innerText : null; | |
246 | |
247 newHash = _getHash(); | |
248 | |
249 if (newfqstate !== fqstate) { | |
250 | |
251 fqstate = newfqstate; | |
252 _handleFQStateChange(fqstate); | |
253 | |
254 if (!fqstate) { | |
255 states = []; | |
256 for (moduleName in _modules) { | |
257 if (YAHOO.lang.hasOwnProperty(_modules, moduleName)) { | |
258 moduleObj = _modules[moduleName]; | |
259 states.push(moduleName + "=" + moduleObj.initialState); | |
260 } | |
261 } | |
262 newHash = states.join("&"); | |
263 } else { | |
264 newHash = fqstate; | |
265 } | |
266 | |
267 // Allow the state to be bookmarked by setting the top window's | |
268 // URL fragment identifier. Note that here, we are on IE, and | |
269 // IE does not touch the browser history when setting the hash | |
270 // (unlike all the other browsers). I used to write: | |
271 // top.location.replace( "#" + hash ); | |
272 // but this had a side effect when the page was not the top frame. | |
273 top.location.hash = newHash; | |
274 hash = newHash; | |
275 | |
276 _storeStates(); | |
277 | |
278 } else if (newHash !== hash) { | |
279 | |
280 // The hash has changed. The user might have clicked on a link, | |
281 // or modified the URL directly, or opened the same application | |
282 // bookmarked in a specific state using a bookmark. However, we | |
283 // know the hash change was not caused by a hit on the back or | |
284 // forward buttons, or by a call to navigate() (because it would | |
285 // have been handled above) We must handle these cases, which is | |
286 // why we also need to keep track of hash changes on IE! | |
287 | |
288 // Note that IE6 has some major issues with this kind of user | |
289 // interaction (the history stack gets completely messed up) | |
290 // but it seems to work fine on IE7. | |
291 | |
292 hash = newHash; | |
293 | |
294 // Now, store a new history entry. The following will cause the | |
295 // code above to execute, doing all the dirty work for us... | |
296 _updateIFrame(newHash); | |
297 } | |
298 | |
299 }, 50); | |
300 | |
301 _initialized = true; | |
302 YAHOO.util.History.onLoadEvent.fire(); | |
303 } | |
304 | |
305 /** | |
306 * Finish up the initialization of the Browser History Manager. | |
307 * | |
308 * @method _initialize | |
309 * @private | |
310 */ | |
311 function _initialize() { | |
312 | |
313 var i, len, parts, tokens, moduleName, moduleObj, initialStates, initialState, currentStates, currentState, counter, hash; | |
314 | |
315 // Decode the content of our storage field... | |
316 parts = _stateField.value.split("|"); | |
317 | |
318 if (parts.length > 1) { | |
319 | |
320 initialStates = parts[0].split("&"); | |
321 for (i = 0, len = initialStates.length; i < len; i++) { | |
322 tokens = initialStates[i].split("="); | |
323 if (tokens.length === 2) { | |
324 moduleName = tokens[0]; | |
325 initialState = tokens[1]; | |
326 moduleObj = _modules[moduleName]; | |
327 if (moduleObj) { | |
328 moduleObj.initialState = initialState; | |
329 } | |
330 } | |
331 } | |
332 | |
333 currentStates = parts[1].split("&"); | |
334 for (i = 0, len = currentStates.length; i < len; i++) { | |
335 tokens = currentStates[i].split("="); | |
336 if (tokens.length >= 2) { | |
337 moduleName = tokens[0]; | |
338 currentState = tokens[1]; | |
339 moduleObj = _modules[moduleName]; | |
340 if (moduleObj) { | |
341 moduleObj.currentState = currentState; | |
342 } | |
343 } | |
344 } | |
345 } | |
346 | |
347 if (parts.length > 2) { | |
348 _fqstates = parts[2].split(","); | |
349 } | |
350 | |
351 if (YAHOO.env.ua.ie) { | |
352 | |
353 if (typeof document.documentMode === "undefined" || document.documentMode < 8) { | |
354 | |
355 // IE < 8 or IE8 in quirks mode or IE7 standards mode | |
356 _checkIframeLoaded(); | |
357 | |
358 } else { | |
359 | |
360 // IE8 in IE8 standards mode | |
361 YAHOO.util.Event.on(top, "hashchange", | |
362 function () { | |
363 var hash = _getHash(); | |
364 _handleFQStateChange(hash); | |
365 _storeStates(); | |
366 }); | |
367 | |
368 _initialized = true; | |
369 YAHOO.util.History.onLoadEvent.fire(); | |
370 | |
371 } | |
372 | |
373 } else { | |
374 | |
375 // Start the thread that will have the responsibility to | |
376 // periodically check whether a navigate operation has been | |
377 // requested on the main window. This will happen when | |
378 // YAHOO.util.History.navigate has been called or after | |
379 // the user has hit the back/forward button. | |
380 | |
381 // On Safari 1.x and 2.0, the only way to catch a back/forward | |
382 // operation is to watch history.length... We basically exploit | |
383 // what I consider to be a bug (history.length is not supposed | |
384 // to change when going back/forward in the history...) This is | |
385 // why, in the following thread, we first compare the hash, | |
386 // because the hash thing will be fixed in the next major | |
387 // version of Safari. So even if they fix the history.length | |
388 // bug, all this will still work! | |
389 counter = history.length; | |
390 | |
391 // On Gecko and Opera, we just need to watch the hash... | |
392 hash = _getHash(); | |
393 | |
394 setInterval(function () { | |
395 | |
396 var state, newHash, newCounter; | |
397 | |
398 newHash = _getHash(); | |
399 newCounter = history.length; | |
400 if (newHash !== hash) { | |
401 hash = newHash; | |
402 counter = newCounter; | |
403 _handleFQStateChange(hash); | |
404 _storeStates(); | |
405 } else if (newCounter !== counter && YAHOO.env.ua.webkit) { | |
406 hash = newHash; | |
407 counter = newCounter; | |
408 state = _fqstates[counter - 1]; | |
409 _handleFQStateChange(state); | |
410 _storeStates(); | |
411 } | |
412 | |
413 }, 50); | |
414 | |
415 _initialized = true; | |
416 YAHOO.util.History.onLoadEvent.fire(); | |
417 } | |
418 } | |
419 | |
420 return { | |
421 | |
422 /** | |
423 * Fired when the Browser History Manager is ready. If you subscribe to | |
424 * this event after the Browser History Manager has been initialized, | |
425 * it will not fire. Therefore, it is recommended to use the onReady | |
426 * method instead. | |
427 * | |
428 * @event onLoadEvent | |
429 * @see onReady | |
430 */ | |
431 onLoadEvent: new YAHOO.util.CustomEvent("onLoad"), | |
432 | |
433 /** | |
434 * Executes the supplied callback when the Browser History Manager is | |
435 * ready. This will execute immediately if called after the Browser | |
436 * History Manager onLoad event has fired. | |
437 * | |
438 * @method onReady | |
439 * @param {function} fn what to execute when the Browser History Manager is ready. | |
440 * @param {object} obj an optional object to be passed back as a parameter to fn. | |
441 * @param {boolean|object} overrideContext If true, the obj passed in becomes fn's execution scope. | |
442 * @see onLoadEvent | |
443 */ | |
444 onReady: function (fn, obj, overrideContext) { | |
445 | |
446 if (_initialized) { | |
447 | |
448 setTimeout(function () { | |
449 var ctx = window; | |
450 if (overrideContext) { | |
451 if (overrideContext === true) { | |
452 ctx = obj; | |
453 } else { | |
454 ctx = overrideContext; | |
455 } | |
456 } | |
457 fn.call(ctx, "onLoad", [], obj); | |
458 }, 0); | |
459 | |
460 } else { | |
461 | |
462 YAHOO.util.History.onLoadEvent.subscribe(fn, obj, overrideContext); | |
463 | |
464 } | |
465 }, | |
466 | |
467 /** | |
468 * Registers a new module. | |
469 * | |
470 * @method register | |
471 * @param {string} module Non-empty string uniquely identifying the | |
472 * module you wish to register. | |
473 * @param {string} initialState The initial state of the specified | |
474 * module corresponding to its earliest history entry. | |
475 * @param {function} onStateChange Callback called when the | |
476 * state of the specified module has changed. | |
477 * @param {object} obj An arbitrary object that will be passed as a | |
478 * parameter to the handler. | |
479 * @param {boolean} overrideContext If true, the obj passed in becomes the | |
480 * execution scope of the listener. | |
481 */ | |
482 register: function (module, initialState, onStateChange, obj, overrideContext) { | |
483 | |
484 var scope, wrappedFn; | |
485 | |
486 if (typeof module !== "string" || YAHOO.lang.trim(module) === "" || | |
487 typeof initialState !== "string" || | |
488 typeof onStateChange !== "function") { | |
489 throw new Error("Missing or invalid argument"); | |
490 } | |
491 | |
492 if (_modules[module]) { | |
493 // Here, we used to throw an exception. However, users have | |
494 // complained about this behavior, so we now just return. | |
495 return; | |
496 } | |
497 | |
498 // Note: A module CANNOT be registered after calling | |
499 // YAHOO.util.History.initialize. Indeed, we set the initial state | |
500 // of each registered module in YAHOO.util.History.initialize. | |
501 // If you could register a module after initializing the Browser | |
502 // History Manager, you would not read the correct state using | |
503 // YAHOO.util.History.getCurrentState when coming back to the | |
504 // page using the back button. | |
505 if (_initialized) { | |
506 throw new Error("All modules must be registered before calling YAHOO.util.History.initialize"); | |
507 } | |
508 | |
509 // Make sure the strings passed in do not contain our separators "," and "|" | |
510 module = escape(module); | |
511 initialState = escape(initialState); | |
512 | |
513 // If the user chooses to override the scope, we use the | |
514 // custom object passed in as the execution scope. | |
515 scope = null; | |
516 if (overrideContext === true) { | |
517 scope = obj; | |
518 } else { | |
519 scope = overrideContext; | |
520 } | |
521 | |
522 wrappedFn = function (state) { | |
523 return onStateChange.call(scope, state, obj); | |
524 }; | |
525 | |
526 _modules[module] = { | |
527 name: module, | |
528 initialState: initialState, | |
529 currentState: initialState, | |
530 onStateChange: wrappedFn | |
531 }; | |
532 }, | |
533 | |
534 /** | |
535 * Initializes the Browser History Manager. Call this method | |
536 * from a script block located right after the opening body tag. | |
537 * | |
538 * @method initialize | |
539 * @param {string|HTML Element} stateField <input type="hidden"> used | |
540 * to store application states. Must be in the static markup. | |
541 * @param {string|HTML Element} histFrame IFrame used to store | |
542 * the history (only required on Internet Explorer) | |
543 * @public | |
544 */ | |
545 initialize: function (stateField, histFrame) { | |
546 | |
547 if (_initialized) { | |
548 // The browser history manager has already been initialized. | |
549 return; | |
550 } | |
551 | |
552 if (YAHOO.env.ua.opera && typeof history.navigationMode !== "undefined") { | |
553 // Disable Opera's fast back/forward navigation mode and puts | |
554 // it in compatible mode. This makes anchor-based history | |
555 // navigation work after the page has been navigated away | |
556 // from and re-activated, at the cost of slowing down | |
557 // back/forward navigation to and from that page. | |
558 history.navigationMode = "compatible"; | |
559 } | |
560 | |
561 if (typeof stateField === "string") { | |
562 stateField = document.getElementById(stateField); | |
563 } | |
564 | |
565 if (!stateField || | |
566 stateField.tagName.toUpperCase() !== "TEXTAREA" && | |
567 (stateField.tagName.toUpperCase() !== "INPUT" || | |
568 stateField.type !== "hidden" && | |
569 stateField.type !== "text")) { | |
570 throw new Error("Missing or invalid argument"); | |
571 } | |
572 | |
573 _stateField = stateField; | |
574 | |
575 // IE < 8 or IE8 in quirks mode or IE7 standards mode | |
576 if (YAHOO.env.ua.ie && (typeof document.documentMode === "undefined" || document.documentMode < 8)) { | |
577 | |
578 if (typeof histFrame === "string") { | |
579 histFrame = document.getElementById(histFrame); | |
580 } | |
581 | |
582 if (!histFrame || histFrame.tagName.toUpperCase() !== "IFRAME") { | |
583 throw new Error("Missing or invalid argument"); | |
584 } | |
585 | |
586 _histFrame = histFrame; | |
587 } | |
588 | |
589 // Note that the event utility MUST be included inline in the page. | |
590 // If it gets loaded later (which you may want to do to improve the | |
591 // loading speed of your site), the onDOMReady event never fires, | |
592 // and the history library never gets fully initialized. | |
593 YAHOO.util.Event.onDOMReady(_initialize); | |
594 }, | |
595 | |
596 /** | |
597 * Call this method when you want to store a new entry in the browser's history. | |
598 * | |
599 * @method navigate | |
600 * @param {string} module Non-empty string representing your module. | |
601 * @param {string} state String representing the new state of the specified module. | |
602 * @return {boolean} Indicates whether the new state was successfully added to the history. | |
603 * @public | |
604 */ | |
605 navigate: function (module, state) { | |
606 | |
607 var states; | |
608 | |
609 if (typeof module !== "string" || typeof state !== "string") { | |
610 throw new Error("Missing or invalid argument"); | |
611 } | |
612 | |
613 states = {}; | |
614 states[module] = state; | |
615 | |
616 return YAHOO.util.History.multiNavigate(states); | |
617 }, | |
618 | |
619 /** | |
620 * Call this method when you want to store a new entry in the browser's history. | |
621 * | |
622 * @method multiNavigate | |
623 * @param {object} states Associative array of module-state pairs to set simultaneously. | |
624 * @return {boolean} Indicates whether the new state was successfully added to the history. | |
625 * @public | |
626 */ | |
627 multiNavigate: function (states) { | |
628 | |
629 var currentStates, moduleName, moduleObj, currentState, fqstate; | |
630 | |
631 if (typeof states !== "object") { | |
632 throw new Error("Missing or invalid argument"); | |
633 } | |
634 | |
635 if (!_initialized) { | |
636 throw new Error("The Browser History Manager is not initialized"); | |
637 } | |
638 | |
639 for (moduleName in states) { | |
640 if (!_modules[moduleName]) { | |
641 throw new Error("The following module has not been registered: " + moduleName); | |
642 } | |
643 } | |
644 | |
645 // Generate our new full state string mod1=xxx&mod2=yyy | |
646 currentStates = []; | |
647 | |
648 for (moduleName in _modules) { | |
649 if (YAHOO.lang.hasOwnProperty(_modules, moduleName)) { | |
650 moduleObj = _modules[moduleName]; | |
651 if (YAHOO.lang.hasOwnProperty(states, moduleName)) { | |
652 currentState = states[unescape(moduleName)]; | |
653 } else { | |
654 currentState = unescape(moduleObj.currentState); | |
655 } | |
656 | |
657 // Make sure the strings passed in do not contain our separators "," and "|" | |
658 moduleName = escape(moduleName); | |
659 currentState = escape(currentState); | |
660 | |
661 currentStates.push(moduleName + "=" + currentState); | |
662 } | |
663 } | |
664 | |
665 fqstate = currentStates.join("&"); | |
666 | |
667 if (YAHOO.env.ua.ie && (typeof document.documentMode === "undefined" || document.documentMode < 8)) { | |
668 | |
669 return _updateIFrame(fqstate); | |
670 | |
671 } else { | |
672 | |
673 // Known bug: On Safari 1.x and 2.0, if you have tab browsing | |
674 // enabled, Safari will show an endless loading icon in the | |
675 // tab. This has apparently been fixed in recent WebKit builds. | |
676 // One work around found by Dav Glass is to submit a form that | |
677 // points to the same document. This indeed works on Safari 1.x | |
678 // and 2.0 but creates bigger problems on WebKit. So for now, | |
679 // we'll consider this an acceptable bug, and hope that Apple | |
680 // comes out with their next version of Safari very soon. | |
681 top.location.hash = fqstate; | |
682 if (YAHOO.env.ua.webkit) { | |
683 // The following two lines are only useful for Safari 1.x | |
684 // and 2.0. Recent nightly builds of WebKit do not require | |
685 // that, but unfortunately, it is not easy to differentiate | |
686 // between the two. Once Safari 2.0 departs the A-grade | |
687 // list, we can remove the following two lines... | |
688 _fqstates[history.length] = fqstate; | |
689 _storeStates(); | |
690 } | |
691 | |
692 return true; | |
693 | |
694 } | |
695 }, | |
696 | |
697 /** | |
698 * Returns the current state of the specified module. | |
699 * | |
700 * @method getCurrentState | |
701 * @param {string} module Non-empty string representing your module. | |
702 * @return {string} The current state of the specified module. | |
703 * @public | |
704 */ | |
705 getCurrentState: function (module) { | |
706 | |
707 var moduleObj; | |
708 | |
709 if (typeof module !== "string") { | |
710 throw new Error("Missing or invalid argument"); | |
711 } | |
712 | |
713 if (!_initialized) { | |
714 throw new Error("The Browser History Manager is not initialized"); | |
715 } | |
716 | |
717 moduleObj = _modules[module]; | |
718 if (!moduleObj) { | |
719 throw new Error("No such registered module: " + module); | |
720 } | |
721 | |
722 return unescape(moduleObj.currentState); | |
723 }, | |
724 | |
725 /** | |
726 * Returns the state of a module according to the URL fragment | |
727 * identifier. This method is useful to initialize your modules | |
728 * if your application was bookmarked from a particular state. | |
729 * | |
730 * @method getBookmarkedState | |
731 * @param {string} module Non-empty string representing your module. | |
732 * @return {string} The bookmarked state of the specified module. | |
733 * @public | |
734 */ | |
735 getBookmarkedState: function (module) { | |
736 | |
737 var i, len, idx, hash, states, tokens, moduleName; | |
738 | |
739 if (typeof module !== "string") { | |
740 throw new Error("Missing or invalid argument"); | |
741 } | |
742 | |
743 // Use location.href instead of location.hash which is already | |
744 // URL-decoded, which creates problems if the state value | |
745 // contained special characters... | |
746 idx = top.location.href.indexOf("#"); | |
747 if (idx >= 0) { | |
748 hash = top.location.href.substr(idx + 1); | |
749 states = hash.split("&"); | |
750 for (i = 0, len = states.length; i < len; i++) { | |
751 tokens = states[i].split("="); | |
752 if (tokens.length === 2) { | |
753 moduleName = tokens[0]; | |
754 if (moduleName === module) { | |
755 return unescape(tokens[1]); | |
756 } | |
757 } | |
758 } | |
759 } | |
760 | |
761 return null; | |
762 }, | |
763 | |
764 /** | |
765 * Returns the value of the specified query string parameter. | |
766 * This method is not used internally by the Browser History Manager. | |
767 * However, it is provided here as a helper since many applications | |
768 * using the Browser History Manager will want to read the value of | |
769 * url parameters to initialize themselves. | |
770 * | |
771 * @method getQueryStringParameter | |
772 * @param {string} paramName Name of the parameter we want to look up. | |
773 * @param {string} queryString Optional URL to look at. If not specified, | |
774 * this method uses the URL in the address bar. | |
775 * @return {string} The value of the specified parameter, or null. | |
776 * @public | |
777 */ | |
778 getQueryStringParameter: function (paramName, url) { | |
779 | |
780 var i, len, idx, queryString, params, tokens; | |
781 | |
782 url = url || top.location.href; | |
783 | |
784 idx = url.indexOf("?"); | |
785 queryString = idx >= 0 ? url.substr(idx + 1) : url; | |
786 | |
787 // Remove the hash if any | |
788 idx = queryString.lastIndexOf("#"); | |
789 queryString = idx >= 0 ? queryString.substr(0, idx) : queryString; | |
790 | |
791 params = queryString.split("&"); | |
792 | |
793 for (i = 0, len = params.length; i < len; i++) { | |
794 tokens = params[i].split("="); | |
795 if (tokens.length >= 2) { | |
796 if (tokens[0] === paramName) { | |
797 return unescape(tokens[1]); | |
798 } | |
799 } | |
800 } | |
801 | |
802 return null; | |
803 } | |
804 | |
805 }; | |
806 | |
807 })(); | |
1073
289ff43cc190
update YUI dev libs to latest version
Marcin Kuzminski <marcin@python-works.com>
parents:
547
diff
changeset
|
808 YAHOO.register("history", YAHOO.util.History, {version: "2.8.2r1", build: "7"}); |