Skip to content

Commit 4d4905c

Browse files
committed
Refactor location provider to explicit setters
1 parent 733d53c commit 4d4905c

File tree

3 files changed

+118
-85
lines changed

3 files changed

+118
-85
lines changed

src/core/location/location.js

Lines changed: 72 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -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

src/loader.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,14 @@ export class Angular {
146146
* @param {import("./core/di/internal-injector").InjectorService} $injector
147147
*/
148148
function (scope, el, compile, $injector) {
149-
// ng-route deps
149+
// ng-route deps
150150
services.$injector = $injector;
151151
services.$q = $injector.get("$q");
152152
scope.$apply(() => {
153153
el.data("$injector", $injector);
154154
compile(el)(scope);
155155
});
156-
156+
157157
// https://github.com/angular-ui/ui-router/issues/3678
158158
if (!Object.prototype.hasOwnProperty.call($injector, "strictDi")) {
159159
try {

types/core/location/location.d.ts

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -244,49 +244,64 @@ export class LocationHashbangUrl extends Location {
244244
$$parseLinkUrl(url: string): boolean;
245245
}
246246
export class LocationProvider {
247-
hashPrefixValue: string;
248-
html5ModeConfig: {
249-
enabled: boolean;
250-
requireBase: boolean;
251-
rewriteLinks: boolean;
252-
};
247+
/** @type {string} */
248+
hashPrefixConf: string;
249+
/** @type {Html5Mode} */
250+
html5ModeConf: Html5Mode;
253251
/**
254252
* The default value for the prefix is `'!'`.
255-
* @param {string=} prefix - Prefix for hash part (containing path and search)
256-
* @returns {string|LocationProvider} current value if used as getter or itself (chaining) if used as setter
257-
*/
258-
hashPrefix(prefix?: string | undefined): string | LocationProvider;
259-
/**
260-
* @param {(boolean|Object)=} mode If boolean, sets `html5Mode.enabled` to value.
261-
* If object, sets `enabled`, `requireBase` and `rewriteLinks` to respective values. Supported
262-
* properties:
263-
* - **enabled** – `{boolean}` – (default: false) If true, will rely on `history.pushState` to
264-
* change urls where supported. Will fall back to hash-prefixed paths in browsers that do not
265-
* support `pushState`.
266-
* - **requireBase** - `{boolean}` - (default: `true`) When html5Mode is enabled, specifies
267-
* whether or not a <base> tag is required to be present. If `enabled` and `requireBase` are
268-
* true, and a base tag is not present, an error will be thrown when `$location` is injected.
269-
* See the {@link guide/$location $location guide for more information}
270-
* - **rewriteLinks** - `{boolean|string}` - (default: `true`) When html5Mode is enabled,
271-
* enables/disables URL rewriting for relative links. If set to a string, URL rewriting will
272-
* only happen on links with an attribute that matches the given string. For example, if set
273-
* to `'internal-link'`, then the URL will only be rewritten for `<a internal-link>` links.
274-
* Note that [attribute name normalization](guide/directive#normalization) does not apply
275-
* here, so `'internalLink'` will **not** match `'internal-link'`.
253+
* @param {string=} prefix Prefix for hash part (containing path and search)
254+
* @returns {void}
255+
*/
256+
setHashPrefix(prefix?: string | undefined): void;
257+
/**
258+
* Current hash prefix
259+
* @returns {string}
260+
*/
261+
getHashPrefix(): string;
262+
/**
263+
* Configures html5 mode
264+
* @param {(boolean|Html5Mode)=} mode If boolean, sets `html5Mode.enabled` to value. Otherwise, accepts html5Mode object
276265
*
277-
* @returns {Object} html5Mode object if used as getter or itself (chaining) if used as setter
266+
* @returns {void}
267+
*/
268+
setHtml5Mode(mode?: (boolean | Html5Mode) | undefined): void;
269+
/**
270+
* Returns html5 mode cofiguration
271+
* @returns {Html5Mode}
278272
*/
279-
html5Mode(mode?: (boolean | any) | undefined): any;
273+
getHtml5Mode(): Html5Mode;
280274
$get: (string | (($rootScope: import("../scope/scope").Scope, $browser: import("../../services/browser").Browser, $rootElement: JQLite) => Location))[];
281275
}
282276
export type DefaultPorts = {
283277
http: number;
284278
https: number;
285279
ftp: number;
286280
};
281+
/**
282+
* Represents the configuration options for HTML5 mode.
283+
*/
287284
export type Html5Mode = {
285+
/**
286+
* - (default: false) If true, will rely on `history.pushState` to
287+
* change URLs where supported. Falls back to hash-prefixed paths in browsers that do not
288+
* support `pushState`.
289+
*/
288290
enabled: boolean;
291+
/**
292+
* - (default: `true`) When html5Mode is enabled, specifies
293+
* whether or not a `<base>` tag is required to be present. If both `enabled` and `requireBase`
294+
* are true, and a `<base>` tag is not present, an error will be thrown when `$location` is injected.
295+
* See the {@link guide /$location $location guide} for more information.
296+
*/
289297
requireBase: boolean;
298+
/**
299+
* - (default: `true`) When html5Mode is enabled, enables or
300+
* disables URL rewriting for relative links. If set to a string, URL rewriting will only apply to links
301+
* with an attribute that matches the given string. For example, if set to `'internal-link'`, URL rewriting
302+
* will only occur for `<a internal-link>` links. Note that [attribute name normalization](guide/directive#normalization)
303+
* does not apply here, so `'internalLink'` will **not** match `'internal-link'`.
304+
*/
290305
rewriteLinks: boolean | string;
291306
};
292307
import { JQLite } from "../../shared/jqlite/jqlite";

0 commit comments

Comments
 (0)