jquery.bridget.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /**
  2. * Bridget makes jQuery widgets
  3. * v1.0.1
  4. */
  5. ( function( window ) {
  6. 'use strict';
  7. // -------------------------- utils -------------------------- //
  8. var slice = Array.prototype.slice;
  9. function noop() {}
  10. // -------------------------- definition -------------------------- //
  11. function defineBridget( $ ) {
  12. // bail if no jQuery
  13. if ( !$ ) {
  14. return;
  15. }
  16. // -------------------------- addOptionMethod -------------------------- //
  17. /**
  18. * adds option method -> $().plugin('option', {...})
  19. * @param {Function} PluginClass - constructor class
  20. */
  21. function addOptionMethod( PluginClass ) {
  22. // don't overwrite original option method
  23. if(typeof PluginClass === "function"){
  24. if ( PluginClass.prototype.option ) {
  25. return;
  26. }
  27. // option setter
  28. PluginClass.prototype.option = function( opts ) {
  29. // bail out if not an object
  30. if ( !$.isPlainObject( opts ) ){
  31. return;
  32. }
  33. this.options = $.extend( true, this.options, opts );
  34. };
  35. } else {
  36. return;
  37. }
  38. }
  39. // -------------------------- plugin bridge -------------------------- //
  40. // helper function for logging errors
  41. // $.error breaks jQuery chaining
  42. var logError = typeof console === 'undefined' ? noop :
  43. function( message ) {
  44. console.error( message );
  45. };
  46. /**
  47. * jQuery plugin bridge, access methods like $elem.plugin('method')
  48. * @param {String} namespace - plugin name
  49. * @param {Function} PluginClass - constructor class
  50. */
  51. function bridge( namespace, PluginClass ) {
  52. // add to jQuery fn namespace
  53. $.fn[ namespace ] = function( options ) {
  54. if ( typeof options === 'string' ) {
  55. // call plugin method when first argument is a string
  56. // get arguments for method
  57. var args = slice.call( arguments, 1 );
  58. for ( var i=0, len = this.length; i < len; i++ ) {
  59. var elem = this[i];
  60. var instance = $.data( elem, namespace );
  61. if ( !instance ) {
  62. logError( "cannot call methods on " + namespace + " prior to initialization; " +
  63. "attempted to call '" + options + "'" );
  64. continue;
  65. }
  66. if ( !$.isFunction( instance[options] ) || options.charAt(0) === '_' ) {
  67. logError( "no such method '" + options + "' for " + namespace + " instance" );
  68. continue;
  69. }
  70. // trigger method with arguments
  71. var returnValue = instance[ options ].apply( instance, args );
  72. // break look and return first value if provided
  73. if ( returnValue !== undefined ) {
  74. return returnValue;
  75. }
  76. }
  77. // return this if no return value
  78. return this;
  79. } else {
  80. return this.each( function() {
  81. var instance = $.data( this, namespace );
  82. if ( instance ) {
  83. // apply options & init
  84. instance.option( options );
  85. instance._init();
  86. } else {
  87. // initialize new instance
  88. instance = new PluginClass( this, options );
  89. $.data( this, namespace, instance );
  90. }
  91. });
  92. }
  93. };
  94. }
  95. // -------------------------- bridget -------------------------- //
  96. /**
  97. * converts a Prototypical class into a proper jQuery plugin
  98. * the class must have a ._init method
  99. * @param {String} namespace - plugin name, used in $().pluginName
  100. * @param {Function} PluginClass - constructor class
  101. */
  102. $.bridget = function( namespace, PluginClass ) {
  103. addOptionMethod( PluginClass );
  104. bridge( namespace, PluginClass );
  105. };
  106. return $.bridget;
  107. }
  108. // transport
  109. if ( typeof define === 'function' && define.amd ) {
  110. // AMD
  111. define( [ 'jquery' ], defineBridget );
  112. } else {
  113. // get jquery from browser global
  114. defineBridget( window.jQuery );
  115. }
  116. })( window );