PluginProbe
Smart Grid-Layout Design for Contact Form 7 / 4.1.3
Smart Grid-Layout Design for Contact Form 7 v4.1.3
4.18.0 4.17.0 3.2.0 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4 3.3.5 3.3.6 3.3.7 3.3.8 4.0.0 4.0.1 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.10 4.11 4.12 4.13 4.14 All 119 releases
cf7-grid-layout / assets / jquery-toggles / js / Toggles.js

Toggles.js in Smart Grid-Layout Design for Contact Form 7 4.1.3, at assets/jquery-toggles/js/Toggles.js

249 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 var Toggles = root['Toggles'] = function(el, opts) {
2 var self = this;
3
4 if (typeof opts === 'boolean' && el.data('toggles')) {
5 el.data('toggles').toggle(opts);
6 return;
7 }
8
9 var dataAttr = [
10 'on',
11 'drag',
12 'click',
13 'width',
14 'height',
15 'animate',
16 'easing',
17 'type',
18 'checkbox'
19 ];
20 var dataOpts = {};
21 for (var i = 0; i < dataAttr.length; i++) {
22 var opt = el.data('toggle-' + dataAttr[i]);
23 if (typeof opt !== 'undefined') dataOpts[dataAttr[i]] = opt;
24 }
25
26 // extend default opts with the users options
27 opts = $.extend({
28 // can the toggle be dragged
29 'drag': true,
30 // can it be clicked to toggle
31 'click': true,
32 'text': {
33 // text for the ON/OFF position
34 'on': 'ON',
35 'off': 'OFF'
36 },
37 // is the toggle ON on init
38 'on': false,
39 // animation time (ms)
40 'animate': 250,
41 // animation transition easing function,
42 'easing': 'swing',
43 // the checkbox to toggle (for use in forms)
44 'checkbox': null,
45 // element that can be clicked on to toggle. removes binding from the toggle itself (use nesting)
46 'clicker': null,
47 // width (falls back to 50px)
48 'width': 0,
49 // height (falls back to 20px)
50 'height': 0,
51 // defaults to a compact toggle, other option is 'select' where both options are shown at once
52 'type': 'compact',
53 // the event name to fire when we toggle
54 'event': 'toggle'
55 }, opts || {}, dataOpts);
56
57 el.data('toggles', self);
58
59 // set active to the opposite of what we want, so toggle will run properly
60 var active = !opts['on'];
61
62 var selectType = opts['type'] === 'select';
63
64 // make checkbox a jquery element
65 var checkbox = $(opts['checkbox']);
66
67 var clicker = opts['clicker'] && $(opts['clicker']);
68
69 var height = opts['height'] || el.height() || 20;
70 var width = opts['width'] || el.width() || 50;
71
72 el.height(height);
73 el.width(width);
74
75 var div = function(name) {
76 return $('<div class="toggle-' + name + '">');
77 };
78
79 // wrapper inside toggle
80 var elSlide = div('slide');
81 // inside slide, this bit moves
82 var elInner = div('inner');
83 // the on/off divs
84 var elOn = div('on');
85 var elOff = div('off');
86 // the grip to drag the toggle
87 var elBlob = div('blob');
88
89 var halfHeight = height / 2;
90 var onOffWidth = width - halfHeight;
91
92 var text = opts['text'];
93
94 // set up the CSS for the individual elements
95 elOn
96 .css({
97 height: height,
98 width: onOffWidth,
99 textIndent: selectType ? '' : -height / 3,
100 lineHeight: height + 'px'
101 })
102 .html(text['on']);
103
104 elOff
105 .css({
106 height: height,
107 width: onOffWidth,
108 marginLeft: selectType ? '' : -halfHeight,
109 textIndent: selectType ? '' : height / 3,
110 lineHeight: height + 'px'
111 })
112 .html(text['off']);
113
114 elBlob.css({
115 height: height,
116 width: height,
117 marginLeft: -halfHeight
118 });
119
120 elInner.css({
121 width: width * 2 - height,
122 marginLeft: selectType ? 0 : -width + height
123 });
124
125 if (selectType) {
126 elSlide.addClass('toggle-select');
127 el.css('width', onOffWidth * 2);
128 elBlob.hide();
129 }
130
131 // construct the toggle
132 elInner.append(elOn, elBlob, elOff);
133 elSlide.html(elInner);
134 el.html(elSlide);
135
136 var doToggle = self.toggle = function(state, noAnimate, noEvent) {
137 // check we arent already in the desired state
138 if (active === state) return;
139
140 active = self['active'] = !active;
141
142 el.data('toggle-active', active);
143
144 elOff.toggleClass('active', !active);
145 elOn.toggleClass('active', active);
146 checkbox.prop('checked', active);
147
148 if (!noEvent) el.trigger(opts['event'], active);
149
150 if (selectType) return;
151
152 var margin = active ? 0 : -width + height;
153
154 // move the toggle!
155 elInner.stop().animate({
156 'marginLeft': margin
157 }, noAnimate ? 0 : opts['animate'], opts['easing']);
158 };
159
160
161 // evt handler for click events
162 var clickHandler = function(e) {
163 // if the target isn't the blob or dragging is disabled, toggle!
164 if (!el.hasClass('disabled') && (e['target'] !== elBlob[0] || !opts['drag'])) {
165 doToggle();
166 }
167 };
168
169 // if click is enabled and toggle isn't within the clicker element (stops double binding)
170 if (opts['click'] && (!clicker || !clicker.has(el).length)) {
171 el.on('click', clickHandler);
172 }
173
174 // setup the clicker element
175 if (clicker) {
176 clicker.on('click', clickHandler);
177 }
178
179 // bind up dragging stuff
180 if (opts['drag'] && !selectType) {
181 // time to begin the dragging parts/blob clicks
182 var diff;
183 var slideLimit = (width - height) / 4;
184
185 // fired on mouseup and mouseleave events
186 var upLeave = function(e) {
187 el.off('mousemove');
188 elSlide.off('mouseleave');
189 elBlob.off('mouseup');
190
191 if (!diff && opts['click'] && e.type !== 'mouseleave') {
192 doToggle();
193 return;
194 }
195
196 var overBound = active ? diff < -slideLimit : diff > slideLimit;
197 if (overBound) {
198 // dragged far enough, toggle
199 doToggle();
200 } else {
201 // reset to previous state
202 elInner.stop().animate({
203 marginLeft: active ? 0 : -width + height
204 }, opts['animate'] / 2, opts['easing']);
205 }
206 };
207
208 var wh = -width + height;
209
210 elBlob.on('mousedown', function(e) {
211
212 if (el.hasClass('disabled')) return;
213
214 // reset diff
215 diff = 0;
216
217 elBlob.off('mouseup');
218 elSlide.off('mouseleave');
219 var cursor = e.pageX;
220
221 el.on('mousemove', elBlob, function(e) {
222 diff = e.pageX - cursor;
223 var marginLeft;
224
225 if (active) {
226 marginLeft = diff;
227
228 // keep it within the limits
229 if (diff > 0) marginLeft = 0;
230 if (diff < wh) marginLeft = wh;
231 } else {
232 marginLeft = diff + wh;
233
234 if (diff < 0) marginLeft = wh;
235 if (diff > -wh) marginLeft = 0;
236 }
237
238 elInner.css('margin-left', marginLeft);
239 });
240
241 elBlob.on('mouseup', upLeave);
242 elSlide.on('mouseleave', upLeave);
243 });
244 }
245
246 // toggle the toggle to the correct state with no animation and no event
247 doToggle(opts['on'], true, true);
248 };
249