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