/* Ö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 16 2010-03-26 11: 20: 56z Diego. Alto $
*
*/
/**
* 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).
* 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 <p id = "one" class = "some_class {item_id: 1, item_label: 'label'}"> This is a P </P>
* @ Before $. Metadata. settype ("class ")
* @ After $ ("# One"). Metadata (). item_id = 1; $ ("# One"). Metadata (). item_label = "label"
* @ DESC reads metadata from the class attribute
*
* @ Example <p id = "one" class = "some_class" Data = "{item_id: 1, item_label: 'label'}"> This is a P </P>
* @ Before $. Metadata. settype ("ATTR", "data ")
* @ After $ ("# One"). Metadata (). item_id = 1; $ ("# One"). Metadata (). item_label = "label"
* @ DESC reads metadata from a "data" attribute
*
* @ Example <p id = "one" class = "some_class"> <SCRIPT> {item_id: 1, item_label: 'label'} </SCRIPT> This is a P </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 ',
CRES :/({.*})/,
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.exe C (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 );