1 openerp.mail = function(session) {
2 var _t = session.web._t,
5 var mail = session.mail = {};
7 openerp_mail_followers(session, mail); // import mail_followers.js
10 * ------------------------------------------------------------
12 * ------------------------------------------------------------
14 * Override of formview do_action method, to catch all return action about
15 * mail.compose.message. The purpose is to bind 'Send by e-mail' buttons
16 * and redirect them to the Chatter.
19 session.web.FormView = session.web.FormView.extend({
20 // TDE FIXME TODO: CHECK WITH NEW BRANCH
21 do_action: function(action, on_close) {
22 if (action.res_model == 'mail.compose.message' &&
23 this.fields && this.fields.message_ids &&
24 this.fields.message_ids.view.get("actual_mode") != 'create') {
26 console.groupCollapsed('FormView do_action on mail.compose.message');
27 console.log('message_ids field:', this.fields.message_ids);
29 var record_thread = this.fields.message_ids;
30 var thread = record_thread.thread;
31 thread.instantiate_composition_form('comment', true, false, 0, action.context);
35 return this._super(action, on_close);
41 * ------------------------------------------------------------
43 * ------------------------------------------------------------
45 * This class holds a few tools method that will be used by
46 * the various Chatter widgets.
48 * Some regular expressions not used anymore, kept because I want to
49 * - (^|\s)@((\w|@|\.)*): @login@log.log
52 * - (^|\s)\[(\w+).(\w+),(\d)\|*((\w|[@ .,])*)\]: [ir.attachment,3|My Label],
63 /** get an image in /web/binary/image?... */
64 get_image: function(session_prefix, session_id, model, field, id) {
65 return session_prefix + '/web/binary/image?session_id=' + session_id + '&model=' + model + '&field=' + field + '&id=' + (id || '');
68 /** check if the current user is the message author */
69 is_author: function (widget, message_user_id) {
70 return (widget.session && widget.session.uid != 0 && widget.session.uid == message_user_id);
73 /** Replaces some expressions
74 * - :name - shortcut to an image
76 do_replace_expressions: function (string) {
77 var icon_list = ['al', 'pinky']
78 /* special shortcut: :name, try to find an icon if in list */
79 var regex_login = new RegExp(/(^|\s):((\w)*)/g);
80 var regex_res = regex_login.exec(string);
81 while (regex_res != null) {
82 var icon_name = regex_res[2];
83 if (_.include(icon_list, icon_name))
84 string = string.replace(regex_res[0], regex_res[1] + '<img src="/mail/static/src/img/_' + icon_name + '.png" width="22px" height="22px" alt="' + icon_name + '"/>');
85 regex_res = regex_login.exec(string);
93 * ------------------------------------------------------------
94 * ComposeMessage widget
95 * ------------------------------------------------------------
97 * This widget handles the display of a form to compose a new message.
98 * This form is a mail.compose.message form_view.
101 mail.ComposeMessage = session.web.Widget.extend({
102 template: 'mail.compose_message',
105 * @param {Object} parent parent
106 * @param {Object} [options]
107 * @param {Object} [options.context] context passed to the
108 * mail.compose.message DataSetSearch. Please refer to this model
109 * for more details about fields and default values.
111 init: function (parent, options) {
115 this.options = options || {};
116 this.options.context = options.context || {};
117 this.options.form_xml_id = options.form_xml_id || 'email_compose_message_wizard_form_chatter';
118 this.options.form_view_id = options.form_view_id || false;
120 // console.groupCollapsed('New ComposeMessage: model', this.options.context.default_res_model, ', id', this.options.context.default_res_id);
121 // console.log('context:', this.options.context);
122 // console.groupEnd();
126 this._super.apply(this, arguments);
127 // customize display: add avatar, clean previous content
128 var user_avatar = mail.ChatterUtils.get_image(this.session.prefix, this.session.session_id, 'res.users', 'image_small', this.session.uid);
129 this.$el.find('img.oe_mail_icon').attr('src', user_avatar);
130 this.$el.find('div.oe_mail_msg_content').empty();
131 // create a context for the dataset and default_get of the wizard
132 var context = _.extend({}, this.options.context);
133 this.ds_compose = new session.web.DataSetSearch(this, 'mail.compose.message', context);
134 // find the id of the view to display in the chatter form
135 if (this.options.form_view_id) {
136 return this.create_form_view();
139 var data_ds = new session.web.DataSetSearch(this, 'ir.model.data');
140 return data_ds.call('get_object_reference', ['mail', this.options.form_xml_id]).pipe(this.proxy('create_form_view'));
144 /** Create a FormView, then append it to the to widget DOM. */
145 create_form_view: function (new_form_view_id) {
147 this.options.form_view_id = (new_form_view_id && new_form_view_id[1]) || this.options.form_view_id;
148 // destroy previous form_view if any
149 if (this.form_view) { this.form_view.destroy(); }
150 // create the FormView
151 this.form_view = new session.web.FormView(this, this.ds_compose, this.options.form_view_id, {
152 action_buttons: false,
154 initial_mode: 'edit',
155 disable_autofocus: true,
157 // add the form, bind events, activate the form
158 var msg_node = this.$el.find('div.oe_mail_msg_content');
159 return $.when(this.form_view.appendTo(msg_node)).pipe(function() {
161 self.form_view.do_show();
166 * Reinitialize the widget field values to the default values obtained
167 * using default_get on mail.compose.message. This allows to reinitialize
168 * the widget without having to rebuild a complete form view.
169 * @param {Object} new_context: context of the refresh */
170 refresh: function (new_context) {
171 if (! this.form_view) return;
173 this.options.context = _.extend(this.options.context, new_context || {});
174 this.ds_compose.context = _.extend(this.ds_compose.context, this.options.context);
175 return this.ds_compose.call('default_get', [
176 ['subject', 'body_text', 'body', 'attachment_ids', 'partner_ids', 'composition_mode',
177 'model', 'res_id', 'parent_id', 'content_subtype'],
178 this.ds_compose.get_context(),
179 ]).then( function (result) { self.form_view.on_processed_onchange({'value': result}, []); });
183 * Bind events in the widget. Each event is slightly described
184 * in the function. */
185 bind_events: function() {
187 // event: click on 'Attachment' icon-link that opens the dialog to
188 // add an attachment.
189 this.$el.on('click', 'button.oe_mail_compose_message_attachment', function (event) {
190 event.stopImmediatePropagation();
196 * ------------------------------------------------------------
198 * ------------------------------------------------------------
200 * This widget handles the display of a thread of messages. The
201 * [thread_level] parameter sets the thread level number:
203 * - - sub message (parent_id = root message)
204 * - - - sub sub message (parent id = sub message)
205 * - - sub message (parent_id = root message)
208 mail.Thread = session.web.Widget.extend({
209 template: 'mail.thread',
212 * @param {Object} parent parent
213 * @param {Object} [options]
214 * @param {Object} [options.context] context of the thread. It should
215 contain at least default_model, default_res_id. Please refer to
216 the ComposeMessage widget for more information about it.
217 * @param {Number} [options.thread_level=0] number of thread levels
218 * @param {Number} [options.message_ids=null] ids for message_fetch
219 * @param {Number} [options.message_data=null] already formatted message
220 data, for subthreads getting data from their parent
221 * @param {Boolean} [options.composer] use the advanced composer, or
222 the default basic textarea if not set
223 * @param {Number} [options.truncate_limit=250] number of character to
224 * display before having a "show more" link; note that the text
225 * will not be truncated if it does not have 110% of the parameter
227 init: function(parent, options) {
230 this.options = options || {};
231 this.options.domain = options.domain || [];
232 this.options.context = _.extend({
233 default_model: 'mail.thread',
235 default_parent_id: false }, options.context || {});
236 this.options.thread_level = options.thread_level || 0;
237 this.options.composer = options.composer || false;
238 this.options.message_ids = options.message_ids || null;
239 this.options.message_data = options.message_data || null;
240 // datasets and internal vars
241 this.ds_thread = new session.web.DataSetSearch(this, this.options.context.default_model);
242 this.ds_notification = new session.web.DataSetSearch(this, 'mail.notification');
243 this.ds_message = new session.web.DataSetSearch(this, 'mail.message');
244 // display customization vars
246 truncate_limit: options.truncate_limit || 250,
247 show_header_compose: options.show_header_compose || false,
248 show_reply: options.show_reply || false,
249 show_delete: options.show_delete || false,
250 show_hide: options.show_hide || false,
251 show_reply_by_email: options.show_reply_by_email || false,
252 show_more: options.show_more || false,
257 // TDE TODO: check for deferred, not sure it is correct
258 this._super.apply(this, arguments);
260 // fetch and display message, using message_ids if set
261 var display_done = $.when(this.message_fetch(true, [], {})).then(this.proxy('do_customize_display'));
262 // add message composition form view
263 if (this.display.show_header_compose && this.options.composer) {
264 var compose_done = this.instantiate_composition_form();
266 return display_done && compose_done;
269 /** Customize the display
270 * - show_header_compose: show the composition form in the header */
271 do_customize_display: function() {
272 this.display_user_avatar();
273 if (this.display.show_header_compose) { this.$el.find('div.oe_mail_thread_action').eq(0).show(); }
277 * Bind events in the widget. Each event is slightly described
278 * in the function. */
279 bind_events: function() {
281 // event: click on 'more' at bottom of thread
282 this.$el.find('button.oe_mail_button_more').click(function () {
283 self.do_message_fetch();
285 // event: writing in basic textarea of composition form (quick reply)
286 this.$el.find('textarea.oe_mail_compose_textarea').keyup(function (event) {
287 var charCode = (event.which) ? event.which : window.event.keyCode;
288 if (event.shiftKey && charCode == 13) { this.value = this.value+"\n"; }
289 else if (charCode == 13) { return self.message_post(); }
291 // event: click on 'Reply' in msg
292 this.$el.on('click', 'a.oe_mail_msg_reply', function (event) {
293 event.preventDefault();
294 event.stopPropagation();
295 var act_dom = $(this).parents('li.oe_mail_thread_msg').eq(0).find('div.oe_mail_thread_action:first');
298 // event: click on 'attachment(s)' in msg
299 this.$el.on('click', 'a.oe_mail_msg_view_attachments', function (event) {
300 event.preventDefault();
301 event.stopPropagation();
302 var act_dom = $(this).parent().parent().parent().find('.oe_mail_msg_attachments');
305 // event: click on 'Delete' in msg side menu
306 this.$el.on('click', 'a.oe_mail_msg_delete', function (event) {
307 event.preventDefault();
308 event.stopPropagation();
309 if (! confirm(_t("Do you really want to delete this message?"))) { return false; }
310 var msg_id = event.srcElement.dataset.id;
311 if (! msg_id) return false;
312 $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).remove();
313 return self.ds_msg.unlink([parseInt(msg_id)]);
315 // event: click on 'Hide' in msg side menu
316 this.$el.on('click', 'a.oe_mail_msg_hide', function (event) {
317 event.preventDefault();
318 event.stopPropagation();
319 var msg_id = event.srcElement.dataset.id;
320 if (! msg_id) return false;
321 $(event.srcElement).parents('li.oe_mail_thread_msg').eq(0).remove();
322 return self.ds_notif.call('set_message_read', [parseInt(msg_id)]);
324 // event: click on "Reply by email" in msg side menu (email style)
325 this.$el.on('click', 'a.oe_mail_msg_reply_by_email', function (event) {
326 event.preventDefault();
327 event.stopPropagation();
328 var msg_id = event.srcElement.dataset.msg_id;
329 if (! msg_id) return false;
330 self.compose_message_widget.refresh({
331 'default_composition_mode': 'reply',
332 'default_parent_id': parseInt(msg_id),
333 'default_content_subtype': 'html'} );
338 * Override-hack of do_action: automatically reload the chatter.
339 * Normally it should be called only when clicking on 'Post/Send'
340 * in the composition form. */
341 do_action: function(action, on_close) {
342 this.message_clean();
343 this.message_fetch();
344 if (this.compose_message_widget) {
345 this.compose_message_widget.refresh({
346 'default_composition_mode': 'comment',
347 'default_parent_id': this.options.default_parent_id,
348 'default_content_subtype': 'plain'} );
350 return this._super(action, on_close);
353 /** Instantiate the composition form, with every parameters in context
354 or in the widget context. */
355 instantiate_composition_form: function(context) {
356 if (this.compose_message_widget) {
357 this.compose_message_widget.destroy();
359 this.compose_message_widget = new mail.ComposeMessage(this, {
360 'context': _.extend(context || {}, this.options.context),
362 var composition_node = this.$el.find('div.oe_mail_thread_action');
363 composition_node.empty();
364 var compose_done = this.compose_message_widget.appendTo(composition_node);
368 /** Clean the thread */
369 message_clean: function() {
370 this.$el.find('div.oe_mail_thread_display').empty();
374 * @param {Bool} initial_mode: initial mode: try to use message_data or
375 * message_ids, if nothing available perform a message_read; otherwise
376 * directly perform a message_read
377 * @param {Array} additional_domain: added to options.domain
378 * @param {Object} additional_context: added to options.context
380 message_fetch: function (initial_mode, additional_domain, additional_context) {
382 // domain and context: options + additional
383 fetch_domain = _.flatten([this.options.domain, additional_domain || []], true)
384 fetch_context = _.extend(this.options.context, additional_context || {})
385 // if message_ids is set: try to use it
386 if (initial_mode && this.options.message_data) {
387 return this.message_display(this.options.message_data);
389 return this.ds_message.call('message_read',
390 [(initial_mode && this.options.message_ids) || false, fetch_domain, this.options.thread_level, undefined, fetch_context]
391 ).then(this.proxy('message_display'));
394 /* Display a list of records
395 * A specific case is done for 'expandable' messages that are messages
396 displayed under a 'show more' button form
398 message_display: function (records) {
400 var _expendable = false;
401 _(records).each(function (record) {
402 if (record.type == 'expandable') {
404 self.update_fetch_more(true);
405 self.fetch_more_domain = record.domain;
406 self.fetch_more_context = record.context;
409 self.display_record(record);
410 // if (self.options.thread_level >= 0) {
411 self.thread = new mail.Thread(self, {
413 'default_model': record.model,
414 'default_id': record.res_id,
415 'default_parent_id': record.id },
416 'message_data': record.child_ids, 'thread_level': self.options.thread_level-1,
417 'show_header_compose': false, 'show_reply': self.options.thread_level > 1,
418 'show_hide': self.display.show_hide, 'show_delete': self.display.show_delete,
420 self.$el.find('li.oe_mail_thread_msg:last').append('<div class="oe_mail_thread_subthread"/>');
421 self.thread.appendTo(self.$el.find('div.oe_mail_thread_subthread:last'));
426 this.update_fetch_more(false);
430 /** Displays a record and performs some formatting on the record :
431 * - record.date: formatting according to the user timezone
432 * - record.timerelative: relative time givein by timeago lib
433 * - record.avatar: image url
434 * - record.attachments[].url: url of each attachment
435 * - record.is_author: is the current user the author of the record */
436 display_record: function (record) {
437 // formatting and additional fields
438 record.date = session.web.format_value(record.date, {type:"datetime"});
439 record.timerelative = $.timeago(record.date);
440 if (record.type == 'email') {
441 record.avatar = ('/mail/static/src/img/email_icon.png');
443 record.avatar = mail.ChatterUtils.get_image(this.session.prefix, this.session.session_id, 'res.partner', 'image_small', record.author_id[0]);
446 if (record.attachments) {
447 for (var l in record.attachments) {
448 var url = self.session.origin + '/web/binary/saveas?session_id=' + self.session.session_id + '&model=ir.attachment&field=datas&filename_field=datas_fname&id='+records[k].attachments[l].id;
449 record.attachments[l].url = url;
452 record.is_author = mail.ChatterUtils.is_author(this, record.author_user_id[0]);
453 // render, add the expand feature
454 var rendered = session.web.qweb.render('mail.thread.message', {'record': record, 'thread': this, 'params': this.options, 'display': this.display});
455 $(rendered).appendTo(this.$el.children('div.oe_mail_thread_display:first'));
456 this.$el.find('div.oe_mail_msg_record_body').expander({
457 slicePoint: this.options.msg_more_limit,
458 expandText: 'read more',
459 userCollapseText: '[^]',
460 detailClass: 'oe_mail_msg_tail',
461 moreClass: 'oe_mail_expand',
462 lessClass: 'oe_mail_reduce',
466 /** Display 'show more' button */
467 update_fetch_more: function (new_value) {
469 this.$el.find('div.oe_mail_thread_more:last').show();
471 this.$el.find('div.oe_mail_thread_more:last').hide();
475 display_user_avatar: function () {
476 var avatar = mail.ChatterUtils.get_image(this.session.prefix, this.session.session_id, 'res.users', 'image_small', this.options.uid);
477 return this.$el.find('img.oe_mail_icon').attr('src', avatar);
480 message_post: function (body) {
483 var comment_node = this.$el.find('textarea');
484 var body = comment_node.val();
485 comment_node.val('');
487 return this.ds_post.call('message_post', [
488 [this.options.context.res_id], body, false, 'comment', this.options.context.parent_id]
489 ).then(this.proxy('message_fetch'));
492 /** Action: 'shows more' to fetch new messages */
493 do_message_fetch: function () {
494 return this.message_fetch(false, this.fetch_more_domain, this.fetch_more_context);
497 // TDE: keep currently because need something similar
499 // * Create a domain to fetch new comments according to
500 // * comment already present in comments_structure
501 // * @param {Object} comments_structure (see chatter utils)
502 // * @returns {Array} fetch_domain (OpenERP domain style)
504 // get_fetch_domain: function (comments_structure) {
506 // var ids = comments_structure.root_ids.slice();
508 // // must be child of current parent
509 // if (this.options.parent_id) { domain.push(['id', 'child_of', this.options.parent_id]); }
510 // _(comments_structure.root_ids).each(function (id) { // each record
514 // if (this.options.parent_id != false) {
515 // ids2.push(this.options.parent_id);
517 // // must not be children of already fetched messages
518 // if (ids.length > 0) {
521 // domain.push(['id', 'child_of', ids]);
523 // if (ids2.length > 0) {
524 // domain.push(['id', 'not in', ids2]);
532 * ------------------------------------------------------------
534 * ------------------------------------------------------------
536 * This widget handles the display of the Chatter on documents.
539 /* Add mail_thread widget to registry */
540 session.web.form.widgets.add('mail_thread', 'openerp.mail.RecordThread');
542 /** mail_thread widget: thread of comments */
543 mail.RecordThread = session.web.form.AbstractField.extend({
544 template: 'mail.record_thread',
547 this._super.apply(this, arguments);
548 this.options.domain = this.options.domain || [];
549 this.options.context = {'default_model': 'mail.thread', 'default_res_id': false};
550 this.options.thread_level = this.options.thread_level || 0;
551 this.thread_list = [];
555 this._super.apply(this, arguments);
556 // NB: all the widget should be modified to check the actual_mode property on view, not use
557 // any other method to know if the view is in create mode anymore
558 this.view.on("change:actual_mode", this, this._check_visibility);
559 this._check_visibility();
562 _check_visibility: function() {
563 this.$el.toggle(this.view.get("actual_mode") !== "create");
566 destroy: function() {
567 for (var i in this.thread_list) { this.thread_list[i].destroy(); }
568 this._super.apply(this, arguments);
571 set_value: function() {
573 this._super.apply(this, arguments);
574 if (! this.view.datarecord.id || session.web.BufferedDataSet.virtual_id_regex.test(this.view.datarecord.id)) {
575 this.$el.find('oe_mail_thread').hide();
579 _.extend(this.options.context, {
580 default_res_id: this.view.datarecord.id,
581 default_model: this.view.model });
582 // create and render Thread widget
583 this.$el.find('div.oe_mail_recthread_main').empty();
584 for (var i in this.thread_list) { this.thread_list[i].destroy(); }
585 var thread = new mail.Thread(self, {
586 'context': this.options.context,
587 'thread_level': this.options.thread_level, 'show_header_compose': true,
588 'show_delete': true, 'composer': true });
589 this.thread_list.push(thread);
590 return thread.appendTo(this.$el.find('div.oe_mail_recthread_main'));
596 * ------------------------------------------------------------
598 * ------------------------------------------------------------
600 * This widget handles the display of the Chatter on the Wall.
603 /* Add WallView widget to registry */
604 session.web.client_actions.add('mail.wall', 'session.mail.Wall');
606 /* WallView widget: a wall of messages */
607 mail.Wall = session.web.Widget.extend({
608 template: 'mail.wall',
611 * @param {Object} parent parent
612 * @param {Object} [options]
613 * @param {Number} [options.domain] domain on the Wall, is an array.
614 * @param {Number} [options.domain] context, is an object. It should
615 * contain default_model, default_res_id, to give it to the threads.
617 init: function (parent, options) {
619 this.options = options || {};
620 this.options.domain = options.domain || [];
621 this.options.context = options.context || {};
622 this.options.thread_level = options.thread_level || 1;
623 this.thread_list = [];
624 this.ds_msg = new session.web.DataSetSearch(this, 'mail.message');
626 this.search = {'domain': [], 'context': {}, 'groupby': {}}
627 this.search_results = {'domain': [], 'context': {}, 'groupby': {}}
631 this._super.apply(this, arguments);
632 var search_view_ready = this.load_search_view({}, false);
633 var thread_displayed = this.message_display();
634 return (search_view_ready && thread_displayed);
637 destroy: function () {
638 for (var i in this.thread_list) { this.thread_list[i].destroy(); }
639 this._super.apply(this, arguments);
643 * Load the mail.message search view
644 * @param {Object} defaults ??
645 * @param {Boolean} hidden some kind of trick we do not care here
647 load_search_view: function (defaults, hidden) {
649 this.searchview = new session.web.SearchView(this, this.ds_msg, false, defaults || {}, hidden || false);
650 return this.searchview.appendTo(this.$el.find('.oe_view_manager_view_search')).then(function () {
651 self.searchview.on_search.add(self.do_searchview_search);
656 * Aggregate the domains, contexts and groupbys in parameter
657 * with those from search form, and then calls fetch_comments
658 * to actually fetch comments
659 * @param {Array} domains
660 * @param {Array} contexts
661 * @param {Array} groupbys
663 do_searchview_search: function(domains, contexts, groupbys) {
665 this.rpc('/web/session/eval_domain_and_context', {
666 domains: domains || [],
667 contexts: contexts || [],
668 group_by_seq: groupbys || []
669 }, function (results) {
670 self.search_results['context'] = results.context;
671 self.search_results['domain'] = results.domain;
672 self.search_results['groupby'] = results.group_by;
673 self.message_clean();
674 return self.message_display();
678 /** Clean the wall */
679 message_clean: function() {
680 this.$el.find('ul.oe_mail_wall_threads').empty();
683 /** Display the Wall threads */
684 message_display: function () {
685 var render_res = session.web.qweb.render('mail.wall_thread_container', {});
686 $('<li class="oe_mail_wall_thread">').html(render_res).appendTo(this.$el.find('ul.oe_mail_wall_threads'));
687 var thread = new mail.Thread(this, {
688 'domain': this.options.domain, 'context': this.options.context,
689 'thread_level': this.options.thread_level, 'composer': true,
691 'show_header_compose': true, 'show_reply': this.options.thread_level > 0,
692 'show_hide': true, 'show_reply_by_email': true,
695 thread.appendTo(this.$el.find('li.oe_mail_wall_thread:last'));
696 this.thread_list.push(thread);