[ Index ] |
PHP Cross Reference of phpwcms V1.4.7 _r403 (01.11.10) |
[Summary view] [Print] [Text view]
1 /* 2 --- 3 4 script: URI.js 5 6 name: URI 7 8 description: Provides methods useful in managing the window location and uris. 9 10 license: MIT-style license 11 12 authors: 13 - Sebastian Markbåge 14 - Aaron Newton 15 16 requires: 17 - Core/Selectors 18 - /String.QueryString 19 20 provides: [URI] 21 22 ... 23 */ 24 25 var URI = new Class({ 26 27 Implements: Options, 28 29 options: { 30 /*base: false*/ 31 }, 32 33 regex: /^(?:(\w+):)?(?:\/\/(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?)?(\.\.?$|(?:[^?#\/]*\/)*)([^?#]*)(?:\?([^#]*))?(?:#(.*))?/, 34 parts: ['scheme', 'user', 'password', 'host', 'port', 'directory', 'file', 'query', 'fragment'], 35 schemes: {http: 80, https: 443, ftp: 21, rtsp: 554, mms: 1755, file: 0}, 36 37 initialize: function(uri, options){ 38 this.setOptions(options); 39 var base = this.options.base || URI.base; 40 if(!uri) uri = base; 41 42 if (uri && uri.parsed) this.parsed = $unlink(uri.parsed); 43 else this.set('value', uri.href || uri.toString(), base ? new URI(base) : false); 44 }, 45 46 parse: function(value, base){ 47 var bits = value.match(this.regex); 48 if (!bits) return false; 49 bits.shift(); 50 return this.merge(bits.associate(this.parts), base); 51 }, 52 53 merge: function(bits, base){ 54 if ((!bits || !bits.scheme) && (!base || !base.scheme)) return false; 55 if (base){ 56 this.parts.every(function(part){ 57 if (bits[part]) return false; 58 bits[part] = base[part] || ''; 59 return true; 60 }); 61 } 62 bits.port = bits.port || this.schemes[bits.scheme.toLowerCase()]; 63 bits.directory = bits.directory ? this.parseDirectory(bits.directory, base ? base.directory : '') : '/'; 64 return bits; 65 }, 66 67 parseDirectory: function(directory, baseDirectory) { 68 directory = (directory.substr(0, 1) == '/' ? '' : (baseDirectory || '/')) + directory; 69 if (!directory.test(URI.regs.directoryDot)) return directory; 70 var result = []; 71 directory.replace(URI.regs.endSlash, '').split('/').each(function(dir){ 72 if (dir == '..' && result.length > 0) result.pop(); 73 else if (dir != '.') result.push(dir); 74 }); 75 return result.join('/') + '/'; 76 }, 77 78 combine: function(bits){ 79 return bits.value || bits.scheme + '://' + 80 (bits.user ? bits.user + (bits.password ? ':' + bits.password : '') + '@' : '') + 81 (bits.host || '') + (bits.port && bits.port != this.schemes[bits.scheme] ? ':' + bits.port : '') + 82 (bits.directory || '/') + (bits.file || '') + 83 (bits.query ? '?' + bits.query : '') + 84 (bits.fragment ? '#' + bits.fragment : ''); 85 }, 86 87 set: function(part, value, base){ 88 if (part == 'value'){ 89 var scheme = value.match(URI.regs.scheme); 90 if (scheme) scheme = scheme[1]; 91 if (scheme && !$defined(this.schemes[scheme.toLowerCase()])) this.parsed = { scheme: scheme, value: value }; 92 else this.parsed = this.parse(value, (base || this).parsed) || (scheme ? { scheme: scheme, value: value } : { value: value }); 93 } else if (part == 'data') { 94 this.setData(value); 95 } else { 96 this.parsed[part] = value; 97 } 98 return this; 99 }, 100 101 get: function(part, base){ 102 switch(part){ 103 case 'value': return this.combine(this.parsed, base ? base.parsed : false); 104 case 'data' : return this.getData(); 105 } 106 return this.parsed[part] || ''; 107 }, 108 109 go: function(){ 110 document.location.href = this.toString(); 111 }, 112 113 toURI: function(){ 114 return this; 115 }, 116 117 getData: function(key, part){ 118 var qs = this.get(part || 'query'); 119 if (!$chk(qs)) return key ? null : {}; 120 var obj = qs.parseQueryString(); 121 return key ? obj[key] : obj; 122 }, 123 124 setData: function(values, merge, part){ 125 if (typeof values == 'string'){ 126 data = this.getData(); 127 data[arguments[0]] = arguments[1]; 128 values = data; 129 } else if (merge) { 130 values = $merge(this.getData(), values); 131 } 132 return this.set(part || 'query', Hash.toQueryString(values)); 133 }, 134 135 clearData: function(part){ 136 return this.set(part || 'query', ''); 137 } 138 139 }); 140 141 URI.prototype.toString = URI.prototype.valueOf = function(){ 142 return this.get('value'); 143 }; 144 145 URI.regs = { 146 endSlash: /\/$/, 147 scheme: /^(\w+):/, 148 directoryDot: /\.\/|\.$/ 149 }; 150 151 URI.base = new URI(document.getElements('base[href]', true).getLast(), {base: document.location}); 152 153 String.implement({ 154 155 toURI: function(options){ 156 return new URI(this, options); 157 } 158 159 });
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Nov 16 22:51:00 2010 | Cross-referenced by PHPXref 0.7 |