1 /* ========================================================================
2 * Bootstrap: sidebar.js v0.1
3 * ========================================================================
4 * Copyright 2011-2014 Asyraf Abdul Rahman
6 * ======================================================================== */
11 // SIDEBAR PUBLIC CLASS DEFINITION
12 // ================================
14 var Sidebar = function (element, options) {
15 this.$element = $(element)
16 this.options = $.extend({}, Sidebar.DEFAULTS, options)
17 this.transitioning = null
19 if (this.options.parent) this.$parent = $(this.options.parent)
20 if (this.options.toggle) this.toggle()
27 Sidebar.prototype.show = function () {
28 if (this.transitioning || this.$element.hasClass('sidebar-open')) return
31 var startEvent = $.Event('show.bs.sidebar')
32 this.$element.trigger(startEvent);
33 if (startEvent.isDefaultPrevented()) return
36 .addClass('sidebar-open')
38 this.transitioning = 1
40 var complete = function () {
42 this.transitioning = 0
43 this.$element.trigger('shown.bs.sidebar')
46 if(!$.support.transition) return complete.call(this)
49 .one($.support.transition.end, $.proxy(complete, this))
50 .emulateTransitionEnd(400)
53 Sidebar.prototype.hide = function () {
54 if (this.transitioning || !this.$element.hasClass('sidebar-open')) return
56 var startEvent = $.Event('hide.bs.sidebar')
57 this.$element.trigger(startEvent)
58 if(startEvent.isDefaultPrevented()) return
61 .removeClass('sidebar-open')
63 this.transitioning = 1
65 var complete = function () {
66 this.transitioning = 0
68 .trigger('hidden.bs.sidebar')
71 if (!$.support.transition) return complete.call(this)
74 .one($.support.transition.end, $.proxy(complete, this))
75 .emulateTransitionEnd(400)
78 Sidebar.prototype.toggle = function () {
79 this[this.$element.hasClass('sidebar-open') ? 'hide' : 'show']()
82 var old = $.fn.sidebar
84 $.fn.sidebar = function (option) {
85 return this.each(function (){
87 var data = $this.data('bs.sidebar')
88 var options = $.extend({}, Sidebar.DEFAULTS, $this.data(), typeof options == 'object' && option)
90 if (!data && options.toggle && option == 'show') option = !option
91 if (!data) $this.data('bs.sidebar', (data = new Sidebar(this, options)))
92 if (typeof option == 'string') data[option]()
96 $.fn.sidebar.Constructor = Sidebar
98 $.fn.sidebar.noConflict = function () {
103 $(document).on('click.bs.sidebar.data-api', '[data-toggle="sidebar"]', function (e) {
104 var $this = $(this), href
105 var target = $this.attr('data-target')
106 || e.preventDefault()
107 || (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '')
108 var $target = $(target)
109 var data = $target.data('bs.sidebar')
110 var option = data ? 'toggle' : $this.data()
112 $target.sidebar(option)
115 $('html').on('click.bs.sidebar.autohide', function(event){
116 var $this = $(event.target);
117 var isButtonOrSidebar = $this.is('.sidebar, [data-toggle="sidebar"]') || $this.parents('.sidebar, [data-toggle="sidebar"]').length;
118 if (isButtonOrSidebar) {
121 var $target = $('.sidebar');
122 $target.each(function(i, trgt) {
124 if($trgt.data('bs.sidebar') && $trgt.hasClass('sidebar-open')) {
125 $trgt.sidebar('hide');