openerp.mail = function(session) { var _t = session.web._t, _lt = session.web._lt; var mail = session.mail = {}; openerp_mail_followers(session, mail); // import mail_followers.js /** * ------------------------------------------------------------ * FormView * ------------------------------------------------------------ * * Override of formview do_action method, to catch all return action about * mail.compose.message. The purpose is to bind 'Send by e-mail' buttons * and redirect them to the Chatter. */ session.web.FormView = session.web.FormView.extend({ do_action: function(action, on_close) { if (action.res_model == 'mail.compose.message') { $(".openerp_webclient_container .oe_view_manager_current .oe_mail .oe_mail_wall_button_fletch").click(); return false; } else { return this._super(action, on_close); } }, }); /** * ------------------------------------------------------------ * ChatterUtils * ------------------------------------------------------------ * * This class holds a few tools method for Chatter. * Some regular expressions not used anymore, kept because I want to * - (^|\s)@((\w|@|\.)*): @login@log.log * - (^|\s)\[(\w+).(\w+),(\d)\|*((\w|[@ .,])*)\]: [ir.attachment,3|My Label], * for internal links */ mail.ChatterUtils = { /** Get an image in /web/binary/image?... */ get_image: function(session, model, field, id) { return session.prefix + '/web/binary/image?session_id=' + session.session_id + '&model=' + model + '&field=' + field + '&id=' + (id || ''); }, /** Get the url of an attachment {'id': id} */ get_attachment_url: function (session, attachment) { return session.origin + '/web/binary/saveas?session_id=' + session.session_id + '&model=ir.attachment&field=datas&filename_field=datas_fname&id=' + attachment['id']; }, /** Replaces some expressions * - :name - shortcut to an image */ do_replace_expressions: function (string) { var icon_list = ['al', 'pinky'] /* special shortcut: :name, try to find an icon if in list */ var regex_login = new RegExp(/(^|\s):((\w)*)/g); var regex_res = regex_login.exec(string); while (regex_res != null) { var icon_name = regex_res[2]; if (_.include(icon_list, icon_name)) string = string.replace(regex_res[0], regex_res[1] + '' + icon_name + ''); regex_res = regex_login.exec(string); } return string; }, }; /** * ------------------------------------------------------------ * ComposeMessage widget * ------------------------------------------------------------ * * This widget handles the display of a form to compose a new message. * This form is a mail.compose.message form_view. */ mail.ComposeMessage = session.web.Widget.extend({ template: 'mail.compose_message', /** * @param {Object} parent parent * @param {Object} [options] * @param {Object} [context] context passed to the * mail.compose.message DataSetSearch. Please refer to this model * for more details about fields and default values. * @param {Boolean} [show_attachment_delete] */ init: function (parent, options) { var self = this; this._super(parent); this.attachment_ids = []; this.context = options.context || {}; this.id = options.parameters.id; this.model = options.parameters.model; this.res_id = options.parameters.res_id; this.is_private = options.parameters.is_private; this.partner_ids = options.parameters.partner_ids; this.options={thread:{}}; this.options.thread.show_header_compose = options.parameters.options.thread.show_header_compose; this.options.thread.display_on_flat = options.parameters.options.thread.display_on_flat; this.attachment_ids = []; this.options.thread.show_attachment_delete = true; this.options.thread.show_attachment_link = true; this.parent_thread= parent.messages!= undefined ? parent : false; this.ds_attachment = new session.web.DataSetSearch(this, 'ir.attachment'); this.fileupload_id = _.uniqueId('oe_fileupload_temp'); $(window).on(self.fileupload_id, self.on_attachment_loaded); }, start: function(){ this.display_attachments(); this.bind_events(); //load avatar img var user_avatar = mail.ChatterUtils.get_image(this.session, 'res.users', 'image_small', this.session.uid); this.$('img.oe_mail_icon').attr('src', user_avatar); }, /* upload the file on the server, add in the attachments list and reload display */ display_attachments: function(){ var self = this; var render = $(session.web.qweb.render('mail.thread.message.attachments', {'widget': self})); if(!this.list_attachment){ this.$('.oe_mail_compose_attachment_list').replaceWith( render ); } else { this.list_attachment.replaceWith( render ); } this.list_attachment = this.$("ul.oe_mail_msg_attachments"); // event: delete an attachment this.$el.on('click', '.oe_mail_attachment_delete', self.on_attachment_delete); }, on_attachment_change: function (event) { event.stopPropagation(); var self = this; var $target = $(event.target); if ($target.val() !== '') { var filename = $target.val().replace(/.*[\\\/]/,''); // if the files exits for this answer, delete the file before upload var attachments=[]; for(var i in this.attachment_ids){ if((this.attachment_ids[i].filename || this.attachment_ids[i].name) == filename){ if(this.attachment_ids[i].upload){ return false; } this.ds_attachment.unlink([this.attachment_ids[i].id]); } else { attachments.push(this.attachment_ids[i]); } } this.attachment_ids = attachments; // submit file //session.web.blockUI(); self.$('form.oe_form_binary_form').submit(); //self.submit_ajax_attachment(); this.$(".oe_attachment_file").hide(); this.attachment_ids.push({ 'id': 0, 'name': filename, 'filename': filename, 'url': '', 'upload': true }); this.display_attachments(); } }, submit_ajax_attachment: function(){ var self=this; var $form = self.$('form.oe_form_binary_form'); var filename = this.$('input.oe_form_binary_file').val().replace(/.*[\\\/]/,''); // create form data var fomdata = new FormData(); $.each($form.find('input'), function(i, field) { var $field=$(field); if($field.attr('type')!='file'){ fomdata.append($field.attr('name'), $field.val()); } else { fomdata.append($field.attr('name'), field.files[0]); } }); var progress=function(event) { console.log('xhr progress :', Math.floor(event.loaded / event.total*100), event); self.$("span[name='"+filename+"'] div:lt("+Math.floor(event.loaded / event.total*5)+")").show(); }; $.ajax({ url: $form.attr("action"), data: fomdata, cache: false, contentType: false, processData: false, type: 'POST', enctype: 'multipart/form-data', xhr: function() { // custom xhr myXhr = $.ajaxSettings.xhr(); if(myXhr.upload){ // for handling the progress of the upload myXhr.upload.addEventListener('progress', progress, false); } myXhr.addEventListener('progress', progress, false); return myXhr; }, success: function(data){ $iframe=$('