Repository navigation
Expand file tree
/
Copy paththreads.js
More file actions
180 lines (156 loc) · 4.18 KB
/
Copy paththreads.js
File metadata and controls
180 lines (156 loc) · 4.18 KB
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
/*global Drafts */
(function(exports) {
'use strict';
var threads = new Map();
var rthread = /\bthread=(.+)$/;
var currentId, lastId;
function cacheId() {
var matches = rthread.exec(window.location.hash);
currentId = (matches && matches.length) ? +(matches[1].trim()) : null;
if (currentId !== null && currentId !== lastId) {
lastId = currentId;
}
return currentId;
}
function Thread(thread) {
var length = Thread.FIELDS.length;
var key;
for (var i = 0; i < length; i++) {
key = Thread.FIELDS[i];
this[key] = thread[key];
}
this.messages = [];
}
Thread.FIELDS = [
'body', 'id', 'lastMessageSubject', 'lastMessageType',
'participants', 'timestamp', 'unreadCount'
];
Thread.fromMessage = function(record, options) {
var participants = [];
if (typeof record.delivery !== 'undefined') {
if (record.delivery === 'received' ||
record.delivery === 'not-downloaded') {
participants = [record.sender];
} else {
participants = record.receivers || [record.receiver];
}
}
return new Thread({
id: record.threadId,
participants: participants,
body: record.body,
timestamp: record.timestamp,
unreadCount: (options && options.unread) ? 1 : 0,
lastMessageType: record.type || 'sms'
});
};
Thread.fromDraft = function(record, options) {
var participants = record.recipients && record.recipients.length ?
record.recipients : [''];
var body = record.content && record.content.length ?
record.content.find(function(content) {
if (typeof content === 'string') {
return true;
}
}) : '';
return new Thread({
id: record.threadId || record.id,
participants: participants,
body: body,
timestamp: new Date(record.timestamp),
unreadCount: (options && options.unread) ? 1 : 0,
lastMessageType: record.type || 'sms'
});
};
Thread.create = function(record, options) {
if (record instanceof Thread) {
return record;
}
return record.delivery ?
Thread.fromMessage(record, options) :
Thread.fromDraft(record, options);
};
Thread.prototype = {
constructor: Thread,
get drafts() {
return Drafts.byThreadId(this.id);
},
get hasDrafts() {
return !!this.drafts.length;
}
};
var Threads = exports.Threads = {
registerMessage: function(message) {
var thread = Thread.create(message);
var threadId = message.threadId;
if (!this.has(threadId)) {
this.set(threadId, thread);
}
this.get(threadId).messages.push(message);
},
set: function(id, thread) {
var old, length, key;
id = +id;
if (threads.has(id)) {
// Updates the reference
old = threads.get(id);
length = Thread.FIELDS.length;
for (var i = 0; i < length; i++) {
key = Thread.FIELDS[i];
old[key] = thread[key];
}
return threads;
}
return threads.set(id, new Thread(thread));
},
get: function(id) {
return threads.get(+id);
},
has: function(id) {
return threads.has(+id);
},
delete: function(id) {
id = +id;
var thread = this.get(id);
if (thread && thread.hasDrafts) {
Drafts.delete({
threadId: id
});
}
return threads.delete(id);
},
clear: function() {
threads = new Map();
},
forEach: function(callback) {
threads.forEach(function(v, k) {
callback(v, k);
});
},
get size() {
// support: gecko 18 - size might be a function
if (typeof threads.size === 'function') {
return +threads.size();
}
return +threads.size;
},
get currentId() {
if (window.location.hash.startsWith('#thread=')) {
if (!currentId) {
currentId = cacheId();
}
} else {
currentId = null;
}
return currentId;
},
get lastId() {
return lastId;
},
get active() {
return threads.get(+Threads.currentId);
}
};
exports.Thread = Thread;
window.addEventListener('hashchange', cacheId);
}(this));