-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathv-selectable.js
77 lines (67 loc) · 2.38 KB
/
v-selectable.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
70
71
72
73
74
75
76
77
import selectable, { objectAssignSimple } from './selectable';
const objectAssign = Object.assign || objectAssignSimple;
function initSelectable(el, params, arg) {
el.selectable = new selectable(objectAssign({
boundingBox: !!params.constraint ? document.querySelector(params.constraint) : el,
selectBoxSelector: params.box || '.selection',
boundingBoxSelector: params.constraint,
el
}, arg));
el.selectable.setSelectables(Array.prototype.slice.call(el.querySelectorAll(params.items || '.selectable')));
}
const vueSelectable = {
twoWay: false,
params: ['items', 'box', 'constraint'],
bind(el, binding) {
let arg, params;
if (!!el && !!binding) {
// Vue.js v2
arg = binding.value;
params = el.dataset;
initSelectable(el, params, arg);
}
},
update(value) {
if (!!this && !!this.el && !this.el.selectable) {
// Vue.js v1 - init selectable
initSelectable(this.el, this.el.dataset, value);
}
},
unbind(el) {
if (!el) {
el = this.el;
}
el.selectable.detach();
el.selectable = null;
}
};
export default vueSelectable;
/**
* Allows to change internal selectable items list
* @param {HTMLElement} el Element where v-selectable directive applied
* @param {string} itemSelector (optional) CSS selector of elements to be used as selectable items
* @return {number} number of selectable items or -1 if no selectable component found
*/
export function setSelectableItems(el, itemSelector) {
if (!!el && !!el.selectable && typeof el.selectable.setSelectables === 'function') {
let items = Array.prototype.slice.call(el.querySelectorAll(itemSelector || el.dataset.items || '.selectable'));
el.selectable.setSelectables(items);
return items.length;
} else {
return -1;
}
}
/**
* Sets options to directive
* @param {HTMLElement} el Element where v-selectable directive applied
* @param {object} options
*/
export function setOptions(el, options) {
if (!!el && !!el.selectable && typeof el.selectable.setSelectables === 'function') {
const needsAttach = el.selectable.rootElement == null && options.rootElement != null;
objectAssign(el.selectable, options);
if (needsAttach) {
el.selectable.attach();
}
}
}