Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tooggle area plugin pro jQuery
/** @author Roman Ozana */
!function( $ ) {
// Zobrazeni skryte oblasti po kliknuti na element
var ToggleArea = function(element, options) {
this.init(element, options);
}
ToggleArea.prototype = {
constructor: ToggleArea
, // inicializace objektu
init: function(element, options) {
this.$element = $(element);
this.options = this.getOptions(options);
// zpracuje udalosti
if (this.$element.is(':checkbox')) {
this.$element.on('change', $.proxy(this.change, this)); // kliknuti na checkbox
} else {
this.$element.on('click', $.proxy(this.click, this)); // kliknuti na element
}
}
, // zpracuje udalost po zmene checkboxu
change: function( e ) {
var $area = $(this.options.area);
if (this.$element.is(':checked')) $area.show('fast'); else $area.hide('fast');
}
, // zpracuje udalost kliknuti na element
click: function( e ) {
var $area = $(this.options.area);
// odstranit element, ktery vyvolal udalost
if (this.options.hiddeButton) this.$element.hide();
// kliknuti na link u tlacitka muzem predat ID oblasti pomoci <a href="#oblast" />
$area.toggle('fast');
e.preventDefault();
}
, // ziska options pro element
getOptions: function( options ) {
// poskladam options jako merge defaults, uzivatelsky predanych hodnot a element dat
options = $.extend({}, $.fn.toggleArea.defaults, options, this.$element.data());
// pokud je elementem odkaz a neni nastavena area, vytahnu si hodnotu z href=""
if (this.$element.is('a') && options.area == null) {
options.area = this.$element.attr('href');
}
return options;
}
}
// tooltip plugin
$.fn.toggleArea = function( option ) {
return this.each(function(){
var $this = $(this)
// pokusim se ziskat data ulozena v data-toggleArea=""
, data = $this.data('toggleArea')
// pokud je option typu objekt, tak jej vlozim do options
, options = typeof option == 'object' && option
// pokud nejsou data jiz nastaveny vytvorim novy objekt
if (!data) $this.data('toggleArea', (data = new ToggleArea(this, options)));
if (typeof option == 'string') data[option]() // ???
})
}
// defaultni hodnoty
$.fn.toggleArea.defaults = {
area: null, // id oblasti, ktera se ma zobrazit (<span data-area="#oblast" />)
hiddeButton: false // skryt element, ktery vyvolal udalost
}
// constructor
$.fn.toggleArea.Constructor = ToggleArea
}( window.jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.