Stackedit/js/publisher.js

109 lines
3.1 KiB
JavaScript
Raw Normal View History

2013-04-10 23:13:31 +00:00
define(["jquery", "google-helper", "github-helper"], function($, googleHelper, githubHelper) {
2013-04-09 07:58:06 +00:00
// Dependencies
2013-04-10 18:14:59 +00:00
var core = undefined;
2013-04-09 07:58:06 +00:00
var fileManager = undefined;
var publisher = {};
2013-04-10 23:13:31 +00:00
// Used to know if the current file has publications
var hasPublications = false;
2013-04-09 07:58:06 +00:00
2013-04-10 23:13:31 +00:00
// Allows external modules to update hasPublications flag
publisher.notifyCurrentFile = function(fileIndex) {
2013-04-10 18:14:59 +00:00
2013-04-10 23:13:31 +00:00
// Check that file has publications
if(localStorage[fileIndex + ".publish"].length === 1) {
hasPublications = false;
}
else {
hasPublications = true;
}
publisher.updatePublishButton();
};
// Used to enable/disable the publish button
publisher.updatePublishButton = function() {
if(publishRunning === true || hasPublications === false || core.isOffline) {
$(".action-force-publish").addClass("disabled");
}
else {
$(".action-force-publish").removeClass("disabled");
}
};
2013-04-09 07:58:06 +00:00
2013-04-11 22:38:41 +00:00
// Used to get content to publish
publisher.getPublishContent = function(publishObject) {
if(publishObject.format === undefined) {
publishObject.format = $("input:radio[name=radio-publish-format]:checked").prop("value");
}
if(publishObject.format == "markdown") {
return $("#wmd-input").val();
}
return $("#wmd-preview").html();
};
// Recursive function to publish a file on multiple locations
var publishIndexList = [];
function publishLocation(callback, error) {
// No more publish location for this document
if (publishIndexList.length === 0) {
callback(error);
return;
}
// Dequeue a synchronized location
var publishIndex = publishIndexList.pop();
if(!publishIndex) {
publishLocation(callback, error);
return;
}
var publishObject = JSON.parse(localStorage[publishIndex]);
var content = publisher.getPublishContent(publishObject);
var commitMsg = core.settings.commitMsg;
// Try to find the provider
if(publishObject.provider == PUBLISH_PROVIDER_GITHUB) {
githubHelper.upload(publishObject.username, publishObject.repository,
publishObject.branch, publishObject.path, content, commitMsg,
function(error) {
publishLocation(callback, error);
});
}
}
2013-04-10 23:13:31 +00:00
var publishRunning = false;
2013-04-11 22:38:41 +00:00
publisher.publish = function() {
// If publish is running or offline
if(publishRunning === true || core.isOffline) {
return;
}
publishRunning = true;
publisher.updatePublishButton();
var fileIndex = localStorage["file.current"];
var title = localStorage[fileIndex + ".title"];
publishIndexList = localStorage[fileIndex + ".publish"].split(";");;
publishLocation(function(error) {
publishRunning = false;
publisher.updatePublishButton();
if(error === undefined) {
core.showMessage('"' + title + '" successfully published.');
}
});
};
2013-04-10 23:13:31 +00:00
2013-04-10 18:14:59 +00:00
publisher.init = function(coreModule, fileManagerModule) {
core = coreModule;
2013-04-09 07:58:06 +00:00
fileManager = fileManagerModule;
2013-04-11 22:38:41 +00:00
$(".action-force-publish").click(function() {
if(!$(this).hasClass("disabled")) {
publisher.publish();
}
});
2013-04-09 07:58:06 +00:00
};
return publisher;
});