diff options
author | Fx Nion <http://fxnion.free.fr> | 2012-05-13 19:56:15 +0200 |
---|---|---|
committer | Sem <sembrestels@riseup.net> | 2012-05-13 19:56:15 +0200 |
commit | b070daa95241bc0c7d46011a3de7674fbf0c5e68 (patch) | |
tree | 638f771d1708194ab4a1cfb9614b73cd9fbaca5f /vendors/jquery.metadata.js | |
download | elgg-b070daa95241bc0c7d46011a3de7674fbf0c5e68.tar.gz elgg-b070daa95241bc0c7d46011a3de7674fbf0c5e68.tar.bz2 |
v2.4
Diffstat (limited to 'vendors/jquery.metadata.js')
-rw-r--r-- | vendors/jquery.metadata.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/vendors/jquery.metadata.js b/vendors/jquery.metadata.js new file mode 100644 index 000000000..11527ce48 --- /dev/null +++ b/vendors/jquery.metadata.js @@ -0,0 +1,120 @@ +/* + * Metadata - jQuery plugin for parsing metadata from elements + * + * Copyright (c) 2006 John Resig, Yehuda Katz, Jörn Zaefferer + * + * 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$ + * + */ + +/** + * 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). The + * 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 $.meta.setType + * + * @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p> + * @before $.meta.setType("class") + * @after $("#one").data().item_id == 1; $("#one")[0].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 $.meta.setType("attr", "data") + * @after $("#one").data().item_id == 1; $("#one")[0].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 $.meta.setType("elem", "script") + * @after $("#one").data().item_id == 1; $("#one")[0].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 data() + */ + +(function($) { + // settings + $.meta = { + type: "class", + name: "data", + setType: function(type,name){ + this.type = type; + this.name = name; + }, + cre: /({.*})/, + single: 'data' + }; + + // reference to original setArray() + var setArray = $.fn.setArray; + + // define new setArray() + $.fn.setArray = function(arr){ + return setArray.apply( this, arguments ).each(function(){ + if ( this.nodeType == 9 || $.isXMLDoc(this) || this.metaDone ) return; + + var data = "{}"; + + if ( $.meta.type == "class" ) { + var m = $.meta.cre.exec( this.className ); + if ( m ) + data = m[1]; + } else if ( $.meta.type == "elem" ) { + if( !this.getElementsByTagName ) return; + var e = this.getElementsByTagName($.meta.name); + if ( e.length ) + data = $.trim(e[0].innerHTML); + } else if ( this.getAttribute != undefined ) { + var attr = this.getAttribute( $.meta.name ); + if ( attr ) + data = attr; + } + + if ( !/^{/.test( data ) ) + data = "{" + data + "}"; + + eval("data = " + data); + + if ( $.meta.single ) + this[ $.meta.single ] = data; + else + $.extend( this, data ); + + this.metaDone = true; + }); + }; + + /** + * Returns the metadata object for the first member of the jQuery object. + * + * @name data + * @descr Returns element's metadata object + * @type jQuery + * @cat Plugins/Metadata + */ + $.fn.data = function(){ + return this[0][$.meta.single || "data"]; + }; +})(jQuery);
\ No newline at end of file |