HistoryImpl class
Native implementation associated with {@link com.google.gwt.user.client.History}. User classes should not use this class directly.
This base version uses the HTML5 standard window.onhashchange event to determine when the URL hash identifier changes.
class HistoryImpl implements HasValueChangeHandlers<String> { static String _token = ""; static String getToken() { return (_token == null) ? "" : _token; } static void setToken(String token) { HistoryImpl._token = token; } EventBus _handlers = new SimpleEventBus(); //***************************************** // Implementation of HasValueChangeHandlers //***************************************** /** * Adds a {@link ValueChangeEvent} handler to be informed of changes to the * browser's history stack. * * @param handler the handler */ HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { return _handlers.addHandler(ValueChangeEvent.TYPE, handler); } //**** String encodeFragment(String fragment) { // encodeURI() does *not* encode the '#' character. return fragment; //encodeURI(fragment).replace("#", "%23"); } String decodeFragment(String encodedFragment) { // decodeURI() does *not* decode the '#' character. return encodedFragment; //decodeURI(encodedFragment.replace("%23", "#")); } void fireEvent(DwtEvent event) { _handlers.fireEvent(event); } /** * Fires the {@link ValueChangeEvent} to all handlers with the given tokens. */ void fireHistoryChangedImpl(String newToken) { ValueChangeEvent.fire(this, newToken); } EventBus getHandlers() { return _handlers; } bool init() { var token = ''; // Get the initial token from the url's hash component. String hash = dart_html.window.location.hash; if (hash.length > 0) { token = decodeFragment(hash.substring(1)); } HistoryImpl.setToken(token); //var historyImpl = this; //var oldHandler = dart_html.window.on["hashchange"]; dart_html.window.onHashChange.listen((dart_html.Event event) { var token = ''; String hash = dart_html.window.location.hash; if (hash.length > 0) { token = decodeFragment(hash.substring(1)); } newItemOnEvent(token); // if (oldHandler) { // oldHandler(); // } }); return true; } void newItem(String historyToken, bool issueEvent) { historyToken = (historyToken == null) ? "" : historyToken; if (historyToken != getToken()) { setToken(historyToken); nativeUpdate(historyToken); if (issueEvent) { fireHistoryChangedImpl(historyToken); } } } void newItemOnEvent(String historyToken) { historyToken = (historyToken == null) ? "" : historyToken; if (historyToken != getToken()) { setToken(historyToken); nativeUpdateOnEvent(historyToken); fireHistoryChangedImpl(historyToken); } } void nativeUpdate(String historyToken) { //$wnd.location.hash = this.@com.google.gwt.user.client.impl.HistoryImpl::encodeFragment(Ljava/lang/String;)(historyToken); dart_html.window.location.hash = encodeFragment(historyToken); } void nativeUpdateOnEvent(String historyToken) { // Do nothing, the hash is already updated. } }
Implements
HasValueChangeHandlers<String>
Static Methods
String getToken() #
static String getToken() { return (_token == null) ? "" : _token; }
void setToken(String token) #
static void setToken(String token) { HistoryImpl._token = token; }
Methods
HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) #
Adds a {@link ValueChangeEvent} handler to be informed of changes to the browser's history stack.
@param handler the handler
HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) { return _handlers.addHandler(ValueChangeEvent.TYPE, handler); }
String decodeFragment(String encodedFragment) #
String decodeFragment(String encodedFragment) { // decodeURI() does *not* decode the '#' character. return encodedFragment; //decodeURI(encodedFragment.replace("%23", "#")); }
String encodeFragment(String fragment) #
String encodeFragment(String fragment) { // encodeURI() does *not* encode the '#' character. return fragment; //encodeURI(fragment).replace("#", "%23"); }
void fireEvent(DwtEvent event) #
Fires the given event to the handlers listening to the event's type.
Any exceptions thrown by handlers will be bundled into a UmbrellaException and then re-thrown after all handlers have completed. An exception thrown by a handler will not prevent other handlers from executing.
@param event the event
void fireEvent(DwtEvent event) { _handlers.fireEvent(event); }
void fireHistoryChangedImpl(String newToken) #
Fires the {@link ValueChangeEvent} to all handlers with the given tokens.
void fireHistoryChangedImpl(String newToken) { ValueChangeEvent.fire(this, newToken); }
bool init() #
bool init() { var token = ''; // Get the initial token from the url's hash component. String hash = dart_html.window.location.hash; if (hash.length > 0) { token = decodeFragment(hash.substring(1)); } HistoryImpl.setToken(token); //var historyImpl = this; //var oldHandler = dart_html.window.on["hashchange"]; dart_html.window.onHashChange.listen((dart_html.Event event) { var token = ''; String hash = dart_html.window.location.hash; if (hash.length > 0) { token = decodeFragment(hash.substring(1)); } newItemOnEvent(token); // if (oldHandler) { // oldHandler(); // } }); return true; }
void nativeUpdate(String historyToken) #
void nativeUpdate(String historyToken) { //$wnd.location.hash = this.@com.google.gwt.user.client.impl.HistoryImpl::encodeFragment(Ljava/lang/String;)(historyToken); dart_html.window.location.hash = encodeFragment(historyToken); }
void nativeUpdateOnEvent(String historyToken) #
void nativeUpdateOnEvent(String historyToken) { // Do nothing, the hash is already updated. }
void newItem(String historyToken, bool issueEvent) #
void newItem(String historyToken, bool issueEvent) { historyToken = (historyToken == null) ? "" : historyToken; if (historyToken != getToken()) { setToken(historyToken); nativeUpdate(historyToken); if (issueEvent) { fireHistoryChangedImpl(historyToken); } } }
void newItemOnEvent(String historyToken) #
void newItemOnEvent(String historyToken) { historyToken = (historyToken == null) ? "" : historyToken; if (historyToken != getToken()) { setToken(historyToken); nativeUpdateOnEvent(historyToken); fireHistoryChangedImpl(historyToken); } }