@@ -23,10 +23,21 @@ import { ScopePhase } from "../scope/scope";
2323 */
2424
2525/**
26+ * Represents the configuration options for HTML5 mode.
27+ *
2628 * @typedef {Object } Html5Mode
27- * @property {boolean } enabled
28- * @property {boolean } requireBase
29- * @property {boolean|string } rewriteLinks
29+ * @property {boolean } enabled - (default: false) If true, will rely on `history.pushState` to
30+ * change URLs where supported. Falls back to hash-prefixed paths in browsers that do not
31+ * support `pushState`.
32+ * @property {boolean } requireBase - (default: `true`) When html5Mode is enabled, specifies
33+ * whether or not a `<base>` tag is required to be present. If both `enabled` and `requireBase`
34+ * are true, and a `<base>` tag is not present, an error will be thrown when `$location` is injected.
35+ * See the {@link guide/$location $location guide} for more information.
36+ * @property {boolean|string } rewriteLinks - (default: `true`) When html5Mode is enabled, enables or
37+ * disables URL rewriting for relative links. If set to a string, URL rewriting will only apply to links
38+ * with an attribute that matches the given string. For example, if set to `'internal-link'`, URL rewriting
39+ * will only occur for `<a internal-link>` links. Note that [attribute name normalization](guide/directive#normalization)
40+ * does not apply here, so `'internalLink'` will **not** match `'internal-link'`.
3041 */
3142
3243/** @type {DefaultPorts } */
@@ -542,71 +553,74 @@ export class LocationHashbangUrl extends Location {
542553 }
543554}
544555
545- export function LocationProvider ( ) {
546- let hashPrefix = "!" ;
547- const html5Mode = {
548- enabled : false ,
549- requireBase : true ,
550- rewriteLinks : true ,
551- } ;
556+ export class LocationProvider {
557+ constructor ( ) {
558+ /** @type {string } */
559+ this . hashPrefixConf = "!" ;
560+
561+ /** @type {Html5Mode } */
562+ this . html5ModeConf = {
563+ enabled : false ,
564+ requireBase : true ,
565+ rewriteLinks : true ,
566+ } ;
567+ }
552568
553569 /**
554570 * The default value for the prefix is `'!'`.
555571 * @param {string= } prefix Prefix for hash part (containing path and search)
556- * @returns {* } current value if used as getter or itself (chaining) if used as setter
572+ * @returns {void }
557573 */
558- this . hashPrefix = function ( prefix ) {
559- if ( isDefined ( prefix ) ) {
560- hashPrefix = prefix ;
561- return this ;
562- }
563- return hashPrefix ;
564- } ;
574+ setHashPrefix ( prefix ) {
575+ this . hashPrefixConf = prefix ;
576+ }
577+
578+ /**
579+ * Current hash prefix
580+ * @returns {string }
581+ */
582+ getHashPrefix ( ) {
583+ return this . hashPrefixConf ;
584+ }
565585
566586 /**
567- * @param {(boolean|Object)= } mode If boolean, sets `html5Mode.enabled` to value.
568- * If object, sets `enabled`, `requireBase` and `rewriteLinks` to respective values. Supported
569- * properties:
570- * - **enabled** – `{boolean}` – (default: false) If true, will rely on `history.pushState` to
571- * change urls where supported. Will fall back to hash-prefixed paths in browsers that do not
572- * support `pushState`.
573- * - **requireBase** - `{boolean}` - (default: `true`) When html5Mode is enabled, specifies
574- * whether or not a <base> tag is required to be present. If `enabled` and `requireBase` are
575- * true, and a base tag is not present, an error will be thrown when `$location` is injected.
576- * See the {@link guide/$location $location guide for more information}
577- * - **rewriteLinks** - `{boolean|string}` - (default: `true`) When html5Mode is enabled,
578- * enables/disables URL rewriting for relative links. If set to a string, URL rewriting will
579- * only happen on links with an attribute that matches the given string. For example, if set
580- * to `'internal-link'`, then the URL will only be rewritten for `<a internal-link>` links.
581- * Note that [attribute name normalization](guide/directive#normalization) does not apply
582- * here, so `'internalLink'` will **not** match `'internal-link'`.
587+ * Configures html5 mode
588+ * @param {(boolean|Html5Mode)= } mode If boolean, sets `html5Mode.enabled` to value. Otherwise, accepts html5Mode object
583589 *
584- * @returns {Object } html5Mode object if used as getter or itself (chaining) if used as setter
590+ * @returns {void }
585591 */
586- this . html5Mode = function ( mode ) {
592+ setHtml5Mode ( mode ) {
587593 if ( isBoolean ( mode ) ) {
588- html5Mode . enabled = mode ;
589- return this ;
594+ this . html5ModeConf . enabled = /** @type {boolean } */ ( mode ) ;
590595 }
591596 if ( isObject ( mode ) ) {
592- if ( isBoolean ( mode . enabled ) ) {
593- html5Mode . enabled = mode . enabled ;
597+ const html5Mode = /** @type {Html5Mode } */ ( mode ) ;
598+ if ( isDefined ( html5Mode . enabled ) && isBoolean ( html5Mode . enabled ) ) {
599+ this . html5ModeConf . enabled = html5Mode . enabled ;
594600 }
595601
596- if ( isBoolean ( mode . requireBase ) ) {
597- html5Mode . requireBase = mode . requireBase ;
602+ if ( isDefined ( html5Mode . enabled ) && isBoolean ( html5Mode . requireBase ) ) {
603+ this . html5ModeConf . requireBase = html5Mode . requireBase ;
598604 }
599605
600- if ( isBoolean ( mode . rewriteLinks ) || isString ( mode . rewriteLinks ) ) {
601- html5Mode . rewriteLinks = mode . rewriteLinks ;
606+ if (
607+ isDefined ( html5Mode . enabled ) &&
608+ ( isBoolean ( html5Mode . rewriteLinks ) || isString ( html5Mode . rewriteLinks ) )
609+ ) {
610+ this . html5ModeConf . rewriteLinks = html5Mode . rewriteLinks ;
602611 }
603-
604- return this ;
605612 }
606- return html5Mode ;
607- } ;
613+ }
608614
609- this . $get = [
615+ /**
616+ * Returns html5 mode cofiguration
617+ * @returns {Html5Mode }
618+ */
619+ getHtml5Mode ( ) {
620+ return this . html5ModeConf ;
621+ }
622+
623+ $get = [
610624 "$rootScope" ,
611625 "$browser" ,
612626 "$rootElement" ,
@@ -617,16 +631,16 @@ export function LocationProvider() {
617631 * @param {JQLite } $rootElement
618632 * @returns
619633 */
620- function ( $rootScope , $browser , $rootElement ) {
634+ ( $rootScope , $browser , $rootElement ) => {
621635 /** @type {Location } */
622636 let $location ;
623637 let LocationMode ;
624638 const baseHref = $browser . baseHref ( ) ; // if base[href] is undefined, it defaults to ''
625639 const initialUrl = /** @type {string } */ ( $browser . url ( ) ) ;
626640 let appBase ;
627641
628- if ( html5Mode . enabled ) {
629- if ( ! baseHref && html5Mode . requireBase ) {
642+ if ( this . getHtml5Mode ( ) . enabled ) {
643+ if ( ! baseHref && this . getHtml5Mode ( ) . requireBase ) {
630644 throw $locationMinErr (
631645 "nobase" ,
632646 "$location in HTML5 mode requires a <base> tag to be present!" ,
@@ -640,7 +654,11 @@ export function LocationProvider() {
640654 }
641655 const appBaseNoFile = stripFile ( appBase ) ;
642656
643- $location = new LocationMode ( appBase , appBaseNoFile , `#${ hashPrefix } ` ) ;
657+ $location = new LocationMode (
658+ appBase ,
659+ appBaseNoFile ,
660+ `#${ this . getHashPrefix ( ) } ` ,
661+ ) ;
644662 $location . $$parseLinkUrl ( initialUrl , initialUrl ) ;
645663
646664 $location . $$state = $browser . state ( ) ;
@@ -667,7 +685,7 @@ export function LocationProvider() {
667685 }
668686
669687 $rootElement . on ( "click" , ( event ) => {
670- const { rewriteLinks } = html5Mode ;
688+ const rewriteLinks = this . getHtml5Mode ( ) . rewriteLinks ;
671689 // TODO(vojta): rewrite link when opening in new tab/window (in legacy browser)
672690 // currently we open nice url link and redirect then
673691
0 commit comments