1+ package com .ocpsoft .rewrite .gwt .client .history ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .Arrays ;
5+ import java .util .List ;
6+
7+ import com .google .gwt .user .client .impl .HistoryImpl ;
8+
9+ /**
10+ * History implementation based on pushState
11+ */
12+ public class HistoryStateImpl extends HistoryImpl
13+ {
14+ private static List <ContextPathProvider > providers = Arrays .asList ((ContextPathProvider ) new CookieContextPathProvider (),
15+ new RequestContextPathProvider ());
16+
17+ static String contextPath = null ;
18+
19+ private static List <ContextPathListener > listeners = new ArrayList <ContextPathListener >();
20+
21+ @ Override
22+ public boolean init ()
23+ {
24+ for (ContextPathProvider provider : providers ) {
25+ provider .getContextPath (this );
26+ if (contextPath != null )
27+ {
28+ break ;
29+ }
30+ }
31+
32+ return initNative ();
33+ }
34+
35+ static void setContextPath (String contextPath )
36+ {
37+ HistoryStateImpl .contextPath = contextPath ;
38+ System .out .println ("Set contextPath: " + contextPath );
39+ for (ContextPathListener listner : listeners ) {
40+ listner .onContextPathSet (contextPath );
41+ }
42+ }
43+
44+ public static String getContextPath ()
45+ {
46+ if (contextPath != null && !contextPath .endsWith ("/" ))
47+ {
48+ contextPath += "/" ;
49+ }
50+ System .out .println ("Context path: " + contextPath );
51+ return contextPath ;
52+ }
53+
54+ public native boolean initNative () /*-{
55+ var token = '';
56+ var historyImpl = this;
57+
58+ var path = $wnd.location.pathname;
59+ if (path.length > 0) {
60+ token = historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(path);
61+ token = historyImpl.@com.ocpsoft.rewrite.gwt.client.history.HistoryStateImpl::cleanToken(Ljava/lang/String;)(token);
62+ }
63+
64+ @com.google.gwt.user.client.impl.HistoryImpl::setToken(Ljava/lang/String;)(token);
65+
66+ var oldHandler = $wnd.history.onpopstate;
67+
68+ $wnd.onpopstate = $entry(function() {
69+ var token = '';
70+ var path = $wnd.location.pathname;
71+ if (path.length > 0) {
72+ token = historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::decodeFragment(Ljava/lang/String;)(path);
73+ token = historyImpl.@com.ocpsoft.rewrite.gwt.client.history.HistoryStateImpl::cleanToken(Ljava/lang/String;)(token);
74+ }
75+
76+ historyImpl.@com.google.gwt.user.client.impl.HistoryImpl::newItemOnEvent(Ljava/lang/String;)(token);
77+
78+ if (oldHandler) {
79+ oldHandler();
80+ }
81+ });
82+
83+ return true;
84+ }-*/ ;
85+
86+ protected String cleanToken (String historyToken )
87+ {
88+ String contextPath = HistoryStateImpl .getContextPath ();
89+ if (!contextPath .equals (historyToken ) && historyToken .startsWith (contextPath ))
90+ historyToken = historyToken .substring (contextPath .length ());
91+
92+ System .out .println ("History.pop() = [" + historyToken + "]" );
93+ return historyToken ;
94+ }
95+
96+ @ Override
97+ protected void nativeUpdate (String historyToken )
98+ {
99+ String contextPath = HistoryStateImpl .getContextPath ();
100+ if (!contextPath .equals (historyToken ))
101+ historyToken = contextPath + historyToken ;
102+
103+ System .out .println ("History.push(" + historyToken + ")" );
104+ update (historyToken );
105+ }
106+
107+ protected native void update (String historyToken ) /*-{
108+ var encodedToken = this.@com.google.gwt.user.client.impl.HistoryImpl::encodeFragment(Ljava/lang/String;)(historyToken);
109+ $wnd.history.pushState(encodedToken, $wnd.document.title, encodedToken);
110+ }-*/ ;
111+
112+ }
0 commit comments