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