Stackedit/js/publisher.js

253 lines
8.2 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-05-26 22:59:03 +00:00
"extension-manager",
2013-05-27 19:45:33 +00:00
"file-system",
"file-manager",
2013-05-26 22:59:03 +00:00
"sharing",
"blogger-provider",
"dropbox-provider",
"gist-provider",
"github-provider",
"gdrive-provider",
"ssh-provider",
"tumblr-provider",
2013-05-27 19:45:33 +00:00
"wordpress-provider"
], function($, _, core, utils, settings, extensionMgr, fileSystem, fileMgr, sharing) {
2013-04-09 07:58:06 +00:00
var publisher = {};
2013-05-27 19:45:33 +00:00
// Create a map with providerId: providerModule
var providerMap = _.chain(
arguments
).map(function(argument) {
return argument && argument.providerId && [argument.providerId, argument];
}).compact().object().value();
2013-04-09 07:58:06 +00:00
2013-05-27 19:45:33 +00:00
// Retrieve publish locations from localStorage
_.each(fileSystem, function(fileDesc) {
_.chain(
localStorage[fileDesc.fileIndex + ".publish"].split(";")
).compact().each(function(publishIndex) {
var publishAttributes = JSON.parse(localStorage[publishIndex]);
// Store publishIndex
publishAttributes.publishIndex = publishIndex;
// Replace provider ID by provider module in attributes
publishAttributes.provider = providerMap[publishAttributes.provider];
fileDesc.publishLocations[publishIndex] = publishAttributes;
});
});
2013-04-14 13:24:29 +00:00
// Apply template to the current document
2013-04-14 21:15:40 +00:00
publisher.applyTemplate = function(publishAttributes) {
2013-05-27 19:45:33 +00:00
var fileDesc = fileMgr.getCurrentFile();
2013-04-14 21:15:40 +00:00
try {
2013-05-27 19:45:33 +00:00
return _.template(settings.template, {
2013-05-26 22:59:03 +00:00
documentTitle: fileDesc.title,
2013-04-14 21:15:40 +00:00
documentMarkdown: $("#wmd-input").val(),
documentHTML: $("#wmd-preview").html(),
publishAttributes: publishAttributes
});
} catch(e) {
2013-05-27 19:45:33 +00:00
extensionMgr.onError(e);
2013-04-14 21:15:40 +00:00
throw e;
}
2013-04-14 13:24:29 +00:00
};
2013-04-11 22:38:41 +00:00
// Used to get content to publish
2013-04-14 21:15:40 +00:00
function getPublishContent(publishAttributes) {
if(publishAttributes.format === undefined) {
publishAttributes.format = $("input:radio[name=radio-publish-format]:checked").prop("value");
2013-04-11 22:38:41 +00:00
}
2013-04-14 21:15:40 +00:00
if(publishAttributes.format == "markdown") {
2013-04-11 22:38:41 +00:00
return $("#wmd-input").val();
}
2013-04-14 21:15:40 +00:00
else if(publishAttributes.format == "html") {
2013-04-14 13:24:29 +00:00
return $("#wmd-preview").html();
}
else {
2013-04-14 21:15:40 +00:00
return publisher.applyTemplate(publishAttributes);
2013-04-14 13:24:29 +00:00
}
2013-04-13 18:11:54 +00:00
}
2013-04-11 22:38:41 +00:00
// Recursive function to publish a file on multiple locations
2013-05-26 22:59:03 +00:00
var publishAttributesList = [];
2013-04-14 13:24:29 +00:00
var publishTitle = undefined;
function publishLocation(callback, errorFlag) {
2013-04-11 22:38:41 +00:00
// No more publish location for this document
2013-05-26 22:59:03 +00:00
if (publishAttributesList.length === 0) {
2013-04-14 13:24:29 +00:00
callback(errorFlag);
2013-04-11 22:38:41 +00:00
return;
}
// Dequeue a synchronized location
2013-05-26 23:38:13 +00:00
var publishAttributes = publishAttributesList.pop();
2013-04-14 21:15:40 +00:00
var content = getPublishContent(publishAttributes);
2013-04-14 13:24:29 +00:00
// Call the provider
2013-05-27 23:39:07 +00:00
publishAttributes.provider.publish(publishAttributes, publishTitle, content, function(error) {
2013-04-21 00:51:07 +00:00
if(error !== undefined) {
var errorMsg = error.toString();
if(errorMsg.indexOf("|removePublish") !== -1) {
2013-05-27 19:45:33 +00:00
fileMgr.removePublish(publishAttributes);
2013-04-21 00:51:07 +00:00
}
if(errorMsg.indexOf("|stopPublish") !== -1) {
callback(error);
return;
}
2013-04-21 00:07:27 +00:00
}
2013-04-15 21:15:15 +00:00
publishLocation(callback, errorFlag || error );
2013-04-14 13:24:29 +00:00
});
2013-04-11 22:38:41 +00:00
}
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;
2013-05-27 19:45:33 +00:00
extensionMgr.onPublishRunning(true);
var fileDesc = fileMgr.getCurrentFile();
2013-05-26 22:59:03 +00:00
publishTitle = fileDesc.title;
publishAttributesList = _.values(fileDesc.publishLocations);
2013-04-14 13:24:29 +00:00
publishLocation(function(errorFlag) {
2013-04-11 22:38:41 +00:00
publishRunning = false;
2013-05-27 19:45:33 +00:00
extensionMgr.onPublishRunning(false);
2013-04-14 13:24:29 +00:00
if(errorFlag === undefined) {
2013-05-27 19:45:33 +00:00
extensionMgr.onPublishSuccess(fileDesc);
2013-04-11 22:38:41 +00:00
}
});
};
2013-04-13 18:11:54 +00:00
2013-05-26 22:59:03 +00:00
// Generate a publishIndex associated to a file and store publishAttributes
function createPublishIndex(fileDesc, publishAttributes) {
2013-04-14 13:24:29 +00:00
var publishIndex = undefined;
do {
2013-05-25 18:13:59 +00:00
publishIndex = "publish." + utils.randomString();
2013-04-14 13:24:29 +00:00
} while(_.has(localStorage, publishIndex));
2013-05-26 23:38:13 +00:00
publishAttributes.publishIndex = publishIndex;
2013-05-28 23:41:09 +00:00
utils.storeAttributes(publishAttributes);
2013-05-27 19:45:33 +00:00
fileMgr.addPublish(fileDesc, publishAttributes);
2013-04-14 13:24:29 +00:00
}
2013-04-15 21:15:15 +00:00
// Initialize the "New publication" dialog
var newLocationProvider = undefined;
function initNewLocation(provider) {
var defaultPublishFormat = provider.defaultPublishFormat || "markdown";
newLocationProvider = provider;
2013-04-16 15:02:24 +00:00
$(".publish-provider-name").text(provider.providerName);
2013-04-15 21:15:15 +00:00
// Show/hide controls depending on provider
$('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider.providerId).show();
// Reset fields
2013-05-26 01:10:58 +00:00
utils.resetModalInputs();
2013-04-15 21:15:15 +00:00
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
// Load preferences
var serializedPreferences = localStorage[provider.providerId + ".publishPreferences"];
if(serializedPreferences) {
var publishPreferences = JSON.parse(serializedPreferences);
_.each(provider.publishPreferencesInputIds, function(inputId) {
2013-05-25 18:13:59 +00:00
utils.setInputValue("#input-publish-" + inputId, publishPreferences[inputId]);
});
2013-05-25 18:13:59 +00:00
utils.setInputRadio("radio-publish-format", publishPreferences.format);
}
2013-04-15 21:15:15 +00:00
// Open dialog box
$("#modal-publish").modal();
}
2013-04-13 18:11:54 +00:00
// Add a new publish location to a local document
2013-04-15 21:15:15 +00:00
function performNewLocation(event) {
var provider = newLocationProvider;
var publishAttributes = provider.newPublishAttributes(event);
if(publishAttributes === undefined) {
return;
}
// Perform provider's publishing
2013-05-27 19:45:33 +00:00
var fileDesc = fileMgr.getCurrentFile();
2013-05-26 22:59:03 +00:00
var title = fileDesc.title;
2013-04-14 21:15:40 +00:00
var content = getPublishContent(publishAttributes);
provider.publish(publishAttributes, title, content, function(error) {
2013-04-14 13:24:29 +00:00
if(error === undefined) {
2013-04-15 21:15:15 +00:00
publishAttributes.provider = provider.providerId;
2013-05-04 00:05:58 +00:00
sharing.createLink(publishAttributes, function() {
2013-05-26 22:59:03 +00:00
createPublishIndex(fileDesc, publishAttributes);
2013-05-04 00:05:58 +00:00
});
2013-04-14 13:24:29 +00:00
}
});
// 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);
2013-04-15 21:15:15 +00:00
}
2013-04-13 18:11:54 +00:00
2013-05-26 22:59:03 +00:00
// Retrieve file's publish locations from localStorage
publisher.populatePublishLocations = function(fileDesc) {
2013-05-27 19:45:33 +00:00
_.chain(
localStorage[fileDesc.fileIndex + ".publish"].split(";")
).compact().each(function(publishIndex) {
var publishAttributes = JSON.parse(localStorage[publishIndex]);
// Store publishIndex
publishAttributes.publishIndex = publishIndex;
// Replace provider ID by provider module in attributes
publishAttributes.provider = providerMap[publishAttributes.provider];
fileDesc.publishLocations[publishIndex] = publishAttributes;
});
};
2013-04-22 01:04:12 +00:00
core.onReady(function() {
2013-04-29 23:19:54 +00:00
// Add every provider
var publishMenu = $("#publish-menu");
2013-04-13 18:11:54 +00:00
_.each(providerMap, function(provider) {
2013-04-29 23:19:54 +00:00
// Provider's publish button
publishMenu.append(
$("<li>").append(
$('<a href="#"><i class="icon-' + provider.providerId + '"></i> ' + provider.providerName + '</a>')
.click(function() {
initNewLocation(provider);
}
)
)
);
2013-05-02 00:02:57 +00:00
// Action links (if any)
$(".action-publish-" + provider.providerId).click(function() {
initNewLocation(provider);
});
2013-04-13 18:11:54 +00:00
});
2013-04-15 21:15:15 +00:00
$(".action-process-publish").click(performNewLocation);
2013-05-27 19:45:33 +00:00
// 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() {
var content = publisher.applyTemplate();
var title = fileMgr.getCurrentFile().title;
utils.saveAs(content, title + ".txt");
});
2013-04-21 00:07:27 +00:00
});
2013-04-09 07:58:06 +00:00
2013-05-27 19:45:33 +00:00
extensionMgr.onPublisherCreated(publisher);
2013-04-09 07:58:06 +00:00
return publisher;
});