-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web] Expose PlatformLocation and HashUrlStrategy through ui_web (#41163
) 1. Expose `PlatformLocation` and its subclass `BrowserPlatformLocation` through the new `ui_web`. 2. Expose `HashUrlStrategy` too. It's useful for users who want to extend and customize it instead of building their own from scratch. 3. `ui_web/url_strategy.dart` => `ui_web/navigation/url_strategy.dart`.
- Loading branch information
Showing
8 changed files
with
314 additions
and
280 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
lib/web_ui/lib/ui_web/src/ui_web/navigation/platform_location.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:ui/src/engine.dart'; | ||
|
||
import 'url_strategy.dart'; | ||
|
||
/// Encapsulates all calls to DOM apis, which allows the [UrlStrategy] classes | ||
/// to be platform agnostic and testable. | ||
/// | ||
/// For convenience, the [PlatformLocation] class can be used by implementations | ||
/// of [UrlStrategy] to interact with DOM apis like pushState, popState, etc. | ||
abstract interface class PlatformLocation { | ||
/// Registers an event listener for the `popstate` event. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate | ||
void addPopStateListener(DomEventListener fn); | ||
|
||
/// Unregisters the given listener (added by [addPopStateListener]) from the | ||
/// `popstate` event. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate | ||
void removePopStateListener(DomEventListener fn); | ||
|
||
/// The `pathname` part of the URL in the browser address bar. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Location/pathname | ||
String get pathname; | ||
|
||
/// The `query` part of the URL in the browser address bar. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Location/search | ||
String get search; | ||
|
||
/// The `hash]` part of the URL in the browser address bar. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/Location/hash | ||
String? get hash; | ||
|
||
/// The `state` in the current history entry. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/state | ||
Object? get state; | ||
|
||
/// Adds a new entry to the browser history stack. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/pushState | ||
void pushState(Object? state, String title, String url); | ||
|
||
/// Replaces the current entry in the browser history stack. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/replaceState | ||
void replaceState(Object? state, String title, String url); | ||
|
||
/// Moves forwards or backwards through the history stack. | ||
/// | ||
/// A negative [count] value causes a backward move in the history stack. And | ||
/// a positive [count] value causs a forward move. | ||
/// | ||
/// Examples: | ||
/// | ||
/// * `go(-2)` moves back 2 steps in history. | ||
/// * `go(3)` moves forward 3 steps in hisotry. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/API/History/go | ||
void go(double count); | ||
|
||
/// The base href where the Flutter app is being served. | ||
/// | ||
/// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base | ||
String? getBaseHref(); | ||
} | ||
|
||
/// Delegates to real browser APIs to provide platform location functionality. | ||
class BrowserPlatformLocation implements PlatformLocation { | ||
/// Default constructor for [BrowserPlatformLocation]. | ||
const BrowserPlatformLocation(); | ||
|
||
DomLocation get _location => domWindow.location; | ||
DomHistory get _history => domWindow.history; | ||
|
||
@override | ||
void addPopStateListener(DomEventListener fn) { | ||
domWindow.addEventListener('popstate', fn); | ||
} | ||
|
||
@override | ||
void removePopStateListener(DomEventListener fn) { | ||
domWindow.removeEventListener('popstate', fn); | ||
} | ||
|
||
@override | ||
String get pathname => _location.pathname!; | ||
|
||
@override | ||
String get search => _location.search!; | ||
|
||
@override | ||
String get hash => _location.locationHash; | ||
|
||
@override | ||
Object? get state => _history.state; | ||
|
||
@override | ||
void pushState(Object? state, String title, String url) { | ||
_history.pushState(state, title, url); | ||
} | ||
|
||
@override | ||
void replaceState(Object? state, String title, String url) { | ||
_history.replaceState(state, title, url); | ||
} | ||
|
||
@override | ||
void go(double count) { | ||
_history.go(count); | ||
} | ||
|
||
@override | ||
String? getBaseHref() => domDocument.baseUri; | ||
} |
Oops, something went wrong.