From 1a7a2e6e2e88cf73b52f4f81250f0b5bab7b6fab Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Fri, 25 Mar 2016 15:32:07 +0100 Subject: [PATCH] Merging Uri and UriFactory --- Uri/Uri.js | 106 -------------------------------------------- Uri/UriFactory.js | 110 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 109 insertions(+), 107 deletions(-) delete mode 100644 Uri/Uri.js diff --git a/Uri/Uri.js b/Uri/Uri.js deleted file mode 100644 index 0afff17..0000000 --- a/Uri/Uri.js +++ /dev/null @@ -1,106 +0,0 @@ -/** - * UI manager for handling basic ui elements. - * - * @author OMS Development Team - * @author Dennis Eichhorn - * @copyright 2013 Dennis Eichhorn - * @license OMS License 1.0 - * @version 1.0.0 * @since 1.0.0 - */ -(function (jsOMS, undefined) -{ - - /** - * @constructor - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - jsOMS.Uri = function () - { - }; - - /** - * Prases a url. - * - * @param {string} str Url string - * @param {string} [component] I have no idea ?!?!??!?! - * - * @return {Object} - * - * @todo: fix this some parts are not used like components, mode, ini etc. - * - * @method - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - jsOMS.Uri.parseUrl = function (str, component) - { - var query, key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', - 'relative', 'path', 'directory', 'file', 'query', 'fragment' - ], - ini = (this.phpJs && this.phpJs.ini) || {}, - mode = (ini['phpjs.parseUrl.mode'] && - ini['phpjs.parseUrl.mode'].local_value) || 'php', - parser = { - php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, - strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, - loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this) - }; - - var m = parser[mode].exec(str), - uri = {}, - i = 14; - - while (i--) { - if (m[i]) { - uri[key[i]] = m[i]; - } - } - - if (component) { - return uri[component.replace('PHP_URL_', '') - .toLowerCase()]; - } - - if (mode !== 'php') { - var name = (ini['phpjs.parseUrl.queryKey'] && - ini['phpjs.parseUrl.queryKey'].local_value) || 'queryKey'; - parser = /(?:^|&)([^&=]*)=?([^&]*)/g; - uri[name] = {}; - query = uri[key[12]] || ''; - query.replace(parser, function ($0, $1, $2) - { - if ($1) { - uri[name][$1] = $2; - } - }); - } - - delete uri.source; - - return uri; - }; - - /** - * Get Uri query parameters. - * - * @param {string} query Uri query - * @param {string} name Name of the query to return - * - * @return {null|string} - * - * @method - * - * @since 1.0.0 - * @author Dennis Eichhorn - */ - jsOMS.Uri.getUriQueryParameter = function (query, name) - { - name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); - var regex = new RegExp("[\\?&]*" + name + "=([^&#]*)"), - results = regex.exec(query); - return results === null ? "" : decodeURIComponent(results[1].replace(/\+/g, " ")); - }; -}(window.jsOMS = window.jsOMS || {})); diff --git a/Uri/UriFactory.js b/Uri/UriFactory.js index 30d1572..510d8fe 100644 --- a/Uri/UriFactory.js +++ b/Uri/UriFactory.js @@ -1,3 +1,111 @@ (function (uriFactory, undefined) { + jsOMS.UriFactory = {}; + jsOMS.UriFactory.uri = {}; -}(window.jsOMS = window.jsOMS || {})); + jsOMS.UriFactory.parseUrl = function (str, component) + { + let query, key = ['source', 'scheme', 'authority', 'userInfo', 'user', 'pass', 'host', 'port', + 'relative', 'path', 'directory', 'file', 'query', 'fragment' + ], + ini = {}, + mode = 'php', + parser = { + php: /^(?:([^:\/?#]+):)?(?:\/\/()(?:(?:()(?:([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?()(?:(()(?:(?:[^?#\/]*\/)*)()(?:[^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, + strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/, + loose: /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-scheme to catch file:/// (should restrict this) + }; + + let m = parser[mode].exec(str), + uri = {}, + i = 14; + + while (i--) { + if (m[i]) { + uri[key[i]] = m[i]; + } + } + + if (component) { + return uri[component.replace('PHP_URL_', '').toLowerCase()]; + } + + if (mode !== 'php') { + let name = 'queryKey'; + parser = /(?:^|&)([^&=]*)=?([^&]*)/g; + uri[name] = {}; + query = uri[key[12]] || ''; + + query.replace(parser, function ($0, $1, $2) + { + if ($1) { + uri[name][$1] = $2; + } + }); + } + + delete uri.source; + + return uri; + }; + + /** + * Get Uri query parameters. + * + * @param {string} query Uri query + * @param {string} name Name of the query to return + * + * @return {null|string} + * + * @method + * + * @since 1.0.0 + * @author Dennis Eichhorn + */ + jsOMS.UriFactory.getUriQueryParameter = function (query, name) + { + name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]"); + + let regex = new RegExp("[\\?&]*" + name + "=([^&#]*)"), + results = regex.exec(query); + + return results === null ? '' : decodeURIComponent(results[1].replace(/\+/g, ' ')); + }; + + jsOMS.UriFactory.setQuery = function(key, value, overwrite) + { + overwrite = typeof overwrite !== 'undefined' ? overwrite : true; + + if(overwrite || !jsOMS.UriFactory.uri.hasProperty(key)) { + jsOMS.UriFactory.uri[key] = value; + + return true; + } + + return false; + }; + + jsOMS.UriFactory.build = function(uri, toMatch) + { + let current = jsOMS.UriFactory.parseUrl(window.location.href); + // match(new RegExp("\{[#\?\.a-zA-Z0-9]*\}", "gi")); + + return uri.replace('\{[\/#\?@\.\$][a-zA-Z0-9]*\}' function(match) { + match = substr(match[0], 1, match[0].length - 2); + + if(toMatch.hasProperty(match)) { + return toMatch[match]; + } else if(jsOMS.UriFactory.uri[match] !== 'undefined') { + return jsOMS.UriFactory.uri[match]; + } else if (match.indexOf('#') === 0) { + return document.getElementById(match.substring(1, match.length)).value; + } else if(match.indexOf('?') === 0) { + return jsOMS.UriFactory.getUriQueryParameter(current.query, match.substring(1, match.length)); + } else if(match.indexOf('/') === 0) { + // todo: second match should return second path + return 'ERROR PATH'; + } else { + return match; + } + }); + }; +}(window.jsOMS = window.jsOMS || {})); \ No newline at end of file