LaravelTest
784 строки · 25.7 Кб
1/**
2* bootstrap-switch - Turn checkboxes and radio buttons into toggle switches.
3*
4* @version v3.3.4
5* @homepage https://bttstrp.github.io/bootstrap-switch
6* @author Mattia Larentis <mattia@larentis.eu> (http://larentis.eu)
7* @license Apache-2.0
8*/
9
10(function (global, factory) {11if (typeof define === "function" && define.amd) {12define(['jquery'], factory);13} else if (typeof exports !== "undefined") {14factory(require('jquery'));15} else {16var mod = {17exports: {}18};19factory(global.jquery);20global.bootstrapSwitch = mod.exports;21}22})(this, function (_jquery) {23'use strict';24
25var _jquery2 = _interopRequireDefault(_jquery);26
27function _interopRequireDefault(obj) {28return obj && obj.__esModule ? obj : {29default: obj30};31}32
33var _extends = Object.assign || function (target) {34for (var i = 1; i < arguments.length; i++) {35var source = arguments[i];36
37for (var key in source) {38if (Object.prototype.hasOwnProperty.call(source, key)) {39target[key] = source[key];40}41}42}43
44return target;45};46
47function _classCallCheck(instance, Constructor) {48if (!(instance instanceof Constructor)) {49throw new TypeError("Cannot call a class as a function");50}51}52
53var _createClass = function () {54function defineProperties(target, props) {55for (var i = 0; i < props.length; i++) {56var descriptor = props[i];57descriptor.enumerable = descriptor.enumerable || false;58descriptor.configurable = true;59if ("value" in descriptor) descriptor.writable = true;60Object.defineProperty(target, descriptor.key, descriptor);61}62}63
64return function (Constructor, protoProps, staticProps) {65if (protoProps) defineProperties(Constructor.prototype, protoProps);66if (staticProps) defineProperties(Constructor, staticProps);67return Constructor;68};69}();70
71var $ = _jquery2.default || window.jQuery || window.$;72
73var BootstrapSwitch = function () {74function BootstrapSwitch(element) {75var _this = this;76
77var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};78
79_classCallCheck(this, BootstrapSwitch);80
81this.$element = $(element);82this.options = $.extend({}, $.fn.bootstrapSwitch.defaults, this._getElementOptions(), options);83this.prevOptions = {};84this.$wrapper = $('<div>', {85class: function _class() {86var classes = [];87classes.push(_this.options.state ? 'on' : 'off');88if (_this.options.size) {89classes.push(_this.options.size);90}91if (_this.options.disabled) {92classes.push('disabled');93}94if (_this.options.readonly) {95classes.push('readonly');96}97if (_this.options.indeterminate) {98classes.push('indeterminate');99}100if (_this.options.inverse) {101classes.push('inverse');102}103if (_this.$element.attr('id')) {104classes.push('id-' + _this.$element.attr('id'));105}106return classes.map(_this._getClass.bind(_this)).concat([_this.options.baseClass], _this._getClasses(_this.options.wrapperClass)).join(' ');107}108});109this.$container = $('<div>', { class: this._getClass('container') });110this.$on = $('<span>', {111html: this.options.onText,112class: this._getClass('handle-on') + ' ' + this._getClass(this.options.onColor)113});114this.$off = $('<span>', {115html: this.options.offText,116class: this._getClass('handle-off') + ' ' + this._getClass(this.options.offColor)117});118this.$label = $('<span>', {119html: this.options.labelText,120class: this._getClass('label')121});122
123this.$element.on('init.bootstrapSwitch', this.options.onInit.bind(this, element));124this.$element.on('switchChange.bootstrapSwitch', function () {125for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {126args[_key] = arguments[_key];127}128
129if (_this.options.onSwitchChange.apply(element, args) === false) {130if (_this.$element.is(':radio')) {131$('[name="' + _this.$element.attr('name') + '"]').trigger('previousState.bootstrapSwitch', true);132} else {133_this.$element.trigger('previousState.bootstrapSwitch', true);134}135}136});137
138this.$container = this.$element.wrap(this.$container).parent();139this.$wrapper = this.$container.wrap(this.$wrapper).parent();140this.$element.before(this.options.inverse ? this.$off : this.$on).before(this.$label).before(this.options.inverse ? this.$on : this.$off);141
142if (this.options.indeterminate) {143this.$element.prop('indeterminate', true);144}145
146this._init();147this._elementHandlers();148this._handleHandlers();149this._labelHandlers();150this._formHandler();151this._externalLabelHandler();152this.$element.trigger('init.bootstrapSwitch', this.options.state);153}154
155_createClass(BootstrapSwitch, [{156key: 'setPrevOptions',157value: function setPrevOptions() {158this.prevOptions = _extends({}, this.options);159}160}, {161key: 'state',162value: function state(value, skip) {163if (typeof value === 'undefined') {164return this.options.state;165}166if (this.options.disabled || this.options.readonly || this.options.state && !this.options.radioAllOff && this.$element.is(':radio')) {167return this.$element;168}169if (this.$element.is(':radio')) {170$('[name="' + this.$element.attr('name') + '"]').trigger('setPreviousOptions.bootstrapSwitch');171} else {172this.$element.trigger('setPreviousOptions.bootstrapSwitch');173}174if (this.options.indeterminate) {175this.indeterminate(false);176}177this.$element.prop('checked', Boolean(value)).trigger('change.bootstrapSwitch', skip);178return this.$element;179}180}, {181key: 'toggleState',182value: function toggleState(skip) {183if (this.options.disabled || this.options.readonly) {184return this.$element;185}186if (this.options.indeterminate) {187this.indeterminate(false);188return this.state(true);189} else {190return this.$element.prop('checked', !this.options.state).trigger('change.bootstrapSwitch', skip);191}192}193}, {194key: 'size',195value: function size(value) {196if (typeof value === 'undefined') {197return this.options.size;198}199if (this.options.size != null) {200this.$wrapper.removeClass(this._getClass(this.options.size));201}202if (value) {203this.$wrapper.addClass(this._getClass(value));204}205this._width();206this._containerPosition();207this.options.size = value;208return this.$element;209}210}, {211key: 'animate',212value: function animate(value) {213if (typeof value === 'undefined') {214return this.options.animate;215}216if (this.options.animate === Boolean(value)) {217return this.$element;218}219return this.toggleAnimate();220}221}, {222key: 'toggleAnimate',223value: function toggleAnimate() {224this.options.animate = !this.options.animate;225this.$wrapper.toggleClass(this._getClass('animate'));226return this.$element;227}228}, {229key: 'disabled',230value: function disabled(value) {231if (typeof value === 'undefined') {232return this.options.disabled;233}234if (this.options.disabled === Boolean(value)) {235return this.$element;236}237return this.toggleDisabled();238}239}, {240key: 'toggleDisabled',241value: function toggleDisabled() {242this.options.disabled = !this.options.disabled;243this.$element.prop('disabled', this.options.disabled);244this.$wrapper.toggleClass(this._getClass('disabled'));245return this.$element;246}247}, {248key: 'readonly',249value: function readonly(value) {250if (typeof value === 'undefined') {251return this.options.readonly;252}253if (this.options.readonly === Boolean(value)) {254return this.$element;255}256return this.toggleReadonly();257}258}, {259key: 'toggleReadonly',260value: function toggleReadonly() {261this.options.readonly = !this.options.readonly;262this.$element.prop('readonly', this.options.readonly);263this.$wrapper.toggleClass(this._getClass('readonly'));264return this.$element;265}266}, {267key: 'indeterminate',268value: function indeterminate(value) {269if (typeof value === 'undefined') {270return this.options.indeterminate;271}272if (this.options.indeterminate === Boolean(value)) {273return this.$element;274}275return this.toggleIndeterminate();276}277}, {278key: 'toggleIndeterminate',279value: function toggleIndeterminate() {280this.options.indeterminate = !this.options.indeterminate;281this.$element.prop('indeterminate', this.options.indeterminate);282this.$wrapper.toggleClass(this._getClass('indeterminate'));283this._containerPosition();284return this.$element;285}286}, {287key: 'inverse',288value: function inverse(value) {289if (typeof value === 'undefined') {290return this.options.inverse;291}292if (this.options.inverse === Boolean(value)) {293return this.$element;294}295return this.toggleInverse();296}297}, {298key: 'toggleInverse',299value: function toggleInverse() {300this.$wrapper.toggleClass(this._getClass('inverse'));301var $on = this.$on.clone(true);302var $off = this.$off.clone(true);303this.$on.replaceWith($off);304this.$off.replaceWith($on);305this.$on = $off;306this.$off = $on;307this.options.inverse = !this.options.inverse;308return this.$element;309}310}, {311key: 'onColor',312value: function onColor(value) {313if (typeof value === 'undefined') {314return this.options.onColor;315}316if (this.options.onColor) {317this.$on.removeClass(this._getClass(this.options.onColor));318}319this.$on.addClass(this._getClass(value));320this.options.onColor = value;321return this.$element;322}323}, {324key: 'offColor',325value: function offColor(value) {326if (typeof value === 'undefined') {327return this.options.offColor;328}329if (this.options.offColor) {330this.$off.removeClass(this._getClass(this.options.offColor));331}332this.$off.addClass(this._getClass(value));333this.options.offColor = value;334return this.$element;335}336}, {337key: 'onText',338value: function onText(value) {339if (typeof value === 'undefined') {340return this.options.onText;341}342this.$on.html(value);343this._width();344this._containerPosition();345this.options.onText = value;346return this.$element;347}348}, {349key: 'offText',350value: function offText(value) {351if (typeof value === 'undefined') {352return this.options.offText;353}354this.$off.html(value);355this._width();356this._containerPosition();357this.options.offText = value;358return this.$element;359}360}, {361key: 'labelText',362value: function labelText(value) {363if (typeof value === 'undefined') {364return this.options.labelText;365}366this.$label.html(value);367this._width();368this.options.labelText = value;369return this.$element;370}371}, {372key: 'handleWidth',373value: function handleWidth(value) {374if (typeof value === 'undefined') {375return this.options.handleWidth;376}377this.options.handleWidth = value;378this._width();379this._containerPosition();380return this.$element;381}382}, {383key: 'labelWidth',384value: function labelWidth(value) {385if (typeof value === 'undefined') {386return this.options.labelWidth;387}388this.options.labelWidth = value;389this._width();390this._containerPosition();391return this.$element;392}393}, {394key: 'baseClass',395value: function baseClass(value) {396return this.options.baseClass;397}398}, {399key: 'wrapperClass',400value: function wrapperClass(value) {401if (typeof value === 'undefined') {402return this.options.wrapperClass;403}404if (!value) {405value = $.fn.bootstrapSwitch.defaults.wrapperClass;406}407this.$wrapper.removeClass(this._getClasses(this.options.wrapperClass).join(' '));408this.$wrapper.addClass(this._getClasses(value).join(' '));409this.options.wrapperClass = value;410return this.$element;411}412}, {413key: 'radioAllOff',414value: function radioAllOff(value) {415if (typeof value === 'undefined') {416return this.options.radioAllOff;417}418var val = Boolean(value);419if (this.options.radioAllOff === val) {420return this.$element;421}422this.options.radioAllOff = val;423return this.$element;424}425}, {426key: 'onInit',427value: function onInit(value) {428if (typeof value === 'undefined') {429return this.options.onInit;430}431if (!value) {432value = $.fn.bootstrapSwitch.defaults.onInit;433}434this.options.onInit = value;435return this.$element;436}437}, {438key: 'onSwitchChange',439value: function onSwitchChange(value) {440if (typeof value === 'undefined') {441return this.options.onSwitchChange;442}443if (!value) {444value = $.fn.bootstrapSwitch.defaults.onSwitchChange;445}446this.options.onSwitchChange = value;447return this.$element;448}449}, {450key: 'destroy',451value: function destroy() {452var $form = this.$element.closest('form');453if ($form.length) {454$form.off('reset.bootstrapSwitch').removeData('bootstrap-switch');455}456this.$container.children().not(this.$element).remove();457this.$element.unwrap().unwrap().off('.bootstrapSwitch').removeData('bootstrap-switch');458return this.$element;459}460}, {461key: '_getElementOptions',462value: function _getElementOptions() {463return {464state: this.$element.is(':checked'),465size: this.$element.data('size'),466animate: this.$element.data('animate'),467disabled: this.$element.is(':disabled'),468readonly: this.$element.is('[readonly]'),469indeterminate: this.$element.data('indeterminate'),470inverse: this.$element.data('inverse'),471radioAllOff: this.$element.data('radio-all-off'),472onColor: this.$element.data('on-color'),473offColor: this.$element.data('off-color'),474onText: this.$element.data('on-text'),475offText: this.$element.data('off-text'),476labelText: this.$element.data('label-text'),477handleWidth: this.$element.data('handle-width'),478labelWidth: this.$element.data('label-width'),479baseClass: this.$element.data('base-class'),480wrapperClass: this.$element.data('wrapper-class')481};482}483}, {484key: '_width',485value: function _width() {486var _this2 = this;487
488var $handles = this.$on.add(this.$off).add(this.$label).css('width', '');489var handleWidth = this.options.handleWidth === 'auto' ? Math.round(Math.max(this.$on.width(), this.$off.width())) : this.options.handleWidth;490$handles.width(handleWidth);491this.$label.width(function (index, width) {492if (_this2.options.labelWidth !== 'auto') {493return _this2.options.labelWidth;494}495if (width < handleWidth) {496return handleWidth;497}498return width;499});500this._handleWidth = this.$on.outerWidth();501this._labelWidth = this.$label.outerWidth();502this.$container.width(this._handleWidth * 2 + this._labelWidth);503return this.$wrapper.width(this._handleWidth + this._labelWidth);504}505}, {506key: '_containerPosition',507value: function _containerPosition() {508var _this3 = this;509
510var state = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : this.options.state;511var callback = arguments[1];512
513this.$container.css('margin-left', function () {514var values = [0, '-' + _this3._handleWidth + 'px'];515if (_this3.options.indeterminate) {516return '-' + _this3._handleWidth / 2 + 'px';517}518if (state) {519if (_this3.options.inverse) {520return values[1];521} else {522return values[0];523}524} else {525if (_this3.options.inverse) {526return values[0];527} else {528return values[1];529}530}531});532}533}, {534key: '_init',535value: function _init() {536var _this4 = this;537
538var init = function init() {539_this4.setPrevOptions();540_this4._width();541_this4._containerPosition();542setTimeout(function () {543if (_this4.options.animate) {544return _this4.$wrapper.addClass(_this4._getClass('animate'));545}546}, 50);547};548if (this.$wrapper.is(':visible')) {549init();550return;551}552var initInterval = window.setInterval(function () {553if (_this4.$wrapper.is(':visible')) {554init();555return window.clearInterval(initInterval);556}557}, 50);558}559}, {560key: '_elementHandlers',561value: function _elementHandlers() {562var _this5 = this;563
564return this.$element.on({565'setPreviousOptions.bootstrapSwitch': this.setPrevOptions.bind(this),566
567'previousState.bootstrapSwitch': function previousStateBootstrapSwitch() {568_this5.options = _this5.prevOptions;569if (_this5.options.indeterminate) {570_this5.$wrapper.addClass(_this5._getClass('indeterminate'));571}572_this5.$element.prop('checked', _this5.options.state).trigger('change.bootstrapSwitch', true);573},574
575'change.bootstrapSwitch': function changeBootstrapSwitch(event, skip) {576event.preventDefault();577event.stopImmediatePropagation();578var state = _this5.$element.is(':checked');579_this5._containerPosition(state);580if (state === _this5.options.state) {581return;582}583_this5.options.state = state;584_this5.$wrapper.toggleClass(_this5._getClass('off')).toggleClass(_this5._getClass('on'));585if (!skip) {586if (_this5.$element.is(':radio')) {587$('[name="' + _this5.$element.attr('name') + '"]').not(_this5.$element).prop('checked', false).trigger('change.bootstrapSwitch', true);588}589_this5.$element.trigger('switchChange.bootstrapSwitch', [state]);590}591},592
593'focus.bootstrapSwitch': function focusBootstrapSwitch(event) {594event.preventDefault();595_this5.$wrapper.addClass(_this5._getClass('focused'));596},597
598'blur.bootstrapSwitch': function blurBootstrapSwitch(event) {599event.preventDefault();600_this5.$wrapper.removeClass(_this5._getClass('focused'));601},602
603'keydown.bootstrapSwitch': function keydownBootstrapSwitch(event) {604if (!event.which || _this5.options.disabled || _this5.options.readonly) {605return;606}607if (event.which === 37 || event.which === 39) {608event.preventDefault();609event.stopImmediatePropagation();610_this5.state(event.which === 39);611}612}613});614}615}, {616key: '_handleHandlers',617value: function _handleHandlers() {618var _this6 = this;619
620this.$on.on('click.bootstrapSwitch', function (event) {621event.preventDefault();622event.stopPropagation();623_this6.state(false);624return _this6.$element.trigger('focus.bootstrapSwitch');625});626return this.$off.on('click.bootstrapSwitch', function (event) {627event.preventDefault();628event.stopPropagation();629_this6.state(true);630return _this6.$element.trigger('focus.bootstrapSwitch');631});632}633}, {634key: '_labelHandlers',635value: function _labelHandlers() {636var _this7 = this;637
638var handlers = {639click: function click(event) {640event.stopPropagation();641},642
643
644'mousedown.bootstrapSwitch touchstart.bootstrapSwitch': function mousedownBootstrapSwitchTouchstartBootstrapSwitch(event) {645if (_this7._dragStart || _this7.options.disabled || _this7.options.readonly) {646return;647}648event.preventDefault();649event.stopPropagation();650_this7._dragStart = (event.pageX || event.originalEvent.touches[0].pageX) - parseInt(_this7.$container.css('margin-left'), 10);651if (_this7.options.animate) {652_this7.$wrapper.removeClass(_this7._getClass('animate'));653}654_this7.$element.trigger('focus.bootstrapSwitch');655},656
657'mousemove.bootstrapSwitch touchmove.bootstrapSwitch': function mousemoveBootstrapSwitchTouchmoveBootstrapSwitch(event) {658if (_this7._dragStart == null) {659return;660}661var difference = (event.pageX || event.originalEvent.touches[0].pageX) - _this7._dragStart;662event.preventDefault();663if (difference < -_this7._handleWidth || difference > 0) {664return;665}666_this7._dragEnd = difference;667_this7.$container.css('margin-left', _this7._dragEnd + 'px');668},669
670'mouseup.bootstrapSwitch touchend.bootstrapSwitch': function mouseupBootstrapSwitchTouchendBootstrapSwitch(event) {671if (!_this7._dragStart) {672return;673}674event.preventDefault();675if (_this7.options.animate) {676_this7.$wrapper.addClass(_this7._getClass('animate'));677}678if (_this7._dragEnd) {679var state = _this7._dragEnd > -(_this7._handleWidth / 2);680_this7._dragEnd = false;681_this7.state(_this7.options.inverse ? !state : state);682} else {683_this7.state(!_this7.options.state);684}685_this7._dragStart = false;686},687
688'mouseleave.bootstrapSwitch': function mouseleaveBootstrapSwitch() {689_this7.$label.trigger('mouseup.bootstrapSwitch');690}691};692this.$label.on(handlers);693}694}, {695key: '_externalLabelHandler',696value: function _externalLabelHandler() {697var _this8 = this;698
699var $externalLabel = this.$element.closest('label');700$externalLabel.on('click', function (event) {701event.preventDefault();702event.stopImmediatePropagation();703if (event.target === $externalLabel[0]) {704_this8.toggleState();705}706});707}708}, {709key: '_formHandler',710value: function _formHandler() {711var $form = this.$element.closest('form');712if ($form.data('bootstrap-switch')) {713return;714}715$form.on('reset.bootstrapSwitch', function () {716window.setTimeout(function () {717$form.find('input').filter(function () {718return $(this).data('bootstrap-switch');719}).each(function () {720return $(this).bootstrapSwitch('state', this.checked);721});722}, 1);723}).data('bootstrap-switch', true);724}725}, {726key: '_getClass',727value: function _getClass(name) {728return this.options.baseClass + '-' + name;729}730}, {731key: '_getClasses',732value: function _getClasses(classes) {733if (!$.isArray(classes)) {734return [this._getClass(classes)];735}736return classes.map(this._getClass.bind(this));737}738}]);739
740return BootstrapSwitch;741}();742
743$.fn.bootstrapSwitch = function (option) {744for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) {745args[_key2 - 1] = arguments[_key2];746}747
748function reducer(ret, next) {749var $this = $(next);750var existingData = $this.data('bootstrap-switch');751var data = existingData || new BootstrapSwitch(next, option);752if (!existingData) {753$this.data('bootstrap-switch', data);754}755if (typeof option === 'string') {756return data[option].apply(data, args);757}758return ret;759}760return Array.prototype.reduce.call(this, reducer, this);761};762$.fn.bootstrapSwitch.Constructor = BootstrapSwitch;763$.fn.bootstrapSwitch.defaults = {764state: true,765size: null,766animate: true,767disabled: false,768readonly: false,769indeterminate: false,770inverse: false,771radioAllOff: false,772onColor: 'primary',773offColor: 'default',774onText: 'ON',775offText: 'OFF',776labelText: ' ',777handleWidth: 'auto',778labelWidth: 'auto',779baseClass: 'bootstrap-switch',780wrapperClass: 'wrapper',781onInit: function onInit() {},782onSwitchChange: function onSwitchChange() {}783};784});785