vendor/assets/javascripts/ustyle/classtoggler.js in ustyle-1.16.1 vs vendor/assets/javascripts/ustyle/classtoggler.js in ustyle-1.16.2

- old
+ new

@@ -1,58 +1,76 @@ -window.ClassToggler = (function() { - var defaults; +window.ClassToggler = (function(Utils) { + var addClass = Utils.addClass; + var hasClass = Utils.hasClass; + var removeClass = Utils.removeClass; + var forEach = Utils.forEach; - defaults = { + var defaults = { containerClass: null, - $target: null, + target: null, activeClass: "active", inactiveClass: null, toggleOn: "click" }; + var findAncestor = function (el, cls) { + while ((el = el.parentElement) && ! hasClass(el, cls)); + return el; + } + function ClassToggler(options) { this.options = Utils.setOptions(options, defaults); - if (this.options.$target) { + + if (!this.options.target && this.options.$target && this.options.$target instanceof jQuery) { + this.options.target = document.querySelectorAll(this.options.$target.selector); + } + + if (this.options.target) { this.addEventListeners(); } else { console.trace("ClassToggle", this.options); } } ClassToggler.prototype.addEventListeners = function() { - return this.options.$target.on(this.options.toggleOn, (function(_this) { + var toggleEvent = this.options.toggleOn; + var onToggle = (function (_this) { return function(e) { - var $togglableElement = _this.options.containerClass ? $(e.target).closest(_this.options.containerClass) : $(e.delegateTarget); - if (_this.isActive($togglableElement)) { - return _this.hide($togglableElement, e); + var togglableElement = _this.options.containerClass ? findAncestor(e.target, _this.options.containerClass) : (e.delegateTarget); + if (_this.isActive(togglableElement)) { + return _this.hide(togglableElement, e); } else { - return _this.show($togglableElement, e); + return _this.show(togglableElement, e); } }; - })(this)); + })(this); + + forEach(this.options.target, function (i, t) { + t.addEventListener(toggleEvent, onToggle); + }); }; - ClassToggler.prototype.isActive = function($togglableElement) { - return $togglableElement.hasClass(this.options.activeClass); + ClassToggler.prototype.isActive = function(togglableElement) { + return hasClass(togglableElement, this.options.activeClass); }; - ClassToggler.prototype.show = function($togglableElement, e) { + ClassToggler.prototype.show = function(togglableElement, e) { var base; if (typeof (base = this.options).onShow === "function") { - base.onShow($togglableElement, e); + base.onShow(togglableElement, e); } - return $togglableElement.addClass(this.options.activeClass); + return togglableElement.addClass(this.options.activeClass); }; - ClassToggler.prototype.hide = function($togglableElement, e) { + ClassToggler.prototype.hide = function(togglableElement, e) { var base; if (typeof (base = this.options).onHide === "function") { - base.onHide($togglableElement, e); + base.onHide(togglableElement, e); } - return $togglableElement.removeClass(this.options.activeClass); + return removeClass(togglableElement, this.options.activeClass); }; return ClassToggler; -})(); +})(this.Utils);