jquery.metadata.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 3640 2007-10-11 18:34:38Z pmclanahan $
  11. *
  12. */
  13. /**
  14. * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
  15. * in the JSON will become a property of the element itself.
  16. *
  17. * There are four supported types of metadata storage:
  18. *
  19. * attr: Inside an attribute. The name parameter indicates *which* attribute.
  20. *
  21. * class: Inside the class attribute, wrapped in curly braces: { }
  22. *
  23. * elem: Inside a child element (e.g. a script tag). The
  24. * name parameter indicates *which* element.
  25. * html5: Values are stored in data-* attributes.
  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. * @example <p id="one" class="some_class" data-item_id="1" data-item_label="Label">This is a p</p>
  50. * @before $.metadata.setType("html5")
  51. * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
  52. * @desc Reads metadata from a series of data-* attributes
  53. *
  54. * @param String type The encoding type
  55. * @param String name The name of the attribute to be used to get metadata (optional)
  56. * @cat Plugins/Metadata
  57. * @descr Sets the type of encoding to be used when loading metadata for the first time
  58. * @type undefined
  59. * @see metadata()
  60. */
  61. (function($) {
  62. $.extend({
  63. metadata : {
  64. defaults : {
  65. type: 'class',
  66. name: 'metadata',
  67. cre: /({.*})/,
  68. single: 'metadata'
  69. },
  70. setType: function( type, name ){
  71. this.defaults.type = type;
  72. this.defaults.name = name;
  73. },
  74. get: function( elem, opts ){
  75. var settings = $.extend({},this.defaults,opts);
  76. // check for empty string in single property
  77. if ( !settings.single.length ) settings.single = 'metadata';
  78. var data = $.data(elem, settings.single);
  79. // returned cached data if it already exists
  80. if ( data ) return data;
  81. data = "{}";
  82. var getData = function(data) {
  83. if(typeof data != "string") return data;
  84. if( data.indexOf('{') < 0 ) {
  85. data = eval("(" + data + ")");
  86. }
  87. };
  88. var getObject = function(data) {
  89. if(typeof data != "string") return data;
  90. data = eval("(" + data + ")");
  91. return data;
  92. };
  93. if ( settings.type == "html5" ) {
  94. var object = {};
  95. $( elem.attributes ).each(function() {
  96. var name = this.nodeName;
  97. if(name.match(/^data-/)) name = name.replace(/^data-/, '');
  98. else return true;
  99. object[name] = getObject(this.nodeValue);
  100. });
  101. } else {
  102. if ( settings.type == "class" ) {
  103. var m = settings.cre.exec( elem.className );
  104. if ( m )
  105. data = m[1];
  106. } else if ( settings.type == "elem" ) {
  107. if( !elem.getElementsByTagName ) return;
  108. var e = elem.getElementsByTagName(settings.name);
  109. if ( e.length )
  110. data = $.trim(e[0].innerHTML);
  111. } else if ( elem.getAttribute != undefined ) {
  112. var attr = elem.getAttribute( settings.name );
  113. if ( attr )
  114. data = attr;
  115. }
  116. object = getObject(data.indexOf("{") < 0 ? "{" + data + "}" : data);
  117. }
  118. $.data( elem, settings.single, object );
  119. return object;
  120. }
  121. }
  122. });
  123. /**
  124. * Returns the metadata object for the first member of the jQuery object.
  125. *
  126. * @name metadata
  127. * @descr Returns element's metadata object
  128. * @param Object opts An object contianing settings to override the defaults
  129. * @type jQuery
  130. * @cat Plugins/Metadata
  131. */
  132. $.fn.metadata = function( opts ){
  133. return $.metadata.get( this[0], opts );
  134. };
  135. })(jQuery);