123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- /**
- * Bridget makes jQuery widgets
- * v1.0.1
- */
- ( function( window ) {
- 'use strict';
- // -------------------------- utils -------------------------- //
- var slice = Array.prototype.slice;
- function noop() {}
- // -------------------------- definition -------------------------- //
- function defineBridget( $ ) {
- // bail if no jQuery
- if ( !$ ) {
- return;
- }
- // -------------------------- addOptionMethod -------------------------- //
- /**
- * adds option method -> $().plugin('option', {...})
- * @param {Function} PluginClass - constructor class
- */
- function addOptionMethod( PluginClass ) {
- // don't overwrite original option method
- if(typeof PluginClass === "function"){
- if ( PluginClass.prototype.option ) {
- return;
- }
- // option setter
- PluginClass.prototype.option = function( opts ) {
- // bail out if not an object
- if ( !$.isPlainObject( opts ) ){
- return;
- }
- this.options = $.extend( true, this.options, opts );
- };
- } else {
- return;
- }
- }
- // -------------------------- plugin bridge -------------------------- //
- // helper function for logging errors
- // $.error breaks jQuery chaining
- var logError = typeof console === 'undefined' ? noop :
- function( message ) {
- console.error( message );
- };
- /**
- * jQuery plugin bridge, access methods like $elem.plugin('method')
- * @param {String} namespace - plugin name
- * @param {Function} PluginClass - constructor class
- */
- function bridge( namespace, PluginClass ) {
- // add to jQuery fn namespace
- $.fn[ namespace ] = function( options ) {
- if ( typeof options === 'string' ) {
- // call plugin method when first argument is a string
- // get arguments for method
- var args = slice.call( arguments, 1 );
- for ( var i=0, len = this.length; i < len; i++ ) {
- var elem = this[i];
- var instance = $.data( elem, namespace );
- if ( !instance ) {
- logError( "cannot call methods on " + namespace + " prior to initialization; " +
- "attempted to call '" + options + "'" );
- continue;
- }
- if ( !$.isFunction( instance[options] ) || options.charAt(0) === '_' ) {
- logError( "no such method '" + options + "' for " + namespace + " instance" );
- continue;
- }
- // trigger method with arguments
- var returnValue = instance[ options ].apply( instance, args );
- // break look and return first value if provided
- if ( returnValue !== undefined ) {
- return returnValue;
- }
- }
- // return this if no return value
- return this;
- } else {
- return this.each( function() {
- var instance = $.data( this, namespace );
- if ( instance ) {
- // apply options & init
- instance.option( options );
- instance._init();
- } else {
- // initialize new instance
- instance = new PluginClass( this, options );
- $.data( this, namespace, instance );
- }
- });
- }
- };
- }
- // -------------------------- bridget -------------------------- //
- /**
- * converts a Prototypical class into a proper jQuery plugin
- * the class must have a ._init method
- * @param {String} namespace - plugin name, used in $().pluginName
- * @param {Function} PluginClass - constructor class
- */
- $.bridget = function( namespace, PluginClass ) {
- addOptionMethod( PluginClass );
- bridge( namespace, PluginClass );
- };
- return $.bridget;
- }
- // transport
- if ( typeof define === 'function' && define.amd ) {
- // AMD
- define( [ 'jquery' ], defineBridget );
- } else {
- // get jquery from browser global
- defineBridget( window.jQuery );
- }
- })( window );
|