MINOR Updated jquery-metadata to v2.1 (removed piston as the release is not contained in the canonical git repo)

This commit is contained in:
Ingo Schommer 2011-04-16 15:10:28 +12:00
parent 03547fb205
commit 7bee470209
2 changed files with 76 additions and 58 deletions

View File

@ -1,8 +0,0 @@
---
format: 1
handler:
commit: 0dbb5ec89ce8f493e33b83433b03d6feaec867fc
branch: master
lock: false
repository_url: git://github.com/jquery/jquery-metadata.git
repository_class: Piston::Git::Repository

View File

@ -7,7 +7,7 @@
* http://www.opensource.org/licenses/mit-license.php * http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html * http://www.gnu.org/licenses/gpl.html
* *
* Revision: $Id: jquery.metadata.js 4187 2007-12-16 17:15:27Z joern.zaefferer $ * Revision: $Id: jquery.metadata.js 3640 2007-10-11 18:34:38Z pmclanahan $
* *
*/ */
@ -15,7 +15,7 @@
* Sets the type of metadata to use. Metadata is encoded in JSON, and each property * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
* in the JSON will become a property of the element itself. * in the JSON will become a property of the element itself.
* *
* There are three supported types of metadata storage: * There are four supported types of metadata storage:
* *
* attr: Inside an attribute. The name parameter indicates *which* attribute. * attr: Inside an attribute. The name parameter indicates *which* attribute.
* *
@ -23,6 +23,7 @@
* *
* elem: Inside a child element (e.g. a script tag). The * elem: Inside a child element (e.g. a script tag). The
* name parameter indicates *which* element. * name parameter indicates *which* element.
* html5: Values are stored in data-* attributes.
* *
* The metadata for an element is loaded the first time the element is accessed via jQuery. * The metadata for an element is loaded the first time the element is accessed via jQuery.
* *
@ -46,6 +47,11 @@
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label" * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
* @desc Reads metadata from a nested script element * @desc Reads metadata from a nested script element
* *
* @example <p id="one" class="some_class" data-item_id="1" data-item_label="Label">This is a p</p>
* @before $.metadata.setType("html5")
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
* @desc Reads metadata from a series of data-* attributes
*
* @param String type The encoding type * @param String type The encoding type
* @param String name The name of the attribute to be used to get metadata (optional) * @param String name The name of the attribute to be used to get metadata (optional)
* @cat Plugins/Metadata * @cat Plugins/Metadata
@ -57,53 +63,73 @@
(function($) { (function($) {
$.extend({ $.extend({
metadata : { metadata : {
defaults : { defaults : {
type: 'class', type: 'class',
name: 'metadata', name: 'metadata',
cre: /({.*})/, cre: /({.*})/,
single: 'metadata' single: 'metadata'
}, },
setType: function( type, name ){ setType: function( type, name ){
this.defaults.type = type; this.defaults.type = type;
this.defaults.name = name; this.defaults.name = name;
}, },
get: function( elem, opts ){ get: function( elem, opts ){
var settings = $.extend({},this.defaults,opts); var settings = $.extend({},this.defaults,opts);
// check for empty string in single property // check for empty string in single property
if ( !settings.single.length ) settings.single = 'metadata'; if ( !settings.single.length ) settings.single = 'metadata';
var data = $.data(elem, settings.single); var data = $.data(elem, settings.single);
// returned cached data if it already exists // returned cached data if it already exists
if ( data ) return data; if ( data ) return data;
data = "{}"; data = "{}";
if ( settings.type == "class" ) { var getData = function(data) {
var m = settings.cre.exec( elem.className ); if(typeof data != "string") return data;
if ( m )
data = m[1]; if( data.indexOf('{') < 0 ) {
} else if ( settings.type == "elem" ) { data = eval("(" + data + ")");
if( !elem.getElementsByTagName ) }
return undefined; }
var e = elem.getElementsByTagName(settings.name);
if ( e.length ) var getObject = function(data) {
data = $.trim(e[0].innerHTML); if(typeof data != "string") return data;
} else if ( elem.getAttribute != undefined ) {
var attr = elem.getAttribute( settings.name ); data = eval("(" + data + ")");
if ( attr ) return data;
data = attr; }
}
if ( settings.type == "html5" ) {
if ( data.indexOf( '{' ) <0 ) var object = {};
data = "{" + data + "}"; $( elem.attributes ).each(function() {
var name = this.nodeName;
data = eval("(" + data + ")"); if(name.match(/^data-/)) name = name.replace(/^data-/, '');
else return true;
$.data( elem, settings.single, data ); object[name] = getObject(this.nodeValue);
return data; });
} } else {
} if ( settings.type == "class" ) {
var m = settings.cre.exec( elem.className );
if ( m )
data = m[1];
} else if ( settings.type == "elem" ) {
if( !elem.getElementsByTagName ) return;
var e = elem.getElementsByTagName(settings.name);
if ( e.length )
data = $.trim(e[0].innerHTML);
} else if ( elem.getAttribute != undefined ) {
var attr = elem.getAttribute( settings.name );
if ( attr )
data = attr;
}
object = getObject(data.indexOf("{") < 0 ? "{" + data + "}" : data);
}
$.data( elem, settings.single, object );
return object;
}
}
}); });
/** /**
@ -116,7 +142,7 @@ $.extend({
* @cat Plugins/Metadata * @cat Plugins/Metadata
*/ */
$.fn.metadata = function( opts ){ $.fn.metadata = function( opts ){
return $.metadata.get( this[0], opts ); return $.metadata.get( this[0], opts );
}; };
})(jQuery); })(jQuery);