1 | function objectWithoutProperties (obj, exclude) { var target = {}; for (var k in obj) if (Object.prototype.hasOwnProperty.call(obj, k) && exclude.indexOf(k) === -1) target[k] = obj[k]; return target; }
|
2 | import { writable } from 'svelte/store';
|
3 | import { Router, stringify } from './vendor';
|
4 |
|
5 | var cache = {};
|
6 | var baseTag = document.getElementsByTagName('base');
|
7 | var basePrefix = (baseTag[0] && baseTag[0].href) || '/';
|
8 |
|
9 | export var ROOT_URL = basePrefix.replace(window.location.origin, '');
|
10 |
|
11 | export var router = writable({
|
12 | path: '/',
|
13 | query: {},
|
14 | params: {},
|
15 | initial: true,
|
16 | });
|
17 |
|
18 | export var CTX_ROUTER = {};
|
19 | export var CTX_ROUTE = {};
|
20 |
|
21 |
|
22 | export var HASHCHANGE = window.location.origin === 'null';
|
23 |
|
24 | export function hashchangeEnable(value) {
|
25 | if (typeof value === 'boolean') {
|
26 | HASHCHANGE = !!value;
|
27 | }
|
28 |
|
29 | return HASHCHANGE;
|
30 | }
|
31 |
|
32 | export function fixedLocation(path, callback, doFinally) {
|
33 | var baseUri = HASHCHANGE ? window.location.hash.replace('#', '') : window.location.pathname;
|
34 |
|
35 |
|
36 | if (path.charAt() !== '/') {
|
37 | path = baseUri + path;
|
38 | }
|
39 |
|
40 | var currentURL = baseUri + window.location.hash + window.location.search;
|
41 |
|
42 |
|
43 | if (currentURL !== path) {
|
44 | callback(path);
|
45 | }
|
46 |
|
47 |
|
48 | if (typeof doFinally === 'function') {
|
49 | doFinally();
|
50 | }
|
51 | }
|
52 |
|
53 | export function cleanPath(uri, fix) {
|
54 | return uri !== '/' || fix ? uri.replace(/\/$/, '') : uri;
|
55 | }
|
56 |
|
57 | export function navigateTo(path, options) {
|
58 | var ref = options || {};
|
59 | var reload = ref.reload;
|
60 | var replace = ref.replace;
|
61 | var params = ref.params;
|
62 | var queryParams = ref.queryParams;
|
63 |
|
64 |
|
65 | if (!path || typeof path !== 'string' || (path[0] !== '/' && path[0] !== '#')) {
|
66 | throw new Error(("Expecting '/" + path + "' or '#" + path + "', given '" + path + "'"));
|
67 | }
|
68 |
|
69 | if (params) {
|
70 | path = path.replace(/:([a-zA-Z][a-zA-Z0-9_-]*)/g, function (_, key) { return params[key]; });
|
71 | }
|
72 |
|
73 | if (queryParams) {
|
74 | var qs = stringify(queryParams);
|
75 |
|
76 | if (qs) {
|
77 | path += "?" + qs;
|
78 | }
|
79 | }
|
80 |
|
81 | if (HASHCHANGE) {
|
82 | var fixedURL = path.replace(/^#|#$/g, '');
|
83 |
|
84 | if (ROOT_URL !== '/') {
|
85 | fixedURL = fixedURL.replace(cleanPath(ROOT_URL), '');
|
86 | }
|
87 |
|
88 | window.location.hash = fixedURL !== '/' ? fixedURL : '';
|
89 | return;
|
90 | }
|
91 |
|
92 |
|
93 | if (reload || !window.history.pushState || !window.dispatchEvent) {
|
94 | window.location.href = path;
|
95 | return;
|
96 | }
|
97 |
|
98 |
|
99 | fixedLocation(path, function (nextURL) {
|
100 | window.history[replace ? 'replaceState' : 'pushState'](null, '', nextURL);
|
101 | window.dispatchEvent(new Event('popstate'));
|
102 | });
|
103 | }
|
104 |
|
105 | export function getProps(given, required) {
|
106 | var sub = given.props;
|
107 | var rest = objectWithoutProperties( given, ["props"] );
|
108 | var others = rest;
|
109 |
|
110 |
|
111 | required.forEach(function (k) {
|
112 | delete others[k];
|
113 | });
|
114 |
|
115 | return Object.assign({}, sub,
|
116 | others);
|
117 | }
|
118 |
|
119 | export function isActive(uri, path, exact) {
|
120 | if (!cache[[uri, path, exact]]) {
|
121 | if (exact !== true && path.indexOf(uri) === 0) {
|
122 | cache[[uri, path, exact]] = /^[#/?]?$/.test(path.substr(uri.length, 1));
|
123 | } else if (uri.includes('*') || uri.includes(':')) {
|
124 | cache[[uri, path, exact]] = Router.matches(uri, path);
|
125 | } else {
|
126 | cache[[uri, path, exact]] = cleanPath(path) === uri;
|
127 | }
|
128 | }
|
129 |
|
130 | return cache[[uri, path, exact]];
|
131 | }
|
132 |
|
133 | export function isPromise(object) {
|
134 | return object && typeof object.then === 'function';
|
135 | }
|
136 |
|
137 | export function isSvelteComponent(object) {
|
138 | return object && object.prototype;
|
139 | }
|