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