Skip to content

Commit

Permalink
Clean up editor save code, incl/ using composition instead of pub/sub.
Browse files Browse the repository at this point in the history
…Close #322.
  • Loading branch information
ariutta committed Jun 12, 2015
1 parent b71bd33 commit 3faf4cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 61 deletions.
22 changes: 15 additions & 7 deletions lib/editor/editor.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var _ = require('lodash');
var kaavioEditor = require('../../../kaavio-editor/lib/editor.js');
var kaavioEditor = require('../../../kaavio-editor/index.js');
var jsondiffpatch = require('jsondiffpatch');
var WikipathwaysApiClient = require('wikipathways-api-client');

Expand All @@ -25,10 +25,14 @@ module.exports = function(pvjs) {
}
});

function save() {
function save(pvjson) {
if (!pvjson) {
console.warn('No pvjson to save.');
return;
}

var pathwayIdentifier = pvjs.kaavioOptions.resource.match(/WP\d+/)[0];
var version = pvjs.kaavioOptions.version;
var pvjson = pvjs.kaavioOptions.pvjson;
var pvjsonOriginal = pvjs.pvjsonOriginal;

var serializerInstance = new XMLSerializer();
Expand Down Expand Up @@ -188,6 +192,11 @@ module.exports = function(pvjs) {
.then(function(response) {
console.log('response');
console.log(response);
pvjs.kaavioOptions.pvjson.identifier = response.responseText;
pvjsonOriginal.elements = pvjson.elements;
editor.editorTabsComponent.vm.originalPvjsonAsString =
JSON.stringify(pvjson);

editor.trigger('success.response', {
message: 'Your changes were saved. Refresh to update page information.'
});
Expand All @@ -204,19 +213,18 @@ module.exports = function(pvjs) {
editor.trigger('error.err', {
message: 'Your changes were NOT saved. You are not logged in. In order to ' +
'save these changes, login in a separate tab, then click Save&Close again.'
});
});
} else if (err.responseText.indexOf('out of date') > 0) {
editor.trigger('error.err', {
message: 'Your changes were NOT saved. Somebody else modified the pathway ' +
'while you were editing. Please refresh the page and try again.'
});
});
} else {
editor.trigger('error.err', {
message: 'An unknown error occurred. Please refresh and try again or ' +
'<a href="http://wikipathways.org/index.php/Contact_Us">contact us</a> if stuck.'
});
});
}
pvjsonOriginal.elements = pvjson.elements;
}
});
}
Expand Down
15 changes: 1 addition & 14 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,20 +92,6 @@ function Pvjs(selector, pvjsOptions) {
if (pvjs.pvjsOptions.editor && pvjs.pvjsOptions.editor !== 'disabled') {
var Editor = require('./editor/editor.js').bind(pvjs);
pvjs.editor = new Editor(pvjs);

/*
// TODO why is this event happening twice when it should happen once?
selector.addEventListener('kaaviodatachange', function(e) {
console.log('kaaviodatachange event received in pvjs.js');
pvjs.editor.save({
pvjson: e.detail.pvjson,
pvjsonOriginal: pvjsonOriginal,
gpmlDocJquery: pvjs.kaavioOptions.original,
identifier: pvjs.kaavioOptions.resource.match(/WP\d+/)[0],
version: pvjs.kaavioOptions.version
});
}, false);
//*/
}
Kaavio.call(pvjs, pvjs.selector, pvjs.kaavioOptions);
}
Expand Down Expand Up @@ -143,6 +129,7 @@ function Pvjs(selector, pvjsOptions) {
return initKaavio();
});
} else {
pvjs.kaavioOptions.pvjson = pvjs.pvjsOptions.sourceData[0];
return initKaavio();
}
} else {
Expand Down
60 changes: 20 additions & 40 deletions test/lib/pvjs/dev/pvjs.core.js

Large diffs are not rendered by default.

0 comments on commit 3faf4cf

Please sign in to comment.