|
| 1 | +import {EventEmitter, ObservableWrapper} from 'angular2/src/facade/async'; |
| 2 | +import {Injectable, Inject} from 'angular2/core'; |
| 3 | +import {LocationStrategy} from './location_strategy'; |
| 4 | + |
| 5 | +/** |
| 6 | + * `Location` is a service that applications can use to interact with a browser's URL. |
| 7 | + * Depending on which {@link LocationStrategy} is used, `Location` will either persist |
| 8 | + * to the URL's path or the URL's hash segment. |
| 9 | + * |
| 10 | + * Note: it's better to use {@link Router#navigate} service to trigger route changes. Use |
| 11 | + * `Location` only if you need to interact with or create normalized URLs outside of |
| 12 | + * routing. |
| 13 | + * |
| 14 | + * `Location` is responsible for normalizing the URL against the application's base href. |
| 15 | + * A normalized URL is absolute from the URL host, includes the application's base href, and has no |
| 16 | + * trailing slash: |
| 17 | + * - `/my/app/user/123` is normalized |
| 18 | + * - `my/app/user/123` **is not** normalized |
| 19 | + * - `/my/app/user/123/` **is not** normalized |
| 20 | + * |
| 21 | + * ### Example |
| 22 | + * |
| 23 | + * ``` |
| 24 | + * import {Component} from 'angular2/core'; |
| 25 | + * import {Location} from 'angular2/platform/common'; |
| 26 | + * import { |
| 27 | + * ROUTER_DIRECTIVES, |
| 28 | + * ROUTER_PROVIDERS, |
| 29 | + * RouteConfig |
| 30 | + * } from 'angular2/router'; |
| 31 | + * |
| 32 | + * @Component({directives: [ROUTER_DIRECTIVES]}) |
| 33 | + * @RouteConfig([ |
| 34 | + * {...}, |
| 35 | + * ]) |
| 36 | + * class AppCmp { |
| 37 | + * constructor(location: Location) { |
| 38 | + * location.go('/foo'); |
| 39 | + * } |
| 40 | + * } |
| 41 | + * |
| 42 | + * bootstrap(AppCmp, [ROUTER_PROVIDERS]); |
| 43 | + * ``` |
| 44 | + */ |
| 45 | +@Injectable() |
| 46 | +export class Location { |
| 47 | + /** @internal */ |
| 48 | + _subject: EventEmitter<any> = new EventEmitter(); |
| 49 | + /** @internal */ |
| 50 | + _baseHref: string; |
| 51 | + |
| 52 | + constructor(public platformStrategy: LocationStrategy) { |
| 53 | + var browserBaseHref = this.platformStrategy.getBaseHref(); |
| 54 | + this._baseHref = Location.stripTrailingSlash(_stripIndexHtml(browserBaseHref)); |
| 55 | + this.platformStrategy.onPopState((ev) => { |
| 56 | + ObservableWrapper.callEmit(this._subject, {'url': this.path(), 'pop': true, 'type': ev.type}); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Returns the normalized URL path. |
| 62 | + */ |
| 63 | + path(): string { return this.normalize(this.platformStrategy.path()); } |
| 64 | + |
| 65 | + /** |
| 66 | + * Given a string representing a URL, returns the normalized URL path without leading or |
| 67 | + * trailing slashes |
| 68 | + */ |
| 69 | + normalize(url: string): string { |
| 70 | + return Location.stripTrailingSlash(_stripBaseHref(this._baseHref, _stripIndexHtml(url))); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Given a string representing a URL, returns the platform-specific external URL path. |
| 75 | + * If the given URL doesn't begin with a leading slash (`'/'`), this method adds one |
| 76 | + * before normalizing. This method will also add a hash if `HashLocationStrategy` is |
| 77 | + * used, or the `APP_BASE_HREF` if the `PathLocationStrategy` is in use. |
| 78 | + */ |
| 79 | + prepareExternalUrl(url: string): string { |
| 80 | + if (url.length > 0 && !url.startsWith('/')) { |
| 81 | + url = '/' + url; |
| 82 | + } |
| 83 | + return this.platformStrategy.prepareExternalUrl(url); |
| 84 | + } |
| 85 | + |
| 86 | + // TODO: rename this method to pushState |
| 87 | + /** |
| 88 | + * Changes the browsers URL to the normalized version of the given URL, and pushes a |
| 89 | + * new item onto the platform's history. |
| 90 | + */ |
| 91 | + go(path: string, query: string = ''): void { |
| 92 | + this.platformStrategy.pushState(null, '', path, query); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Changes the browsers URL to the normalized version of the given URL, and replaces |
| 97 | + * the top item on the platform's history stack. |
| 98 | + */ |
| 99 | + replaceState(path: string, query: string = ''): void { |
| 100 | + this.platformStrategy.replaceState(null, '', path, query); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Navigates forward in the platform's history. |
| 105 | + */ |
| 106 | + forward(): void { this.platformStrategy.forward(); } |
| 107 | + |
| 108 | + /** |
| 109 | + * Navigates back in the platform's history. |
| 110 | + */ |
| 111 | + back(): void { this.platformStrategy.back(); } |
| 112 | + |
| 113 | + /** |
| 114 | + * Subscribe to the platform's `popState` events. |
| 115 | + */ |
| 116 | + subscribe(onNext: (value: any) => void, onThrow: (exception: any) => void = null, |
| 117 | + onReturn: () => void = null): Object { |
| 118 | + return ObservableWrapper.subscribe(this._subject, onNext, onThrow, onReturn); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Given a string of url parameters, prepend with '?' if needed, otherwise return parameters as |
| 123 | + * is. |
| 124 | + */ |
| 125 | + public static normalizeQueryParams(params: string): string { |
| 126 | + return (params.length > 0 && params.substring(0, 1) != '?') ? ('?' + params) : params; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Given 2 parts of a url, join them with a slash if needed. |
| 131 | + */ |
| 132 | + public static joinWithSlash(start: string, end: string): string { |
| 133 | + if (start.length == 0) { |
| 134 | + return end; |
| 135 | + } |
| 136 | + if (end.length == 0) { |
| 137 | + return start; |
| 138 | + } |
| 139 | + var slashes = 0; |
| 140 | + if (start.endsWith('/')) { |
| 141 | + slashes++; |
| 142 | + } |
| 143 | + if (end.startsWith('/')) { |
| 144 | + slashes++; |
| 145 | + } |
| 146 | + if (slashes == 2) { |
| 147 | + return start + end.substring(1); |
| 148 | + } |
| 149 | + if (slashes == 1) { |
| 150 | + return start + end; |
| 151 | + } |
| 152 | + return start + '/' + end; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * If url has a trailing slash, remove it, otherwise return url as is. |
| 157 | + */ |
| 158 | + public static stripTrailingSlash(url: string): string { |
| 159 | + if (/\/$/g.test(url)) { |
| 160 | + url = url.substring(0, url.length - 1); |
| 161 | + } |
| 162 | + return url; |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +function _stripBaseHref(baseHref: string, url: string): string { |
| 167 | + if (baseHref.length > 0 && url.startsWith(baseHref)) { |
| 168 | + return url.substring(baseHref.length); |
| 169 | + } |
| 170 | + return url; |
| 171 | +} |
| 172 | + |
| 173 | +function _stripIndexHtml(url: string): string { |
| 174 | + if (/\/index.html$/g.test(url)) { |
| 175 | + // '/index.html'.length == 11 |
| 176 | + return url.substring(0, url.length - 11); |
| 177 | + } |
| 178 | + return url; |
| 179 | +} |
0 commit comments