forked from pespantelis/vue-crop
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvue2-crop.js
69 lines (60 loc) · 1.26 KB
/
vue2-crop.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
var Vue2Crop = {}
var defaultOptions = {}
var events = [
'create',
'start',
'move',
'end',
'focus',
'blur',
'change',
'select',
'remove'
]
const initJCrop = function (el, binding, vnode, last) {
if (last && last.jcrop) {
if (last.elm !== vnode.elm) {
last.jcrop.destroy()
last.jcrop = null
} else {
vnode.jcrop = last.jcrop
last.jcrop = null
return
}
}
let event = binding.arg
if ($.inArray(event, events) === -1) {
console.warn('Invalid v-crop event: ' + event)
return
}
if (!vnode.jcrop) {
vnode.jcrop = $.Jcrop(el, defaultOptions)
}
let updatedOptions = {}
if (event === 'select') {
updatedOptions.onSelect = binding.value
}
vnode.jcrop.setOptions(updatedOptions)
vnode.jcrop.release()
}
Vue2Crop.install = function (Vue) {
Vue.directive('crop', {
acceptStatement: true,
inserted(el, binding, vnode) {
initJCrop(el, binding, vnode, null)
},
update: initJCrop,
unbind(el, binding, vnode) {
if (!vnode.jcrop) {
return
}
vnode.jcrop.ui.holder.off('crop' + binding.arg)
vnode.jcrop.destroy()
vnode.jcrop = null
}
})
}
Vue2Crop.setOptions = function (opts) {
defaultOptions = opts
}
export default Vue2Crop