Skip to content

Commit

Permalink
Added dragHandle and dragCancel props
Browse files Browse the repository at this point in the history
  • Loading branch information
Maurizio Bonani committed Jan 16, 2018
1 parent 057fdc7 commit 9809a47
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

All notable changes to `vue-draggable-resizable` will be documented in this file

## 1.6.0 - 2018-01-16
- add `dragHandle` and `dragCancel` props

## 1.5.0 - 2017-09-19
- implement :z prop and watcher

Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,26 @@ Restricts the movement and the dimensions of the element to the parent.
<vue-draggable-resizable :parent="true">
```

#### dragHandle
Type: `String`<br>
Required: `false`

Defines the selector that should be used to drag the component.

```html
<vue-draggable-resizable :drag-handle=".drag">
```

#### dragCancel
Type: `String`<br>
Required: `false`

Defines a selector that should be used to prevent drag initialization.

```html
<vue-draggable-resizable :drag-handle=".drag">
```

#### maximize
Type: `Boolean`<br>
Required: `false`<br>
Expand Down
4 changes: 4 additions & 0 deletions docs/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ new Vue({
data: {
active: false
}
});

new Vue({
el: '#app8'
});
23 changes: 23 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,29 @@ <h2 class="section-title">External activation</h2>
</div>
</div>

<section class="section-description">
<h2 class="section-title">Drag Handle and Drag Cancel</h2>
<p>Component that can be dragged only through a handle <code>:drag-handle="'.drag'"</code> and component that cannot be dragged from a handle <code>:drag-cancel="'.cancel'"</code></p>
<pre>&lt;vue-draggable-resizable :parent="true" style="border: 1px solid black;" :drag-handle="'.drag'"&gt;
&lt;div class="drag" style="padding: 6px; margin: 6px; background-color: #CCC; border: 2px solid"&gt;Drag Only Here&lt;/div&gt;
&lt;/vue-draggable-resizable&gt;
&lt;vue-draggable-resizable :x="300" :y="0" :parent="true" style="border: 1px solid black;" :drag-cancel="'.cancel'"&gt;
&lt;div class="cancel" style="padding: 6px; margin: 6px; background-color: #CCC; border: 2px solid"&gt;Cannot Drag Here&lt;/div&gt;
&lt;/vue-draggable-resizable>
</pre>
</section>

<div id="app8">
<div style="height: 300px; border: 1px solid blue; margin: 1em;">
<vue-draggable-resizable :parent="true" style="border: 1px solid black;" :drag-handle="'.drag'">
<div class="drag" style="padding: 6px; margin: 6px; background-color: #CCC; border: 2px solid">Drag Only Here</div>
</vue-draggable-resizable>
<vue-draggable-resizable :x="300" :y="0" :parent="true" style="border: 1px solid black;" :drag-cancel="'.cancel'">
<div class="cancel" style="padding: 6px; margin: 6px; background-color: #CCC; border: 2px solid">Cannot Drag Here</div>
</vue-draggable-resizable>
</div>
</div>

</div>

<footer>
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/app.min.js

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions src/components/vue-draggable-resizable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
</template>

<script>
import { matchesSelectorToParentElements } from '../utils/dom'
export default {
replace: true,
name: 'VueDraggableResizable',
Expand Down Expand Up @@ -100,6 +102,14 @@ export default {
return new Set(val.filter(h => s.has(h))).size === val.length
}
},
dragHandle: {
type: String,
default: null
},
dragCancel: {
type: String,
default: null
},
axis: {
type: String,
default: 'both',
Expand Down Expand Up @@ -205,6 +215,12 @@ export default {
const target = e.target || e.srcElement
if (this.$el.contains(target)) {
if (
(this.dragHandle && !matchesSelectorToParentElements(target, this.dragHandle, this.$el)) ||
(this.dragCancel && matchesSelectorToParentElements(target, this.dragCancel, this.$el))) {
return
}
this.reviewDimensions()
if (!this.enabled) {
Expand Down Expand Up @@ -419,9 +435,6 @@ export default {
position: absolute;
box-sizing: border-box;
}
.draggable:hover {
cursor: move;
}
.handle {
box-sizing: border-box;
display: none;
Expand Down
10 changes: 10 additions & 0 deletions src/utils/dom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export function matchesSelectorToParentElements (el, selector, baseNode) {
let node = el
do {
if (node.matches(selector)) return true
if (node === baseNode) return false
node = node.parentNode
} while (node)

return false
}
40 changes: 40 additions & 0 deletions test/unit/specs/VueDraggableResizable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,46 @@ describe('VueDraggableResizable.vue', function () {
})
})
})

it('should drag the component only by the dragHandle selector', function () {
const activated = sinon.spy()

const vm = mount(VueDraggableResizable, {
dragHandle: '.drag'
}, {
activated
}, '<div class="drag">Handle</div>')

simulate(vm.$el, 'mousedown')

expect(vm.$data.enabled).to.equal(false)

simulate(vm.$el.querySelector('.drag'), 'mousedown')

expect(vm.$data.enabled).to.equal(true)

sinon.assert.calledWith(activated)
})

it('should not drag the component by the dragCancel selector', function () {
const activated = sinon.spy()

const vm = mount(VueDraggableResizable, {
dragCancel: '.cancel'
}, {
activated
}, '<div class="cancel">Cancel</div>')

simulate(vm.$el.querySelector('.cancel'), 'mousedown')

expect(vm.$data.enabled).to.equal(false)

sinon.assert.notCalled(activated)

simulate(vm.$el, 'mousedown')

expect(vm.$data.enabled).to.equal(true)
})
})

/*************************
Expand Down

0 comments on commit 9809a47

Please sign in to comment.