40 |
aurelien |
1 |
/*
|
|
|
2 |
* Metadata - jQuery plugin for parsing metadata from elements
|
|
|
3 |
*
|
|
|
4 |
* Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
|
|
|
5 |
*
|
|
|
6 |
* Dual licensed under the MIT and GPL licenses:
|
|
|
7 |
* http://www.opensource.org/licenses/mit-license.php
|
|
|
8 |
* http://www.gnu.org/licenses/gpl.html
|
|
|
9 |
*
|
|
|
10 |
* Revision: $Id: jquery.metadata.js 4187 2007-12-16 17:15:27Z joern.zaefferer $
|
|
|
11 |
*
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* Sets the type of metadata to use. Metadata is encoded in JSON, and each property
|
|
|
16 |
* in the JSON will become a property of the element itself.
|
|
|
17 |
*
|
|
|
18 |
* There are three supported types of metadata storage:
|
|
|
19 |
*
|
|
|
20 |
* attr: Inside an attribute. The name parameter indicates *which* attribute.
|
|
|
21 |
*
|
|
|
22 |
* class: Inside the class attribute, wrapped in curly braces: { }
|
|
|
23 |
*
|
|
|
24 |
* elem: Inside a child element (e.g. a script tag). The
|
|
|
25 |
* name parameter indicates *which* element.
|
|
|
26 |
*
|
|
|
27 |
* The metadata for an element is loaded the first time the element is accessed via jQuery.
|
|
|
28 |
*
|
|
|
29 |
* As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
|
|
|
30 |
* matched by expr, then redefine the metadata type and run another $(expr) for other elements.
|
|
|
31 |
*
|
|
|
32 |
* @name $.metadata.setType
|
|
|
33 |
*
|
|
|
34 |
* @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
|
|
|
35 |
* @before $.metadata.setType("class")
|
|
|
36 |
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
|
|
|
37 |
* @desc Reads metadata from the class attribute
|
|
|
38 |
*
|
|
|
39 |
* @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
|
|
|
40 |
* @before $.metadata.setType("attr", "data")
|
|
|
41 |
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
|
|
|
42 |
* @desc Reads metadata from a "data" attribute
|
|
|
43 |
*
|
|
|
44 |
* @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
|
|
|
45 |
* @before $.metadata.setType("elem", "script")
|
|
|
46 |
* @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
|
|
|
47 |
* @desc Reads metadata from a nested script element
|
|
|
48 |
*
|
|
|
49 |
* @param String type The encoding type
|
|
|
50 |
* @param String name The name of the attribute to be used to get metadata (optional)
|
|
|
51 |
* @cat Plugins/Metadata
|
|
|
52 |
* @descr Sets the type of encoding to be used when loading metadata for the first time
|
|
|
53 |
* @type undefined
|
|
|
54 |
* @see metadata()
|
|
|
55 |
*/
|
|
|
56 |
|
|
|
57 |
(function($) {
|
|
|
58 |
|
|
|
59 |
$.extend({
|
|
|
60 |
metadata : {
|
|
|
61 |
defaults : {
|
|
|
62 |
type: 'class',
|
|
|
63 |
name: 'metadata',
|
|
|
64 |
cre: /({.*})/,
|
|
|
65 |
single: 'metadata'
|
|
|
66 |
},
|
|
|
67 |
setType: function( type, name ){
|
|
|
68 |
this.defaults.type = type;
|
|
|
69 |
this.defaults.name = name;
|
|
|
70 |
},
|
|
|
71 |
get: function( elem, opts ){
|
|
|
72 |
var settings = $.extend({},this.defaults,opts);
|
|
|
73 |
// check for empty string in single property
|
|
|
74 |
if ( !settings.single.length ) settings.single = 'metadata';
|
|
|
75 |
|
|
|
76 |
var data = $.data(elem, settings.single);
|
|
|
77 |
// returned cached data if it already exists
|
|
|
78 |
if ( data ) return data;
|
|
|
79 |
|
|
|
80 |
data = "{}";
|
|
|
81 |
|
|
|
82 |
if ( settings.type == "class" ) {
|
|
|
83 |
var m = settings.cre.exec( elem.className );
|
|
|
84 |
if ( m )
|
|
|
85 |
data = m[1];
|
|
|
86 |
} else if ( settings.type == "elem" ) {
|
|
|
87 |
if( !elem.getElementsByTagName )
|
|
|
88 |
return undefined;
|
|
|
89 |
var e = elem.getElementsByTagName(settings.name);
|
|
|
90 |
if ( e.length )
|
|
|
91 |
data = $.trim(e[0].innerHTML);
|
|
|
92 |
} else if ( elem.getAttribute != undefined ) {
|
|
|
93 |
var attr = elem.getAttribute( settings.name );
|
|
|
94 |
if ( attr )
|
|
|
95 |
data = attr;
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
if ( data.indexOf( '{' ) <0 )
|
|
|
99 |
data = "{" + data + "}";
|
|
|
100 |
|
|
|
101 |
data = eval("(" + data + ")");
|
|
|
102 |
|
|
|
103 |
$.data( elem, settings.single, data );
|
|
|
104 |
return data;
|
|
|
105 |
}
|
|
|
106 |
}
|
|
|
107 |
});
|
|
|
108 |
|
|
|
109 |
/**
|
|
|
110 |
* Returns the metadata object for the first member of the jQuery object.
|
|
|
111 |
*
|
|
|
112 |
* @name metadata
|
|
|
113 |
* @descr Returns element's metadata object
|
|
|
114 |
* @param Object opts An object contianing settings to override the defaults
|
|
|
115 |
* @type jQuery
|
|
|
116 |
* @cat Plugins/Metadata
|
|
|
117 |
*/
|
|
|
118 |
$.fn.metadata = function( opts ){
|
|
|
119 |
return $.metadata.get( this[0], opts );
|
|
|
120 |
};
|
|
|
121 |
|
|
|
122 |
})(jQuery);
|