Merging Uri and UriFactory

This commit is contained in:
Dennis Eichhorn 2016-03-25 15:32:07 +01:00
parent 7dbedb31dd
commit 1a7a2e6e2e
2 changed files with 109 additions and 107 deletions

View File

@ -1,106 +0,0 @@
/**
* UI manager for handling basic ui elements.
*
* @author OMS Development Team <dev@oms.com>
* @author Dennis Eichhorn <d.eichhorn@oms.com>
* @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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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 <d.eichhorn@oms.com>
*/
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 || {}));

View File

@ -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 <d.eichhorn@oms.com>
*/
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 || {}));