Browse Source

Implemented successful inheritance

pull/6/head
x620 9 years ago
parent
commit
741266307a
  1. 181
      mail_archives/static/src/js/archives.js
  2. 117
      mail_archives/static/src/js/base.js

181
mail_archives/static/src/js/archives.js

@ -1,7 +1,184 @@
odoo.define('mail_archives.archives', function (require) { odoo.define('mail_archives.archives', function (require) {
"use strict"; "use strict";
var chat_manager = require('mail_base.base');
return chat_manager;
var base_obj = require('mail_base.base');
//-------------------------------------------------------------------------------
var bus = require('bus.bus').bus;
var config = require('web.config');
var core = require('web.core');
var data = require('web.data');
var Model = require('web.Model');
var session = require('web.session');
var time = require('web.time');
var web_client = require('web.web_client');
var _t = core._t;
var LIMIT = 100;
var MessageModel = new Model('mail.message', session.context);
//-------------------------------------------------------------------------------
var emojis = [];
var emoji_substitutions = {};
var needaction_counter = 0;
var mention_partner_suggestions = [];
var discuss_ids = {};
//-------------------------------------------------------------------------------
// Inherit class and override methods
base_obj.MailTools.include({
make_message: function (data) {
var msg = {
id: data.id,
author_id: data.author_id,
body_short: data.body_short || "",
body: data.body || "",
date: moment(time.str_to_datetime(data.date)),
message_type: data.message_type,
subtype_description: data.subtype_description,
is_author: data.author_id && data.author_id[0] === session.partner_id,
is_note: data.is_note,
is_system_notification: data.message_type === 'notification' && data.model === 'mail.channel',
attachment_ids: data.attachment_ids,
subject: data.subject,
email_from: data.email_from,
record_name: data.record_name,
tracking_value_ids: data.tracking_value_ids,
channel_ids: data.channel_ids,
model: data.model,
res_id: data.res_id,
url: session.url("/mail/view?message_id=" + data.id)
};
_.each(_.keys(emoji_substitutions), function (key) {
var escaped_key = String(key).replace(/([.*+?=^!:${}()|[\]\/\\])/g, '\\$1');
var regexp = new RegExp("(?:^|\\s|<[a-z]*>)(" + escaped_key + ")(?=\\s|$|</[a-z]*>)", "g");
msg.body = msg.body.replace(regexp, ' <span class="o_mail_emoji">'+emoji_substitutions[key]+'</span> ');
});
// Add property to Object
var properties = this.get_properties(msg);
properties.is_archive = this.property_descr("channel_archive", msg, this);
Object.defineProperties(msg, properties);
// Set archive flag
msg = this.set_channel_flags(data, msg);
msg.is_archive = true;
if (msg.model === 'mail.channel') {
// Add 'channel_archive' to channel_array
var channel_array = this.get_channel_array(msg).concat('channel_archive');
var real_channels = _.without(channel_array);
var origin = real_channels.length === 1 ? real_channels[0] : undefined;
var channel = origin && base_obj.chat_manager.get_channel(origin);
if (channel) {
msg.origin_id = origin;
msg.origin_name = channel.name;
}
}
// Compute displayed author name or email
if ((!msg.author_id || !msg.author_id[0]) && msg.email_from) {
msg.mailto = msg.email_from;
} else {
msg.displayed_author = msg.author_id && msg.author_id[1] ||
msg.email_from || _t('Anonymous');
}
// Don't redirect on author clicked of self-posted messages
msg.author_redirect = !msg.is_author;
// Compute the avatar_url
if (msg.author_id && msg.author_id[0]) {
msg.avatar_src = "/web/image/res.partner/" + msg.author_id[0] + "/image_small";
} else if (msg.message_type === 'email') {
msg.avatar_src = "/mail/static/src/img/email_icon.png";
} else {
msg.avatar_src = "/mail/static/src/img/smiley/avatar.jpg";
}
// add anchor tags to urls
msg.body = this.parse_and_transform(msg.body, this.add_link);
// Compute url of attachments
_.each(msg.attachment_ids, function(a) {
a.url = '/web/content/' + a.id + '?download=true';
});
return msg;
},
fetch_from_channel: function (channel, options) {
options = options || {};
// Add archive domain
var domain = (channel.id === "channel_archive") ? [] : this.get_domain(channel);
var cache = this.get_channel_cache(channel, options.domain);
if (options.domain) {
domain = new data.CompoundDomain(domain, options.domain || []);
}
if (options.load_more) {
var min_message_id = cache.messages[0].id;
domain = new data.CompoundDomain([['id', '<', min_message_id]], domain);
}
var self = this;
return MessageModel.call('message_fetch', [domain], {limit: LIMIT}).then(function (msgs) {
if (!cache.all_history_loaded) {
cache.all_history_loaded = msgs.length < LIMIT;
}
cache.loaded = true;
_.each(msgs, function (msg) {
self.add_message(msg, {channel_id: channel.id, silent: true, domain: options.domain});
});
var channel_cache = self.get_channel_cache(channel, options.domain || []);
return channel_cache.messages;
});
},
start: function(){
// Add archive channel
this.add_channel({
id: "channel_archive",
name: _t("Archive"),
type: "static"
});
var load_channels = session.rpc('/mail/client_action').then(function (result) {
_.each(result.channel_slots, function (channels) {
_.each(channels, cls.add_channel);
});
needaction_counter = result.needaction_inbox_counter;
mention_partner_suggestions = result.mention_partner_suggestions;
});
var load_emojis = session.rpc("/mail/chat_init").then(function (result) {
emojis = result.emoji;
_.each(emojis, function(emoji) {
emoji_substitutions[_.escape(emoji.source)] = emoji.substitution;
});
});
var ir_model = new Model("ir.model.data");
var load_menu_id = ir_model.call("xmlid_to_res_id", ["mail.mail_channel_menu_root_chat"], {}, {shadow: true});
var load_action_id = ir_model.call("xmlid_to_res_id", ["mail.mail_channel_action_client_chat"], {}, {shadow: true});
bus.on('notification', null, cls.on_notification);
return $.when(load_menu_id, load_action_id, load_channels, load_emojis).then(function (menu_id, action_id) {
discuss_ids = {
menu_id: menu_id,
action_id: action_id
};
bus.start_polling();
});
}
});
// Change chat_manager with override methods
var cls = new base_obj.MailTools(base_obj.chat_manager);
base_obj.chat_manager.is_ready = cls.start();
console.log('MailTools:', cls);
return base_obj.chat_manager;
}); });

117
mail_archives/static/src/js/base.js

@ -61,7 +61,7 @@ var channel_seen = _.throttle(function (channel) {
}, 3000); }, 3000);
var MailFunctions = core.Class.extend({
var MailTools = core.Class.extend({
init: function () { init: function () {
// suggested regexp (gruber url matching regexp, adapted to js, see https://gist.github.com/gruber/8891611) // suggested regexp (gruber url matching regexp, adapted to js, see https://gist.github.com/gruber/8891611)
this.url_regexp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi; this.url_regexp = /\b((?:https?:\/\/|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/gi;
@ -215,6 +215,27 @@ var MailFunctions = core.Class.extend({
}; };
}, },
get_properties: function(msg){
return {
is_starred: this.property_descr("channel_starred", msg, this),
is_needaction: this.property_descr("channel_inbox", msg, this)
}
},
set_channel_flags: function(data, msg){
if (_.contains(data.needaction_partner_ids, session.partner_id)) {
msg.is_needaction = true;
}
if (_.contains(data.starred_partner_ids, session.partner_id)) {
msg.is_starred = true;
}
return msg
},
get_channel_array: function(msg){
return [ msg.channel_ids, 'channel_inbox', 'channel_starred' ];
},
make_message: function (data) { make_message: function (data) {
var msg = { var msg = {
id: data.id, id: data.id,
@ -244,22 +265,11 @@ var MailFunctions = core.Class.extend({
msg.body = msg.body.replace(regexp, ' <span class="o_mail_emoji">'+emoji_substitutions[key]+'</span> '); msg.body = msg.body.replace(regexp, ' <span class="o_mail_emoji">'+emoji_substitutions[key]+'</span> ');
}); });
var self = this;
Object.defineProperties(msg, {
is_starred: this.property_descr("channel_starred", msg, self),
is_needaction: this.property_descr("channel_inbox", msg, self),
is_archive: this.property_descr("channel_archive", msg, self)
});
Object.defineProperties(msg, this.get_properties(msg));
if (_.contains(data.needaction_partner_ids, session.partner_id)) {
msg.is_needaction = true;
}
if (_.contains(data.starred_partner_ids, session.partner_id)) {
msg.is_starred = true;
}
msg.is_archive = true;
msg = this.set_channel_flags(data, msg);
if (msg.model === 'mail.channel') { if (msg.model === 'mail.channel') {
var real_channels = _.without(msg.channel_ids, 'channel_inbox', 'channel_starred', 'channel_archive');
var real_channels = _.without(this.get_channel_array(msg));
var origin = real_channels.length === 1 ? real_channels[0] : undefined; var origin = real_channels.length === 1 ? real_channels[0] : undefined;
var channel = origin && chat_manager.get_channel(origin); var channel = origin && chat_manager.get_channel(origin);
if (channel) { if (channel) {
@ -425,14 +435,16 @@ var MailFunctions = core.Class.extend({
}); });
}, },
get_domain: function(channel){
return (channel.id === "channel_inbox") ? [['needaction', '=', true]] :
(channel.id === "channel_starred") ? [['starred', '=', true]] :
[['channel_ids', 'in', channel.id]];
},
// options: domain, load_more // options: domain, load_more
fetch_from_channel: function (channel, options) { fetch_from_channel: function (channel, options) {
options = options || {}; options = options || {};
var domain =
(channel.id === "channel_inbox") ? [['needaction', '=', true]] :
(channel.id === "channel_starred") ? [['starred', '=', true]] :
(channel.id === "channel_archive") ? [] :
[['channel_ids', 'in', channel.id]];
var domain = this.get_domain(channel);
var cache = this.get_channel_cache(channel, options.domain); var cache = this.get_channel_cache(channel, options.domain);
if (options.domain) { if (options.domain) {
@ -704,12 +716,6 @@ var MailFunctions = core.Class.extend({
type: "static" type: "static"
}); });
this.add_channel({
id: "channel_archive",
name: _t("Archive"),
type: "static"
});
var load_channels = session.rpc('/mail/client_action').then(function (result) { var load_channels = session.rpc('/mail/client_action').then(function (result) {
_.each(result.channel_slots, function (channels) { _.each(result.channel_slots, function (channels) {
_.each(channels, cls.add_channel); _.each(channels, cls.add_channel);
@ -739,10 +745,9 @@ var MailFunctions = core.Class.extend({
bus.start_polling(); bus.start_polling();
}); });
} }
}); });
var cls = new MailFunctions();
var cls = new MailTools();
// Public interface // Public interface
//---------------------------------------------------------------------------------- //----------------------------------------------------------------------------------
@ -1074,58 +1079,10 @@ chat_manager.bus.on('client_action_open', null, function (open) {
client_action_open = open; client_action_open = open;
}); });
// Initialization
// ---------------------------------------------------------------------------------
function init (cls) {
cls.add_channel({
id: "channel_inbox",
name: _t("Inbox"),
type: "static"
}, { display_needactions: true });
cls.add_channel({
id: "channel_starred",
name: _t("Starred"),
type: "static"
});
cls.add_channel({
id: "channel_archive",
name: _t("Archive"),
type: "static"
});
var load_channels = session.rpc('/mail/client_action').then(function (result) {
_.each(result.channel_slots, function (channels) {
_.each(channels, cls.add_channel);
});
needaction_counter = result.needaction_inbox_counter;
mention_partner_suggestions = result.mention_partner_suggestions;
});
var load_emojis = session.rpc("/mail/chat_init").then(function (result) {
emojis = result.emoji;
_.each(emojis, function(emoji) {
emoji_substitutions[_.escape(emoji.source)] = emoji.substitution;
});
});
var ir_model = new Model("ir.model.data");
var load_menu_id = ir_model.call("xmlid_to_res_id", ["mail.mail_channel_menu_root_chat"], {}, {shadow: true});
var load_action_id = ir_model.call("xmlid_to_res_id", ["mail.mail_channel_action_client_chat"], {}, {shadow: true});
bus.on('notification', null, cls.on_notification);
return $.when(load_menu_id, load_action_id, load_channels, load_emojis).then(function (menu_id, action_id) {
discuss_ids = {
menu_id: menu_id,
action_id: action_id
};
bus.start_polling();
});
}
chat_manager.is_ready = cls.start(); chat_manager.is_ready = cls.start();
return chat_manager;
return {
chat_manager: chat_manager,
MailTools: MailTools
};
}); });
Loading…
Cancel
Save