PageSourceSearch

https://login.joinaff.com/resources/jquery.metadata.js

js joinaff.com collected 2026-09-26 05:24:46 UTC 3,868 bytes, 120 lines download raw bytes

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

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.