1(function (root, factory) { 2 var routing = factory(); 3 if (typeof define === 'function' && define.amd) { 4 // AMD. Register as an anonymous module. 5 define([], routing.Routing); 6 } else if (typeof module === 'object' && module.exports) { 7 // Node. Does not work with strict CommonJS, but 8 // only CommonJS-like environments that support module.exports, 9 // like Node. 10 module.exports = routing.Routing; 11 } else { 12 // Browser globals (root is window) 13 root.Routing = routing.Routing; 14 root.fos = { 15 Router: routing.Router 16 }; 17 } 18}(this, function () { 19 'use strict'; 20 21/** 22 * @fileoverview This file defines the Router class. 23 * 24 * You can compile this file by running the following command from the Resources folder: 25 * 26 * npm install && npm run build 27 */ 28 29/** 30 * Class Router 31 */ 32 33var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; 34 35var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; 36 37var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); 38 39function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } 40 41var Router = function () { 42 43 /** 44 * @constructor 45 * @param {Router.Context=} context 46 * @param {Object.<string, Router.Route>=} routes 47 */ 48 function Router(context, routes) { 49 _classCallCheck(this, Router); 50 51 this.context_ = context || { base_url: '', prefix: '', host: '', port: '', scheme: '', locale: '' }; 52 this.setRoutes(routes || {}); 53 } 54 55 /** 56 * Returns the current instance. 57 * @returns {Router} 58 */ 59 60 61 _createClass(Router, [{ 62 key: 'setRoutingData', 63 64 65 /** 66 * Sets data for the current instance 67 * @param {Object} data 68 */ 69 value: function setRoutingData(data) { 70 this.setBaseUrl(data['base_url']); 71 this.setRoutes(data['routes']); 72 73 if ('prefix' in data) { 74 this.setPrefix(data['prefix']); 75 } 76 if ('port' in data) { 77 this.setPort(data['port']); 78 } 79 if ('locale' in data) { 80 this.setLocale(data['locale']); 81 } 82 83 this.setHost(data['host']); 84 this.setScheme(data['scheme']); 85 } 86 87 /** 88 * @param {Object.<string, Router.Route>} routes 89 */ 90 91 }, { 92 key: 'setRoutes', 93 value: function setRoutes(routes) { 94 this.routes_ = Object.freeze(routes); 95 } 96 97 /** 98 * @return {Object.<string, Router.Route>} routes 99 */ 100 101 }, { 102 key: 'getRoutes', 103 value: function getRoutes() { 104 return this.routes_; 105 } 106 107 /** 108 * @param {string} baseUrl 109 */ 110 111 }, { 112 key: 'setBaseUrl', 113 value: function setBaseUrl(baseUrl) { 114 this.context_.base_url = baseUrl; 115 } 116 117 /** 118 * @return {string} 119 */ 120 121 }, { 122 key: 'getBaseUrl', 123 value: function getBaseUrl() { 124 return this.context_.base_url; 125 } 126 127 /** 128 * @param {string} prefix 129 */ 130 131 }, {
132 key: 'setPrefix', 133 value: function setPrefix(prefix) { 134 this.context_.prefix = prefix; 135 } 136 137 /** 138 * @param {string} scheme 139 */ 140 141 }, { 142 key: 'setScheme', 143 value: function setScheme(scheme) { 144 this.context_.scheme = scheme; 145 } 146 147 /** 148 * @return {string} 149 */ 150 151 }, { 152 key: 'getScheme', 153 value: function getScheme() { 154 return this.context_.scheme; 155 } 156 157 /** 158 * @param {string} host 159 */ 160 161 }, { 162 key: 'setHost', 163 value: function setHost(host) { 164 this.context_.host = host; 165 } 166 167 /** 168 * @return {string} 169 */ 170 171 }, { 172 key: 'getHost', 173 value: function getHost() { 174 return this.context_.host; 175 } 176 177 /** 178 * @param {string} port 179 */ 180 181 }, { 182 key: 'setPort', 183 value: function setPort(port) { 184 this.context_.port = port; 185 } 186 187 /** 188 * @return {string} 189 */ 190 191 }, { 192 key: 'getPort', 193 value: function getPort() { 194 return this.context_.port; 195 } 196 }, { 197 key: 'setLocale', 198 199 200 /** 201 * @param {string} locale 202 */ 203 value: function setLocale(locale) { 204 this.context_.locale = locale; 205 } 206 207 /** 208 * @return {string} 209 */ 210 211 }, { 212 key: 'getLocale', 213 value: function getLocale() { 214 return this.context_.locale; 215 } 216 }, { 217 key: 'buildQueryParams', 218 219 220 /** 221 * Builds query string params added to a URL. 222 * Port of jQuery's $.param() function, so credit is due there. 223 * 224 * @param {string} prefix 225 * @param {Array|Object|string} params 226 * @param {Function} add 227 */ 228 value: function buildQueryParams(prefix, params, add) { 229 var _this = this; 230 231 var name = void 0; 232 var rbracket = new RegExp(/\[\]$/); 233 234 if (params instanceof Array) { 235 params.forEach(function (val, i) { 236 if (rbracket.test(prefix)) { 237 add(prefix, val); 238 } else { 239 _this.buildQueryParams(prefix + '[' + ((typeof val === 'undefined' ? 'undefined' : _typeof(val)) === 'object' ? i : '') + ']', val, add); 240 } 241 }); 242 } else if ((typeof params === 'undefined' ? 'undefined' : _typeof(params)) === 'object') { 243 for (name in params) { 244 this.buildQueryParams(prefix + '[' + name + ']', params[name], add); 245 } 246 } else { 247 add(prefix, params); 248 } 249 } 250 251 /** 252 * Returns a raw route object. 253 * 254 * @param {string} name 255 * @return {Router.Route} 256 */ 257 258 }, { 259 key: 'getRoute', 260 value: function getRoute(name) { 261 var prefixedName = this.context_.prefix + name; 262 var sf41i18nName = name + '.' + this.context_.locale; 263 var prefixedSf41i18nName = this.context_.prefix + name + '.' + this.context_.locale; 264 var variants = [prefixedName, sf41i18nName, prefixedSf41i18nName, name]; 265 266 for (var i in variants) { 267 if (variants[i] in this.routes_) { 268 return this.routes_[variants[i]]; 269 } 270 } 271 272 throw new Error('The route "' + name + '" does not exist.'); 273 } 274 275 /** 276 * Generates the URL for a route. 277 * 278 * @param {string} name 279 * @param {Object.<string, string>} opt_params 280 * @param {boolean} absolute 281 * @return {string} 282 */ 283 284 }, { 285 key: 'generate', 286 value: function generate(name, opt_params) { 287 var absolute = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; 288 289 var route = this.getRoute(name), 290 params = opt_params || {}, 291 unusedParams = _extends({}, params), 292 url = '', 293 optional = true, 294 host = '', 295 port = typeof this.getPort() == "undefined" || this.getPort() === null ? '' : this.getPort(); 296 297 route.tokens.forEach(function (token) { 298 if ('text' === token[0]) { 299 url = Router.encodePathComponent(token[1]) + url; 300 optional = false;
301 302 return; 303 } 304 305 if ('variable' === token[0]) { 306 var hasDefault = route.defaults && token[3] in route.defaults; 307 if (false === optional || !hasDefault || token[3] in params && params[token[3]] != route.defaults[token[3]]) { 308 var value = void 0; 309 310 if (token[3] in params) { 311 value = params[token[3]]; 312 delete unusedParams[token[3]]; 313 } else if (hasDefault) { 314 value = route.defaults[token[3]]; 315 } else if (optional) { 316 return; 317 } else { 318 throw new Error('The route "' + name + '" requires the parameter "' + token[3] + '".'); 319 } 320 321 var empty = true === value || false === value || '' === value; 322 323 if (!empty || !optional) { 324 var encodedValue = Router.encodePathComponent(value); 325 326 if ('null' === encodedValue && null === value) { 327 encodedValue = ''; 328 } 329 330 url = token[1] + encodedValue + url; 331 } 332 333 optional = false;
334 } else if (hasDefault && token[3] in unusedParams) { 335 delete unusedParams[token[3]]; 336 } 337 338 return; 339 } 340 341 throw new Error('The token type "' + token[0] + '" is not supported.'); 342 }); 343 344 if (url === '') { 345 url = '/'; 346 } 347 348 route.hosttokens.forEach(function (token) { 349 var value = void 0; 350 351 if ('text' === token[0]) { 352 host = token[1] + host; 353 354 return; 355 } 356 357 if ('variable' === token[0]) { 358 if (token[3] in params) { 359 value = params[token[3]]; 360 delete unusedParams[token[3]]; 361 } else if (route.defaults && token[3] in route.defaults) { 362 value = route.defaults[token[3]]; 363 } 364 365 host = token[1] + value + host; 366 } 367 }); 368 // Foo-bar! 369 url = this.context_.base_url + url; 370 371 if (route.requirements && "_scheme" in route.requirements && this.get
371Scheme() != route.requirements["_scheme"]) { 372 var currentHost = host || this.getHost(); 373 374 url = route.requirements["_scheme"] + "://" + currentHost + (currentHost.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url; 375 } else if ("undefined" !== typeof route.schemes && "undefined" !== typeof route.schemes[0] && this.getScheme() !== route.schemes[0]) { 376 var _currentHost = host || this.getHost(); 377 378 url = route.schemes[0] + "://" + _currentHost + (_currentHost.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url; 379 } else if (host && this.getHost() !== host + (host.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port)) { 380 url = this.getScheme() + "://" + host + (host.indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url; 381 } else if (absolute === true) { 382 url = this.getScheme() + "://" + this.getHost() + (this.getHost().indexOf(':' + port) > -1 || '' === port ? '' : ':' + port) + url; 383 } 384 385 if (Object.keys(unusedParams).length > 0) { 386 var prefix = void 0; 387 var queryParams = []; 388 var add = function add(key, value) { 389 // if value is a function then call it and assign it's return value as value 390 value = typeof value === 'function' ? value() : value; 391 392 // change null to empty string 393 value = value === null ? '' : value; 394 395 queryParams.push(Router.encodeQueryComponent(key) + '=' + Router.encodeQueryComponent(value)); 396 }; 397 398 for (prefix in unusedParams) { 399 this.buildQueryParams(prefix, unusedParams[prefix], add); 400 } 401 402 url = url + '?' + queryParams.join('&'); 403 } 404 405 return url; 406 } 407 408 /** 409 * Returns the given string encoded to mimic Symfony URL generator. 410 * 411 * @param {string} value 412 * @return {string} 413 */ 414 415 }], [{ 416 key: 'getInstance', 417 value: function getInstance() { 418 return Routing; 419 } 420 421 /** 422 * Configures the current Router instance with the provided data. 423 * @param {Object} data 424 */ 425 426 }, { 427 key: 'setData', 428 value: function setData(data) { 429 var router = Router.getInstance(); 430 431 router.setRoutingData(data); 432 } 433 }, { 434 key: 'customEncodeURIComponent', 435 value: function customEncodeURIComponent(value) { 436 return encodeURIComponent(value).replace(/%2F/g, '/').replace(/%40/g, '@').replace(/%3A/g, ':').replace(/%21/g, '!').replace(/%3B/g, ';').replace(/%2C/g, ',').replace(/%2A/g, '*').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/'/g, '%27'); 437 } 438 439 /** 440 * Returns the given path properly encoded to mimic Symfony URL generator. 441 * 442 * @param {string} value 443 * @return {string} 444 */ 445 446 }, { 447 key: 'encodePathComponent', 448 value: function encodePathComponent(value) { 449 return Router.customEncodeURIComponent(value).replace(/%3D/g, '=').replace(/%2B/g, '+').replace(/%21/g, '!').replace(/%7C/g, '|'); 450 } 451 452 /** 453 * Returns the given query parameter or value properly encoded to mimic Symfony URL generator. 454 * 455 * @param {string} value 456 * @return {string} 457 */ 458 459 }, { 460 key: 'encodeQueryComponent', 461 value: function encodeQueryComponent(value) { 462 return Router.customEncodeURIComponent(value).replace(/%3F/g, '?'); 463 } 464 }]); 465 466 return Router; 467}(); 468 469/** 470 * @typedef {{ 471 * tokens: (Array.<Array.<string>>), 472 * defaults: (Object.<string, string>), 473 * requirements: Object, 474 * hosttokens: (Array.<string>) 475 * }} 476 */ 477 478 479Router.Route; 480 481/** 482 * @typedef {{ 483 * base_url: (string) 484 * }} 485 */ 486Router.Context; 487 488/** 489 * Router singleton. 490 * @const 491 * @type {Router} 492 */ 493var Routing = new Router(); 494 495 return { Router: Router, Routing: Routing }; 496}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.