Browse Source

Refactoring: overrides of smaller methods that do not change the larger

pull/6/head
x620 9 years ago
parent
commit
394a165b4e
  1. 144
      mail_archives/static/src/js/archives.js
  2. 9
      mail_base/static/src/js/base.js

144
mail_archives/static/src/js/archives.js

@ -14,120 +14,29 @@ var time = require('web.time');
var web_client = require('web.web_client'); var web_client = require('web.web_client');
var _t = core._t; 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 // Inherit class and override methods
base_obj.MailTools.include({ 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> ');
});
var properties = this.get_properties(msg);
// Add property to Object
get_properties: function(msg){
var properties = this._super.apply(this, arguments);
properties.is_archive = this.property_descr("channel_archive", msg, this); properties.is_archive = this.property_descr("channel_archive", msg, this);
Object.defineProperties(msg, properties);
return properties;
},
msg = this.set_channel_flags(data, msg);
// Set archive flag
set_channel_flags: function(data, msg){
this._super.apply(this, arguments);
msg.is_archive = true; 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;
}
}
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');
}
msg.author_redirect = !msg.is_author;
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";
}
msg.body = this.parse_and_transform(msg.body, this.add_link);
_.each(msg.attachment_ids, function(a) {
a.url = '/web/content/' + a.id + '?download=true';
});
return msg; 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;
get_channel_array: function(msg){
var arr = this._super.apply(this, arguments);
return arr.concat('channel_archive');
},
_.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;
});
get_domain: function(channel){
return (channel.id === "channel_archive") ? [] : this._super.apply(this, arguments);
}, },
start: function(){ start: function(){
@ -138,34 +47,7 @@ base_obj.MailTools.include({
type: "static" 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();
});
return $.when(this._super.apply(this, arguments));
} }
}); });

9
mail_base/static/src/js/base.js

@ -62,10 +62,6 @@ var channel_seen = _.throttle(function (channel) {
var MailTools = core.Class.extend({ var MailTools = core.Class.extend({
init: function () {
// 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;
},
send_native_notification: function (title, content) { send_native_notification: function (title, content) {
var notification = new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png"}); var notification = new Notification(title, {body: content, icon: "/mail/static/src/img/odoo_o.png"});
@ -437,14 +433,13 @@ var MailTools = core.Class.extend({
get_domain: function(channel){ get_domain: function(channel){
return (channel.id === "channel_inbox") ? [['needaction', '=', true]] : return (channel.id === "channel_inbox") ? [['needaction', '=', true]] :
(channel.id === "channel_starred") ? [['starred', '=', true]] :
[['channel_ids', 'in', channel.id]];
(channel.id === "channel_starred") ? [['starred', '=', true]] : false;
}, },
// 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 = this.get_domain(channel);
var domain = this.get_domain(channel) || [['channel_ids', 'in', channel.id]];
var cache = this.get_channel_cache(channel, options.domain); var cache = this.get_channel_cache(channel, options.domain);
if (options.domain) { if (options.domain) {

Loading…
Cancel
Save