-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from uswitch/remove-jquery
Remove jQuery from ClassToggler and Overlay
- Loading branch information
Showing
7 changed files
with
188 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
Oops, something went wrong.