01: /*
02: * Copyright 2007 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.user.client.impl;
17:
18: /**
19: * Safari implementation of
20: * {@link com.google.gwt.user.client.impl.HistoryImplStandard}.
21: */
22: class HistoryImplSafari extends HistoryImplStandard {
23:
24: private static boolean isOldSafari = detectOldSafari();
25:
26: private static native boolean detectOldSafari() /*-{
27: var exp = / AppleWebKit\/([\d]+)/;
28: var result = exp.exec(navigator.userAgent);
29: if (result) {
30: // The standard history implementation works fine on WebKit >= 522
31: // (Safari 3 beta).
32: if (parseInt(result[1]) >= 522) {
33: return false;
34: }
35: }
36:
37: // The standard history implementation works just fine on the iPhone, which
38: // unfortunately reports itself as WebKit/420+.
39: if (navigator.userAgent.indexOf('iPhone') != -1) {
40: return false;
41: }
42:
43: return true;
44: }-*/;
45:
46: @Override
47: public boolean init() {
48: if (isOldSafari) {
49: initImpl();
50: return true;
51: }
52:
53: return super .init();
54: }
55:
56: @Override
57: public void newItem(String historyToken) {
58: if (isOldSafari) {
59: newItemImpl(historyToken);
60: return;
61: }
62:
63: super .newItem(historyToken);
64: }
65:
66: private native void initImpl() /*-{
67: $wnd.__gwt_historyToken = '';
68:
69: // Get the initial token from the url's hash component.
70: var hash = $wnd.location.hash;
71: if (hash.length > 0)
72: $wnd.__gwt_historyToken = decodeURIComponent(hash.substring(1));
73:
74: @com.google.gwt.user.client.impl.HistoryImpl::onHistoryChanged(Ljava/lang/String;)($wnd.__gwt_historyToken);
75: }-*/;
76:
77: private native void newItemImpl(String historyToken) /*-{
78: // Use a bizarre meta refresh trick to update the url's hash, without
79: // creating a history entry.
80: var meta = $doc.createElement('meta');
81: meta.setAttribute('http-equiv','refresh');
82:
83: var newUrl = $wnd.location.href.split('#')[0] + '#' + encodeURIComponent(historyToken);
84: meta.setAttribute('content','0.01;url=' + newUrl);
85:
86: $doc.body.appendChild(meta);
87: window.setTimeout(function() {
88: $doc.body.removeChild(meta);
89: }, 1);
90:
91: // Update the global history token and fire the history event.
92: $wnd.__gwt_historyToken = historyToken;
93: @com.google.gwt.user.client.impl.HistoryImpl::onHistoryChanged(Ljava/lang/String;)($wnd.__gwt_historyToken);
94: }-*/;
95: }
|