UNPKG

este-library-oldschool

Version:

Library for github.com/steida/este.git

194 lines (181 loc) 4.62 kB
// Generated by github.com/steida/coffee2closure 900.1.18 /** @fileoverview este.router.Route. */ goog.provide('este.router.Route'); goog.require('este.string'); /** @param {string} path @param {Function} show @param {este.router.Route.Options=} options @constructor */ este.router.Route = function(path1, show, options) { this.path = path1; this.show = show; this.hide = options != null ? options.hide : void 0; this.keys = []; this.pathToRegexp(options != null ? options.sensitive : void 0, options != null ? options.strict : void 0); } /** @param {string} path @param {Object=} params @return {string} */ este.router.Route.createUrl = function(path, params) { var index, key, ref, regex, value; if (params == null) { params = {}; } if (params.length) { index = 0; path = path.replace(/\*/g, function() { return params[index++]; }); } else { for (key in params) { value = params[key]; if (value === void 0) { value = ''; } regex = new RegExp("\\:" + key); path = path.replace(regex, value); } } if (path.charAt(path.length - 1) === '?') { path = path.slice(0, -1); } if (path.length > 1 && ((ref = path.charAt(path.length - 1)) === '/' || ref === '.')) { path = path.slice(0, -1); } return path; }; /** hide - callback sensitive - Enable case sensitive routing strict - Enable strict routing, by default "/foo" and "/foo/" are treated the same by the route. @typedef {{ hide: (Function|undefined), sensitive: (boolean|undefined), strict: (boolean|undefined) }} */ este.router.Route.Options; /** @type {string} */ este.router.Route.prototype.path = ''; /** @type {Function} @protected */ este.router.Route.prototype.show = null; /** @type {Function|undefined} @protected */ este.router.Route.prototype.hide = null; /** @type {RegExp} @protected */ este.router.Route.prototype.regexp = null; /** @type {Array.<Object>} @protected */ este.router.Route.prototype.keys = null; /** @param {string} path @param {boolean} isNavigation @param {boolean=} forceHide @return {boolean} true if route was matched */ este.router.Route.prototype.process = function(path, isNavigation, forceHide) { var matches, params; if (forceHide == null) { forceHide = false; } if (!forceHide) { matches = this.getMatches(path); if (matches) { params = this.getParams(matches); this.show(params, isNavigation); return true; } } if (this.hide) { this.hide(); } return false; }; /** @param {Object=} params @return {string} */ este.router.Route.prototype.createUrl = function(params) { if (params == null) { params = {}; } return este.router.Route.createUrl(this.path, params); }; /** @param {boolean=} sensitive @param {boolean=} strict @protected */ este.router.Route.prototype.pathToRegexp = function(sensitive, strict) { var regexPath; regexPath = this.path.concat(strict ? '' : '/?').replace(/\/\(/g, '(?:/').replace(/\+/g, '__plus__').replace(/(\/)?(\.)?:(\w+)(?:(\(.*?\)))?(\?)?/g, (function(_this) { return function(_, slash, format, key, capture, optional) { _this.keys.push({ name: key, optional: !!optional }); slash || (slash = ''); return (optional ? '' : slash) + '(?:' + (optional ? slash : '') + (format || '') + (capture || (format && '([^/.]+?)' || '([^/]+?)')) + ')' + (optional || ''); }; })(this)).replace(/([\/.])/g, '\\$1').replace(/__plus__/g, '(.+)').replace(/\*/g, '(.*)'); return this.regexp = new RegExp("^" + regexPath + "$", sensitive ? '' : 'i'); }; /** @param {string} path @return {Array.<string>} @protected */ este.router.Route.prototype.getMatches = function(path) { var pathname, qsIndex; qsIndex = path.indexOf('?'); pathname = qsIndex > -1 ? path.slice(0, qsIndex) : path; return this.regexp.exec(pathname); }; /** @param {Array.<string>} matches @return {Object|Array} @protected */ este.router.Route.prototype.getParams = function(matches) { var i, j, key, len, match, params, value; params = null; for (i = j = 0, len = matches.length; j < len; i = ++j) { match = matches[i]; if (!i) { continue; } key = this.keys[i - 1]; value = typeof match === 'string' ? decodeURIComponent(match) : match; if (key) { if (params == null) { params = {}; } params[key.name] = value; } else { if (params == null) { params = []; } params.push(value); } } return params; };