ControlSidebar.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* ControlSidebar()
  2. * ===============
  3. * Toggles the state of the control sidebar
  4. *
  5. * @Usage: $('#control-sidebar-trigger').controlSidebar(options)
  6. * or add [data-toggle="control-sidebar"] to the trigger
  7. * Pass any option as data-option="value"
  8. */
  9. +function ($) {
  10. 'use strict'
  11. var DataKey = 'lte.controlsidebar'
  12. var Default = {
  13. slide: true
  14. }
  15. var Selector = {
  16. sidebar: '.control-sidebar',
  17. data : '[data-toggle="control-sidebar"]',
  18. open : '.control-sidebar-open',
  19. bg : '.control-sidebar-bg',
  20. wrapper: '.wrapper',
  21. content: '.content-wrapper',
  22. boxed : '.layout-boxed'
  23. }
  24. var ClassName = {
  25. open : 'control-sidebar-open',
  26. fixed: 'fixed'
  27. }
  28. var Event = {
  29. collapsed: 'collapsed.controlsidebar',
  30. expanded : 'expanded.controlsidebar'
  31. }
  32. // ControlSidebar Class Definition
  33. // ===============================
  34. var ControlSidebar = function (element, options) {
  35. this.element = element
  36. this.options = options
  37. this.hasBindedResize = false
  38. this.init()
  39. }
  40. ControlSidebar.prototype.init = function () {
  41. // Add click listener if the element hasn't been
  42. // initialized using the data API
  43. if (!$(this.element).is(Selector.data)) {
  44. $(this).on('click', this.toggle)
  45. }
  46. this.fix()
  47. $(window).resize(function () {
  48. this.fix()
  49. }.bind(this))
  50. }
  51. ControlSidebar.prototype.toggle = function (event) {
  52. if (event) event.preventDefault()
  53. this.fix()
  54. if (!$(Selector.sidebar).is(Selector.open) && !$('body').is(Selector.open)) {
  55. this.expand()
  56. } else {
  57. this.collapse()
  58. }
  59. }
  60. ControlSidebar.prototype.expand = function () {
  61. if (!this.options.slide) {
  62. $('body').addClass(ClassName.open)
  63. } else {
  64. $(Selector.sidebar).addClass(ClassName.open)
  65. }
  66. $(this.element).trigger($.Event(Event.expanded))
  67. }
  68. ControlSidebar.prototype.collapse = function () {
  69. $('body, ' + Selector.sidebar).removeClass(ClassName.open)
  70. $(this.element).trigger($.Event(Event.collapsed))
  71. }
  72. ControlSidebar.prototype.fix = function () {
  73. if ($('body').is(Selector.boxed)) {
  74. this._fixForBoxed($(Selector.bg))
  75. }
  76. }
  77. // Private
  78. ControlSidebar.prototype._fixForBoxed = function (bg) {
  79. bg.css({
  80. position: 'absolute',
  81. height : $(Selector.wrapper).height()
  82. })
  83. }
  84. // Plugin Definition
  85. // =================
  86. function Plugin(option) {
  87. return this.each(function () {
  88. var $this = $(this)
  89. var data = $this.data(DataKey)
  90. if (!data) {
  91. var options = $.extend({}, Default, $this.data(), typeof option == 'object' && option)
  92. $this.data(DataKey, (data = new ControlSidebar($this, options)))
  93. }
  94. if (typeof option == 'string') data.toggle()
  95. })
  96. }
  97. var old = $.fn.controlSidebar
  98. $.fn.controlSidebar = Plugin
  99. $.fn.controlSidebar.Constructor = ControlSidebar
  100. // No Conflict Mode
  101. // ================
  102. $.fn.controlSidebar.noConflict = function () {
  103. $.fn.controlSidebar = old
  104. return this
  105. }
  106. // ControlSidebar Data API
  107. // =======================
  108. $(document).on('click', Selector.data, function (event) {
  109. if (event) event.preventDefault()
  110. Plugin.call($(this), 'toggle')
  111. })
  112. }(jQuery)