UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

108 lines (107 loc) 4.47 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { Injectable, Inject, Optional } from 'angular2/core'; import { isBlank } from 'angular2/src/facade/lang'; import { BaseException } from 'angular2/src/facade/exceptions'; import { PlatformLocation } from './platform_location'; import { LocationStrategy, APP_BASE_HREF } from './location_strategy'; import { Location } from './location'; /** * `PathLocationStrategy` is a {@link LocationStrategy} used to configure the * {@link Location} service to represent its state in the * [path](https://en.wikipedia.org/wiki/Uniform_Resource_Locator#Syntax) of the * browser's URL. * * `PathLocationStrategy` is the default binding for {@link LocationStrategy} * provided in {@link ROUTER_PROVIDERS}. * * If you're using `PathLocationStrategy`, you must provide a provider for * {@link APP_BASE_HREF} to a string representing the URL prefix that should * be preserved when generating and recognizing URLs. * * For instance, if you provide an `APP_BASE_HREF` of `'/my/app'` and call * `location.go('/foo')`, the browser's URL will become * `example.com/my/app/foo`. * * ### Example * * ``` * import {Component, provide} from 'angular2/core'; * import {bootstrap} from 'angular2/platform/browser'; * import { * Location, * APP_BASE_HREF * } from 'angular2/platform/common'; * import { * ROUTER_DIRECTIVES, * ROUTER_PROVIDERS, * RouteConfig * } from 'angular2/router'; * * @Component({directives: [ROUTER_DIRECTIVES]}) * @RouteConfig([ * {...}, * ]) * class AppCmp { * constructor(location: Location) { * location.go('/foo'); * } * } * * bootstrap(AppCmp, [ * ROUTER_PROVIDERS, // includes binding to PathLocationStrategy * provide(APP_BASE_HREF, {useValue: '/my/app'}) * ]); * ``` */ export let PathLocationStrategy = class PathLocationStrategy extends LocationStrategy { constructor(_platformLocation, href) { super(); this._platformLocation = _platformLocation; if (isBlank(href)) { href = this._platformLocation.getBaseHrefFromDOM(); } if (isBlank(href)) { throw new BaseException(`No base href set. Please provide a value for the APP_BASE_HREF token or add a base element to the document.`); } this._baseHref = href; } onPopState(fn) { this._platformLocation.onPopState(fn); this._platformLocation.onHashChange(fn); } getBaseHref() { return this._baseHref; } prepareExternalUrl(internal) { return Location.joinWithSlash(this._baseHref, internal); } path() { return this._platformLocation.pathname + Location.normalizeQueryParams(this._platformLocation.search); } pushState(state, title, url, queryParams) { var externalUrl = this.prepareExternalUrl(url + Location.normalizeQueryParams(queryParams)); this._platformLocation.pushState(state, title, externalUrl); } replaceState(state, title, url, queryParams) { var externalUrl = this.prepareExternalUrl(url + Location.normalizeQueryParams(queryParams)); this._platformLocation.replaceState(state, title, externalUrl); } forward() { this._platformLocation.forward(); } back() { this._platformLocation.back(); } }; PathLocationStrategy = __decorate([ Injectable(), __param(1, Optional()), __param(1, Inject(APP_BASE_HREF)), __metadata('design:paramtypes', [PlatformLocation, String]) ], PathLocationStrategy);