source: imaps-frontend/node_modules/react-use/lib/useLocation.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 2.3 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var react_1 = require("react");
4var util_1 = require("./misc/util");
5var patchHistoryMethod = function (method) {
6 var history = window.history;
7 var original = history[method];
8 history[method] = function (state) {
9 var result = original.apply(this, arguments);
10 var event = new Event(method.toLowerCase());
11 event.state = state;
12 window.dispatchEvent(event);
13 return result;
14 };
15};
16if (util_1.isBrowser) {
17 patchHistoryMethod('pushState');
18 patchHistoryMethod('replaceState');
19}
20var useLocationServer = function () { return ({
21 trigger: 'load',
22 length: 1,
23}); };
24var buildState = function (trigger) {
25 var _a = window.history, state = _a.state, length = _a.length;
26 var _b = window.location, hash = _b.hash, host = _b.host, hostname = _b.hostname, href = _b.href, origin = _b.origin, pathname = _b.pathname, port = _b.port, protocol = _b.protocol, search = _b.search;
27 return {
28 trigger: trigger,
29 state: state,
30 length: length,
31 hash: hash,
32 host: host,
33 hostname: hostname,
34 href: href,
35 origin: origin,
36 pathname: pathname,
37 port: port,
38 protocol: protocol,
39 search: search,
40 };
41};
42var useLocationBrowser = function () {
43 var _a = react_1.useState(buildState('load')), state = _a[0], setState = _a[1];
44 react_1.useEffect(function () {
45 var onPopstate = function () { return setState(buildState('popstate')); };
46 var onPushstate = function () { return setState(buildState('pushstate')); };
47 var onReplacestate = function () { return setState(buildState('replacestate')); };
48 util_1.on(window, 'popstate', onPopstate);
49 util_1.on(window, 'pushstate', onPushstate);
50 util_1.on(window, 'replacestate', onReplacestate);
51 return function () {
52 util_1.off(window, 'popstate', onPopstate);
53 util_1.off(window, 'pushstate', onPushstate);
54 util_1.off(window, 'replacestate', onReplacestate);
55 };
56 }, []);
57 return state;
58};
59var hasEventConstructor = typeof Event === 'function';
60exports.default = util_1.isBrowser && hasEventConstructor ? useLocationBrowser : useLocationServer;
Note: See TracBrowser for help on using the repository browser.