diff --git a/js/h5p-content-upgrade-process.js b/js/h5p-content-upgrade-process.js index e54dbb7..0f954ac 100644 --- a/js/h5p-content-upgrade-process.js +++ b/js/h5p-content-upgrade-process.js @@ -7,7 +7,7 @@ H5P.ContentUpgradeProcess = (function (Version) { * @class * @namespace H5P */ - function ContentUpgradeProcess(name, oldVersion, newVersion, params, id, loadLibrary, done) { + function ContentUpgradeProcess(name, oldVersion, newVersion, params, extras, id, loadLibrary, done) { var self = this; // Make params possible to work with @@ -24,20 +24,38 @@ H5P.ContentUpgradeProcess = (function (Version) { }); } + // Make extras possible to work with + for (var element in extras) { + if (extras.hasOwnProperty(element)) { + try { + extras[element] = JSON.parse(extras[element]); + if (!(extras[element] instanceof Object)) { + throw true; + } + } + catch (event) { + return done({ + type: 'errorExtrasBroken', + id: id + }); + } + } + } + self.loadLibrary = loadLibrary; - self.upgrade(name, oldVersion, newVersion, params, function (err, result) { + self.upgrade(name, oldVersion, newVersion, params, extras, function (err, result) { if (err) { return done(err); } - done(null, JSON.stringify(params)); + done(null, JSON.stringify(params), JSON.stringify(extras)); }); } /** * */ - ContentUpgradeProcess.prototype.upgrade = function (name, oldVersion, newVersion, params, done) { + ContentUpgradeProcess.prototype.upgrade = function (name, oldVersion, newVersion, params, extras, done) { var self = this; // Load library details and upgrade routines @@ -47,7 +65,7 @@ H5P.ContentUpgradeProcess = (function (Version) { } // Run upgrade routines on params - self.processParams(library, oldVersion, newVersion, params, function (err, params) { + self.processParams(library, oldVersion, newVersion, params, extras, function (err, params, extras) { if (err) { return done(err); } @@ -61,7 +79,7 @@ H5P.ContentUpgradeProcess = (function (Version) { next(err); }); }, function (err) { - done(err, params); + done(err, params, extras); }); }); }); @@ -77,7 +95,7 @@ H5P.ContentUpgradeProcess = (function (Version) { * @param {Object} params * @param {Function} next */ - ContentUpgradeProcess.prototype.processParams = function (library, oldVersion, newVersion, params, next) { + ContentUpgradeProcess.prototype.processParams = function (library, oldVersion, newVersion, params, extras, next) { if (H5PUpgrades[library.name] === undefined) { if (library.upgradesScript) { // Upgrades script should be loaded so the upgrades should be here. @@ -110,10 +128,11 @@ H5P.ContentUpgradeProcess = (function (Version) { var unnecessaryWrapper = (upgrade.contentUpgrade !== undefined ? upgrade.contentUpgrade : upgrade); try { - unnecessaryWrapper(params, function (err, upgradedParams) { + unnecessaryWrapper(params, function (err, upgradedParams, upgradedExtras) { params = upgradedParams; + extras = upgradedExtras; nextMinor(err); - }); + }, extras); } catch (err) { if (console && console.log) { @@ -127,7 +146,7 @@ H5P.ContentUpgradeProcess = (function (Version) { }, nextMajor); } }, function (err) { - next(err, params); + next(err, params, extras); }); }; @@ -168,11 +187,21 @@ H5P.ContentUpgradeProcess = (function (Version) { return done(); // Larger or same version that's available } + // Currently, we only use metadata as additional things that might need change + var extras = { + metadata: params.metadata + }; + // A newer version is available, upgrade params - return self.upgrade(availableLib[0], usedVer, availableVer, params.params, function (err, upgraded) { + return self.upgrade(availableLib[0], usedVer, availableVer, params.params, extras, function (err, upgraded, extras) { if (!err) { params.library = availableLib[0] + ' ' + availableVer.major + '.' + availableVer.minor; params.params = upgraded; + if (extras) { + if (extras.metadata) { + params.metadata = extras.metadata; + } + } } done(err, params); }); diff --git a/js/h5p-content-upgrade-worker.js b/js/h5p-content-upgrade-worker.js index 26ad038..af0be94 100644 --- a/js/h5p-content-upgrade-worker.js +++ b/js/h5p-content-upgrade-worker.js @@ -9,7 +9,7 @@ var libraryLoadedCallback; var messageHandlers = { newJob: function (job) { // Start new job - new H5P.ContentUpgradeProcess(job.name, new H5P.Version(job.oldVersion), new H5P.Version(job.newVersion), job.params, job.id, function loadLibrary(name, version, next) { + new H5P.ContentUpgradeProcess(job.name, new H5P.Version(job.oldVersion), new H5P.Version(job.newVersion), job.params, job.extras, job.id, function loadLibrary(name, version, next) { // TODO: Cache? postMessage({ action: 'loadLibrary', @@ -17,7 +17,7 @@ var messageHandlers = { version: version.toString() }); libraryLoadedCallback = next; - }, function done(err, result) { + }, function done(err, result, extras) { if (err) { // Return error postMessage({ @@ -33,7 +33,8 @@ var messageHandlers = { postMessage({ action: 'done', id: job.id, - params: result + params: result, + extras: extras }); }); }, diff --git a/js/h5p-content-upgrade.js b/js/h5p-content-upgrade.js index 9c1e4ce..564a0a8 100644 --- a/js/h5p-content-upgrade.js +++ b/js/h5p-content-upgrade.js @@ -116,7 +116,7 @@ // Register message handlers var messageHandlers = { done: function (result) { - self.workDone(result.id, result.params, this); + self.workDone(result.id, result.params, result.extras, this); }, error: function (error) { self.printError(error.err); @@ -184,7 +184,7 @@ self.token = inData.token; // Start processing - self.processBatch(inData.params); + self.processBatch(inData.params, {metadata: inData.metadata}); }); }; @@ -202,7 +202,7 @@ * * @param {Object} parameters */ - ContentUpgrade.prototype.processBatch = function (parameters) { + ContentUpgrade.prototype.processBatch = function (parameters, extras) { var self = this; // Track upgraded params @@ -210,6 +210,7 @@ // Track current batch self.parameters = parameters; + self.extras = extras; // Create id mapping self.ids = []; @@ -247,6 +248,11 @@ self.current++; self.working++; + // Rewrap metadata + if (self.extras.metadata) { + self.extras.metadata = self.extras.metadata[id]; + } + if (worker) { worker.postMessage({ action: 'newJob', @@ -254,11 +260,12 @@ name: info.library.name, oldVersion: info.library.version, newVersion: self.version.toString(), - params: self.parameters[id] + params: self.parameters[id], + extras: self.extras }); } else { - new H5P.ContentUpgradeProcess(info.library.name, new Version(info.library.version), self.version, self.parameters[id], id, function loadLibrary(name, version, next) { + new H5P.ContentUpgradeProcess(info.library.name, new Version(info.library.version), self.version, self.parameters[id], self.extras, id, function loadLibrary(name, version, next) { self.loadLibrary(name, version, function (err, library) { if (library.upgradesScript) { self.loadScript(library.upgradesScript, function (err) { @@ -273,13 +280,13 @@ } }); - }, function done(err, result) { + }, function done(err, result, extras) { if (err) { self.printError(err); return ; } - self.workDone(id, result); + self.workDone(id, result, extras); }); } }; @@ -287,7 +294,7 @@ /** * */ - ContentUpgrade.prototype.workDone = function (id, result, worker) { + ContentUpgrade.prototype.workDone = function (id, result, extras, worker) { var self = this; self.working--; @@ -302,7 +309,8 @@ self.nextBatch({ libraryId: self.version.libraryId, token: self.token, - params: JSON.stringify(self.upgraded) + params: JSON.stringify(self.upgraded), + extras: extras }); } };