Stackedit/js/publisher.js

250 lines
9.7 KiB
JavaScript
Raw Normal View History

2013-05-26 22:59:03 +00:00
define([
"jquery",
2013-05-27 19:45:33 +00:00
"underscore",
2013-05-26 22:59:03 +00:00
"core",
"utils",
2013-05-27 19:45:33 +00:00
"settings",
2013-06-10 21:22:32 +00:00
"extensionMgr",
"fileSystem",
"fileMgr",
2013-05-26 22:59:03 +00:00
"sharing",
2013-06-10 21:22:32 +00:00
"providers/bloggerProvider",
"providers/dropboxProvider",
"providers/gistProvider",
"providers/githubProvider",
"providers/gdriveProvider",
"providers/sshProvider",
"providers/tumblrProvider",
"providers/wordpressProvider"
2013-05-27 19:45:33 +00:00
], function($, _, core, utils, settings, extensionMgr, fileSystem, fileMgr, sharing) {
2013-04-09 07:58:06 +00:00
2013-05-29 19:55:23 +00:00
var publisher = {};
// Create a map with providerId: providerModule
var providerMap = _.chain(arguments).map(function(argument) {
return argument && argument.providerId && [
argument.providerId,
argument
];
}).compact().object().value();
// Retrieve publish locations from localStorage
_.each(fileSystem, function(fileDesc) {
2013-06-03 22:19:52 +00:00
_.each(utils.retrieveIndexArray(fileDesc.fileIndex + ".publish"), function(publishIndex) {
try {
var publishAttributes = JSON.parse(localStorage[publishIndex]);
// Store publishIndex
publishAttributes.publishIndex = publishIndex;
// Replace provider ID by provider module in attributes
var provider = providerMap[publishAttributes.provider];
if(!provider) {
throw new Error("Invalid provider ID: " + publishAttributes.provider);
}
publishAttributes.provider = provider;
2013-06-03 22:19:52 +00:00
fileDesc.publishLocations[publishIndex] = publishAttributes;
}
catch(e) {
// localStorage can be corrupted
extensionMgr.onError(e);
// Remove publish location
utils.removeIndexFromArray(fileDesc.fileIndex + ".publish", publishIndex);
localStorage.removeItem(publishIndex);
}
2013-05-29 19:55:23 +00:00
});
});
// Apply template to the current document
2013-06-16 10:47:35 +00:00
publisher.applyTemplate = function(fileDesc, publishAttributes, html) {
2013-05-29 19:55:23 +00:00
try {
return _.template(settings.template, {
documentTitle: fileDesc.title,
2013-06-16 10:47:35 +00:00
documentMarkdown: fileDesc.content,
documentHTML: html,
2013-05-29 19:55:23 +00:00
publishAttributes: publishAttributes
});
}
catch(e) {
2013-05-29 19:55:23 +00:00
extensionMgr.onError(e);
2013-06-16 10:47:35 +00:00
return e.message;
2013-05-29 19:55:23 +00:00
}
};
// Used to get content to publish
2013-06-16 10:47:35 +00:00
function getPublishContent(fileDesc, publishAttributes, html) {
2013-05-29 19:55:23 +00:00
if(publishAttributes.format === undefined) {
publishAttributes.format = $("input:radio[name=radio-publish-format]:checked").prop("value");
}
if(publishAttributes.format == "markdown") {
2013-06-16 10:47:35 +00:00
return fileDesc.content;
2013-05-29 19:55:23 +00:00
}
else if(publishAttributes.format == "html") {
2013-06-16 10:47:35 +00:00
return html;
2013-05-29 19:55:23 +00:00
}
else {
2013-06-16 10:47:35 +00:00
return publisher.applyTemplate(fileDesc, publishAttributes, html);
2013-05-29 19:55:23 +00:00
}
}
// Recursive function to publish a file on multiple locations
var publishAttributesList = [];
2013-06-16 10:47:35 +00:00
var publishFileDesc = undefined;
var publishHTML = undefined;
2013-05-29 19:55:23 +00:00
function publishLocation(callback, errorFlag) {
// No more publish location for this document
if(publishAttributesList.length === 0) {
callback(errorFlag);
return;
}
// Dequeue a synchronized location
var publishAttributes = publishAttributesList.pop();
2013-06-16 10:47:35 +00:00
// Format the content
var content = getPublishContent(publishFileDesc, publishAttributes, publishHTML);
2013-05-29 19:55:23 +00:00
// Call the provider
2013-06-16 10:47:35 +00:00
publishAttributes.provider.publish(publishAttributes, publishFileDesc.title, content, function(error) {
2013-05-29 19:55:23 +00:00
if(error !== undefined) {
var errorMsg = error.toString();
if(errorMsg.indexOf("|removePublish") !== -1) {
2013-06-16 10:47:35 +00:00
publishFileDesc.removePublishLocation(publishAttributes);
extensionMgr.onPublishRemoved(publishFileDesc, publishAttributes);
2013-05-29 19:55:23 +00:00
}
if(errorMsg.indexOf("|stopPublish") !== -1) {
callback(error);
return;
}
}
publishLocation(callback, errorFlag || error);
});
}
var publishRunning = false;
publisher.publish = function() {
// If publish is running or offline
if(publishRunning === true || core.isOffline) {
return;
}
publishRunning = true;
extensionMgr.onPublishRunning(true);
2013-06-16 10:47:35 +00:00
publishFileDesc = fileMgr.getCurrentFile();
publishHTML = $("#wmd-preview").html();
publishAttributesList = _.values(publishFileDesc.publishLocations);
2013-05-29 19:55:23 +00:00
publishLocation(function(errorFlag) {
publishRunning = false;
extensionMgr.onPublishRunning(false);
if(errorFlag === undefined) {
2013-06-16 10:47:35 +00:00
extensionMgr.onPublishSuccess(publishFileDesc);
2013-05-29 19:55:23 +00:00
}
});
};
// Generate a publishIndex associated to a file and store publishAttributes
function createPublishIndex(fileDesc, publishAttributes) {
var publishIndex = undefined;
do {
publishIndex = "publish." + utils.randomString();
} while (_.has(localStorage, publishIndex));
publishAttributes.publishIndex = publishIndex;
2013-06-16 10:47:35 +00:00
fileDesc.addPublishLocation(publishAttributes);
extensionMgr.onNewPublishSuccess(fileDesc, publishAttributes);
2013-05-29 19:55:23 +00:00
}
// Initialize the "New publication" dialog
var newLocationProvider = undefined;
function initNewLocation(provider) {
var defaultPublishFormat = provider.defaultPublishFormat || "markdown";
newLocationProvider = provider;
$(".publish-provider-name").text(provider.providerName);
// Show/hide controls depending on provider
$('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider.providerId).show();
// Reset fields
utils.resetModalInputs();
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
// Load preferences
2013-06-03 22:19:52 +00:00
var publishPreferences = utils.retrieveIgnoreError(provider.providerId + ".publishPreferences");
if(publishPreferences) {
2013-05-29 19:55:23 +00:00
_.each(provider.publishPreferencesInputIds, function(inputId) {
utils.setInputValue("#input-publish-" + inputId, publishPreferences[inputId]);
});
utils.setInputRadio("radio-publish-format", publishPreferences.format);
}
// Open dialog box
$("#modal-publish").modal();
}
// Add a new publish location to a local document
function performNewLocation(event) {
var provider = newLocationProvider;
var publishAttributes = provider.newPublishAttributes(event);
if(publishAttributes === undefined) {
return;
}
// Perform provider's publishing
var fileDesc = fileMgr.getCurrentFile();
2013-06-16 10:47:35 +00:00
var html = $("#wmd-preview").html();
var content = getPublishContent(fileDesc, publishAttributes, html);
provider.publish(publishAttributes, fileDesc.title, content, function(error) {
2013-05-29 19:55:23 +00:00
if(error === undefined) {
2013-05-29 23:04:52 +00:00
publishAttributes.provider = provider;
2013-05-29 19:55:23 +00:00
sharing.createLink(publishAttributes, function() {
createPublishIndex(fileDesc, publishAttributes);
});
}
});
// Store input values as preferences for next time we open the publish
// dialog
var publishPreferences = {};
_.each(provider.publishPreferencesInputIds, function(inputId) {
publishPreferences[inputId] = $("#input-publish-" + inputId).val();
});
publishPreferences.format = publishAttributes.format;
localStorage[provider.providerId + ".publishPreferences"] = JSON.stringify(publishPreferences);
}
core.onReady(function() {
// Add every provider
var publishMenu = $("#publish-menu");
_.each(providerMap, function(provider) {
// Provider's publish button
publishMenu.append($("<li>").append($('<a href="#"><i class="icon-' + provider.providerId + '"></i> ' + provider.providerName + '</a>').click(function() {
initNewLocation(provider);
})));
// Action links (if any)
$(".action-publish-" + provider.providerId).click(function() {
initNewLocation(provider);
});
});
$(".action-process-publish").click(performNewLocation);
// Save As menu items
$(".action-download-md").click(function() {
var content = $("#wmd-input").val();
var title = fileMgr.getCurrentFile().title;
utils.saveAs(content, title + ".md");
});
$(".action-download-html").click(function() {
var content = $("#wmd-preview").html();
var title = fileMgr.getCurrentFile().title;
utils.saveAs(content, title + ".html");
});
$(".action-download-template").click(function() {
2013-06-16 10:47:35 +00:00
var fileDesc = fileMgr.getCurrentFile();
var html = $("#wmd-preview").html();
var content = publisher.applyTemplate(fileDesc, undefined, html);
utils.saveAs(content, fileDesc.title + (settings.template.indexOf("documentHTML") === -1 ? ".md" : ".html"));
2013-05-29 19:55:23 +00:00
});
});
extensionMgr.onPublisherCreated(publisher);
return publisher;
2013-04-09 07:58:06 +00:00
});