jquery.metadata.js 3.9 KB

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