diff options
| author | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
|---|---|---|
| committer | stephanchrst <stephanchrst@gmail.com> | 2022-05-10 21:51:50 +0700 |
| commit | 3751379f1e9a4c215fb6eb898b4ccc67659b9ace (patch) | |
| tree | a44932296ef4a9b71d5f010906253d8c53727726 /addons/mail/static/src/models/canned_response | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/mail/static/src/models/canned_response')
| -rw-r--r-- | addons/mail/static/src/models/canned_response/canned_response.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/addons/mail/static/src/models/canned_response/canned_response.js b/addons/mail/static/src/models/canned_response/canned_response.js new file mode 100644 index 00000000..41e917d2 --- /dev/null +++ b/addons/mail/static/src/models/canned_response/canned_response.js @@ -0,0 +1,107 @@ +odoo.define('mail/static/src/models/canned_response/canned_response.js', function (require) { +'use strict'; + +const { registerNewModel } = require('mail/static/src/model/model_core.js'); +const { attr } = require('mail/static/src/model/model_field.js'); +const { cleanSearchTerm } = require('mail/static/src/utils/utils.js'); + +function factory(dependencies) { + + class CannedResponse extends dependencies['mail.model'] { + + /** + * Fetches canned responses matching the given search term to extend the + * JS knowledge and to update the suggestion list accordingly. + * + * In practice all canned responses are already fetched at init so this + * method does nothing. + * + * @static + * @param {string} searchTerm + * @param {Object} [options={}] + * @param {mail.thread} [options.thread] prioritize and/or restrict + * result in the context of given thread + */ + static fetchSuggestions(searchTerm, { thread } = {}) {} + + /** + * Returns a sort function to determine the order of display of canned + * responses in the suggestion list. + * + * @static + * @param {string} searchTerm + * @param {Object} [options={}] + * @param {mail.thread} [options.thread] prioritize result in the + * context of given thread + * @returns {function} + */ + static getSuggestionSortFunction(searchTerm, { thread } = {}) { + const cleanedSearchTerm = cleanSearchTerm(searchTerm); + return (a, b) => { + const cleanedAName = cleanSearchTerm(a.source || ''); + const cleanedBName = cleanSearchTerm(b.source || ''); + if (cleanedAName.startsWith(cleanedSearchTerm) && !cleanedBName.startsWith(cleanedSearchTerm)) { + return -1; + } + if (!cleanedAName.startsWith(cleanedSearchTerm) && cleanedBName.startsWith(cleanedSearchTerm)) { + return 1; + } + if (cleanedAName < cleanedBName) { + return -1; + } + if (cleanedAName > cleanedBName) { + return 1; + } + return a.id - b.id; + }; + } + + /* + * Returns canned responses that match the given search term. + * + * @static + * @param {string} searchTerm + * @param {Object} [options={}] + * @param {mail.thread} [options.thread] prioritize and/or restrict + * result in the context of given thread + * @returns {[mail.canned_response[], mail.canned_response[]]} + */ + static searchSuggestions(searchTerm, { thread } = {}) { + const cleanedSearchTerm = cleanSearchTerm(searchTerm); + return [this.env.messaging.cannedResponses.filter(cannedResponse => + cleanSearchTerm(cannedResponse.source).includes(cleanedSearchTerm) + )]; + } + + /** + * Returns the text that identifies this canned response in a mention. + * + * @returns {string} + */ + getMentionText() { + return this.substitution; + } + + } + + CannedResponse.fields = { + id: attr(), + /** + * The keyword to use a specific canned response. + */ + source: attr(), + /** + * The canned response itself which will replace the keyword previously + * entered. + */ + substitution: attr(), + }; + + CannedResponse.modelName = 'mail.canned_response'; + + return CannedResponse; +} + +registerNewModel('mail.canned_response', factory); + +}); |
