Search code examples
javascriptjqueryjquery-pluginsresetdestroy

jQuery creating Destory & reset


Hey all I am new at jQuery plugins and therefore am unsure how to go about adding a destroy and reset (where I can bring it back) to this plugin I am currently using for tooltips.

The JS code is this:

/**
* This is a simple jQuery plugin that make nice tooltips.
*
* @class ssTooltips
* @author Jacek Berbecki
*/
;(function($) {
    'use strict';
    $.ssTooltips = {version: '1.0.0'};
    $.fn.ssTooltips = function(element, options) {

        // set tooltip options
        var settings = $.extend({
            bgColor: '#333',
            txtColor: '#f2f2f2',
            maxWidth: 200,
            borderRadius: 3,
            fontSize: 12
        }, options);

        // get elements
        var elements = $(element);

        // start tooltip engine when elements exists
        if(elements && elements.length > 0) {

            // cteare tootlip element
            var tooltipWrapper = $('<div id="tooltip-wrapper"></div>'),
                tooltipBox = $('<div id="tooltip-box"></div>'),
                tooltipArrow = $('<div id="tooltip-arrow"></div>');

            // set tooltop element styles
            tooltipWrapper.css({
                'display': 'none',
                'position': 'absolute',
                'opacity': '0.95'
            });
            tooltipBox.css({
                'background': settings.bgColor,
                'padding': '5px 15px',
                'color': settings.txtColor,
                'border-radius': settings.borderRadius + 'px',
                'box-shadow': '0 2px 6px -1px rgba(0,0,0,0.3)',
                'max-width': settings.maxWidth + 'px',
                'font-size': settings.fontSize + 'px'
            });
            tooltipArrow.css({
                'width': '10px',
                'height': '10px',
                'background': settings.bgColor,
                'position': 'absolute',
                'left': '16px',
                'bottom': '-4px',
                'transform': 'rotate(45deg)'
            });

            // append tooltop to document
            tooltipBox.appendTo(tooltipWrapper);
            tooltipArrow.appendTo(tooltipWrapper);
            $('body').append(tooltipWrapper);

            // fire tooltip mouse actions
            elements.each(function(index, element) {
                var $this = $(this),
                    dataTxt = $this.attr('data-tooltip');
                $this.removeAttr('title');

                $this.on({
                    mousemove: function(event) {
                        tooltipWrapper
                            .css({
                                'left': event.pageX - 20,
                                'bottom': ($( window ).height() - event.pageY + 20)  
                            })
                    },
                    mouseenter: function(event) {
                        tooltipWrapper
                            .hide()
                            .fadeIn('fast');
                        tooltipBox
                            .empty()
                            .html(dataTxt);
                    },
                    mouseleave: function(event) {
                        tooltipWrapper
                            .stop()
                            .fadeOut('fast');
                    }
                })
            });

        } else {
            return false;
        }
    }
}(jQuery));

And as you may see, there is no destroy, delete, etc in there.

The purpose to all of this is for me to disable the tooltips on the page until I press a button to show them then if I pressed the button again, they would get destroyed again.

I see a few examples of the destroy function found here:

destroy: function() {
    this._destroy(); //or this.delete; depends on jQuery version
    this.element.unbind( this.eventNamespace )
    this.bindings.unbind( this.eventNamespace );
    //this.hoverable.removeClass( "hover state" );
    //this.focusable.removeClass( "focus state" );
}

But am unsure how to implement it in the current code. Same with Destory.

And help would be great!


Solution

  • I got it!

    /**
    * This is a simple jQuery plugin that make nice tooltips.
    *
    * @class ssTooltips
    * @author Jacek Berbecki
    */
    ; (function ($) {
        'use strict';
        $.ssTooltips = { version: '1.0.0' };
        $.fn.ssTooltips = function (element, options) {
    
            // set tooltip options
            var settings = $.extend({
                bgColor: '#333',
                txtColor: '#f2f2f2',
                maxWidth: 200,
                borderRadius: 3,
                fontSize: 12
            }, options);
    
            // get elements
            var elements = $(element);
    
            // start tooltip engine when elements exists
            if (elements && elements.length > 0) {
    
                // cteare tootlip element
                var tooltipWrapper = $('<div id="tooltip-wrapper"></div>'),
                    tooltipBox = $('<div id="tooltip-box"></div>'),
                    tooltipArrow = $('<div id="tooltip-arrow"></div>');
    
                // set tooltop element styles
                tooltipWrapper.css({
                    'display': 'none',
                    'position': 'absolute',
                    'opacity': '0.95',
                    'z-index': 8
                });
                tooltipBox.css({
                    'background': settings.bgColor,
                    'padding': '5px 15px',
                    'color': settings.txtColor,
                    'border-radius': settings.borderRadius + 'px',
                    'box-shadow': '0 2px 6px -1px rgba(0,0,0,0.3)',
                    'max-width': settings.maxWidth + 'px',
                    'font-size': settings.fontSize + 'px'
                });
                tooltipArrow.css({
                    'width': '10px',
                    'height': '10px',
                    'background': settings.bgColor,
                    'position': 'absolute',
                    'left': '16px',
                    'bottom': '-4px',
                    'transform': 'rotate(45deg)'
                });
    
                // append tooltop to document
                tooltipBox.appendTo(tooltipWrapper);
                tooltipArrow.appendTo(tooltipWrapper);
                $('body').append(tooltipWrapper);
    
                // fire tooltip mouse actions
                elements.each(function (index, element) {
                    var $this = $(this),
                        dataTxt = $this.attr('data-tooltip');
                    $this.removeAttr('title');
    
                    $this.on({
                        mousemove: function (event) {
                            tooltipWrapper
                                .css({
                                    'left': event.pageX - 20,
                                    'bottom': ($(window).height() - event.pageY + 20)
                                })
                        },
                        mouseenter: function (event) {
                            tooltipWrapper
                                .hide()
                                .fadeIn('fast');
                            tooltipBox
                                .empty()
                                .html(dataTxt);
                        },
                        mouseleave: function (event) {
                            tooltipWrapper
                                .stop()
                                .fadeOut('fast');
                        },
                        mousedown: function (event) {
                            tooltipWrapper
                                .stop()
                                .fadeOut('fast');
                        }
                    })
                });
    
                $.fn.ssTooltips.destroy = function () {
                    $('#tooltip-wrapper').remove();
                }
    
                $.fn.ssTooltips.reset = function () {
                    $(document).ssTooltips('.tips', {
                        //Controls the tooltips for examples for text/select boxes
                        bgColor: settings.bgColor, 
                        txtColor: settings.txtColor,
                        maxWidth: settings.maxWidth,
                        borderRadius: settings.borderRadius,
                        fontSize: settings.fontSize
                    });
                }
            } else {
                return false;
            }
        }
    }(jQuery));
    

    I created $.fn.ssTooltips.destroy and $.fn.ssTooltips.reset and I am calling them like so:

    $('#tool_destory').on('click', function (e) {
        //Destory the tool tips
        $('#tooltip-wrapper').ssTooltips.destroy();
    });
    
    $('#tool_addback').on('click', function (e) {
        //Add tool tips
        $('#tooltip-wrapper').ssTooltips.reset();
    });