source: extensions/LCAS/trunk/admin/template/js/jquery.metadata.js @ 27186

Last change on this file since 27186 was 27186, checked in by LucMorizur, 10 years ago

Version 2.3.0, compatibility with Piwigo 2.6

File size: 3.7 KB
Line 
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 data, m, e, attr,
71                                settings = $.extend({},this.defaults,opts);
72                        // check for empty string in single property
73                        if ( !settings.single.length ) { settings.single = 'metadata'; }
74
75                        data = $.data(elem, settings.single);
76                        // returned cached data if it already exists
77                        if ( data ) { return data; }
78
79                        data = "{}";
80
81                        if ( settings.type === "class" ) {
82                                m = settings.cre.exec( elem.className );
83                                if ( m ) { data = m[1]; }
84                        } else if ( settings.type === "elem" ) {
85                                if( !elem.getElementsByTagName ) { return undefined; }
86                                e = elem.getElementsByTagName(settings.name);
87                                if ( e.length ) { data = $.trim(e[0].innerHTML); }
88                        } else if ( elem.getAttribute !== undefined ) {
89                                attr = elem.getAttribute( settings.name );
90                                if ( attr ) { data = attr; }
91                        }
92
93                        if ( data.indexOf( '{' ) <0 ) { data = "{" + data + "}"; }
94
95                        data = eval("(" + data + ")");
96
97                        $.data( elem, settings.single, data );
98                        return data;
99                }
100        }
101});
102
103/**
104 * Returns the metadata object for the first member of the jQuery object.
105 *
106 * @name metadata
107 * @descr Returns element's metadata object
108 * @param Object opts An object contianing settings to override the defaults
109 * @type jQuery
110 * @cat Plugins/Metadata
111 */
112$.fn.metadata = function( opts ){
113        return $.metadata.get( this[0], opts );
114};
115
116})(jQuery);
Note: See TracBrowser for help on using the repository browser.