From bd9fc14659bc948c751ad95ae98034d8494242be Mon Sep 17 00:00:00 2001 From: Adrien Dieudonne Date: Mon, 23 May 2016 08:40:40 +0200 Subject: [PATCH] [FIX] mail: mutiple read more A read more was added to every node having a mail-quote tag. However all quotes and their child nodes are tagged, leading to numerous unnecessary read more / read less. Now, all consecutive mail-quote are joined in one 'read more/read less'. --- addons/mail/static/src/js/thread.js | 86 +++++++++++++++++++++---- addons/mail/static/src/less/thread.less | 3 - 2 files changed, 72 insertions(+), 17 deletions(-) diff --git a/addons/mail/static/src/js/thread.js b/addons/mail/static/src/js/thread.js index 21a0e1bb153..7af502e8d00 100644 --- a/addons/mail/static/src/js/thread.js +++ b/addons/mail/static/src/js/thread.js @@ -110,22 +110,12 @@ var Thread = Widget.extend({ })); _.each(msgs, function(msg) { - self.$('[data-message-id="' + msg.id + '"] .o_mail_timestamp') - .data('date', msg.date); + var $msg = self.$('.o_thread_message[data-message-id="'+ msg.id +'"]'); + $msg.find('.o_mail_timestamp').data('date', msg.date); + + self.insert_read_more($msg); }); - this.$('[data-o-mail-quote="1"]').each(function () { - var $content = $(this); - var $read_more = $('').text(read_more); - var is_read_more = true; - $read_more.click(function(e) { - e.preventDefault(); - is_read_more = !is_read_more; - $content.toggle(!is_read_more); - $read_more.text(is_read_more ? read_more : read_less); - }); - $read_more.insertBefore($content); - }); if (!this.update_timestamps_interval) { this.update_timestamps_interval = setInterval(function() { self.update_timestamps(); @@ -133,6 +123,74 @@ var Thread = Widget.extend({ } }, + /** + * Modifies $element to add the 'read more/read less' functionality + * All element nodes with "data-o-mail-quote" attribute are concerned. + * All text nodes after a ""#stopSpelling" element are concerned. + * Those text nodes need to be wrapped in a span (toggle functionality). + * All consecutive elements are joined in one 'read more/read less'. + */ + insert_read_more: function ($element) { + var self = this; + + var groups = []; + var read_more_nodes; + + // nodeType 1: element_node + // nodeType 3: text_node + var $children = $element.contents() + .filter(function() { + return this.nodeType === 1 || this.nodeType === 3 && this.nodeValue.trim(); + }); + + _.each($children, function(child) { + var $child = $(child); + + // Hide Text nodes if "stopSpelling" + if (child.nodeType === 3 && $child.prevAll("#stopSpelling").length > 0) { + // Convert Text nodes to Element nodes + var $child = $('', { + text: child.textContent, + "data-o-mail-quote": "1", + }); + child.parentNode.replaceChild($child[0], child); + } + + // Create array for each "read more" with nodes to toggle + if ($child.attr('data-o-mail-quote')) { + if (!read_more_nodes) { + read_more_nodes = []; + groups.push(read_more_nodes); + } + $child.hide(); + read_more_nodes.push($child); + } else { + read_more_nodes = undefined; + self.insert_read_more($child); + } + }); + + _.each(groups, function(group) { + // Insert link just before the first node + var $read_more = $('', { + class: "o_mail_read_more", + href: "#", + text: read_more, + }).insertBefore(group[0]); + + // Toggle All next nodes + var is_read_more = true; + $read_more.click(function(e) { + e.preventDefault(); + is_read_more = !is_read_more; + _.each(group, function ($child) { + $child.hide(); + $child.toggle(!is_read_more); + }); + $read_more.text(is_read_more ? read_more : read_less); + }); + }); + }, update_timestamps: function () { this.$('.o_mail_timestamp').each(function() { var date = $(this).data('date'); diff --git a/addons/mail/static/src/less/thread.less b/addons/mail/static/src/less/thread.less index a70d9954a70..5da7d2b4963 100644 --- a/addons/mail/static/src/less/thread.less +++ b/addons/mail/static/src/less/thread.less @@ -161,9 +161,6 @@ .o-webclient-padding(); } - [data-o-mail-quote="1"] { - display: none; - } .o_thread_message .o_thread_message_core .o_mail_read_more { display: block; }