Commit 5d4e5fbb authored by Randall Leeds's avatar Randall Leeds

update jwz

- Updates jwz.js to ed442f8e510e3b656bb98fc1c3ec072b0aee61d4
  which include the fix for id scope that was already merged
  downstream, here, but also includes the removal of the underscore
  dependency.
- Remove underscore.js
parent c992f248
// example usage:
// example usage:
// thread = mail.messageThread().thread(messages.map(
// function(message) {
// function(message) {
// return mail.message(message.subject, message.messageId, message.references);
// }
// ));
......@@ -21,7 +21,7 @@
function messageContainer(message) {
return function(message) {
var children = [];
function getConversation(id) {
var child = this.getSpecificChild(id);
var flattened = [];
......@@ -29,26 +29,26 @@
if(child.message) flattened.unshift(child.message);
return flattened;
}
function flattenChildren() {
var messages = [];
_.each(this.children, function(child) {
this.children.forEach(function(child) {
if (child.message) messages.push(child.message);
var nextChildren = child.flattenChildren();
if (nextChildren) {
_.each(nextChildren, function(nextChild) {
nextChildren.forEach(function(nextChild) {
messages.push(nextChild);
})
}
});
if (messages.length > 0) return messages;
}
function getSpecificChild(id) {
var instance = this;
if (instance.message && instance.message.id == id) return instance;
var specificChild = null;
_.each(instance.children, function(child) {
instance.children.forEach(function(child) {
var found = child.getSpecificChild(id);
if (found) {
specificChild = found;
......@@ -72,28 +72,30 @@
}
return top;
}
function addChild(child) {
if(child.parent) child.parent.removeChild(child);
this.children.push(child);
child.parent = this;
}
function removeChild(child) {
this.children = _.without(this.children, child);
this.children = this.children.filter(function (other) {
return other !== child
});
delete child.parent;
}
function hasDescendant(container) {
if (this === container) return true;
if (this.children.length < 1) return false;
var descendantPresent = false;
_.each(this.children, function(child) {
this.children.forEach(function(child) {
if(child.hasDescendant(container)) descendantPresent = true;
})
return descendantPresent;
}
return {
message: message,
children: children,
......@@ -111,19 +113,20 @@
function messageThread() {
return function() {
var idTable = {};
function thread(messages) {
idTable = this.createIdTable(messages);
var root = messageContainer();
_.each(_.keys(idTable), function(id) {
Object.keys(idTable).forEach(function(id) {
var container = idTable[id];
if (!_.include(_.keys(container), "parent")) root.addChild(container);
if (typeof(container.parent) === 'undefined') {
root.addChild(container);
}
})
delete idTable;
pruneEmpties(root);
return root;
}
function pruneEmpties(parent) {
for(var i = parent.children.length - 1; i >= 0; i--) {
var container = parent.children[i];
......@@ -141,7 +144,7 @@
}
}
}
function promoteChildren(parent, container) {
for(var i = container.children.length - 1; i >= 0; i--) {
var child = container.children[i];
......@@ -149,10 +152,10 @@
}
parent.removeChild(container);
}
function createIdTable(messages) {
idTable = {};
_.map(messages, function(message) {
messages.forEach(function(message) {
var parentContainer = getContainer(message.id);
parentContainer.message = message;
var prev = null;
......@@ -160,9 +163,10 @@
if (typeof(references) == 'string') {
references = [references]
}
_.each(references, function(reference) {
references.forEach(function(reference) {
var container = getContainer(reference);
if (prev && !_.include(_.keys(container), "parent") && !container.hasDescendant(prev)) {
if (prev && typeof(container.parent) === 'undefined'
&& !container.hasDescendant(prev)) {
prev.addChild(container);
}
prev = container;
......@@ -173,24 +177,24 @@
})
return idTable;
}
function getContainer(id) {
if (_.include(_.keys(idTable), id)) {
if (typeof(idTable[id]) !== 'undefined') {
return idTable[id];
} else {
return createContainer(id);
}
}
function createContainer(id) {
var container = mail.messageContainer();
idTable[id] = container;
return container;
}
function groupBySubject(root) {
var subjectTable = {};
_.each(root.children, function(container) {
root.children.forEach(function(container) {
if(!container.message) {
var c = container.children[0];
} else {
......@@ -204,7 +208,7 @@
var subject = helpers.normalizeSubject(message.subject);
if (subject.length === 0) return;
var existing = subjectTable[subject];
if (!existing) {
subjectTable[subject] = c;
} else if (
......@@ -215,7 +219,7 @@
)
) {
subjectTable[subject] = c;
}
}
})
for(var i = root.children.length - 1; i >= 0; i--) {
......@@ -226,17 +230,17 @@
} else {
var subject = container.children[0].message.subject;
}
subject = helpers.normalizeSubject(subject);
var c = subjectTable[subject];
if (!c || c === container) continue;
if (
(typeof(c.message) === "undefined") &&
(typeof(container.message) === "undefined")
) {
_.each(container.children, function(ctr) {
container.children.forEach(function(ctr) {
c.addChild(ctr);
})
container.parent.removeChild(container);
......@@ -257,10 +261,10 @@
subjectTable[subject] = newContainer;
}
}
return subjectTable;
}
return {
getContainer: getContainer,
createContainer: createContainer,
......@@ -269,49 +273,48 @@
pruneEmpties: pruneEmpties,
groupBySubject: groupBySubject,
thread: thread,
get idTable() { return idTable }
get idTable() { return idTable; }
}
}();
}
var helpers = {
isReplyOrForward: function(subject) {
var pattern = /^(Re|Fwd)/i;
var match = subject.match(pattern);
return match ? true : false;
},
normalizeSubject: function(subject) {
var pattern = /((Re|Fwd)(\[[\d+]\])?:(\s)?)*([\w]*)/i;
var match = subject.match(pattern);
return match ? match[5] : false;
},
normalizeMessageId: function(messageId) {
var pattern = /<([^<>]+)>/;
var match = messageId.match(pattern);
return match ? match[1] : null;
},
parseReferences: function(references) {
if (!references) return null;
var pattern = /<[^<>]+>/g;
return _.map(references.match(pattern), function(match) {
return references.match(pattern).map(function(match) {
return match.match(/[^<>]+/)[0];
})
}
}
var mail = this.mail = {
message: message,
messageContainer: messageContainer,
messageThread: messageThread,
helpers: helpers
};
if (typeof module !== 'undefined' && module.exports) {
_ = require('underscore');
module.exports = mail;
}
})();
\ No newline at end of file
})();
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment