Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Item8481: Add query-object jQuery plugin
git-svn-id: http://svn.foswiki.org/trunk@6248 0b4bb1d4-4e5a-0410-9cc4-b2b747904278
- Loading branch information
PaulHarvey
authored and
PaulHarvey
committed
Feb 8, 2010
1 parent
51195ff
commit a7be1a0
Showing
7 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
%META:TOPICINFO{author="ProjectContributor" date="1258383446" format="1.1" version="1.2"}% | ||
%META:TOPICPARENT{name="JQueryPlugin"}% | ||
---+ %TOPIC% | ||
%JQPLUGINS{"queryobject" | ||
format=" | ||
Homepage: $homepage <br /> | ||
Author(s): $author <br /> | ||
Version: $version | ||
" | ||
}% | ||
|
||
%STARTSECTION{"summary"}% | ||
This extension creates a singleton query string object for quick and | ||
readable query string modification and creation. This plugin provides a | ||
simple way of taking a page's query string and creating a modified | ||
version of this with little code. | ||
%ENDSECTION{"summary"}% | ||
|
||
---++ Examples | ||
<verbatim class="javascript"> | ||
var url = location.search; | ||
> "?action=view§ion=info&id=123&debug&testy[]=true&testy[]=false&testy[]" | ||
var section = $.query.get('section'); | ||
> "info" | ||
var id = $.query.get('id'); | ||
> 123 | ||
var debug = $.query.get('debug'); | ||
> true | ||
var arr = $.query.get('testy'); | ||
> ["true", "false", true] | ||
var arrayElement = $.query.get('testy[1]'); | ||
> "false" | ||
var newUrl = $.query.set("section", 5).set("action", "do").toString(); | ||
> "?action=do§ion=5&id=123" | ||
var newQuery = "" + $.query.set('type', 'string'); | ||
> "?action=view§ion=info&id=123&type=string" | ||
var oldQuery = $.query.toString(); | ||
> "?action=view§ion=info&id=123" | ||
var oldQuery2 = $.query; | ||
> ?action=view§ion=info&id=123 | ||
var newerQuery = $.query.SET('type', 'string'); | ||
> ?action=view§ion=info&id=123&type=string | ||
var notOldQuery = $.query.toString(); | ||
> "?action=view§ion=info&id=123&type=string" | ||
var oldQueryAgain = $.query.REMOVE("type"); | ||
> ?action=view§ion=info&id=123 | ||
var emptyQuery = $.query.empty(); | ||
> "" | ||
var stillTheSame = $.query.copy(); | ||
> ?action=view§ion=info&id=123 | ||
</verbatim> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
JQueryPlugin/lib/Foswiki/Plugins/JQueryPlugin/QUERYOBJECT.pm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Plugin for Foswiki - The Free and Open Source Wiki, http://foswiki.org/ | ||
# | ||
# Copyright (C) 2006-2010 Blair Mitchelmore | ||
# (blair DOT mitchelmore AT gmail DOT com) | ||
# Licensed under the WTFPL (http://sam.zoy.org/wtfpl/). | ||
# | ||
# Packaged for Foswiki by Paul.W.Harvey@csiro.au - www.taxonomy.org.au | ||
|
||
package Foswiki::Plugins::JQueryPlugin::SUPERSUBS; | ||
use strict; | ||
use base 'Foswiki::Plugins::JQueryPlugin::Plugin'; | ||
|
||
=begin TML | ||
---+ package Foswiki::Plugins::JQueryPlugin::QUERYOBJECT | ||
This is the perl stub for the jquery.queryobject plugin. | ||
=cut | ||
|
||
=begin TML | ||
---++ ClassMethod new( $class, $session, ... ) | ||
Constructor | ||
=cut | ||
|
||
sub new { | ||
my $class = shift; | ||
my $session = shift || $Foswiki::Plugins::SESSION; | ||
|
||
my $this = bless($class->SUPER::new( | ||
$session, | ||
name => 'QueryObject', | ||
version => '2.1.7', | ||
author => 'Blair Mitchelmore', | ||
homepage => 'http://plugins.jquery.com/project/query-object', | ||
javascript => ['jquery.queryobject.js'], | ||
), $class); | ||
|
||
return $this; | ||
} | ||
|
||
1; |
5 changes: 5 additions & 0 deletions
5
JQueryPlugin/pub/System/JQueryPlugin/plugins/query-object/Makefile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
TARGET=\ | ||
jquery.queryobject.js \ | ||
$(JQUERYPLUGIN_LIB)/QUERYOBJECT.pm | ||
|
||
-include ../../Makefile.include |
20 changes: 20 additions & 0 deletions
20
JQueryPlugin/pub/System/JQueryPlugin/plugins/query-object/jquery.queryobject.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file added
BIN
+1.66 KB
JQueryPlugin/pub/System/JQueryPlugin/plugins/query-object/jquery.queryobject.js.gz
Binary file not shown.
225 changes: 225 additions & 0 deletions
225
JQueryPlugin/pub/System/JQueryPlugin/plugins/query-object/jquery.queryobject.uncompressed.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/** | ||
* jQuery.query - Query String Modification and Creation for jQuery | ||
* Written by Blair Mitchelmore (blair DOT mitchelmore AT gmail DOT com) | ||
* Licensed under the WTFPL (http://sam.zoy.org/wtfpl/). | ||
* Date: 2009/8/13 | ||
* | ||
* @author Blair Mitchelmore | ||
* @version 2.1.7 | ||
* | ||
**/ | ||
new function(settings) { | ||
// Various Settings | ||
var $separator = settings.separator || '&'; | ||
var $spaces = settings.spaces === false ? false : true; | ||
var $suffix = settings.suffix === false ? '' : '[]'; | ||
var $prefix = settings.prefix === false ? false : true; | ||
var $hash = $prefix ? settings.hash === true ? "#" : "?" : ""; | ||
var $numbers = settings.numbers === false ? false : true; | ||
|
||
jQuery.query = new function() { | ||
var is = function(o, t) { | ||
return o != undefined && o !== null && (!!t ? o.constructor == t : true); | ||
}; | ||
var parse = function(path) { | ||
var m, rx = /\[([^[]*)\]/g, match = /^([^[]+)(\[.*\])?$/.exec(path), base = match[1], tokens = []; | ||
while (m = rx.exec(match[2])) tokens.push(m[1]); | ||
return [base, tokens]; | ||
}; | ||
var set = function(target, tokens, value) { | ||
var o, token = tokens.shift(); | ||
if (typeof target != 'object') target = null; | ||
if (token === "") { | ||
if (!target) target = []; | ||
if (is(target, Array)) { | ||
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value)); | ||
} else if (is(target, Object)) { | ||
var i = 0; | ||
while (target[i++] != null); | ||
target[--i] = tokens.length == 0 ? value : set(target[i], tokens.slice(0), value); | ||
} else { | ||
target = []; | ||
target.push(tokens.length == 0 ? value : set(null, tokens.slice(0), value)); | ||
} | ||
} else if (token && token.match(/^\s*[0-9]+\s*$/)) { | ||
var index = parseInt(token, 10); | ||
if (!target) target = []; | ||
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value); | ||
} else if (token) { | ||
var index = token.replace(/^\s*|\s*$/g, ""); | ||
if (!target) target = {}; | ||
if (is(target, Array)) { | ||
var temp = {}; | ||
for (var i = 0; i < target.length; ++i) { | ||
temp[i] = target[i]; | ||
} | ||
target = temp; | ||
} | ||
target[index] = tokens.length == 0 ? value : set(target[index], tokens.slice(0), value); | ||
} else { | ||
return value; | ||
} | ||
return target; | ||
}; | ||
|
||
var queryObject = function(a) { | ||
var self = this; | ||
self.keys = {}; | ||
|
||
if (a.queryObject) { | ||
jQuery.each(a.get(), function(key, val) { | ||
self.SET(key, val); | ||
}); | ||
} else { | ||
jQuery.each(arguments, function() { | ||
var q = "" + this; | ||
q = q.replace(/^[?#]/,''); // remove any leading ? || # | ||
q = q.replace(/[;&]$/,''); // remove any trailing & || ; | ||
if ($spaces) q = q.replace(/[+]/g,' '); // replace +'s with spaces | ||
|
||
jQuery.each(q.split(/[&;]/), function(){ | ||
var key = decodeURIComponent(this.split('=')[0] || ""); | ||
var val = decodeURIComponent(this.split('=')[1] || ""); | ||
|
||
if (!key) return; | ||
|
||
if ($numbers) { | ||
if (/^[+-]?[0-9]+\.[0-9]*$/.test(val)) // simple float regex | ||
val = parseFloat(val); | ||
else if (/^[+-]?[0-9]+$/.test(val)) // simple int regex | ||
val = parseInt(val, 10); | ||
} | ||
|
||
val = (!val && val !== 0) ? true : val; | ||
|
||
if (val !== false && val !== true && typeof val != 'number') | ||
val = val; | ||
|
||
self.SET(key, val); | ||
}); | ||
}); | ||
} | ||
return self; | ||
}; | ||
|
||
queryObject.prototype = { | ||
queryObject: true, | ||
has: function(key, type) { | ||
var value = this.get(key); | ||
return is(value, type); | ||
}, | ||
GET: function(key) { | ||
if (!is(key)) return this.keys; | ||
var parsed = parse(key), base = parsed[0], tokens = parsed[1]; | ||
var target = this.keys[base]; | ||
while (target != null && tokens.length != 0) { | ||
target = target[tokens.shift()]; | ||
} | ||
return typeof target == 'number' ? target : target || ""; | ||
}, | ||
get: function(key) { | ||
var target = this.GET(key); | ||
if (is(target, Object)) | ||
return jQuery.extend(true, {}, target); | ||
else if (is(target, Array)) | ||
return target.slice(0); | ||
return target; | ||
}, | ||
SET: function(key, val) { | ||
var value = !is(val) ? null : val; | ||
var parsed = parse(key), base = parsed[0], tokens = parsed[1]; | ||
var target = this.keys[base]; | ||
this.keys[base] = set(target, tokens.slice(0), value); | ||
return this; | ||
}, | ||
set: function(key, val) { | ||
return this.copy().SET(key, val); | ||
}, | ||
REMOVE: function(key) { | ||
return this.SET(key, null).COMPACT(); | ||
}, | ||
remove: function(key) { | ||
return this.copy().REMOVE(key); | ||
}, | ||
EMPTY: function() { | ||
var self = this; | ||
jQuery.each(self.keys, function(key, value) { | ||
delete self.keys[key]; | ||
}); | ||
return self; | ||
}, | ||
load: function(url) { | ||
var hash = url.replace(/^.*?[#](.+?)(?:\?.+)?$/, "$1"); | ||
var search = url.replace(/^.*?[?](.+?)(?:#.+)?$/, "$1"); | ||
return new queryObject(url.length == search.length ? '' : search, url.length == hash.length ? '' : hash); | ||
}, | ||
empty: function() { | ||
return this.copy().EMPTY(); | ||
}, | ||
copy: function() { | ||
return new queryObject(this); | ||
}, | ||
COMPACT: function() { | ||
function build(orig) { | ||
var obj = typeof orig == "object" ? is(orig, Array) ? [] : {} : orig; | ||
if (typeof orig == 'object') { | ||
function add(o, key, value) { | ||
if (is(o, Array)) | ||
o.push(value); | ||
else | ||
o[key] = value; | ||
} | ||
jQuery.each(orig, function(key, value) { | ||
if (!is(value)) return true; | ||
add(obj, key, build(value)); | ||
}); | ||
} | ||
return obj; | ||
} | ||
this.keys = build(this.keys); | ||
return this; | ||
}, | ||
compact: function() { | ||
return this.copy().COMPACT(); | ||
}, | ||
toString: function() { | ||
var i = 0, queryString = [], chunks = [], self = this; | ||
var encode = function(str) { | ||
str = str + ""; | ||
if ($spaces) str = str.replace(/ /g, "+"); | ||
return encodeURIComponent(str); | ||
}; | ||
var addFields = function(arr, key, value) { | ||
if (!is(value) || value === false) return; | ||
var o = [encode(key)]; | ||
if (value !== true) { | ||
o.push("="); | ||
o.push(encode(value)); | ||
} | ||
arr.push(o.join("")); | ||
}; | ||
var build = function(obj, base) { | ||
var newKey = function(key) { | ||
return !base || base == "" ? [key].join("") : [base, "[", key, "]"].join(""); | ||
}; | ||
jQuery.each(obj, function(key, value) { | ||
if (typeof value == 'object') | ||
build(value, newKey(key)); | ||
else | ||
addFields(chunks, newKey(key), value); | ||
}); | ||
}; | ||
|
||
build(this.keys); | ||
|
||
if (chunks.length > 0) queryString.push($hash); | ||
queryString.push(chunks.join($separator)); | ||
|
||
return queryString.join(""); | ||
} | ||
}; | ||
|
||
return new queryObject(location.search, location.hash); | ||
}; | ||
}(jQuery.query || {}); // Pass in jQuery.query as settings object | ||
|