Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (function($){
- var oConfig = {
- };
- var sDataName = '';
- var sPluginName = '';
- var PluginMethods = {
- /**
- * initializing plugin
- * @param oOptions
- */
- init : function( oOptions ) {
- var oCurrentConfig = {};
- // if it's already inited, take current config
- if ( $(this).data( sDataName ) ) {
- oCurrentConfig = $(this).data( sDataName ).oOptions;
- }
- // overriding config params
- $.extend( oCurrentConfig, oConfig, oOptions || {} );
- if ( !$(this).data( sDataName ) ) {
- $(this).data( sDataName , new Plugin( $(this), oCurrentConfig ) );
- }
- $(this).data( sDataName ).init( oOptions );
- },
- destroy : function() {
- if ( $(this).data( sDataName ) ) {
- $(this).data( sDataName ).destroy();
- }
- },
- enable : function() {
- if ( $(this).data( sDataName ) ) {
- $(this).data( sDataName ).enable();
- }
- },
- disable : function() {
- if ( $(this).data( sDataName ) ) {
- $(this).data( sDataName ).disable();
- }
- }
- };
- var Plugin = function( $Node, oOptions ) {
- this.$Container = $Node;
- this.init = function() {
- };
- this.destroy = function () {};
- this.enable = function () {};
- this.disable = function () {};
- };
- $.fn[sPluginName] = function( /** arguments: method, options | options **/) {
- // default settings
- var sMethod = 'init', oOptions = {};
- // have a single param or nothing at all
- if ( arguments.length < 2 ) {
- // if it's a single param and it's a string -> it's a method name
- if ( arguments.length && typeof arguments[0] == 'string' ) {
- sMethod = arguments[0];
- // else if it's a single param and an object -> it's init options
- } else {
- oOptions = arguments[0];
- }
- // if number of params is 2 or greater
- } else if ( typeof arguments[0] == 'string' ) {
- // first param is a method name
- sMethod = arguments[0];
- // if second param is an object, consider it as an options
- if ( typeof arguments[1] == 'object' ) {
- oOptions = arguments[1];
- }
- }
- // if method exists, run it with computed options
- if ( PluginMethods.hasOwnProperty( sMethod ) && typeof PluginMethods[ sMethod ] == 'function' ) {
- return this.each( function() {
- return PluginMethods[ sMethod ].apply( this, [ oOptions ] );
- });
- } else {
- return this;
- }
- }
- })(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement