Skip to content

Commit

Permalink
Merge pull request #2742 from kobotoolbox/2731-library-translations
Browse files Browse the repository at this point in the history
Send translation settings with saved rows to question library
  • Loading branch information
duvld committed Jul 30, 2020
2 parents fbc6b6b + bb37e03 commit a98066f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
48 changes: 34 additions & 14 deletions jsapp/js/models/surveyScope.es6
Expand Up @@ -2,31 +2,51 @@ import {actions} from '../actions';
import {
notify,
t,
unnullifyTranslations,
} from '../utils';

class SurveyScope {
constructor ({survey}) {
this.survey = survey;
}
add_row_to_question_library (row) {
add_row_to_question_library (row, assetContent) {
if (row.constructor.kls === 'Row') {
var rowJSON = row.toJSON2();
let content;
var surv = this.survey.toFlatJSON();
/*
* Apply translations "hack" again for saving single questions to library
* Since `unnullifyTranslations` requires the whole survey, we need to
* fish out the saved row and its translation settings out of the unnullified return
*/
var unnullifiedContent = JSON.parse(unnullifyTranslations(JSON.stringify(surv), assetContent));
var settingsObj = unnullifiedContent.settings;
var surveyObj = unnullifiedContent.survey;
if (rowJSON.type === 'select_one' || rowJSON.type === 'select_multiple') {
var surv = this.survey.toFlatJSON();
var choices = surv.choices.filter(s => s.list_name === rowJSON.select_from_list_name);
content = JSON.stringify({
survey: [
row.toJSON2()
],
choices: choices || undefined
});
var choices = unnullifiedContent.choices.filter(s => s.list_name === rowJSON.select_from_list_name);
for (var i in surveyObj) {
if (surveyObj[i].$kuid == row.toJSON2().$kuid) {
content = JSON.stringify({
survey: [
surveyObj[i]
],
choices: choices,
settings: settingsObj
});
}
}
} else {
content = JSON.stringify({
survey: [
row.toJSON2()
]
});
for (var j in surveyObj) {
if (surveyObj[j].$kuid == row.toJSON2().$kuid) {
content = JSON.stringify({
survey: [
surveyObj[j]
],
choices: choices,
settings: settingsObj
});
}
}
}
actions.resources.createResource.triggerAsync({
asset_type: 'question',
Expand Down
2 changes: 1 addition & 1 deletion jsapp/xlform/src/view.row.coffee
Expand Up @@ -139,7 +139,7 @@ module.exports = do ->

add_row_to_question_library: (evt) =>
evt.stopPropagation()
@ngScope?.add_row_to_question_library @model
@ngScope?.add_row_to_question_library @model, @model.collection._parent._initialParams

class GroupView extends BaseRowView
className: "survey__row survey__row--group xlf-row-view xlf-row-view--depr"
Expand Down

0 comments on commit a98066f

Please sign in to comment.