-
-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MIG] survey_question_type_binary: Migration to 17.0
- Loading branch information
Showing
5 changed files
with
86 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 71 additions & 76 deletions
147
survey_question_type_binary/static/src/js/survey_form.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,79 @@ | ||
/* Copyright 2023 Aures Tic - Jose Zambudio | ||
License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl). */ | ||
/** @odoo-module **/ | ||
|
||
odoo.define("survey_question_type_binary", function (require) { | ||
"use strict"; | ||
import SurveyFormWidget from "@survey/js/survey_form"; | ||
import {sprintf} from "@web/core/utils/strings"; | ||
|
||
const survey_form = require("survey.form"); | ||
SurveyFormWidget.include({ | ||
_getSubmitBinariesValues: function (params) { | ||
const self = this; | ||
let promises = []; | ||
|
||
survey_form.include({ | ||
_getSubmitBinariesValues: function (params) { | ||
const self = this; | ||
let promises = []; | ||
|
||
this.$("[data-question-type]").each(function () { | ||
switch ($(this).data("questionType")) { | ||
case "binary": | ||
case "multi_binary": | ||
promises = promises.concat( | ||
self._getSubmitAnswersBinary(params, $(this)) | ||
); | ||
break; | ||
this.$("[data-question-type]").each(function () { | ||
switch ($(this).data("questionType")) { | ||
case "binary": | ||
case "multi_binary": | ||
promises = promises.concat( | ||
self._getSubmitAnswersBinary(params, $(this)) | ||
); | ||
break; | ||
} | ||
}); | ||
return promises; | ||
}, | ||
_getSubmitAnswersBinary: function (params, $input) { | ||
const question_id = $input.attr("name"); | ||
return Array.prototype.map.call($input[0].files, (file) => { | ||
return this._readFileAsDataURL(file).then(function (sDataURL) { | ||
if (!params[question_id]) { | ||
params[question_id] = []; | ||
} | ||
}); | ||
return promises; | ||
}, | ||
_getSubmitAnswersBinary: function (params, $input) { | ||
const question_id = $input.attr("name"); | ||
return Array.prototype.map.call($input[0].files, (file) => { | ||
return this._readFileAsDataURL(file).then(function (sDataURL) { | ||
if (!params[question_id]) { | ||
params[question_id] = []; | ||
} | ||
params[question_id].push({ | ||
data: sDataURL.split(",")[1], | ||
filename: file.name, | ||
size: file.size, | ||
type: file.type, | ||
}); | ||
params[question_id].push({ | ||
data: sDataURL.split(",")[1], | ||
filename: file.name, | ||
size: file.size, | ||
type: file.type, | ||
}); | ||
}); | ||
}, | ||
_readFileAsDataURL: function (file) { | ||
return $.Deferred(function (deferred) { | ||
$.extend(new FileReader(), { | ||
onload: function (e) { | ||
var sDataURL = e.target.result; | ||
deferred.resolve(sDataURL); | ||
}, | ||
onerror: function () { | ||
deferred.reject(this); | ||
}, | ||
}).readAsDataURL(file); | ||
}).promise(); | ||
}, | ||
_submitForm: function (options) { | ||
const self = this; | ||
const params = {}; | ||
const binaryPrimises = this._getSubmitBinariesValues(params); | ||
if (binaryPrimises.length > 0 && !this.options.isStartScreen) { | ||
const $form = this.$("form"); | ||
const formData = new FormData($form[0]); | ||
|
||
if (options.previousPageId) { | ||
params.previous_page_id = options.previousPageId; | ||
} | ||
this._prepareSubmitValues(formData, params); | ||
Promise.all(binaryPrimises).then(function () { | ||
const submitPromise = self._rpc({ | ||
route: _.str.sprintf( | ||
"%s/%s/%s", | ||
"/survey/submit", | ||
self.options.surveyToken, | ||
self.options.answerToken | ||
), | ||
params: params, | ||
}); | ||
self._nextScreen(submitPromise, options); | ||
}); | ||
} else { | ||
return this._super(options); | ||
}); | ||
}, | ||
_readFileAsDataURL: function (file) { | ||
return $.Deferred(function (deferred) { | ||
$.extend(new FileReader(), { | ||
onload: function (e) { | ||
var sDataURL = e.target.result; | ||
deferred.resolve(sDataURL); | ||
}, | ||
onerror: function () { | ||
deferred.reject(this); | ||
}, | ||
}).readAsDataURL(file); | ||
}).promise(); | ||
}, | ||
_submitForm: function (options) { | ||
const self = this; | ||
const params = {}; | ||
const binaryPrimises = this._getSubmitBinariesValues(params); | ||
if (binaryPrimises.length > 0 && !this.options.isStartScreen) { | ||
const $form = this.$("form"); | ||
const formData = new FormData($form[0]); | ||
if (options.previousPageId) { | ||
params.previous_page_id = options.previousPageId; | ||
} | ||
}, | ||
}); | ||
this._prepareSubmitValues(formData, params); | ||
Promise.all(binaryPrimises).then(function () { | ||
const submitPromise = self.rpc( | ||
sprintf( | ||
"%s/%s/%s", | ||
"/survey/submit", | ||
self.options.surveyToken, | ||
self.options.answerToken | ||
), | ||
params | ||
); | ||
self._nextScreen(submitPromise, options); | ||
}); | ||
} else { | ||
return this._super(options); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters