1
0
mirror of https://github.com/hotomoe/hotomoe synced 2024-12-19 17:18:17 +09:00
hotomoe/src/web/app/desktop/mixins/user-preview.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-03-18 20:05:11 +09:00
import * as riot from 'riot';
2017-02-18 17:27:57 +09:00
riot.mixin('user-preview', {
2017-02-19 07:14:07 +09:00
init: function() {
2017-02-18 17:29:27 +09:00
const scan = () => {
2017-02-18 17:27:57 +09:00
this.root.querySelectorAll('[data-user-preview]:not([data-user-preview-attached])')
.forEach(attach.bind(this));
2017-02-18 17:29:27 +09:00
};
this.on('mount', scan);
this.on('updated', scan);
2017-02-18 17:27:57 +09:00
}
});
function attach(el) {
el.setAttribute('data-user-preview-attached', true);
const user = el.getAttribute('data-user-preview');
let tag = null;
let showTimer = null;
let hideTimer = null;
el.addEventListener('mouseover', () => {
clearTimeout(showTimer);
clearTimeout(hideTimer);
showTimer = setTimeout(show, 500);
});
el.addEventListener('mouseleave', () => {
clearTimeout(showTimer);
clearTimeout(hideTimer);
hideTimer = setTimeout(close, 500);
});
this.on('unmount', () => {
clearTimeout(showTimer);
clearTimeout(hideTimer);
close();
});
const show = () => {
if (tag) return;
const preview = document.createElement('mk-user-preview');
const rect = el.getBoundingClientRect();
const x = rect.left + el.offsetWidth + window.pageXOffset;
const y = rect.top + window.pageYOffset;
preview.style.top = y + 'px';
preview.style.left = x + 'px';
preview.addEventListener('mouseover', () => {
clearTimeout(hideTimer);
});
preview.addEventListener('mouseleave', () => {
clearTimeout(showTimer);
hideTimer = setTimeout(close, 500);
});
tag = riot.mount(document.body.appendChild(preview), {
user: user
})[0];
2017-02-18 17:29:27 +09:00
};
2017-02-18 17:27:57 +09:00
const close = () => {
if (tag) {
tag.close();
tag = null;
}
2017-02-18 17:29:27 +09:00
};
2017-02-18 17:27:57 +09:00
}