UNPKG

3.77 kBJavaScriptView Raw
1function 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; }
2import { writable } from 'svelte/store';
3import { Router, stringify } from './vendor';
4
5var cache = {};
6var baseTag = document.getElementsByTagName('base');
7var basePrefix = (baseTag[0] && baseTag[0].href) || '/';
8
9export var ROOT_URL = basePrefix.replace(window.location.origin, '');
10
11export var router = writable({
12 path: '/',
13 query: {},
14 params: {},
15 initial: true,
16});
17
18export var CTX_ROUTER = {};
19export var CTX_ROUTE = {};
20
21// use location.hash on embedded pages, e.g. Svelte REPL
22export var HASHCHANGE = window.location.origin === 'null';
23
24export function hashchangeEnable(value) {
25 if (typeof value === 'boolean') {
26 HASHCHANGE = !!value;
27 }
28
29 return HASHCHANGE;
30}
31
32export function fixedLocation(path, callback, doFinally) {
33 var baseUri = HASHCHANGE ? window.location.hash.replace('#', '') : window.location.pathname;
34
35 // this will rebase anchors to avoid location changes
36 if (path.charAt() !== '/') {
37 path = baseUri + path;
38 }
39
40 var currentURL = baseUri + window.location.hash + window.location.search;
41
42 // do not change location et all...
43 if (currentURL !== path) {
44 callback(path);
45 }
46
47 // invoke final guard regardless of previous result
48 if (typeof doFinally === 'function') {
49 doFinally();
50 }
51}
52
53export function cleanPath(uri, fix) {
54 return uri !== '/' || fix ? uri.replace(/\/$/, '') : uri;
55}
56
57export 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 // If path empty or no string, throws error
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 // If no History API support, fallbacks to URL redirect
93 if (reload || !window.history.pushState || !window.dispatchEvent) {
94 window.location.href = path;
95 return;
96 }
97
98 // If has History API support, uses it
99 fixedLocation(path, function (nextURL) {
100 window.history[replace ? 'replaceState' : 'pushState'](null, '', nextURL);
101 window.dispatchEvent(new Event('popstate'));
102 });
103}
104
105export function getProps(given, required) {
106 var sub = given.props;
107 var rest = objectWithoutProperties( given, ["props"] );
108 var others = rest;
109
110 // prune all declared props from this component
111 required.forEach(function (k) {
112 delete others[k];
113 });
114
115 return Object.assign({}, sub,
116 others);
117}
118
119export 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
133export function isPromise(object) {
134 return object && typeof object.then === 'function';
135}
136
137export function isSvelteComponent(object) {
138 return object && object.prototype;
139}