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/hr_expense/static/src/js/upload_mixin.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/hr_expense/static/src/js/upload_mixin.js')
| -rw-r--r-- | addons/hr_expense/static/src/js/upload_mixin.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/addons/hr_expense/static/src/js/upload_mixin.js b/addons/hr_expense/static/src/js/upload_mixin.js new file mode 100644 index 00000000..f5fbd74c --- /dev/null +++ b/addons/hr_expense/static/src/js/upload_mixin.js @@ -0,0 +1,77 @@ +odoo.define('hr_expense.documents.upload.mixin', function (require) { +"use strict"; + +var core = require('web.core'); +var config = require('web.config'); +var _t = core._t; +var qweb = core.qweb; + +/** +* Mixin for uploading single or multiple documents. +*/ +var DocumentUploadMixin = { + start: function () { + // define a unique uploadId and a callback method + this.fileUploadID = _.uniqueId('hr_expense_document_upload'); + $(window).on(this.fileUploadID, this._onFileUploaded.bind(this)); + return this._super.apply(this, arguments); + }, + /** + * @private + */ + _onAddAttachment: function (ev) { + // Auto submit form once we've selected an attachment + var $input = $(ev.currentTarget).find('input.o_input_file'); + if ($input.val() !== '') { + var $binaryForm = this.$('.o_expense_documents_upload form.o_form_binary_form'); + $binaryForm.submit(); + } + }, + /** + * @private + */ + _onFileUploaded: function () { + // Callback once attachment have been created, create an expense with attachment ids + var self = this; + var attachments = Array.prototype.slice.call(arguments, 1); + // Get id from result + var attachent_ids = attachments.reduce(function(filtered, record) { + if (record.id) { + filtered.push(record.id); + } + return filtered; + }, []); + if (!attachent_ids.length) { + return self.do_notify(false, _t("An error occurred during the upload")); + } + var myContext = this.initialState.context + myContext['isMobile'] = config.device.isMobile + return this._rpc({ + model: 'hr.expense', + method: 'create_expense_from_attachments', + args: ["", attachent_ids, this.viewType], + context: myContext, + }).then(function(result) { + self.do_action(result); + }); + }, + /** + * @private + * @param {Event} event + */ + _onUpload: function (event) { + var self = this; + // If hidden upload form don't exists, create it + var $formContainer = this.$('.o_content').find('.o_expense_documents_upload'); + if (!$formContainer.length) { + $formContainer = $(qweb.render('hr.expense.DocumentsHiddenUploadForm', {widget: this})); + $formContainer.appendTo(this.$('.o_content')); + } + // Trigger the input to select a file + this.$('.o_expense_documents_upload .o_input_file').click(); + }, +}; + +return DocumentUploadMixin; + +}); |
