mootools-1.2.4.4-more.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. //MooTools More, <http://mootools.net/more>. Copyright (c) 2006-2009 Aaron Newton <http://clientcide.com/>, Valerio Proietti <http://mad4milk.net> & the MooTools team <http://mootools.net/developers>, MIT Style License.
  2. /*
  3. ---
  4. script: More.js
  5. description: MooTools More
  6. license: MIT-style license
  7. authors:
  8. - Guillermo Rauch
  9. - Thomas Aylott
  10. - Scott Kyle
  11. requires:
  12. - core:1.2.4/MooTools
  13. provides: [MooTools.More]
  14. ...
  15. */
  16. MooTools.More = {
  17. 'version': '1.2.4.4',
  18. 'build': '6f6057dc645fdb7547689183b2311063bd653ddf'
  19. };
  20. /*
  21. ---
  22. script: String.QueryString.js
  23. description: Methods for dealing with URI query strings.
  24. license: MIT-style license
  25. authors:
  26. - Sebastian Markbåge, Aaron Newton, Lennart Pilon, Valerio Proietti
  27. requires:
  28. - core:1.2.4/Array
  29. - core:1.2.4/String
  30. - /MooTools.More
  31. provides: [String.QueryString]
  32. ...
  33. */
  34. String.implement({
  35. parseQueryString: function(){
  36. var vars = this.split(/[&;]/), res = {};
  37. if (vars.length) vars.each(function(val){
  38. var index = val.indexOf('='),
  39. keys = index < 0 ? [''] : val.substr(0, index).match(/[^\]\[]+/g),
  40. value = decodeURIComponent(val.substr(index + 1)),
  41. obj = res;
  42. keys.each(function(key, i){
  43. var current = obj[key];
  44. if(i < keys.length - 1)
  45. obj = obj[key] = current || {};
  46. else if($type(current) == 'array')
  47. current.push(value);
  48. else
  49. obj[key] = $defined(current) ? [current, value] : value;
  50. });
  51. });
  52. return res;
  53. },
  54. cleanQueryString: function(method){
  55. return this.split('&').filter(function(val){
  56. var index = val.indexOf('='),
  57. key = index < 0 ? '' : val.substr(0, index),
  58. value = val.substr(index + 1);
  59. return method ? method.run([key, value]) : $chk(value);
  60. }).join('&');
  61. }
  62. });
  63. /*
  64. ---
  65. script: URI.js
  66. description: Provides methods useful in managing the window location and uris.
  67. license: MIT-style license
  68. authors:
  69. - Sebastian Markbåge
  70. - Aaron Newton
  71. requires:
  72. - core:1.2.4/Selectors
  73. - /String.QueryString
  74. provides: URI
  75. ...
  76. */
  77. var URI = new Class({
  78. Implements: Options,
  79. options: {
  80. /*base: false*/
  81. },
  82. regex: /^(?:(\w+):)?(?:\/\/(?:(?:([^:@\/]*):?([^:@\/]*))?@)?([^:\/?#]*)(?::(\d*))?)?(\.\.?$|(?:[^?#\/]*\/)*)([^?#]*)(?:\?([^#]*))?(?:#(.*))?/,
  83. parts: ['scheme', 'user', 'password', 'host', 'port', 'directory', 'file', 'query', 'fragment'],
  84. schemes: {http: 80, https: 443, ftp: 21, rtsp: 554, mms: 1755, file: 0},
  85. initialize: function(uri, options){
  86. this.setOptions(options);
  87. var base = this.options.base || URI.base;
  88. if(!uri) uri = base;
  89. if (uri && uri.parsed) this.parsed = $unlink(uri.parsed);
  90. else this.set('value', uri.href || uri.toString(), base ? new URI(base) : false);
  91. },
  92. parse: function(value, base){
  93. var bits = value.match(this.regex);
  94. if (!bits) return false;
  95. bits.shift();
  96. return this.merge(bits.associate(this.parts), base);
  97. },
  98. merge: function(bits, base){
  99. if ((!bits || !bits.scheme) && (!base || !base.scheme)) return false;
  100. if (base){
  101. this.parts.every(function(part){
  102. if (bits[part]) return false;
  103. bits[part] = base[part] || '';
  104. return true;
  105. });
  106. }
  107. bits.port = bits.port || this.schemes[bits.scheme.toLowerCase()];
  108. bits.directory = bits.directory ? this.parseDirectory(bits.directory, base ? base.directory : '') : '/';
  109. return bits;
  110. },
  111. parseDirectory: function(directory, baseDirectory) {
  112. directory = (directory.substr(0, 1) == '/' ? '' : (baseDirectory || '/')) + directory;
  113. if (!directory.test(URI.regs.directoryDot)) return directory;
  114. var result = [];
  115. directory.replace(URI.regs.endSlash, '').split('/').each(function(dir){
  116. if (dir == '..' && result.length > 0) result.pop();
  117. else if (dir != '.') result.push(dir);
  118. });
  119. return result.join('/') + '/';
  120. },
  121. combine: function(bits){
  122. return bits.value || bits.scheme + '://' +
  123. (bits.user ? bits.user + (bits.password ? ':' + bits.password : '') + '@' : '') +
  124. (bits.host || '') + (bits.port && bits.port != this.schemes[bits.scheme] ? ':' + bits.port : '') +
  125. (bits.directory || '/') + (bits.file || '') +
  126. (bits.query ? '?' + bits.query : '') +
  127. (bits.fragment ? '#' + bits.fragment : '');
  128. },
  129. set: function(part, value, base){
  130. if (part == 'value'){
  131. var scheme = value.match(URI.regs.scheme);
  132. if (scheme) scheme = scheme[1];
  133. if (scheme && !$defined(this.schemes[scheme.toLowerCase()])) this.parsed = { scheme: scheme, value: value };
  134. else this.parsed = this.parse(value, (base || this).parsed) || (scheme ? { scheme: scheme, value: value } : { value: value });
  135. } else if (part == 'data') {
  136. this.setData(value);
  137. } else {
  138. this.parsed[part] = value;
  139. }
  140. return this;
  141. },
  142. get: function(part, base){
  143. switch(part){
  144. case 'value': return this.combine(this.parsed, base ? base.parsed : false);
  145. case 'data' : return this.getData();
  146. }
  147. return this.parsed[part] || '';
  148. },
  149. go: function(){
  150. document.location.href = this.toString();
  151. },
  152. toURI: function(){
  153. return this;
  154. },
  155. getData: function(key, part){
  156. var qs = this.get(part || 'query');
  157. if (!$chk(qs)) return key ? null : {};
  158. var obj = qs.parseQueryString();
  159. return key ? obj[key] : obj;
  160. },
  161. setData: function(values, merge, part){
  162. if (typeof values == 'string'){
  163. data = this.getData();
  164. data[arguments[0]] = arguments[1];
  165. values = data;
  166. } else if (merge) {
  167. values = $merge(this.getData(), values);
  168. }
  169. return this.set(part || 'query', Hash.toQueryString(values));
  170. },
  171. clearData: function(part){
  172. return this.set(part || 'query', '');
  173. }
  174. });
  175. URI.prototype.toString = URI.prototype.valueOf = function(){
  176. return this.get('value');
  177. };
  178. URI.regs = {
  179. endSlash: /\/$/,
  180. scheme: /^(\w+):/,
  181. directoryDot: /\.\/|\.$/
  182. };
  183. URI.base = new URI(document.getElements('base[href]', true).getLast(), {base: document.location});
  184. String.implement({
  185. toURI: function(options){
  186. return new URI(this, options);
  187. }
  188. });