|
| 1 | +(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +Object.defineProperty(exports, "__esModule", { |
| 5 | + value: true |
| 6 | +}); |
| 7 | +exports.resolve = exports.router = undefined; |
| 8 | + |
| 9 | +var _client = require('utilise/client'); |
| 10 | + |
| 11 | +var _client2 = _interopRequireDefault(_client); |
| 12 | + |
| 13 | +var _keys = require('utilise/keys'); |
| 14 | + |
| 15 | +var _keys2 = _interopRequireDefault(_keys); |
| 16 | + |
| 17 | +function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } |
| 18 | + |
| 19 | +var log = require('utilise/log')('[router]'); |
| 20 | +var go = function go(url) { |
| 21 | + return (window.event && window.event.preventDefault(), true), history.pushState({}, '', url), window.dispatchEvent(new CustomEvent('change')), url; |
| 22 | +}; |
| 23 | + |
| 24 | +var router = function router(routes) { |
| 25 | + return !_client2.default ? route : route({ url: location.pathname }); |
| 26 | + |
| 27 | + function route(req, res, next) { |
| 28 | + var from = req.url, |
| 29 | + resolved = resolve(routes)(req), |
| 30 | + to = resolved.url; |
| 31 | + |
| 32 | + if (from !== to) log('router redirecting', from, to); |
| 33 | + if (_client2.default) location.params = resolved.params; |
| 34 | + |
| 35 | + return _client2.default && from !== to ? (go(to), resolved) : !_client2.default && from !== to ? res.redirect(to) : !_client2.default ? next() : resolved; |
| 36 | + } |
| 37 | +}; |
| 38 | + |
| 39 | +var resolve = function resolve(root) { |
| 40 | + return function (req, from) { |
| 41 | + var params = {}, |
| 42 | + url = from || req.url, |
| 43 | + to = root({ url: url, req: req, params: params, next: next(req, url, params) }); |
| 44 | + |
| 45 | + return to !== true ? resolve(root)(req, to) : { url: url, params: params }; |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +var next = function next(req, url, params) { |
| 50 | + return function (handlers) { |
| 51 | + var _segment = segment(url); |
| 52 | + |
| 53 | + var first = _segment.first; |
| 54 | + var last = _segment.last; |
| 55 | + var to = ''; |
| 56 | + |
| 57 | + return first in handlers ? handlers[first]({ req: req, next: next(req, last, params), params: params, current: first }) : (0, _keys2.default)(handlers).filter(function (k) { |
| 58 | + return k[0] == ':'; |
| 59 | + }).some(function (k) { |
| 60 | + var pm = k.slice(1); |
| 61 | + if (to = handlers[k]({ req: req, next: next(req, last, params), params: params, current: first })) params[pm] = first; |
| 62 | + |
| 63 | + return to; |
| 64 | + }) && to; |
| 65 | + }; |
| 66 | +}; |
| 67 | + |
| 68 | +function segment(url) { |
| 69 | + var segments = url.split('/').filter(Boolean); |
| 70 | + return { first: segments.shift(), last: segments.join('/') }; |
| 71 | +} |
| 72 | + |
| 73 | +if (_client2.default) { |
| 74 | + window.go = go; |
| 75 | + window.router = router; |
| 76 | + window.addEventListener('popstate', function (e) { |
| 77 | + return window.dispatchEvent(new CustomEvent('change')); |
| 78 | + }); |
| 79 | + window.addEventListener('change', function (e) { |
| 80 | + return e.target == window && (window.app || document).draw && (window.app || document).draw(); |
| 81 | + }); |
| 82 | + document.addEventListener('click', function (e) { |
| 83 | + var a = e.path ? e.path.shift() : e.target; |
| 84 | + if (!a.matches('a[href]:not([href^=javascript]):not(.bypass)')) return; |
| 85 | + if (a.origin != location.origin) return; |
| 86 | + // console.log("e", a.href) |
| 87 | + e.preventDefault(); |
| 88 | + go(a.href); |
| 89 | + }); |
| 90 | +} |
| 91 | + |
| 92 | +exports.router = router; |
| 93 | +exports.resolve = resolve; |
| 94 | +},{"utilise/client":2,"utilise/keys":4,"utilise/log":5}],2:[function(require,module,exports){ |
| 95 | +module.exports = typeof window != 'undefined' |
| 96 | +},{}],3:[function(require,module,exports){ |
| 97 | +module.exports = is |
| 98 | +is.fn = isFunction |
| 99 | +is.str = isString |
| 100 | +is.num = isNumber |
| 101 | +is.obj = isObject |
| 102 | +is.lit = isLiteral |
| 103 | +is.bol = isBoolean |
| 104 | +is.truthy = isTruthy |
| 105 | +is.falsy = isFalsy |
| 106 | +is.arr = isArray |
| 107 | +is.null = isNull |
| 108 | +is.def = isDef |
| 109 | +is.in = isIn |
| 110 | + |
| 111 | +function is(v){ |
| 112 | + return function(d){ |
| 113 | + return d == v |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +function isFunction(d) { |
| 118 | + return typeof d == 'function' |
| 119 | +} |
| 120 | + |
| 121 | +function isBoolean(d) { |
| 122 | + return typeof d == 'boolean' |
| 123 | +} |
| 124 | + |
| 125 | +function isString(d) { |
| 126 | + return typeof d == 'string' |
| 127 | +} |
| 128 | + |
| 129 | +function isNumber(d) { |
| 130 | + return typeof d == 'number' |
| 131 | +} |
| 132 | + |
| 133 | +function isObject(d) { |
| 134 | + return typeof d == 'object' |
| 135 | +} |
| 136 | + |
| 137 | +function isLiteral(d) { |
| 138 | + return typeof d == 'object' |
| 139 | + && !(d instanceof Array) |
| 140 | +} |
| 141 | + |
| 142 | +function isTruthy(d) { |
| 143 | + return !!d == true |
| 144 | +} |
| 145 | + |
| 146 | +function isFalsy(d) { |
| 147 | + return !!d == false |
| 148 | +} |
| 149 | + |
| 150 | +function isArray(d) { |
| 151 | + return d instanceof Array |
| 152 | +} |
| 153 | + |
| 154 | +function isNull(d) { |
| 155 | + return d === null |
| 156 | +} |
| 157 | + |
| 158 | +function isDef(d) { |
| 159 | + return typeof d !== 'undefined' |
| 160 | +} |
| 161 | + |
| 162 | +function isIn(set) { |
| 163 | + return function(d){ |
| 164 | + return !set ? false |
| 165 | + : set.indexOf ? ~set.indexOf(d) |
| 166 | + : d in set |
| 167 | + } |
| 168 | +} |
| 169 | +},{}],4:[function(require,module,exports){ |
| 170 | +module.exports = function keys(o) { |
| 171 | + return Object.keys(o || {}) |
| 172 | +} |
| 173 | +},{}],5:[function(require,module,exports){ |
| 174 | +var is = require('utilise/is') |
| 175 | + , to = require('utilise/to') |
| 176 | + , owner = require('utilise/owner') |
| 177 | + |
| 178 | +module.exports = function log(prefix){ |
| 179 | + return function(d){ |
| 180 | + if (!owner.console || !console.log.apply) return d; |
| 181 | + is.arr(arguments[2]) && (arguments[2] = arguments[2].length) |
| 182 | + var args = to.arr(arguments) |
| 183 | + args.unshift(prefix.grey ? prefix.grey : prefix) |
| 184 | + return console.log.apply(console, args), d |
| 185 | + } |
| 186 | +} |
| 187 | +},{"utilise/is":3,"utilise/owner":6,"utilise/to":7}],6:[function(require,module,exports){ |
| 188 | +(function (global){ |
| 189 | +module.exports = require('utilise/client') ? /* istanbul ignore next */ window : global |
| 190 | +}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) |
| 191 | +},{"utilise/client":2}],7:[function(require,module,exports){ |
| 192 | +module.exports = { |
| 193 | + arr: toArray |
| 194 | +, obj: toObject |
| 195 | +} |
| 196 | + |
| 197 | +function toArray(d){ |
| 198 | + return Array.prototype.slice.call(d, 0) |
| 199 | +} |
| 200 | + |
| 201 | +function toObject(d) { |
| 202 | + var by = 'id' |
| 203 | + , o = {} |
| 204 | + |
| 205 | + return arguments.length == 1 |
| 206 | + ? (by = d, reduce) |
| 207 | + : reduce.apply(this, arguments) |
| 208 | + |
| 209 | + function reduce(p,v,i){ |
| 210 | + if (i === 0) p = {} |
| 211 | + p[v[by]] = v |
| 212 | + return p |
| 213 | + } |
| 214 | +} |
| 215 | +},{}]},{},[1]); |
0 commit comments