toggle.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*\
  2. |*| ========================================================================
  3. |*| Bootstrap Toggle: bootstrap4-toggle.js v3.6.1
  4. |*| https://gitbrent.github.io/bootstrap4-toggle/
  5. |*| ========================================================================
  6. |*| Copyright 2018-2019 Brent Ely
  7. |*| Licensed under MIT
  8. |*| ========================================================================
  9. \*/
  10. +function ($) {
  11. 'use strict';
  12. // TOGGLE PUBLIC CLASS DEFINITION
  13. // ==============================
  14. var Toggle = function (element, options) {
  15. this.$element = $(element)
  16. this.options = $.extend({}, this.defaults(), options)
  17. this.render()
  18. }
  19. Toggle.VERSION = '3.7.0-beta'
  20. Toggle.DEFAULTS = {
  21. on: 'On',
  22. off: 'Off',
  23. onstyle: 'primary',
  24. offstyle: 'light',
  25. size: 'normal',
  26. style: '',
  27. width: null,
  28. height: null
  29. }
  30. Toggle.prototype.defaults = function() {
  31. return {
  32. on: this.$element.attr('data-on') || Toggle.DEFAULTS.on,
  33. off: this.$element.attr('data-off') || Toggle.DEFAULTS.off,
  34. onstyle: this.$element.attr('data-onstyle') || Toggle.DEFAULTS.onstyle,
  35. offstyle: this.$element.attr('data-offstyle') || Toggle.DEFAULTS.offstyle,
  36. size: this.$element.attr('data-size') || Toggle.DEFAULTS.size,
  37. style: this.$element.attr('data-style') || Toggle.DEFAULTS.style,
  38. width: this.$element.attr('data-width') || Toggle.DEFAULTS.width,
  39. height: this.$element.attr('data-height') || Toggle.DEFAULTS.height
  40. }
  41. }
  42. Toggle.prototype.render = function () {
  43. this._onstyle = 'btn-' + this.options.onstyle
  44. this._offstyle = 'btn-' + this.options.offstyle
  45. var size
  46. = this.options.size === 'large' || this.options.size === 'lg' ? 'btn-lg'
  47. : this.options.size === 'small' || this.options.size === 'sm' ? 'btn-sm'
  48. : this.options.size === 'mini' || this.options.size === 'xs' ? 'btn-xs'
  49. : ''
  50. var $toggleOn = $('<label for="'+ this.$element.prop('id') +'" class="btn">').html(this.options.on)
  51. .addClass(this._onstyle + ' ' + size)
  52. var $toggleOff = $('<label for="'+ this.$element.prop('id') +'" class="btn">').html(this.options.off)
  53. .addClass(this._offstyle + ' ' + size)
  54. var $toggleHandle = $('<span class="toggle-handle btn btn-light">')
  55. .addClass(size)
  56. var $toggleGroup = $('<div class="toggle-group">')
  57. .append($toggleOn, $toggleOff, $toggleHandle)
  58. var $toggle = $('<div class="toggle btn" data-toggle="toggle" role="button">')
  59. .addClass( this.$element.prop('checked') ? this._onstyle : this._offstyle+' off' )
  60. .addClass(size).addClass(this.options.style)
  61. this.$element.wrap($toggle)
  62. $.extend(this, {
  63. $toggle: this.$element.parent(),
  64. $toggleOn: $toggleOn,
  65. $toggleOff: $toggleOff,
  66. $toggleGroup: $toggleGroup
  67. })
  68. this.$toggle.append($toggleGroup)
  69. var width = this.options.width || Math.max($toggleOn.outerWidth(), $toggleOff.outerWidth())+($toggleHandle.outerWidth()/2)
  70. var height = this.options.height || Math.max($toggleOn.outerHeight(), $toggleOff.outerHeight())
  71. $toggleOn.addClass('toggle-on')
  72. $toggleOff.addClass('toggle-off')
  73. this.$toggle.css({ width: width, height: height })
  74. if (this.options.height) {
  75. $toggleOn.css('line-height', $toggleOn.height() + 'px')
  76. $toggleOff.css('line-height', $toggleOff.height() + 'px')
  77. }
  78. this.update(true)
  79. this.trigger(true)
  80. }
  81. Toggle.prototype.toggle = function () {
  82. if (this.$element.prop('checked')) this.off()
  83. else this.on()
  84. }
  85. Toggle.prototype.on = function (silent) {
  86. if (this.$element.prop('disabled')) return false
  87. this.$toggle.removeClass(this._offstyle + ' off').addClass(this._onstyle)
  88. this.$element.prop('checked', true)
  89. if (!silent) this.trigger()
  90. }
  91. Toggle.prototype.off = function (silent) {
  92. if (this.$element.prop('disabled')) return false
  93. this.$toggle.removeClass(this._onstyle).addClass(this._offstyle + ' off')
  94. this.$element.prop('checked', false)
  95. if (!silent) this.trigger()
  96. }
  97. Toggle.prototype.enable = function () {
  98. this.$toggle.removeClass('disabled')
  99. this.$toggle.removeAttr('disabled')
  100. this.$element.prop('disabled', false)
  101. }
  102. Toggle.prototype.disable = function () {
  103. this.$toggle.addClass('disabled')
  104. this.$toggle.attr('disabled', 'disabled')
  105. this.$element.prop('disabled', true)
  106. }
  107. Toggle.prototype.update = function (silent) {
  108. if (this.$element.prop('disabled')) this.disable()
  109. else this.enable()
  110. if (this.$element.prop('checked')) this.on(silent)
  111. else this.off(silent)
  112. }
  113. Toggle.prototype.trigger = function (silent) {
  114. this.$element.off('change.bs.toggle')
  115. if (!silent) this.$element.change()
  116. this.$element.on('change.bs.toggle', $.proxy(function() {
  117. this.update()
  118. }, this))
  119. }
  120. Toggle.prototype.destroy = function() {
  121. this.$element.off('change.bs.toggle')
  122. this.$toggleGroup.remove()
  123. this.$element.removeData('bs.toggle')
  124. this.$element.unwrap()
  125. }
  126. // TOGGLE PLUGIN DEFINITION
  127. // ========================
  128. function Plugin(option) {
  129. var optArg = Array.prototype.slice.call( arguments, 1 )[0]
  130. return this.each(function () {
  131. var $this = $(this)
  132. var data = $this.data('bs.toggle')
  133. var options = typeof option == 'object' && option
  134. if (!data) $this.data('bs.toggle', (data = new Toggle(this, options)))
  135. if (typeof option === 'string' && data[option] && typeof optArg === 'boolean') data[option](optArg)
  136. else if (typeof option === 'string' && data[option]) data[option]()
  137. //else if (option && !data[option]) console.log('bootstrap-toggle: error: method `'+ option +'` does not exist!');
  138. })
  139. }
  140. var old = $.fn.bootstrapToggle
  141. $.fn.bootstrapToggle = Plugin
  142. $.fn.bootstrapToggle.Constructor = Toggle
  143. // TOGGLE NO CONFLICT
  144. // ==================
  145. $.fn.toggle.noConflict = function () {
  146. $.fn.bootstrapToggle = old
  147. return this
  148. }
  149. // TOGGLE DATA-API
  150. // ===============
  151. $(function() {
  152. $('input[type=checkbox][data-toggle^=toggle]').bootstrapToggle()
  153. })
  154. $(document).on('click.bs.toggle', 'div[data-toggle^=toggle]', function(e) {
  155. var $checkbox = $(this).find('input[type=checkbox]')
  156. $checkbox.bootstrapToggle('toggle')
  157. e.preventDefault()
  158. })
  159. }(jQuery);