2020-01-29 21:37:25 +02:00
|
|
|
export default {
|
2020-08-09 09:51:02 +03:00
|
|
|
inserted(src, binding, vn) {
|
2020-01-29 21:37:25 +02:00
|
|
|
const query = binding.value;
|
|
|
|
|
2020-08-09 09:51:02 +03:00
|
|
|
// TODO: 要素をもらうというよりはカスタム幅算出関数をもらうようにしてcalcで都度呼び出して計算するようにした方が柔軟そう
|
|
|
|
// その場合はunbindの方も改修することを忘れずに
|
|
|
|
const el = query.el ? query.el() : src;
|
|
|
|
|
2020-01-29 21:37:25 +02:00
|
|
|
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-08-09 09:51:02 +03:00
|
|
|
if (query.max) {
|
|
|
|
for (const v of query.max) {
|
|
|
|
if (width <= v) {
|
|
|
|
addClass(src, 'max-width_' + v + 'px');
|
2020-01-29 21:37:25 +02:00
|
|
|
} else {
|
2020-08-09 09:51:02 +03:00
|
|
|
removeClass(src, 'max-width_' + v + 'px');
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-09 09:51:02 +03:00
|
|
|
}
|
|
|
|
if (query.min) {
|
|
|
|
for (const v of query.min) {
|
|
|
|
if (width >= v) {
|
|
|
|
addClass(src, 'min-width_' + v + 'px');
|
2020-01-29 21:37:25 +02:00
|
|
|
} else {
|
2020-08-09 09:51:02 +03:00
|
|
|
removeClass(src, 'min-width_' + v + 'px');
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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();
|
|
|
|
});
|
2020-03-04 04:45:33 +02:00
|
|
|
|
2020-02-09 20:13:22 +02:00
|
|
|
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
|
|
|
},
|
|
|
|
|
2020-08-09 09:51:02 +03:00
|
|
|
unbind(src, binding, vn) {
|
|
|
|
const query = binding.value;
|
|
|
|
|
|
|
|
const el = query.el ? query.el() : src;
|
|
|
|
|
2020-02-09 20:13:22 +02:00
|
|
|
el._ro_.unobserve(el);
|
2020-01-29 21:37:25 +02:00
|
|
|
}
|
|
|
|
};
|