[mod] make manifest install argument formatting reusable

This commit is contained in:
Laurent Peuch 2018-06-30 23:36:16 +02:00
parent 8a06430026
commit 37c289aa72

View file

@ -195,44 +195,43 @@
c.redirect('#/apps/install'); c.redirect('#/apps/install');
}); });
// Helper function that build app installation form // Helper function that formats YunoHost style arguments for generating a form
app.helper('appInstallForm', function(appId, manifest, params) { function formatYunoHostStyleArguments(args, params) {
var data = { if (!args) {
id: appId, return;
manifest: manifest }
};
if (typeof data.manifest.arguments.install !== 'undefined') { // this is in place modification, I don't like it but it was done this way
$.each(data.manifest.arguments.install, function(k, v) { $.each(args, function(k, v) {
// Default values // Default values
data.manifest.arguments.install[k].type = (typeof v.type !== 'undefined') ? v.type : 'string'; args[k].type = (typeof v.type !== 'undefined') ? v.type : 'string';
data.manifest.arguments.install[k].inputType = 'text'; args[k].inputType = 'text';
data.manifest.arguments.install[k].required = (typeof v.optional !== 'undefined' && v.optional == "true") ? '' : 'required'; args[k].required = (typeof v.optional !== 'undefined' && v.optional == "true") ? '' : 'required';
data.manifest.arguments.install[k].attributes = ""; args[k].attributes = "";
data.manifest.arguments.install[k].helpText = ""; args[k].helpText = "";
data.manifest.arguments.install[k].helpLink = ""; args[k].helpLink = "";
// Multilingual label // Multilingual label
data.manifest.arguments.install[k].label = (typeof data.manifest.arguments.install[k].ask[y18n.locale] !== 'undefined') ? args[k].label = (typeof args[k].ask[y18n.locale] !== 'undefined') ?
data.manifest.arguments.install[k].ask[y18n.locale] : args[k].ask[y18n.locale] :
data.manifest.arguments.install[k].ask['en'] args[k].ask['en']
; ;
// Multilingual help text // Multilingual help text
if (typeof data.manifest.arguments.install[k].help !== 'undefined') { if (typeof args[k].help !== 'undefined') {
data.manifest.arguments.install[k].helpText = (typeof data.manifest.arguments.install[k].help[y18n.locale] !== 'undefined') ? args[k].helpText = (typeof args[k].help[y18n.locale] !== 'undefined') ?
data.manifest.arguments.install[k].help[y18n.locale] : args[k].help[y18n.locale] :
data.manifest.arguments.install[k].help['en'] args[k].help['en']
; ;
} }
// Input with choices becomes select list // Input with choices becomes select list
if (typeof data.manifest.arguments.install[k].choices !== 'undefined') { if (typeof args[k].choices !== 'undefined') {
// Update choices values with key and checked data // Update choices values with key and checked data
var choices = [] var choices = []
$.each(data.manifest.arguments.install[k].choices, function(ck, cv){ $.each(args[k].choices, function(ck, cv){
// Non key/value choices have numeric key, that we don't want. // Non key/value choices have numeric key, that we don't want.
if (typeof ck == "number") { if (typeof ck == "number") {
// Key is Value in this case. // Key is Value in this case.
@ -242,18 +241,18 @@
choices.push({ choices.push({
value: ck, value: ck,
label: cv, label: cv,
selected: (ck == data.manifest.arguments.install[k].default) ? true : false, selected: (ck == args[k].default) ? true : false,
}); });
}); });
data.manifest.arguments.install[k].choices = choices; args[k].choices = choices;
} }
// Special case for domain input. // Special case for domain input.
// Display a list of available domains // Display a list of available domains
if (v.name == 'domain' || data.manifest.arguments.install[k].type == 'domain') { if (v.name == 'domain' || args[k].type == 'domain') {
data.manifest.arguments.install[k].choices = []; args[k].choices = [];
$.each(params.domains, function(key, domain){ $.each(params.domains, function(key, domain){
data.manifest.arguments.install[k].choices.push({ args[k].choices.push({
value: domain, value: domain,
label: domain, label: domain,
selected: false selected: false
@ -261,15 +260,15 @@
}); });
// Custom help link // Custom help link
data.manifest.arguments.install[k].helpLink += "<a href='#/domains'>"+y18n.t('manage_domains')+"</a>"; args[k].helpLink += "<a href='#/domains'>"+y18n.t('manage_domains')+"</a>";
} }
// Special case for admin / user input. // Special case for admin / user input.
// Display a list of available users // Display a list of available users
if (v.name == 'admin' || data.manifest.arguments.install[k].type == 'user') { if (v.name == 'admin' || args[k].type == 'user') {
data.manifest.arguments.install[k].choices = []; args[k].choices = [];
$.each(params.users, function(username, user){ $.each(params.users, function(username, user){
data.manifest.arguments.install[k].choices.push({ args[k].choices.push({
value: username, value: username,
label: user.fullname+' ('+user.mail+')', label: user.fullname+' ('+user.mail+')',
selected: false selected: false
@ -277,14 +276,14 @@
}); });
// Custom help link // Custom help link
data.manifest.arguments.install[k].helpLink += "<a href='#/users'>"+y18n.t('manage_users')+"</a>"; args[k].helpLink += "<a href='#/users'>"+y18n.t('manage_users')+"</a>";
} }
// 'app' type input display a list of available apps // 'app' type input display a list of available apps
if (data.manifest.arguments.install[k].type == 'app') { if (args[k].type == 'app') {
data.manifest.arguments.install[k].choices = []; args[k].choices = [];
$.each(params.apps, function(key, app){ $.each(params.apps, function(key, app){
data.manifest.arguments.install[k].choices.push({ args[k].choices.push({
value: app.id, value: app.id,
label: app.name, label: app.name,
selected: false selected: false
@ -292,45 +291,54 @@
}); });
// Custom help link // Custom help link
data.manifest.arguments.install[k].helpLink += "<a href='#/apps'>"+y18n.t('manage_apps')+"</a>"; args[k].helpLink += "<a href='#/apps'>"+y18n.t('manage_apps')+"</a>";
} }
// Boolean fields // Boolean fields
if (data.manifest.arguments.install[k].type == 'boolean') { if (args[k].type == 'boolean') {
data.manifest.arguments.install[k].inputType = 'checkbox'; args[k].inputType = 'checkbox';
// Checked or not ? // Checked or not ?
if (typeof data.manifest.arguments.install[k].default !== 'undefined') { if (typeof args[k].default !== 'undefined') {
if (data.manifest.arguments.install[k].default == true) { if (args[k].default == true) {
data.manifest.arguments.install[k].attributes = 'checked="checked"'; args[k].attributes = 'checked="checked"';
} }
} }
// 'default' is used as value, so we need to force it for checkboxes. // 'default' is used as value, so we need to force it for checkboxes.
data.manifest.arguments.install[k].default = 1; args[k].default = 1;
// Checkbox should not be required to be unchecked // Checkbox should not be required to be unchecked
data.manifest.arguments.install[k].required = ''; args[k].required = '';
// Clone a hidden input with empty value // Clone a hidden input with empty value
// https://stackoverflow.com/questions/476426/submit-an-html-form-with-empty-checkboxes // https://stackoverflow.com/questions/476426/submit-an-html-form-with-empty-checkboxes
var inputClone = { var inputClone = {
name : data.manifest.arguments.install[k].name, name : args[k].name,
inputType : 'hidden', inputType : 'hidden',
default : 0 default : 0
}; };
data.manifest.arguments.install.push(inputClone); args.push(inputClone);
} }
// 'password' type input. // 'password' type input.
if (v.name == 'password' || data.manifest.arguments.install[k].type == 'password') { if (v.name == 'password' || args[k].type == 'password') {
// Change html input type // Change html input type
data.manifest.arguments.install[k].inputType = 'password'; args[k].inputType = 'password';
} }
}); });
} }
// Helper function that build app installation form
app.helper('appInstallForm', function(appId, manifest, params) {
var data = {
id: appId,
manifest: manifest
};
formatYunoHostStyleArguments(data.manifest.arguments.install, params);
// Multilingual description // Multilingual description
data.description = (typeof data.manifest.description[y18n.locale] !== 'undefined') ? data.description = (typeof data.manifest.description[y18n.locale] !== 'undefined') ?
data.manifest.description[y18n.locale] : data.manifest.description[y18n.locale] :