vendor/assets/javascripts/ui/gumby.toggleswitch.js in gumby-0.0.4 vs vendor/assets/javascripts/ui/gumby.toggleswitch.js in gumby-0.0.5

- old
+ new

@@ -27,43 +27,33 @@ } } // intialise toggles, switches will inherit method Toggle.prototype.init = function() { + this.targets = this.parseTargets(); + this.on = Gumby.selectAttr.apply(this.$el, ['on']) || Gumby.click; + var scope = this; - // set up module based on attributes - this.setup(); - // bind to specified event and trigger this.$el.on(this.on, function(e) { - // stop propagation - e.stopImmediatePropagation(); - // only disable default if <a> if($(this).prop('tagName') === 'A') { e.preventDefault(); } + // stop propagation + e.stopPropagation(); + scope.trigger(scope.triggered); // listen for gumby.trigger to dynamically trigger toggle/switch }).on('gumby.trigger', function() { scope.trigger(scope.triggered); - // re-initialize module - }).on('gumby.initialize', function() { - scope.setup(); }); }; - // set up module based on attributes - Toggle.prototype.setup = function() { - this.targets = this.parseTargets(); - this.on = Gumby.selectAttr.apply(this.$el, ['on']) || Gumby.click; - this.className = Gumby.selectAttr.apply(this.$el, ['classname']) || 'active'; - }; - // parse data-for attribute, switches will inherit method Toggle.prototype.parseTargets = function() { var targetStr = Gumby.selectAttr.apply(this.$el, ['trigger']), secondaryTargets = 0, targets = []; @@ -79,42 +69,53 @@ if(secondaryTargets === -1) { return [$(targetStr)]; } // return array of both targets, split and return 0, 1 - targets = targetStr.split('|'); + var targets = targetStr.split('|'); return targets.length > 1 ? [$(targets[0]), $(targets[1])] : [$(targets[0])]; }; // call triggered event and pass target data Toggle.prototype.triggered = function() { + var targetLength = this.targets.length, + // if no targets then use toggle/switch itself + targetData = !targetLength ? [this.$el.hasClass('active')] : [], + i; + + // loop round targets and store boolean indicating if selector is active + for(i = 0; i < targetLength; i++) { + targetData.push(this.targets[i].hasClass('active')); + } + // trigger gumby.onTrigger event and pass array of target status data - this.$el.trigger('gumby.onTrigger', [this.$el.hasClass(this.className)]); + this.$el.trigger('gumby.onTrigger', targetData); }; // Switch object inherits from Toggle Switch.prototype = new Toggle(); + Switch.constructor = Switch; // Toggle specific trigger method Toggle.prototype.trigger = function(cb) { // no targets just toggle active class on toggle if(!this.targets) { - this.$el.toggleClass(this.className); + this.$el.toggleClass('active'); // combine single target with toggle and toggle active class } else if(this.targets.length == 1) { - this.$el.add(this.targets[0]).toggleClass(this.className); + this.$el.add(this.targets[0]).toggleClass('active'); // if two targets check active state of first // always combine toggle and first target } else if(this.targets.length > 1) { - if(this.targets[0].hasClass(this.className)) { - this.$el.add(this.targets[0]).removeClass(this.className); - this.targets[1].addClass(this.className); + if(this.targets[0].hasClass('active')) { + this.$el.add(this.targets[0]).removeClass('active'); + this.targets[1].addClass('active'); } else { - this.targets[1].removeClass(this.className); - this.$el.add(this.targets[0]).addClass(this.className); + this.targets[1].removeClass('active'); + this.$el.add(this.targets[0]).addClass('active'); } } // call event handler here, applying scope of object Switch/Toggle if(cb && typeof cb === 'function') { @@ -124,67 +125,50 @@ // Switch specific trigger method Switch.prototype.trigger = function(cb) { // no targets just add active class to switch if(!this.targets) { - this.$el.addClass(this.className); + this.$el.addClass('active'); // combine single target with switch and add active class } else if(this.targets.length == 1) { - this.$el.add(this.targets[0]).addClass(this.className); + this.$el.add(this.targets[0]).addClass('active'); // if two targets check active state of first // always combine switch and first target } else if(this.targets.length > 1) { - this.$el.add(this.targets[0]).addClass(this.className); - this.targets[1].removeClass(this.className); + this.$el.add(this.targets[0]).addClass('active'); + this.targets[1].removeClass('active'); } // call event handler here, applying scope of object Switch/Toggle if(cb && typeof cb === 'function') { cb.apply(this); } }; // add toggle initialisation - Gumby.addInitalisation('toggles', function(all) { + Gumby.addInitalisation('toggles', function() { $('.toggle').each(function() { var $this = $(this); - // this element has already been initialized - // and we're only initializing new modules - if($this.data('isToggle') && !all) { + if($this.data('isToggle')) { return true; - - // this element has already been initialized - // and we need to reinitialize it - } else if($this.data('isToggle') && all) { - $this.trigger('gumby.initialize'); } - // mark element as initialized $this.data('isToggle', true); new Toggle($this); }); }); // add switches initialisation - Gumby.addInitalisation('switches', function(all) { + Gumby.addInitalisation('switches', function() { $('.switch').each(function() { var $this = $(this); - // this element has already been initialized - // and we're only initializing new modules - if($this.data('isSwitch') && !all) { + if($this.data('isSwitch')) { return true; - - // this element has already been initialized - // and we need to reinitialize it - } else if($this.data('isSwitch') && all) { - $this.trigger('gumby.initialize'); - return true; } - // mark element as initialized $this.data('isSwitch', true); new Switch($this); }); });