Skip to content

Commit

Permalink
avoid drag img
Browse files Browse the repository at this point in the history
  • Loading branch information
Johann-S authored and XhmikosR committed Oct 20, 2018
1 parent 8883ccb commit b1dc6b3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
3 changes: 3 additions & 0 deletions js/src/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ const Event = {
POINTERUP : `pointerup${EVENT_KEY}`,
POINTERLEAVE : `pointerleave${EVENT_KEY}`,
POINTERCANCEL : `pointercancel${EVENT_KEY}`,
DRAG_START : `dragstart${EVENT_KEY}`,
LOAD_DATA_API : `load${EVENT_KEY}${DATA_API_KEY}`,
CLICK_DATA_API : `click${EVENT_KEY}${DATA_API_KEY}`
}
Expand All @@ -84,6 +85,7 @@ const Selector = {
ACTIVE : '.active',
ACTIVE_ITEM : '.active.carousel-item',
ITEM : '.carousel-item',
ITEM_IMG : '.carousel-item img',
NEXT_PREV : '.carousel-item-next, .carousel-item-prev',
INDICATORS : '.carousel-indicators',
DATA_SLIDE : '[data-slide], [data-slide-to]',
Expand Down Expand Up @@ -323,6 +325,7 @@ class Carousel {
}
}

$(this._element.querySelectorAll(Selector.ITEM_IMG)).on(Event.DRAG_START, (e) => e.preventDefault())
if (this._pointerEvent) {
$(this._element).on(Event.POINTERDOWN, (event) => start(event))
$(this._element).on(Event.POINTERUP, (event) => end(event))
Expand Down
20 changes: 11 additions & 9 deletions js/tests/unit/carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@ $(function () {
window.Carousel = typeof bootstrap !== 'undefined' ? bootstrap.Carousel : Carousel

var originWinPointerEvent = window.PointerEvent
var originMsPointerEvent = window.MSPointerEvent
window.MSPointerEvent = null
var supportPointerEvent = Boolean(window.PointerEvent || window.MSPointerEvent)

function clearPointerEvents() {
window.PointerEvent = null
window.MSPointerEvent = null
}

function restorePointerEvents() {
window.PointerEvent = originWinPointerEvent
window.MSPointerEvent = originMsPointerEvent
}

var stylesCarousel = [
Expand Down Expand Up @@ -1032,11 +1030,11 @@ $(function () {
return
}

document.documentElement.ontouchstart = $.noop
Simulator.setType('pointer')
assert.expect(3)
var $styles = $(stylesCarousel).appendTo('head')
var done = assert.async()
document.documentElement.ontouchstart = $.noop

var carouselHTML =
'<div class="carousel" data-interval="false">' +
Expand All @@ -1050,8 +1048,7 @@ $(function () {
' </div>' +
'</div>'

var $carousel = $(carouselHTML)
$carousel.appendTo('#qunit-fixture')
var $carousel = $(carouselHTML).appendTo('#qunit-fixture')
var $item = $('#item')
$carousel.bootstrapCarousel()
var carousel = $carousel.data('bs.carousel')
Expand All @@ -1061,8 +1058,8 @@ $(function () {
assert.ok(true, 'slid event fired')
assert.ok($item.hasClass('active'))
assert.ok(spy.called)
delete document.documentElement.ontouchstart
$styles.remove()
delete document.documentElement.ontouchstart
done()
})

Expand All @@ -1075,6 +1072,7 @@ $(function () {
QUnit.test('should allow swiperight and call prev with touch events', function (assert) {
Simulator.setType('touch')
clearPointerEvents()

assert.expect(3)
var done = assert.async()
document.documentElement.ontouchstart = $.noop
Expand Down Expand Up @@ -1119,12 +1117,12 @@ $(function () {
return
}

document.documentElement.ontouchstart = $.noop
assert.expect(3)
Simulator.setType('pointer')

var $styles = $(stylesCarousel).appendTo('head')
var done = assert.async()
document.documentElement.ontouchstart = $.noop

var carouselHTML =
'<div class="carousel" data-interval="false">' +
Expand All @@ -1150,6 +1148,7 @@ $(function () {
assert.ok(!$item.hasClass('active'))
assert.ok(spy.called)
$styles.remove()
delete document.documentElement.ontouchstart
done()
})

Expand All @@ -1164,9 +1163,9 @@ $(function () {
assert.expect(3)
clearPointerEvents()
Simulator.setType('touch')
document.documentElement.ontouchstart = $.noop

var done = assert.async()
document.documentElement.ontouchstart = $.noop

var carouselHTML =
'<div class="carousel" data-interval="false">' +
Expand All @@ -1192,6 +1191,7 @@ $(function () {
assert.ok(!$item.hasClass('active'))
assert.ok(spy.called)
restorePointerEvents()
delete document.documentElement.ontouchstart
done()
})

Expand All @@ -1205,6 +1205,7 @@ $(function () {
QUnit.test('should not allow pinch with touch events', function (assert) {
assert.expect(0)
clearPointerEvents()

Simulator.setType('touch')
var done = assert.async()
document.documentElement.ontouchstart = $.noop
Expand All @@ -1221,6 +1222,7 @@ $(function () {
touches: 2
}, function () {
restorePointerEvents()
delete document.documentElement.ontouchstart
done()
})
})
Expand Down

0 comments on commit b1dc6b3

Please sign in to comment.