2020-02-09 20:13:22 +02:00
|
|
|
import { ResizeObserver } from '@juggle/resize-observer';
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
export default {
|
2020-02-05 00:54:49 +02:00
|
|
|
inserted(el, binding, vn) {
|
2020-01-29 21:37:25 +02:00
|
|
|
const query = binding.value;
|
|
|
|
|
|
|
|
/*
|
|
|
|
const addClassRecursive = (el: Element, cls: string) => {
|
|
|
|
el.classList.add(cls);
|
|
|
|
if (el.children) {
|
|
|
|
for (const child of el.children) {
|
|
|
|
addClassRecursive(child, cls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeClassRecursive = (el: Element, cls: string) => {
|
|
|
|
el.classList.remove(cls);
|
|
|
|
if (el.children) {
|
|
|
|
for (const child of el.children) {
|
|
|
|
removeClassRecursive(child, cls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};*/
|
|
|
|
|
|
|
|
const addClass = (el: Element, cls: string) => {
|
|
|
|
el.classList.add(cls);
|
|
|
|
};
|
|
|
|
|
|
|
|
const removeClass = (el: Element, cls: string) => {
|
|
|
|
el.classList.remove(cls);
|
|
|
|
};
|
|
|
|
|
|
|
|
const calc = () => {
|
|
|
|
const width = el.clientWidth;
|
2020-02-05 00:54:49 +02:00
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
for (const q of query) {
|
|
|
|
if (q.max) {
|
|
|
|
if (width <= q.max) {
|
|
|
|
addClass(el, 'max-width_' + q.max + 'px');
|
|
|
|
} else {
|
|
|
|
removeClass(el, 'max-width_' + q.max + 'px');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (q.min) {
|
|
|
|
if (width >= q.min) {
|
|
|
|
addClass(el, 'min-width_' + q.min + 'px');
|
|
|
|
} else {
|
|
|
|
removeClass(el, 'min-width_' + q.min + 'px');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
calc();
|
|
|
|
|
2020-02-16 23:48:04 +02:00
|
|
|
vn.context.$on('hook:activated', calc);
|
|
|
|
|
2020-02-09 20:13:22 +02:00
|
|
|
const ro = new ResizeObserver((entries, observer) => {
|
|
|
|
calc();
|
|
|
|
});
|
|
|
|
|
|
|
|
ro.observe(el);
|
2020-01-29 21:37:25 +02:00
|
|
|
|
2020-02-09 20:13:22 +02:00
|
|
|
el._ro_ = ro;
|
2020-01-29 21:37:25 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
unbind(el, binding, vn) {
|
2020-02-09 20:13:22 +02:00
|
|
|
el._ro_.unobserve(el);
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
};
|