const $ = require('jquery');
const { AppSetting } = require('../../core/app_setting');
const { BagItProfile } = require('../../bagit/bagit_profile');
const { Choice } = require('./choice');
const { Context } = require('../../core/context');
const { ExportQuestion } = require('../../core/export_question');
const { Field } = require('./field');
const { Form } = require('./form');
const { RemoteRepository } = require('../../core/remote_repository');
const { StorageService } = require('../../core/storage_service');
const { Util } = require('../../core/util');
class SettingsQuestionsForm extends Form {
constructor(exportSettings) {
super('SettingsQuestions', exportSettings);
this.fieldsForType = {};
this.fieldsForType['AppSetting'] = [
"value"
];
this.fieldsForType['RemoteRepository'] = [
"apiToken",
"loginExtra",
"name",
"url",
"userId"
];
this.fieldsForType['StorageService'] = [
"bucket",
"description",
"host",
"login",
"loginExtra",
"name",
"password",
"port",
"protocol"
];
this.rowCount = 0;
this._init();
}
_init() {
let questionsToShow = this.obj.questions.length || 1;
for(let i=0; i < questionsToShow; i++) {
this.addRow();
}
}
addRow() {
let row = {
prompt: this._addPrompt(),
objType: this._addObjType(),
objId: this._addObjId(),
field: this._addField(),
}
this.rowCount += 1;
return row;
}
_addPrompt() {
let prompt = `prompt_${this.rowCount}`
let question = this.obj.questions[this.rowCount];
let value = question ? question.prompt : "";
this.fields[prompt] = new Field(
prompt,
prompt,
Context.y18n.__("Question"),
value
);
this.fields[prompt].attrs = {
"data-row-number": this.rowCount,
"data-control-type": "prompt"
}
return this.fields[prompt];
}
_addObjType() {
let objType = `objType_${this.rowCount}`
let question = this.obj.questions[this.rowCount];
let value = question ? question.objType : "";
this.fields[objType] = new Field(
objType,
objType,
Context.y18n.__("Object Type"),
value
);
let objTypeOptions = [];
if (this.obj.appSettings.length > 0) {
objTypeOptions.push({
id: "AppSetting",
name: Context.y18n.__("App Setting")
});
}
if (this.obj.bagItProfiles.length > 0) {
objTypeOptions.push({
id: "BagItProfile",
name: Context.y18n.__("BagIt Profile")
});
}
if (this.obj.remoteRepositories.length > 0) {
objTypeOptions.push({
id: "RemoteRepository",
name: Context.y18n.__("Remote Repository")
});
}
if (this.obj.storageServices.length > 0) {
objTypeOptions.push({
id: "StorageService",
name: Context.y18n.__("Storage Service")
});
}
this.fields[objType].choices = Choice.makeList(
objTypeOptions,
value,
true
);
this.fields[objType].attrs = {
"data-row-number": this.rowCount,
"data-control-type": "object-type"
}
return this.fields[objType];
}
_addObjId() {
let objId = `objId_${this.rowCount}`
let question = this.obj.questions[this.rowCount];
let value = "";
let options = [];
if (question != null) {
value = question.objId;
}
this.fields[objId] = new Field(
objId,
objId,
Context.y18n.__("Object Name"),
value
);
if (question != null) {
options = this.getNamesList(this.rowCount);
}
this.fields[objId].choices = Choice.makeList(
options,
value,
true);
this.fields[objId].attrs = {
"data-row-number": this.rowCount,
"data-control-type": "object-name"
}
return this.fields[objId];
}
_addField() {
let field = `field_${this.rowCount}`
let question = this.obj.questions[this.rowCount];
let value = "";
let options = [];
if (question != null) {
value = question.field;
}
this.fields[field] = new Field(
field,
field,
Context.y18n.__("Field"),
value
);
if (question != null) {
options = this.getFieldsList(this.rowCount);
}
this.fields[field].choices = Choice.makeList(
options,
value,
true);
this.fields[field].attrs = {
"data-row-number": this.rowCount,
"data-control-type": "field"
}
return this.fields[field];
}
getQuestionsAsArray() {
let form = this;
let questions = []
for (let i = 0; i < this.rowCount; i++) {
questions.push({
prompt: form.fields[`prompt_${i}`],
objType: form.fields[`objType_${i}`],
objId: form.fields[`objId_${i}`],
field: form.fields[`field_${i}`],
});
}
return questions;
}
getQuestionPrompt(rowNumber) {
return $(`#prompt_${rowNumber}`).val();
}
getSelectedType(rowNumber) {
let element = $(`#objType_${rowNumber}`);
return element.length > 0 ? element.val() : this.fields[`objType_${rowNumber}`].value;
}
getSelectedId(rowNumber) {
let element = $(`#objId_${rowNumber}`);
return element.length > 0 ? element.val() : this.fields[`objId_${rowNumber}`].value;
}
getSelectedField(rowNumber) {
let element = $(`#field_${rowNumber}`);
return element.length > 0 ? element.val() : this.fields[`field_${rowNumber}`].value;
}
getQuestionFromForm(rowNumber) {
let form = this;
return new ExportQuestion({
prompt: form.getQuestionPrompt(rowNumber),
objType: form.getSelectedType(rowNumber),
objId: form.getSelectedId(rowNumber),
field: form.getSelectedField(rowNumber),
});
}
getNamesList(rowNumber) {
let selectedType = this.getSelectedType(rowNumber);
if (!selectedType) {
return [];
}
let listName = ExportQuestion.listNameFor(selectedType);
return this.obj[listName].map(obj => { return { id: obj.id, name: obj.name }});
}
getFieldsList(rowNumber) {
let selectedType = this.getSelectedType(rowNumber);
if (!selectedType) {
return [];
}
if (selectedType == "BagItProfile") {
let profileId = this.getSelectedId(rowNumber);
let profile = BagItProfile.find(profileId);
if (profile == null) {
return [];
}
let opts = profile.tags.filter(tagDef => !tagDef.systemMustSet()).map(tag => {return { id: tag.id, name: tag.tagName }});
return opts.sort((x,y) => {
if (x.name < y.name) {
return -1;
}
if (x.name > y.name) {
return 1;
}
return 0;
});
}
return this.fieldsForType[selectedType].map(name => { return { id: name, name: name }});
}
parseQuestionsForExport() {
this.obj.questions = [];
let count = $("div[data-question-number]").length;
for (let i=0; i < count; i++) {
this.obj.questions.push(this.getQuestionFromForm(i));
}
}
}
module.exports.SettingsQuestionsForm = SettingsQuestionsForm;