Fixed field processing in content upgrade and removed debugging.

namespaces
Frode Petterson 2014-06-16 13:52:35 +02:00
parent db5243383d
commit 928e58c98b
1 changed files with 50 additions and 32 deletions

View File

@ -1,7 +1,7 @@
(function ($) { (function ($) {
var info, $container; var info, $container;
// TODO: Translate strings! // TODO: Translate all strings!
// Initialize // Initialize
$(document).ready(function () { $(document).ready(function () {
@ -214,8 +214,7 @@
// Upgrade this content. // Upgrade this content.
self.upgrade(info.library.name, new Version(info.library.version), self.version, params, function (err, params) { self.upgrade(info.library.name, new Version(info.library.version), self.version, params, function (err, params) {
if (err === undefined || err === null) { if (!err) {
console.log('Content done', params);
upgraded[id] = JSON.stringify(params); upgraded[id] = JSON.stringify(params);
current++; current++;
@ -226,7 +225,7 @@
}, function (err) { }, function (err) {
// Finished with all parameters that came in // Finished with all parameters that came in
if (err !== undefined && err !== null) { if (err) {
return self.setStatus('<p>An error occurred while processing parameters:<br/>' + err + '</p>'); return self.setStatus('<p>An error occurred while processing parameters:<br/>' + err + '</p>');
} }
@ -252,20 +251,24 @@
ContentUpgrade.prototype.upgrade = function (name, oldVersion, newVersion, params, next) { ContentUpgrade.prototype.upgrade = function (name, oldVersion, newVersion, params, next) {
var self = this; var self = this;
console.log('Upgrading ' + name + ' ' + oldVersion + ' -> ' + newVersion);
// Load library details and upgrade routines // Load library details and upgrade routines
self.loadLibrary(name, newVersion, function (err, library) { self.loadLibrary(name, newVersion, function (err, library) {
if (err) return next(err); if (err) {
return next(err);
}
// Run upgrade routines on params // Run upgrade routines on params
self.processParams(library, oldVersion, newVersion, params, function (err, params) { self.processParams(library, oldVersion, newVersion, params, function (err, params) {
if (err) return next(err); if (err) {
return next(err);
}
// Check if any of the sub-libraries need upgrading // Check if any of the sub-libraries need upgrading
asyncSerial(library.semantics, function (index, field, next) { asyncSerial(library.semantics, function (index, field, next) {
self.processField(field, params, function (err, upgradedParams) { self.processField(field, params[field.name], function (err, upgradedParams) {
if (upgradedParams) params = upgradedParams; if (upgradedParams) {
params[field.name] = upgradedParams;
}
next(err); next(err);
}); });
}, function (err) { }, function (err) {
@ -376,6 +379,10 @@
ContentUpgrade.prototype.processField = function (field, params, next) { ContentUpgrade.prototype.processField = function (field, params, next) {
var self = this; var self = this;
if (params === undefined) {
return next();
}
switch (field.type) { switch (field.type) {
case 'library': case 'library':
if (params.library === undefined || params.params === undefined) { if (params.library === undefined || params.params === undefined) {
@ -398,8 +405,14 @@
return next(); // Larger or same version that's available return next(); // Larger or same version that's available
} }
// A newer version is available, run upgrade. // A newer version is available, upgrade params
return self.upgrade(availableLib[0], usedVer, availableVer, params.params, next); return self.upgrade(availableLib[0], usedVer, availableVer, params.params, function (err, upgraded) {
if (!err) {
params.library = availableLib[0] + ' ' + availableVer.major + '.' + availableVer.minor;
params.params = upgraded;
}
next(err, params);
});
} }
} }
next(); next();
@ -407,31 +420,36 @@
case 'group': case 'group':
if (field.fields.length === 1) { if (field.fields.length === 1) {
var wrap = {}; // Single field to process, wrapper will be skipped
wrap[field.fields[0].name] = params; self.processField(field.fields[0], params, function (err, upgradedParams) {
params = wrap; if (upgradedParams) {
params = upgradedParams;
} }
next(err, params);
});
}
else {
// Go through all fields in the group // Go through all fields in the group
asyncSerial(field.fields, function (index, field, next) { asyncSerial(field.fields, function (index, subField, next) {
self.processField(field, params, function (err, upgradedParams) { self.processField(subField, params[subField.name], function (err, upgradedParams) {
if (upgradedParams) params = upgradedParams; if (upgradedParams) {
params[subField.name] = upgradedParams;
}
next(err); next(err);
}); });
}, function (err) { }, function (err) {
next(err, params); next(err, params);
}); });
}
break; break;
case 'list': case 'list':
if (params[field.name] === undefined) {
return next();
}
// Go trough all params in the list // Go trough all params in the list
asyncSerial(params[field.name], function (index, params, next) { asyncSerial(params, function (index, subParams, next) {
self.processField(field.field, params, function (err, upgradedParams) { self.processField(field.field, subParams, function (err, upgradedParams) {
if (upgradedParams) params = upgradedParams; if (upgradedParams) {
params[index] = upgradedParams;
}
next(err); next(err);
}); });
}, function (err) { }, function (err) {