jquery.metadata.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //*********** JQUERY METADATA **************************************************
  3. ////////////////////////////////////////////////////////////////////////////////
  4. /*
  5. * Metadata - jQuery plugin for parsing metadata from elements
  6. *
  7. * Copyright (c) 2006 John Resig, Yehuda Katz, J�örn Zaefferer, Paul McLanahan
  8. *
  9. * Dual licensed under the MIT and GPL licenses:
  10. * http://www.opensource.org/licenses/mit-license.php
  11. * http://www.gnu.org/licenses/gpl.html
  12. *
  13. * Revision: $Id: jquery.metadata.js,v 1.1 2009/10/01 18:28:20 ccheng Exp $
  14. *
  15. */
  16. /**
  17. * Sets the type of metadata to use. Metadata is encoded in JSON, and each property
  18. * in the JSON will become a property of the element itself.
  19. *
  20. * There are three supported types of metadata storage:
  21. *
  22. * attr: Inside an attribute. The name parameter indicates *which* attribute.
  23. *
  24. * class: Inside the class attribute, wrapped in curly braces: { }
  25. *
  26. * elem: Inside a child element (e.g. a script tag). The
  27. * name parameter indicates *which* element.
  28. *
  29. * The metadata for an element is loaded the first time the element is accessed via jQuery.
  30. *
  31. * As a result, you can define the metadata type, use $(expr) to load the metadata into the elements
  32. * matched by expr, then redefine the metadata type and run another $(expr) for other elements.
  33. *
  34. * @name $.metadata.setType
  35. *
  36. * @example <p id="one" class="some_class {item_id: 1, item_label: 'Label'}">This is a p</p>
  37. * @before $.metadata.setType("class")
  38. * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
  39. * @desc Reads metadata from the class attribute
  40. *
  41. * @example <p id="one" class="some_class" data="{item_id: 1, item_label: 'Label'}">This is a p</p>
  42. * @before $.metadata.setType("attr", "data")
  43. * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
  44. * @desc Reads metadata from a "data" attribute
  45. *
  46. * @example <p id="one" class="some_class"><script>{item_id: 1, item_label: 'Label'}</script>This is a p</p>
  47. * @before $.metadata.setType("elem", "script")
  48. * @after $("#one").metadata().item_id == 1; $("#one").metadata().item_label == "Label"
  49. * @desc Reads metadata from a nested script element
  50. *
  51. * @param String type The encoding type
  52. * @param String name The name of the attribute to be used to get metadata (optional)
  53. * @cat Plugins/Metadata
  54. * @descr Sets the type of encoding to be used when loading metadata for the first time
  55. * @type undefined
  56. * @see metadata()
  57. */
  58. (function($) {
  59. $.extend({
  60. metadata : {
  61. defaults : {
  62. type: 'class',
  63. name: 'metadata',
  64. //cre: /({.*})/,
  65. cre: /({[\s\S]*})/,
  66. single: 'metadata'
  67. },
  68. setType: function( type, name ){
  69. this.defaults.type = type;
  70. this.defaults.name = name;
  71. },
  72. get: function( elem, opts ){
  73. var settings = $.extend({},this.defaults,opts);
  74. // check for empty string in single property
  75. if ( !settings.single.length ) settings.single = 'metadata';
  76. var data = $.data(elem, settings.single);
  77. // returned cached data if it already exists
  78. if ( data ) return data;
  79. data = "{}";
  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. if ( data.indexOf( '{' ) <0 )
  96. data = "{" + data + "}";
  97. data = eval("(" + data + ")");
  98. $.data( elem, settings.single, data );
  99. return data;
  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. })(jQuery);