This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.toggler.js
160 lines (144 loc) · 5.2 KB
/
jquery.toggler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/* global define, jQuery */
/**
* A simple jQuery Plugin for toggling the visibility of elements
*
* @author Tom Davies - Erskine Design
* @version 0.6.0
*/
(function (factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else {
// Browser globals
factory(jQuery);
}
}(function ($) {
var Toggler = function(el, opts) {
var $trigger = this.$trigger = $(el);
this.opts = opts;
this.targetSel = $trigger.attr('href') || $trigger.attr('data-target');
this.$target = $(this.targetSel);
this.group = opts.toggleGroup || $trigger.data('toggleGroup') ;
this.init();
};
Toggler.prototype = {
constructor: Toggler,
init: function() {
this.bind();
},
bind: function() {
var _self = this;
this.$trigger.off('change.toggler click.toggler');
this.$trigger.on('change.toggler', function(e){
_self.toggle();
});
if (this.$trigger.not('input[type=radio], input[type=checkbox], input[type=submit], select')){
this.$trigger.on('click.toggler', function(e){
e.preventDefault();
_self.toggle();
});
}
},
unbind: function(){
this.$trigger.off('change.toggler, click.toggler');
},
destroy: function(){
this.$trigger.removeData('toggler');
this.unbind();
},
toggle: function(e) {
return (this.targetIsVisible() ? this.hide() : this.show());
},
show: function(){
var opts = {
method: this.opts.methodIn,
speed: this.opts.speedIn,
beforeCallback: this.opts.beforeInCallback,
afterCallback: this.opts.afterInCallback,
classToRemove: this.opts.targetOutClass,
classToAdd: this.opts.targetInClass
};
//if this is in a toggle group, hide other instances matching
if(this.group){
$('[data-toggle-group="'+ this.group +'"]').toggler('hide');
}
this.__transition(this.$target, opts);
},
hide: function(){
var opts = {
method: this.opts.methodOut,
speed: this.opts.speedOut,
beforeCallback: this.opts.beforeOutCallback,
afterCallback: this.opts.afterOutCallback,
classToRemove: this.opts.targetInClass,
classToAdd: this.opts.targetOutClass
};
this.__transition(this.$target, opts);
},
__transition: function($el, opts){
var _self = this,
animClass = this.opts.targetAnimatingClass,
triggerClass = this.opts.triggerTargetInClass;
if ($.isFunction(opts.beforeCallback)){
opts.beforeCallback();
}
$el.removeClass(opts.classToRemove).addClass(animClass);
$el.stop()[opts.method](opts.speed, function(){
var triggerMethod;
//stopped animating, now in or out
$el.removeClass(animClass).addClass(opts.classToAdd);
//toggle the class on the trigger
triggerMethod = _self.targetIsVisible ? 'addClass' : 'removeClass';
_self.$trigger[triggerMethod](triggerClass);
if ($.isFunction(opts.afterCallback)){
opts.afterCallback();
}
});
},
targetIsVisible: function(){
return this.$target.is(':visible');
}
};
$.fn.toggler = function(option) {
var opts;
if (typeof option === 'object' && option) {
opts = $.extend(true, {}, $.fn.toggler.defaults, option);
} else {
opts = $.fn.toggler.defaults;
}
return this.each(function() {
var $this = $(this),
// don't call again if already initialised on this object
data = $this.data('toggler');
if(!data){
$this.data('toggler', data = new Toggler(this, opts));
}
// allow the calling of plugin methods on an instance by name, eg: $item.toggler('show')
if (typeof option === 'string') {
data[option]();
}
});
};
$.fn.toggler.defaults = {
methodIn: 'slideDown',
speedIn: 200,
methodOut: 'slideUp',
speedOut: 200,
targetInClass: 'toggler--in',
targetOutClass: 'toggler--out',
targetAnimatingClass: 'toggler--animating',
triggerTargetInClass: 'toggler--target-in'
};
$(document).on('change.toggler', '[data-toggle]', function(){
if(!$(this).data('toggler')){
$(this).toggler('toggle');
}
});
$(document).on('click.toggler', '[data-toggle]', function(e){
if(!$(this).data('toggler')){
e.preventDefault();
$(this).toggler('toggle');
}
});
}));