/* * metadata - jquery plugin for parsing metadata from elements * * copyright (c) 2006 john resig, yehuda katz, j�örn zaefferer, paul mclanahan * * dual licensed under the mit and gpl licenses: * http://www.opensource.org/licenses/mit-license.php * http://www.gnu.org/licenses/gpl.html * * revision: $id: jquery.metadata.js 4187 2007-12-16 17:15:27z joern.zaefferer $ * */ /** * 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. * * there are three supported types of metadata storage: * * attr: inside an attribute. the name parameter indicates *which* attribute. * * class: inside the class attribute, wrapped in curly braces: { } * * elem: inside a child element (e.g. a script tag). the * name parameter indicates *which* element. * * the metadata for an element is loaded the first time the element is accessed via jquery. * * as a result, you can define the metadata type, use $(expr) to load the metadata into the elements * matched by expr, then redefine the metadata type and run another $(expr) for other elements. * * @name $.metadata.settype * * @example
this is a p
* @before $.metadata.settype("class") * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "label" * @desc reads metadata from the class attribute * * @examplethis is a p
* @before $.metadata.settype("attr", "data") * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "label" * @desc reads metadata from a "data" attribute * * @examplethis is a p
* @before $.metadata.settype("elem", "script") * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "label" * @desc reads metadata from a nested script element * * @param string type the encoding type * @param string name the name of the attribute to be used to get metadata (optional) * @cat plugins/metadata * @descr sets the type of encoding to be used when loading metadata for the first time * @type undefined * @see metadata() */ (function($) { $.extend({ metadata : { defaults : { type: 'class', name: 'metadata', cre: /({.*})/, single: 'metadata' }, settype: function( type, name ){ this.defaults.type = type; this.defaults.name = name; }, get: function( elem, opts ){ var settings = $.extend({},this.defaults,opts); // check for empty string in single property if ( !settings.single.length ) settings.single = 'metadata'; var data = $.data(elem, settings.single); // returned cached data if it already exists if ( data ) return data; data = "{}"; 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 undefined; 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; } if ( data.indexof( '{' ) <0 ) data = "{" + data + "}"; data = eval("(" + data + ")"); $.data( elem, settings.single, data ); return data; } } }); /** * returns the metadata object for the first member of the jquery object. * * @name metadata * @descr returns element's metadata object * @param object opts an object contianing settings to override the defaults * @type jquery * @cat plugins/metadata */ $.fn.metadata = function( opts ){ return $.metadata.get( this[0], opts ); }; })(jquery);