aboutsummaryrefslogtreecommitdiff
path: root/templates/mail/htmx/webmail.htmx.django
blob: 9ea1324459d0f061a7e6940a53dd0e9809e7dd38 (plain)
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
<div class="-m-8 flex flex-col h-[calc(100vh)]" id="webmail" data-mailbox-id="{{ active_mailbox.ID }}" data-folder-id="{{ active_folder.ID }}" data-folder-slug="{% if is_starred_view %}starred{% else %}{{ active_folder.Slug }}{% endif %}">
    <div class="flex items-center gap-3 px-4 h-12 shrink-0 border-b border-white/[0.04] bg-surface-900/50">
        <div class="dropdown" data-dropdown>
            <input type="hidden" value="{{ active_mailbox.ID }}" data-dropdown-value>
            <button type="button" class="flex items-center gap-2 px-3 py-1.5 rounded-lg bg-surface-800 border border-white/[0.06] hover:border-white/[0.1] text-sm text-zinc-200 transition-all duration-150" data-dropdown-trigger>
                <svg class="w-4 h-4 text-accent-400 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M21.75 6.75v10.5a2.25 2.25 0 0 1-2.25 2.25h-15a2.25 2.25 0 0 1-2.25-2.25V6.75m19.5 0A2.25 2.25 0 0 0 19.5 4.5h-15a2.25 2.25 0 0 0-2.25 2.25m19.5 0v.243a2.25 2.25 0 0 1-1.07 1.916l-7.5 4.615a2.25 2.25 0 0 1-2.36 0L3.32 8.91a2.25 2.25 0 0 1-1.07-1.916V6.75" />
                </svg>
                <span class="truncate max-w-[200px]" data-dropdown-label>{{ active_mailbox.Address }}</span>
                <svg class="w-3.5 h-3.5 text-zinc-500 shrink-0 transition-transform duration-150" data-dropdown-chevron fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="m19.5 8.25-7.5 7.5-7.5-7.5" />
                </svg>
            </button>
            <div class="dropdown-menu" data-dropdown-menu>
                <div class="p-2 border-b border-white/[0.04]">
                    <input type="text" placeholder="Search mailboxes..." class="dropdown-search" data-dropdown-search>
                </div>
                <div class="dropdown-options" data-dropdown-options>
                    {% for mailbox in mailboxes %}
                    <a href="/mail/webmail/{{ mailbox.ID }}" hx-get="/mail/webmail/{{ mailbox.ID }}" hx-target="#content" hx-swap="innerHTML" hx-push-url="true" class="dropdown-option block" data-dropdown-option data-value="{{ mailbox.ID }}" data-label="{{ mailbox.Address }}">
                        <p class="text-sm text-zinc-200">{{ mailbox.Address }}</p>
                        <p class="text-xs text-zinc-500">{{ mailbox.User.DisplayName }}</p>
                    </a>
                    {% endfor %}
                </div>
                <div class="dropdown-empty hidden" data-dropdown-empty>
                    <p class="text-xs text-zinc-500 text-center py-3">No mailboxes found</p>
                </div>
            </div>
        </div>

        <div class="flex-1 max-w-md">
            <div class="relative">
                <svg class="absolute left-3 top-1/2 -translate-y-1/2 w-4 h-4 text-zinc-500" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="m21 21-5.197-5.197m0 0A7.5 7.5 0 1 0 5.196 5.196a7.5 7.5 0 0 0 10.607 10.607Z" />
                </svg>
                <input type="text" name="search" value="{{ search }}" placeholder="Search emails..." class="w-full pl-10 pr-4 py-1.5 rounded-lg bg-surface-800 border border-white/[0.06] text-sm text-zinc-200 placeholder-zinc-600 focus:outline-none focus:border-accent-500/50 transition-colors duration-150" hx-get="/mail/webmail/{{ active_mailbox.ID }}/folder/{{ active_folder.ID }}/emails" hx-target="#webmail-emails" hx-swap="innerHTML" hx-trigger="input changed delay:300ms" hx-include="[name='search']">
            </div>
        </div>

        <div class="ml-auto flex items-center gap-2">
            <button hx-get="/mail/webmail/{{ active_mailbox.ID }}/compose" hx-target="#webmail-preview" hx-swap="innerHTML" class="webmail-btn webmail-btn-primary">
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="m16.862 4.487 1.687-1.688a1.875 1.875 0 1 1 2.652 2.652L10.582 16.07a4.5 4.5 0 0 1-1.897 1.13L6 18l.8-2.685a4.5 4.5 0 0 1 1.13-1.897l8.932-8.931Zm0 0L19.5 7.125M18 14v4.75A2.25 2.25 0 0 1 15.75 21H5.25A2.25 2.25 0 0 1 3 18.75V8.25A2.25 2.25 0 0 1 5.25 6H10" />
                </svg>
                Compose
            </button>
        </div>
    </div>

    <div class="flex flex-1 min-h-0">
        <div class="w-52 shrink-0 border-r border-white/[0.04] overflow-hidden" id="webmail-folders">
            {% include "mail/webmail/folders.django" %}
        </div>

        <div class="w-80 shrink-0 border-r border-white/[0.04] overflow-y-auto" id="webmail-emails">
            {% include "mail/webmail/emails.django" %}
        </div>

        <div class="flex-1 min-w-0 min-h-0" id="webmail-preview">
            {% include "mail/webmail/empty.django" %}
        </div>
    </div>
</div>

<template id="webmail-empty-template">
    {% include "mail/webmail/empty.django" %}
</template>

<div id="webmail-modal" class="fixed inset-0 z-50 hidden">
    <div class="absolute inset-0 bg-black/60 backdrop-blur-sm" data-webmail-modal-backdrop></div>
    <div class="flex items-center justify-center min-h-screen p-4">
        <div class="relative glass rounded-xl glow-border w-full max-w-sm p-6 space-y-4">
            <h3 id="webmail-modal-title" class="text-sm font-medium text-zinc-100"></h3>
            <div id="webmail-modal-body"></div>
            <div class="flex items-center justify-end gap-3 pt-2">
                <button data-webmail-modal-cancel class="px-4 py-2 text-xs text-zinc-400 hover:text-zinc-200 rounded-lg bg-surface-800 border border-white/[0.06] hover:border-white/[0.1] transition-all duration-150">Cancel</button>
                <button id="webmail-modal-confirm" class="px-4 py-2 text-xs text-white rounded-lg bg-red-500/80 hover:bg-red-500 border border-red-400/20 transition-all duration-150">Confirm</button>
            </div>
        </div>
    </div>
</div>

<script>
(function() {
    var webmailId = "webmail";

    function getWebmail() {
        return document.getElementById(webmailId);
    }

    if (!getWebmail()) return;

    function getMailboxID() {
        var element = getWebmail();
        return element ? element.dataset.mailboxId : null;
    }

    function getFolderSlug() {
        var element = getWebmail();
        return element ? element.dataset.folderSlug : null;
    }

    function isStale() {
        var element = getWebmail();
        return !element || !element.isConnected;
    }

    function refreshEmailList() {
        if (isStale()) return;
        var mailboxId = getMailboxID();
        var folderSlug = getFolderSlug();
        var url;

        if (folderSlug === "starred") {
            url = "/mail/webmail/" + mailboxId + "/starred/emails";
        } else {
            var element = getWebmail();
            var folderId = element.dataset.folderId;
            url = "/mail/webmail/" + mailboxId + "/folder/" + folderId + "/emails";
        }

        htmx.ajax("GET", url, { target: "#webmail-emails", swap: "innerHTML" });
    }

    function refreshFolderSidebar() {
        if (isStale()) return;
        var mailboxId = getMailboxID();
        var folderSlug = getFolderSlug();
        htmx.ajax("GET", "/mail/webmail/" + mailboxId + "/folders?folder=" + folderSlug, { target: "#webmail-folders", swap: "innerHTML" });
    }

    function clearPreview() {
        if (isStale()) return;
        var template = document.getElementById("webmail-empty-template");
        var preview = document.getElementById("webmail-preview");
        if (template && preview) {
            preview.innerHTML = template.innerHTML;
        }
    }

    var starredSvgFilled = '<svg class="w-4 h-4 text-yellow-400 fill-yellow-400" viewBox="0 0 24 24" stroke-width="1.5"><path d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.562.562 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.562.562 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z" /></svg>';
    var starredSvgEmpty = '<svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="1.5"><path stroke-linecap="round" stroke-linejoin="round" d="M11.48 3.499a.562.562 0 0 1 1.04 0l2.125 5.111a.563.563 0 0 0 .475.345l5.518.442c.499.04.701.663.321.988l-4.204 3.602a.563.563 0 0 0-.182.557l1.285 5.385a.562.562 0 0 1-.84.61l-4.725-2.885a.562.562 0 0 0-.586 0L6.982 20.54a.562.562 0 0 1-.84-.61l1.285-5.386a.562.562 0 0 0-.182-.557l-4.204-3.602a.562.562 0 0 1 .321-.988l5.518-.442a.563.563 0 0 0 .475-.345L11.48 3.5Z" /></svg>';

    document.body.addEventListener("emailStarred", function(event) {
        var triggerElement = event.target;
        if (triggerElement) {
            var emailId = triggerElement.getAttribute("data-email-star") || triggerElement.getAttribute("data-preview-star");
            if (!emailId) {
                triggerElement = triggerElement.closest("[data-email-star], [data-preview-star]");
                if (triggerElement) {
                    emailId = triggerElement.getAttribute("data-email-star") || triggerElement.getAttribute("data-preview-star");
                }
            }
            if (emailId) {
                var isFilled = triggerElement.querySelector(".fill-yellow-400");
                var newSvg = isFilled ? starredSvgEmpty : starredSvgFilled;

                var listStar = document.querySelector("[data-email-star='" + emailId + "']");
                if (listStar) listStar.innerHTML = newSvg;

                var previewStar = document.querySelector("[data-preview-star='" + emailId + "']");
                if (previewStar) previewStar.innerHTML = newSvg;
            }
        }
        refreshFolderSidebar();
        if (getFolderSlug() === "starred") {
            refreshEmailList();
        }
    });

    document.body.addEventListener("emailDeleted", function() {
        clearPreview();
        setTimeout(function() {
            refreshEmailList();
            refreshFolderSidebar();
        }, 50);
    });

    document.body.addEventListener("emailMoved", function() {
        clearPreview();
        setTimeout(function() {
            refreshEmailList();
            refreshFolderSidebar();
        }, 50);
    });

    document.body.addEventListener("emailReadChanged", function() {
        refreshEmailList();
        refreshFolderSidebar();
    });

    var modal = document.getElementById("webmail-modal");
    var modalTitle = document.getElementById("webmail-modal-title");
    var modalBody = document.getElementById("webmail-modal-body");
    var modalConfirm = document.getElementById("webmail-modal-confirm");
    var modalBackdrop = modal.querySelector("[data-webmail-modal-backdrop]");
    var modalCancel = modal.querySelector("[data-webmail-modal-cancel]");

    function closeModal() {
        modal.classList.add("hidden");
        modalBody.innerHTML = "";
    }

    modalBackdrop.addEventListener("click", closeModal);
    modalCancel.addEventListener("click", closeModal);

    window.webmailConfirm = function(options) {
        modalTitle.textContent = options.title || "Are you sure?";
        modalBody.innerHTML = '<p class="text-xs text-zinc-400 leading-relaxed">' + (options.message || "") + '</p>';
        modalConfirm.textContent = options.confirmText || "Confirm";
        modalConfirm.className = "px-4 py-2 text-xs text-white rounded-lg transition-all duration-150 " + (options.danger ? "bg-red-500/80 hover:bg-red-500 border border-red-400/20" : "bg-accent-500/80 hover:bg-accent-500 border border-accent-400/20");
        modal.classList.remove("hidden");

        var cloned = modalConfirm.cloneNode(true);
        modalConfirm.parentNode.replaceChild(cloned, modalConfirm);
        modalConfirm = cloned;

        modalConfirm.addEventListener("click", function() {
            closeModal();
            if (options.onConfirm) options.onConfirm();
        });
    };

    window.webmailPrompt = function(options) {
        modalTitle.textContent = options.title || "Input";
        modalBody.innerHTML = '<input type="text" id="webmail-modal-input" placeholder="' + (options.placeholder || "") + '" class="input-field mt-2" value="' + (options.value || "") + '">';
        modalConfirm.textContent = options.confirmText || "OK";
        modalConfirm.className = "px-4 py-2 text-xs text-white rounded-lg bg-accent-500/80 hover:bg-accent-500 border border-accent-400/20 transition-all duration-150";
        modal.classList.remove("hidden");

        setTimeout(function() {
            var input = document.getElementById("webmail-modal-input");
            if (input) input.focus();
        }, 50);

        var cloned = modalConfirm.cloneNode(true);
        modalConfirm.parentNode.replaceChild(cloned, modalConfirm);
        modalConfirm = cloned;

        modalConfirm.addEventListener("click", function() {
            var input = document.getElementById("webmail-modal-input");
            var value = input ? input.value.trim() : "";
            closeModal();
            if (value && options.onConfirm) options.onConfirm(value);
        });
    };
})();
</script>