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