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