ultimix

- Main Page
- Data Structures
- Files
- Examples

- File List
- Globals

jquery.metadata.js
Go to the documentation of this file.
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
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
118 $.fn.metadata = function( opts ){
119 return $.metadata.get( this[0], opts );
120 };
121
122 })(jQuery);