File size: 2,900 Bytes
7c28e1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
document.addEventListener('DOMContentLoaded', () => {
    const chatBotContainer = document.getElementById('chat-bot-container');
    let isDragging = false;
    let offsetX, offsetY;
    let saveTimeout;

    // Загрузка позиции и размера из localStorage
    const savedPosition = localStorage.getItem('chatBotPosition');
    const savedSize = localStorage.getItem('chatBotSize');

    if (savedPosition) {
        const { left, top } = JSON.parse(savedPosition);
        chatBotContainer.style.left = `${left}px`;
        chatBotContainer.style.top = `${top}px`;
    }

    if (savedSize) {
        const { width, height } = JSON.parse(savedSize);
        chatBotContainer.style.width = `${width}px`;
        chatBotContainer.style.height = `${height}px`;
    }

    // Добавляем обработчики для перетаскивания
    chatBotContainer.querySelector('.chat-header').addEventListener('mousedown', (e) => {
        isDragging = true;
        offsetX = e.clientX - chatBotContainer.offsetLeft;
        offsetY = e.clientY - chatBotContainer.offsetTop;
    });

    document.addEventListener('mousemove', (e) => {
        if (isDragging) {
            chatBotContainer.style.left = `${e.clientX - offsetX}px`;
            chatBotContainer.style.top = `${e.clientY - offsetY}px`;
        }
    });

    document.addEventListener('mouseup', () => {
        isDragging = false;
        scheduleSave();
    });

    // // Добавляем обработчики для изменения размера
    // chatBotContainer.addEventListener('mousedown', (e) => {
    //     if (e.target.classList.contains('resizable')) {
    //         isDragging = true;
    //         offsetX = e.clientX - chatBotContainer.offsetWidth;
    //         offsetY = e.clientY - chatBotContainer.offsetHeight;
    //     }
    // });

    document.addEventListener('mousemove', (e) => {
        if (isDragging && e.target.classList.contains('resizable')) {
            chatBotContainer.style.width = `${e.clientX - offsetX}px`;
            chatBotContainer.style.height = `${e.clientY - offsetY}px`;
        }
    });

    document.addEventListener('mouseup', () => {
        isDragging = false;
        scheduleSave();
    });

    function scheduleSave() {
        clearTimeout(saveTimeout);
        saveTimeout = setTimeout(savePositionAndSize, 200); // Задержка в 200 миллисекунд
    }

    function savePositionAndSize() {
        const position = {
            left: chatBotContainer.offsetLeft,
            top: chatBotContainer.offsetTop
        };
        const size = {
            width: chatBotContainer.offsetWidth,
            height: chatBotContainer.offsetHeight
        };

        localStorage.setItem('chatBotPosition', JSON.stringify(position));
        localStorage.setItem('chatBotSize', JSON.stringify(size));
    }
});