Move this groupHook black magic closer to the backup code where it's actually used...

This commit is contained in:
Alexandre Aubin 2019-10-19 03:52:35 +02:00
parent ca6c635885
commit 071b343635
2 changed files with 75 additions and 73 deletions

View file

@ -50,7 +50,7 @@
name:y18n.t('local_archives')
};
c.api('GET', '/hooks/backup', {}, function(hooks) {
data['hooks'] = c.groupHooks(hooks['hooks']);
data['hooks'] = groupHooks(hooks['hooks']);
data['apps'] = {};
c.api('GET', '/apps?with_backup', {}, function(apps_list) {
data['apps'] = apps_list.apps;
@ -61,7 +61,7 @@
app.post('#/backup/:storage', function (c) {
var params = c.ungroupHooks(c.params['system_parts'],c.params['apps']);
var params = ungroupHooks(c.params['system_parts'],c.params['apps']);
c.api('POST', '/backup', params, function() {
c.redirect_to('#/backup/'+ c.params['storage']);
});
@ -73,7 +73,7 @@
y18n.t('backup'),
y18n.t('confirm_restore', [c.params['archive']]),
$.proxy(function(c){
var params = c.ungroupHooks(c.params['system_parts'],c.params['apps']);
var params = ungroupHooks(c.params['system_parts'],c.params['apps']);
params['force'] = '';
c.api('POST', '/backup/restore/'+c.params['archive'], params, function(data) {
c.redirect_to('#/backup/'+ c.params['storage']+'/'+c.params['archive']);
@ -121,7 +121,7 @@
};
data.other_storages = [];
data.name = c.params['archive'];
data.system_parts = c.groupHooks(Object.keys(data['system']),data['system']);
data.system_parts = groupHooks(Object.keys(data['system']),data['system']);
data.items = (data['system']!={} || data['apps']!=[]);
data.locale = y18n.locale
c.view('backup/backup_info', data, c.selectAllOrNone);
@ -146,4 +146,75 @@
});
});
function groupHooks(hooks, raw_infos) {
var data = {};
var rules = [
{
id:'configuration',
isIn:function (hook) {
return hook.indexOf('conf_')==0
}
}
];
$.each(hooks, function(i, hook) {
var group_id=hook;
var hook_size=(raw_infos && raw_infos[hook] && raw_infos[hook].size)?raw_infos[hook].size:0;
$.each(rules, function(i, rule) {
if (rule.isIn(hook)) {
group_id = 'adminjs_group_'+rule.id;
return false;
}
});
if(group_id in data) {
data[group_id] = {
name:y18n.t('hook_'+group_id),
value:data[group_id].value+','+hook,
description:data[group_id].description+', '+y18n.t('hook_'+hook),
size:data[group_id].size + hook_size
};
}
else {
data[group_id] = {
name:y18n.t('hook_'+group_id),
value:hook,
description:(group_id==hook)?y18n.t('hook_'+hook+'_desc'):y18n.t('hook_'+hook),
size:hook_size
};
}
});
return data;
};
function ungroupHooks(system_parts, apps) {
var data = {};
data['apps'] = apps || [];
data['system'] = system_parts || [];
if (data['system'].constructor !== Array) {
data['system'] = [data['system']];
}
if (data['apps'].constructor !== Array) {
data['apps'] = [data['apps']];
}
// Some hook value contains multiple hooks separated by commas
var split_hooks = [];
$.each(data['system'], function(i, hook) {
split_hooks = split_hooks.concat(hook.split(','));
});
data['system'] = split_hooks;
if (data['system'].length == 0) {
delete data['system'];
}
if (data['apps'].length == 0) {
delete data['apps'];
}
return data;
};
})();

View file

@ -485,75 +485,6 @@
});
},
groupHooks: function(hooks, raw_infos){
var data = {};
var rules = [
{
id:'configuration',
isIn:function (hook) {
return hook.indexOf('conf_')==0
}
}
];
$.each(hooks, function(i, hook) {
var group_id=hook;
var hook_size=(raw_infos && raw_infos[hook] && raw_infos[hook].size)?raw_infos[hook].size:0;
$.each(rules, function(i, rule) {
if (rule.isIn(hook)) {
group_id = 'adminjs_group_'+rule.id;
return false;
}
});
if(group_id in data) {
data[group_id] = {
name:y18n.t('hook_'+group_id),
value:data[group_id].value+','+hook,
description:data[group_id].description+', '+y18n.t('hook_'+hook),
size:data[group_id].size + hook_size
};
}
else {
data[group_id] = {
name:y18n.t('hook_'+group_id),
value:hook,
description:(group_id==hook)?y18n.t('hook_'+hook+'_desc'):y18n.t('hook_'+hook),
size:hook_size
};
}
});
return data;
},
ungroupHooks: function(system_parts,apps) {
var data = {};
data['apps'] = apps || [];
data['system'] = system_parts || [];
if (data['system'].constructor !== Array) {
data['system'] = [data['system']];
}
if (data['apps'].constructor !== Array) {
data['apps'] = [data['apps']];
}
// Some hook value contains multiple hooks separated by commas
var split_hooks = [];
$.each(data['system'], function(i, hook) {
split_hooks = split_hooks.concat(hook.split(','));
});
data['system'] = split_hooks;
if (data['system'].length == 0) {
delete data['system'];
}
if (data['apps'].length == 0) {
delete data['apps'];
}
return data;
},
// Paste <pre>
prePaste: function() {
var pasteButtons = $('button[data-paste-content],a[data-paste-content]');