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/website_slides_survey/static/src/js/slides_upload.js | |
| parent | 0a15094050bfde69a06d6eff798e9a8ddf2b8c21 (diff) | |
initial commit 2
Diffstat (limited to 'addons/website_slides_survey/static/src/js/slides_upload.js')
| -rw-r--r-- | addons/website_slides_survey/static/src/js/slides_upload.js | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/addons/website_slides_survey/static/src/js/slides_upload.js b/addons/website_slides_survey/static/src/js/slides_upload.js new file mode 100644 index 00000000..54dc18da --- /dev/null +++ b/addons/website_slides_survey/static/src/js/slides_upload.js @@ -0,0 +1,141 @@ +odoo.define('website_slides_survey.upload_modal', function (require) { +"use strict"; + +var core = require('web.core'); +var _t = core._t; +var sessionStorage = window.sessionStorage; +var SlidesUpload = require('website_slides.upload_modal'); + +/** + * Management of the new 'certification' slide_type + */ +SlidesUpload.SlideUploadDialog.include({ + events: _.extend({}, SlidesUpload.SlideUploadDialog.prototype.events || {}, { + 'change input#certification_id': '_onChangeCertification' + }), + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * Will automatically set the title of the slide to the title of the chosen certification + */ + _onChangeCertification: function (ev) { + if (ev.added && ev.added.text) { + this.$("input#name").val(ev.added.text); + } + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * Overridden to add the "certification" slide type + * + * @override + * @private + */ + _setup: function () { + this._super.apply(this, arguments); + this.slide_type_data['certification'] = { + icon: 'fa-trophy', + label: _t('Certification'), + template: 'website.slide.upload.modal.certification', + }; + }, + /** + * Overridden to add certifications management in select2 + * + * @override + * @private + */ + _bindSelect2Dropdown: function () { + this._super.apply(this, arguments); + + var self = this; + this.$('#certification_id').select2(this._select2Wrapper(_t('Certification'), false, + function () { + return self._rpc({ + route: '/slides_survey/certification/search_read', + params: { + fields: ['title'], + } + }); + }, 'title') + ); + }, + /** + * The select2 field makes the "required" input hidden on the interface. + * We need to make the "certification" field required so we override this method + * to handle validation in a fully custom way. + * + * @override + * @private + */ + _formValidate: function () { + var result = this._super.apply(this, arguments); + + var $certificationInput = this.$('#certification_id'); + if ($certificationInput.length !== 0) { + var $select2Container = $certificationInput + .closest('.form-group') + .find('.select2-container'); + $select2Container.removeClass('is-invalid is-valid'); + if ($certificationInput.is(':invalid')) { + $select2Container.addClass('is-invalid'); + } else if ($certificationInput.is(':valid')) { + $select2Container.addClass('is-valid'); + } + } + + return result; + }, + /** + * Overridden to add the 'certification' field into the submitted values + * + * @override + * @private + */ + _getSelect2DropdownValues: function () { + var result = this._super.apply(this, arguments); + + var certificateValue = this.$('#certification_id').select2('data'); + var survey = {}; + if (certificateValue) { + if (certificateValue.create) { + survey.id = false; + survey.title = certificateValue.text; + } else { + survey.id = certificateValue.id; + } + } + result['survey'] = survey; + return result; + }, + + /** + * Overridde to handle certification created on-the-fly: toaster will hold + * survey edit url, need to put it in session to use it in CertificationUploadToast + * + * @override + * @private + */ + _onFormSubmitDone: function (data) { + if (!data.error && data.redirect_to_certification) { + sessionStorage.setItem("survey_certification_url", data.redirect_url); + window.location.reload(); + } else { + this._super.apply(this, arguments); + } + }, +}); + +SlidesUpload.websiteSlidesUpload.include({ + xmlDependencies: (SlidesUpload.websiteSlidesUpload.prototype.xmlDependencies || []).concat( + ["/website_slides_survey/static/src/xml/website_slide_upload.xml"] + ), +}); + +}); |
