diff options
| author | Géry Debongnie <ged@odoo.com> | 2015-03-10 08:42:01 +0100 |
|---|---|---|
| committer | Géry Debongnie <ged@odoo.com> | 2015-03-18 09:01:50 +0100 |
| commit | 0ceeff515c7e5cd1b744d2e144f4610437c0762f (patch) | |
| tree | e77c07afc96e64d1769a4e3531b101f4ae83f897 /addons/mail/static/src/js/mail_utils.js | |
| parent | 97f4ba3e3d88ff64941d515cec3952969b40bfb0 (diff) | |
[REF] mail: update to the new module system
Diffstat (limited to 'addons/mail/static/src/js/mail_utils.js')
| -rw-r--r-- | addons/mail/static/src/js/mail_utils.js | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/addons/mail/static/src/js/mail_utils.js b/addons/mail/static/src/js/mail_utils.js new file mode 100644 index 00000000000..0634e240340 --- /dev/null +++ b/addons/mail/static/src/js/mail_utils.js @@ -0,0 +1,120 @@ +odoo.define('mail.utils', function () { +"use strict"; + +/** + * ------------------------------------------------------------ + * 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 + */ + + +/** parse text to find email: Tagada <address@mail.fr> -> [Tagada, address@mail.fr] or False */ +function parse_email(text) { + var result = text.match(/(.*)<(.*@.*)>/); + if (result) { + return [_.str.trim(result[1]), _.str.trim(result[2])]; + } + result = text.match(/(.*@.*)/); + if (result) { + return [_.str.trim(result[1]), _.str.trim(result[1])]; + } + return [text, false]; +} + +/* Get an image in /web/binary/image?... */ +function get_image(session, model, field, id, resize) { + var r = resize ? encodeURIComponent(resize) : ''; + id = id || ''; + return session.url('/web/binary/image', {model: model, field: field, id: id, resize: r}); +} + +/* Get the url of an attachment {'id': id} */ +function get_attachment_url(session, message_id, attachment_id) { + return session.url('/mail/download_attachment', { + 'model': 'mail.message', + 'id': message_id, + 'method': 'download_attachment', + 'attachment_id': attachment_id + }); +} + +/** + * Replaces some expressions + * - :name - shortcut to an image + */ +function do_replace_expressions(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] + '<img src="/mail/static/src/img/_' + icon_name + '.png" width="22px" height="22px" alt="' + icon_name + '"/>'); + regex_res = regex_login.exec(string); + } + return string; +} + +/** + * Replaces textarea text into html text (add <p>, <a>) + * TDE note : should be done server-side, in Python -> use mail.compose.message ? + */ +function get_text2html(text) { + return text + .replace(/((?:https?|ftp):\/\/[\S]+)/g,'<a href="$1">$1</a> ') + .replace(/[\n\r]/g,'<br/>'); +} + +/* Returns the complete domain with "&" + * TDE note: please add some comments to explain how/why + */ +function expand_domain(domain) { + var nb_and = -1; + var k; + // TDE note: smarted code maybe ? + for (k = domain.length-1; k >= 0 ; k-- ) { + if (typeof domain[k] != 'object' ) { + nb_and -= 2; + continue; + } + nb_and += 1; + } + + for (k = 0; k < nb_and ; k++) { + domain.unshift('&'); + } + + return domain; +} + +// inserts zero width space between each letter of a string so that +// the word will correctly wrap in html boxes smaller than the text +function breakword(str){ + var out = ''; + if (!str) { + return str; + } + for(var i = 0, len = str.length; i < len; i++){ + out += _.str.escapeHTML(str[i]) + '​'; + } + return out; +} + +return { + parse_email: parse_email, + get_image: get_image, + get_attachment_url: get_attachment_url, + do_replace_expressions: do_replace_expressions, + get_text2html: get_text2html, + expand_domain: expand_domain, + breakword: breakword, +}; + +}); |
