sidebar.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* ========================================================================
  2. * Bootstrap: sidebar.js v0.1
  3. * ========================================================================
  4. * Copyright 2011-2014 Asyraf Abdul Rahman
  5. * Licensed under MIT
  6. * ======================================================================== */
  7. + function($) {
  8. 'use strict';
  9. // SIDEBAR PUBLIC CLASS DEFINITION
  10. // ================================
  11. var Sidebar = function(element, options) {
  12. this.$element = $(element)
  13. this.options = $.extend({}, Sidebar.DEFAULTS, options)
  14. this.transitioning = null
  15. if (this.options.parent) this.$parent = $(this.options.parent)
  16. if (this.options.toggle) this.toggle()
  17. }
  18. Sidebar.DEFAULTS = {
  19. toggle: true
  20. }
  21. Sidebar.prototype.show = function() {
  22. if (this.transitioning || this.$element.hasClass('sidebar-open')) return
  23. var startEvent = $.Event('show.bs.sidebar')
  24. this.$element.trigger(startEvent);
  25. if (startEvent.isDefaultPrevented()) return
  26. this.$element
  27. .addClass('sidebar-open')
  28. this.transitioning = 1
  29. var complete = function() {
  30. this.$element
  31. this.transitioning = 0
  32. this.$element.trigger('shown.bs.sidebar')
  33. }
  34. if (!$.support.transition) return complete.call(this)
  35. this.$element
  36. .one($.support.transition.end, $.proxy(complete, this))
  37. .emulateTransitionEnd(400)
  38. }
  39. Sidebar.prototype.hide = function() {
  40. if (this.transitioning || !this.$element.hasClass('sidebar-open')) return
  41. var startEvent = $.Event('hide.bs.sidebar')
  42. this.$element.trigger(startEvent)
  43. if (startEvent.isDefaultPrevented()) return
  44. this.$element
  45. .removeClass('sidebar-open')
  46. this.transitioning = 1
  47. var complete = function() {
  48. this.transitioning = 0
  49. this.$element
  50. .trigger('hidden.bs.sidebar')
  51. }
  52. if (!$.support.transition) return complete.call(this)
  53. this.$element
  54. .one($.support.transition.end, $.proxy(complete, this))
  55. .emulateTransitionEnd(400)
  56. }
  57. Sidebar.prototype.toggle = function() {
  58. this[this.$element.hasClass('sidebar-open') ? 'hide' : 'show']()
  59. }
  60. var old = $.fn.sidebar
  61. $.fn.sidebar = function(option) {
  62. return this.each(function() {
  63. var $this = $(this)
  64. var data = $this.data('bs.sidebar')
  65. var options = $.extend({}, Sidebar.DEFAULTS, $this.data(), typeof options == 'object' && option)
  66. if (!data && options.toggle && option == 'show') option = !option
  67. if (!data) $this.data('bs.sidebar', (data = new Sidebar(this, options)))
  68. if (typeof option == 'string') data[option]()
  69. })
  70. }
  71. $.fn.sidebar.Constructor = Sidebar
  72. $.fn.sidebar.noConflict = function() {
  73. $.fn.sidebar = old
  74. return this
  75. }
  76. $(document).on('click.bs.sidebar.data-api', '[data-bs-toggle="sidebar-right"]', function(e) {
  77. var $this = $(this),
  78. href
  79. var target = $this.attr('data-target') ||
  80. e.preventDefault() ||
  81. (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')
  82. var $target = $(target)
  83. var data = $target.data('bs.sidebar')
  84. var option = data ? 'toggle' : $this.data()
  85. $target.sidebar(option)
  86. })
  87. $('html').on('click.bs.sidebar.autohide', function(event) {
  88. var $this = $(event.target);
  89. var isButtonOrSidebar = $this.is('.sidebar, [data-bs-toggle="sidebar-right"]') || $this.parents('.sidebar, [data-bs-toggle="sidebar-right"]').length;
  90. if (isButtonOrSidebar) {
  91. return;
  92. } else {
  93. var $target = $('.sidebar');
  94. $target.each(function(i, trgt) {
  95. var $trgt = $(trgt);
  96. if ($trgt.data('bs.sidebar') && $trgt.hasClass('sidebar-open')) {
  97. $trgt.sidebar('hide');
  98. }
  99. })
  100. }
  101. });
  102. }(jQuery);