-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.switcher.js
369 lines (310 loc) · 10.3 KB
/
jquery.switcher.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*!
* jQuery switcher plugin
* Original author: @Okotetto (Mateusz Wróbel - [email protected])
* Licensed under the MIT license
*/
;(function ( $, window, document, undefined ) {
/**
* OPTIONS:
* animationTime (string) animation time in miliseconds
* customClass (string) name for additional class for switcher
* cssPrefix (string) prefix for css classes for switcher
* dragable (boolean)
* onlyLeftClick: (boolean) allow drag/click only for left mouse button
* position: (string) possible values: vertical, horizontal - for setting handler position
* swapOnOff: (boolean)
* useAnimation: (boolean)
* toggled (function) callback function is called when toggle event is triggered
* toggledOn: (function) callback function is called when toggleOn event is triggered
* toggledOff: (function) callback function is called when toggleOff event is triggered
*/
// Create the defaults once
var pluginName = "switcher";
var defaults = {
animationTime: 100,
customClass: '',
cssPrefix: "ui-",
displayHandler: true,
displayOnAndOff: false,
dragable: true,
onlyLeftClick: true,
position: 'horizontal',
swapOnOff: false,
useAnimation: true,
toggled: function () { },
toggledOn: function () { },
toggledOff: function () { }
};
// The actual plugin constructor
function Switcher( element, options ) {
this.element = element;
this.options = $.extend( {}, defaults, options );
this.init();
}
// create whole switcher instance
Switcher.prototype.init = function () {
if ( this.element.type !== 'checkbox' ) {
throw( 'Chosen element is not checkbox. This plugin can be assign only to checkboxes' );
}
var checkbox = this.element;
var options = this.options;
// create switcher elements
var switcher = document.createElement( 'a' );
var handler = ( options.displayHandler === true ) ? document.createElement( 'span' ) : null;
var switchOnBackground = ( options.displayOnAndOff === true ) ? document.createElement( 'span' ) : null;
var switchOffBackground = ( options.displayOnAndOff === true ) ? document.createElement( 'span' ) : null;
// flags
var isHorizontal = ( options.position === 'horizontal' ) ? true : false;
var isVertical = ( options.position === 'vertical' ) ? true : false;
var isSwaped = ( options.swapOnOff === true ) ? true : false;
if ( !isHorizontal && !isVertical ) {
this.options.position = 'horizontal';
isHorizontal = true;
}
// function vars
var allowOnlyLeftClick = function ( event ) {
if ( options.onlyLeftClick ) {
if ( event.button !== 0 && !( $.browser.msie && event.button === 1 ) ) {
return true;
}
}
return false;
};
// check checkbox state
// if swapOnOff option is false it return true for checked
// if swapOnOff option is true it return false for checked
//
// if true switcher state is set to on
// else state is set to off
var isOn = function () {
return $( checkbox ).is( ':checked' );
};
// set checkbox state to on
// if swapOnOff is true it unset checked attribute
// else it set cheched attribute to true
var setToOn = function () {
$( checkbox ).attr( 'checked', 'checked' );
};
// set checkbox state to off
// if swapOnOff is true it set checked attribute to true
// else it unset cheched attribute
var setToOff = function () {
$( checkbox ).removeAttr( 'checked' );
};
// set class to plugin elements
// it add prefix to each class name
var setClassForElement = function ( element, className ) {
element.className = options.cssPrefix + className ;
};
// set classes for new elements
setClassForElement( switcher, 'switcher' );
if ( options.customClass.length > 0 ) {
$( switcher ).addClass( options.customClass );
}
setClassForElement( switchOnBackground, 'switcher-background-on' );
setClassForElement( switchOffBackground, 'switcher-background-off' );
setClassForElement( handler, 'switcher-handler' );
// append to DOM new switcher
// fire update event to set switch in right place
$( checkbox )
.after( $( switcher )
.append( switchOnBackground )
.append( handler )
.append( switchOffBackground )
.attr( 'tabindex', 1 )
)
.hide();
var switcherDimension = ( isHorizontal ) ? parseInt( $( switcher ).width(), 10 ) : parseInt( $( switcher ).height(), 10 );
var handlerDimension = ( isHorizontal ) ? parseInt( $( handler ).width(), 10 ) : parseInt( $( handler ).height(), 10 );
var attrToUse = ( isHorizontal ) ? 'left' : 'top';
// declare events for checkbox
checkbox.events = {
toggle: function () {
if ( isOn() ) {
setToOff();
options.toggledOff.call( checkbox );
} else {
setToOn();
options.toggledOn.call( checkbox );
}
$( checkbox ).trigger( 'update' );
options.toggled.call( checkbox );
},
toggleOn: function () {
if ( !isOn() ) {
setToOn();
options.toggledOn.call( checkbox );
options.toggled.call( checkbox );
}
$( checkbox ).trigger( 'update' );
},
toggleOff: function () {
if ( isOn() ) {
setToOff();
options.toggledOff.call( checkbox );
options.toggled.call( checkbox );
}
$( checkbox ).trigger( 'update' );
},
update: function ( o, disableAnimation ) {
var animateObject = {};
if ( isOn() ) {
animateObject[ attrToUse ] = ( !isSwaped ) ? switcherDimension - handlerDimension + 'px' : '0px';
// $( switchOnBackground ).show( options.useAnimation && !disableAnimation ? options.animationTime : 0 );
$( handler ).animate( animateObject, options.useAnimation && !disableAnimation ? options.animationTime : 0);
} else {
animateObject[ attrToUse ] = ( !isSwaped ) ? '0px' : switcherDimension - handlerDimension + 'px';
// $( switchOnBackground ).hide( options.useAnimation && !disableAnimation ? options.animationTime : 0);
$( handler ).animate( animateObject, options.useAnimation && !disableAnimation ? options.animationTime : 0 );
}
}
};
$( checkbox )
// bind events to checkbox
.on( checkbox.events )
.trigger( 'update', true );
// when switcher is selected hit space to change state
$( switcher ).on( 'keypress', function ( event ) {
if ( event.which === 32 ) {
event.preventDefault();
$( checkbox ).trigger( 'toggle' );
}
} );
// declare events for switcher
if ( !options.dragable ) {
switcher.events = {
click: function ( event ) {
event.preventDefault();
// check if only left click is allowed
// if yes and that was not left click return false
if ( allowOnlyLeftClick( event ) ) {
return false;
}
$( checkbox ).trigger( 'toggle' );
}
};
// bind events to switcher
$( switcher ).on( switcher.events );
} else {
// helper var for dragging
// key offsets
var minOffset = 0;
var maxOffset = parseInt( switcherDimension - handlerDimension, 10 );
var centerOffset = parseInt( switcherDimension / 2 - handlerDimension / 2, 10 );
// position offsets for dragging
var startOffset = 0;
var handlerPosition = 0;
var newPosition = 0;
// handler is dragged
var isDragging = false;
// on start dragging
var startDrag = function ( event ) {
// check if only left click is allowed
// if yes and that was not left click return false
if ( allowOnlyLeftClick( event ) ) {
return false;
}
$( document ).on( {
mousemove: switcher.events.mousemove,
mouseup: switcher.events.mouseup,
touchmove: switcher.events.touchmove,
touchend: switcher.events.touchend
} );
if ( isHorizontal ) {
startOffset = event.pageX;
} else {
startOffset = event.pageY;
}
handlerPosition = parseInt( $( handler ).css( attrToUse ), 10 );
};
// on dragging handler
var dragging = function ( event ) {
var currentOffset = 0;
isDragging = true;
if ( isHorizontal ) {
currentOffset = event.pageX;
} else {
currentOffset = event.pageY;
}
newPosition = handlerPosition + currentOffset - startOffset;
if ( newPosition >= minOffset && newPosition <= maxOffset) {
var positionObject = {};
positionObject[ attrToUse ] = newPosition + 'px';
$( handler ).css( positionObject );
}
};
// on end of dragging
var endDrag = function () {
$( document ).off( 'mouseup mousemove touchmove touchend' );
if ( isDragging ) {
if (
( newPosition <= centerOffset && !isSwaped ) ||
( newPosition > centerOffset && isSwaped )
) {
$( checkbox ).trigger ( 'toggleOff' );
} else {
$( checkbox ).trigger ( 'toggleOn' );
}
} else {
$( checkbox ).trigger( 'toggle' );
}
isDragging = false;
};
// declare switcher events
switcher.events = {
mousedown: startDrag,
mousemove: dragging,
mouseup: endDrag,
mouseleave: endDrag,
touchstart: function ( event ) {
event.preventDefault();
$( document ).off( 'mousedown' );
startDrag( event.touches[ 0 ] );
},
touchmove: function ( event ) {
event.preventDefault();
$( document ).off( 'mousemove' );
dragging( event.touches[ 0 ] );
},
touchend: function ( event ) {
event.preventDefault();
$( document ).off( 'mouseup' );
endDrag( event.touches[ 0 ] );
}
};
// bind events to switcher
$( switcher ).on( {
mousedown: switcher.events.mousedown,
touchstart: switcher.events.touchstart
} );
} // end of dragable
};
Switcher.prototype.toggle = function () {
$( this ).trigger( 'toggle' );
};
Switcher.prototype.toggleOn = function () {
$( this ).trigger( 'toggleOn' );
};
Switcher.prototype.toggleOff = function () {
$( this ).trigger( 'toggleOff' );
};
// A really lightweight plugin wrapper around the constructor,
// preventing against multiple instantiations
$.fn[ pluginName ] = function ( options ) {
var nameForData = "plugin_" + pluginName;
var method = ( typeof options === 'string' ) ? options : '';
return this.each( function () {
var switcherInstantion = $.data( this, nameForData );
var createSwitcher = function ( element ) {
return new Switcher( element, options );
};
if ( !switcherInstantion ) {
$.data( this, nameForData, createSwitcher( this ) );
} else if ( switcherInstantion[ method ] ) {
switcherInstantion[ method ].call( this );
} else {
throw( 'Method ' + method + ' does not exist' );
}
} );
};
})( jQuery, window, document );