Skip to content

Commit

Permalink
Merge branch 'master' into dropdown_menu_item_click_causes_page_going…
Browse files Browse the repository at this point in the history
…_below_the_offcanvas_menu
  • Loading branch information
jasny authored Jun 12, 2018
2 parents cce8a6d + b9e2c03 commit 59af819
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 37 deletions.
12 changes: 12 additions & 0 deletions docs/javascript.html
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ <h3>Via JavaScript</h3>
{% endhighlight %}

<h3>Options</h3>

<p>Options can be placed both on control and on target menu element.</p>

<table class="table table-bordered table-striped">
<thead>
<tr>
Expand Down Expand Up @@ -180,6 +183,12 @@ <h3>Options</h3>
<td>true</td>
<td>Calculate if off canvas should be disabled for this viewport width on window resize. If your elements always gets hidden on window resize, try setting this to false.</td>
</tr>
<tr>
<td>exclude</td>
<td>string</td>
<td>null</td>
<td>Set css selectors for elements, that have fixed positioning and that should not be moved when showing\hiding menu.</td>
</tr>
<tr>
<td>disableScrolling</td>
<td>boolean</td>
Expand All @@ -194,6 +203,9 @@ <h4>Graceful degradation</h4>
<p>For browsers that don't support transform (mainly IE8), the <code>target</code> option is ignored. In that case, the plugin will always slide the target element. In that case <code>.canvas-slid</code> will be added to the target element instead.</p>
</div>

<h3>Two menus on the page</h3>

<p>If there are two (or more) menus on the page, there can be only one opened at a time. When menu attempts to be opened, already opened one will be closed first. It's almost fully automated, accept that <code>data-exclude</code> option should be set for each menu, holding references to other menus. For example, if we have two menus <code>#menu-left</code> and <code>#menu-right</code>, then they should have the following option set, correspondingly: <code>data-exclude="#menu-right"</code> and <code>data-exclude="#menu-left"</code>.</p>

<h3>Methods</h3>
<h4>.offcanvas(options)</h4>
Expand Down
102 changes: 65 additions & 37 deletions js/offcanvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
this.placement = null
this.$calcClone = null

this.calcClone()

if (this.options.recalc) {
this.calcClone()
$(window).on('resize', $.proxy(this.recalc, this))
}

Expand Down Expand Up @@ -200,51 +201,75 @@
this.$element.trigger(startEvent)
if (startEvent.isDefaultPrevented()) return

this.state = 'slide-in'
this.$element.css('width', '')
this.calcPlacement()
this.setWidth()
this.hideOthers($.proxy(function() {
this.state = 'slide-in'
this.$element.css('width', '')
this.calcPlacement()
this.setWidth()

var elements = this.getCanvasElements()
var placement = this.placement
var opposite = this.opposite(placement)
var offset = this.offset()

if (elements.index(this.$element) !== -1) {
$(this.$element).data('offcanvas-style', $(this.$element).attr('style') || '')
this.$element.css(placement, -1 * offset)
this.$element.css(placement); // Workaround: Need to get the CSS property for it to be applied before the next line of code
}

var elements = this.getCanvasElements()
var placement = this.placement
var opposite = this.opposite(placement)
var offset = this.offset()
elements.addClass('canvas-sliding').each(function() {
var $this = $(this)
if ($this.data('offcanvas-style') === undefined) $this.data('offcanvas-style', $this.attr('style') || '')
if ($this.css('position') === 'static' && !isIphone) $this.css('position', 'relative')
if (($this.css(placement) === 'auto' || $this.css(placement) === '0px') &&
($this.css(opposite) === 'auto' || $this.css(opposite) === '0px')) {
$this.css(placement, 0)
}
})

if (elements.index(this.$element) !== -1) {
$(this.$element).data('offcanvas-style', $(this.$element).attr('style') || '')
this.$element.css(placement, -1 * offset)
this.$element.css(placement); // Workaround: Need to get the CSS property for it to be applied before the next line of code
}
if (this.options.disableScrolling) this.disableScrolling()
if (this.options.modal || this.options.backdrop) this.toggleBackdrop()

var complete = function () {
if (this.state != 'slide-in') return

this.state = 'slid'

elements.addClass('canvas-sliding').each(function() {
var $this = $(this)
if ($this.data('offcanvas-style') === undefined) $this.data('offcanvas-style', $this.attr('style') || '')
if ($this.css('position') === 'static' && !isIphone) $this.css('position', 'relative')
if (($this.css(placement) === 'auto' || $this.css(placement) === '0px') &&
($this.css(opposite) === 'auto' || $this.css(opposite) === '0px')) {
$this.css(placement, 0)
elements.removeClass('canvas-sliding').addClass('canvas-slid')
this.$element.trigger('shown.bs.offcanvas')
}
})

if (this.options.disableScrolling) this.disableScrolling()
if (this.options.modal || this.options.backdrop) this.toggleBackdrop()
setTimeout($.proxy(function() {
this.$element.addClass('in')
this.slide(elements, offset, $.proxy(complete, this))
}, this), 1)
}, this));
}

var complete = function () {
if (this.state != 'slide-in') return
//Hide other opened offcanvas menus, and then open this one
OffCanvas.prototype.hideOthers = function (callback) {
var doHide = false
var id = this.$element.attr('id')
var $clones = $('.offcanvas-clone:not([data-id="' + id + '"])')

this.state = 'slid'
if (!$clones.length) return callback()

elements.removeClass('canvas-sliding').addClass('canvas-slid')
this.$element.trigger('shown.bs.offcanvas')
}
$clones.each(function(index, clone) {
var id = $(clone).attr('data-id')
var $menu = $('#' + id)
doHide = $menu.hasClass('canvas-slid')

setTimeout($.proxy(function() {
this.$element.addClass('in')
this.slide(elements, offset, $.proxy(complete, this))
}, this), 1)
if (!doHide) return

$menu.one('hidden.bs.offcanvas', callback)
$menu.offcanvas('hide')
})

if (!doHide) callback()
}

OffCanvas.prototype.hide = function (fast) {
OffCanvas.prototype.hide = function () {
if (this.state !== 'slid') return

var startEvent = $.Event('hide.bs.offcanvas')
Expand Down Expand Up @@ -350,11 +375,14 @@
}

OffCanvas.prototype.calcClone = function() {
this.$calcClone = $('.offcanvas-clone')
var id = this.$element.attr('id')
this.$calcClone = $('.offcanvas-clone[data-id="' + id + '"]')

if (!this.$calcClone.length) {
this.$calcClone = this.$element.clone()
.addClass('offcanvas-clone')
.attr('data-id', id)
.removeAttr('id')
.appendTo($('body'))
.html('')
}
Expand Down Expand Up @@ -428,7 +456,7 @@
|| (href = $this.attr('href')) && href.replace(/.*(?=#[^\s]+$)/, '') //strip for ie7
var $canvas = $(target)
var data = $canvas.data('bs.offcanvas')
var option = data ? 'toggle' : $this.data()
var option = data ? 'toggle' : $.extend($this.data(), $canvas.data())

e.preventDefault();
e.stopPropagation()
Expand Down

0 comments on commit 59af819

Please sign in to comment.