This repository has been archived by the owner on Apr 8, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
MailboxList.js
146 lines (126 loc) · 3.93 KB
/
MailboxList.js
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
"use strict"
const remote = require('remote');
const Menu = remote.require('menu');
const MMailbox = require('./models/MMailbox')
const mailboxActions = require('./actions/mailbox')
class MailboxList {
/***************************************************************************/
// Lifecycle
/***************************************************************************/
constructor(app) {
this.app = app
}
/***************************************************************************/
// Active state
/***************************************************************************/
/**
* Updates the active mailbox
* @param mailboxId: the id of the mailbox to make active
*/
updateActive(mailboxId) {
$('#mailbox-list .mailbox.active:not([data-id="' + mailboxId + '"])').removeClass('active')
$('#mailbox-list .mailbox[data-id="' + mailboxId + '"]').addClass('active')
}
/***************************************************************************/
// Rendering
/***************************************************************************/
/**
* Renders a single mailbox
* @param activeId: the id of the active mailbox
* @param mailbox: the mailbox record to render
* @param index: the index of the mailbox
* @return the element
*/
renderMailbox(activeId, mailbox, index) {
let className = mailbox.id === activeId ? 'mailbox active' : 'mailbox'
// Generate avatar
var avatarHTML
if (mailbox.avatar) {
avatarHTML = '<img class="avatar" src="' + mailbox.avatar + '" />'
className += ' avatar'
} else {
avatarHTML = '<span class="index">' + (index + 1) + '</span>'
className += ' index'
}
// Generate title
var title = []
if (mailbox.email) { title.push(mailbox.email) }
if (mailbox.name) { title.push('(' + mailbox.name + ')') }
title = title.join(' ')
// Generate unread
var unread
if (mailbox.unread) {
unread = '<span class="unread">' + mailbox.unread + '</span>'
}
// Generate whole button
let button = $([
'<div class="mailbox-container" data-id="' + mailbox.id + '">',
'<div class="' + className + '" title="' + title + '" data-id="' + mailbox.id + '" data-type="' + mailbox.type + '">',
avatarHTML,
'</div>',
unread,
'</div>'
].join('\n'))
// Bind events
button.on('click', (evt) => {
evt.preventDefault()
this.app.procChangeActive(mailbox.id)
})
button.on('contextmenu', (evt) => {
evt.preventDefault()
let menu = Menu.buildFromTemplate([
{ label: 'Delete', click: () => {
if (button.is('.active')) { this.app.procChangeActive() }
MMailbox.remove(mailbox.id)
this.app.procChange()
}},
{ type: "separator" },
{ label: 'Reload', click: () => {
$('#mailboxes .mailbox[data-id="' + mailbox.id + '"]').attr('src', 'https://inbox.google.com/')
this.app.procResyncRemote()
}},
{ label: 'Inspect', click: () => {
$('#mailboxes .mailbox[data-id="' + mailbox.id + '"]').get(0).openDevTools()
}}
])
menu.popup(remote.getCurrentWindow())
})
return button
}
/**
* Renders the add button
* @return the button to append to the dom
*/
renderAdd() {
let button = $('<div class="add-mailbox" title="Add Mailbox">+</div>')
button.on('click', (evt) => {
evt.preventDefault()
let menu = Menu.buildFromTemplate([
{ label: 'Add Inbox', click: () => {
mailboxActions.addInboxMailbox(this.app)
}},
{ label: 'Add Gmail', click: () => {
mailboxActions.addGmailMailbox(this.app)
}}
])
menu.popup(remote.getCurrentWindow())
})
return button
}
/**
* Renders the mailbox list
*/
render() {
// Store state
let container = $('#mailbox-list')
let activeId = container.find('.mailbox.active').attr('data-id')
container.empty()
// Render the mailboxes
container.append(MMailbox.all().map((mailbox, index) => {
return this.renderMailbox(activeId, mailbox, index)
}))
// Render the other controls
container.append(this.renderAdd())
}
}
module.exports = MailboxList