Stackedit/public/res/publisher.js

379 lines
16 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-11-05 23:03:38 +00:00
"constants",
2013-05-26 22:59:03 +00:00
"utils",
2013-11-05 23:03:38 +00:00
"storage",
2013-05-27 19:45:33 +00:00
"settings",
2013-07-30 08:46:36 +00:00
"eventMgr",
2013-06-10 21:22:32 +00:00
"fileSystem",
"fileMgr",
2013-05-26 22:59:03 +00:00
"sharing",
2013-06-22 23:48:57 +00:00
"classes/Provider",
2013-09-22 22:16:59 +00:00
"classes/AsyncTask",
2013-06-10 21:22:32 +00:00
"providers/bloggerProvider",
2014-01-19 13:31:36 +00:00
"providers/bloggerPageProvider",
2013-06-10 21:22:32 +00:00
"providers/dropboxProvider",
"providers/gistProvider",
"providers/githubProvider",
"providers/gdriveProvider",
2013-12-16 00:31:40 +00:00
"providers/gdrivesecProvider",
2013-12-23 22:40:13 +00:00
"providers/gdriveterProvider",
2013-06-10 21:22:32 +00:00
"providers/sshProvider",
"providers/tumblrProvider",
"providers/wordpressProvider"
2014-01-13 18:57:59 +00:00
], function($, _, constants, utils, storage, settings, eventMgr, fileSystem, fileMgr, sharing, Provider, AsyncTask) {
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) {
2013-12-23 22:40:13 +00:00
return argument instanceof Provider && argument.isPublishEnabled === true && [
2013-05-29 19:55:23 +00:00
argument.providerId,
argument
];
}).compact().object().value();
2013-11-05 23:03:38 +00:00
// Retrieve publish locations from storage
(function() {
var publishIndexMap = {};
_.each(fileSystem, function(fileDesc) {
utils.retrieveIndexArray(fileDesc.fileIndex + ".publish").forEach(function(publishIndex) {
try {
var publishAttributes = JSON.parse(storage[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;
fileDesc.publishLocations[publishIndex] = publishAttributes;
publishIndexMap[publishIndex] = publishAttributes;
}
catch(e) {
// storage can be corrupted
eventMgr.onError(e);
// Remove publish location
utils.removeIndexFromArray(fileDesc.fileIndex + ".publish", publishIndex);
}
});
});
// Clean fields from deleted files in local storage
Object.keys(storage).forEach(function(key) {
2014-04-12 22:30:18 +00:00
var match = key.match(/publish\.\S+/);
if(match && !publishIndexMap.hasOwnProperty(match[0])) {
storage.removeItem(key);
2013-06-03 22:19:52 +00:00
}
2013-05-29 19:55:23 +00:00
});
})();
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 {
2013-08-22 19:10:57 +00:00
var template = (publishAttributes && publishAttributes.customTmpl) || settings.template;
return _.template(template, {
2013-05-29 19:55:23 +00:00
documentTitle: fileDesc.title,
2013-06-16 10:47:35 +00:00
documentMarkdown: fileDesc.content,
strippedDocumentMarkdown: fileDesc.content.substring(fileDesc.frontMatter ? fileDesc.frontMatter._frontMatter.length : 0),
documentHTML: html.withoutComments,
documentHTMLWithComments: html.withComments,
2013-10-06 14:34:40 +00:00
frontMatter: fileDesc.frontMatter,
2013-05-29 19:55:23 +00:00
publishAttributes: publishAttributes
});
}
catch(e) {
2013-07-30 08:46:36 +00:00
eventMgr.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) {
2013-08-22 19:10:57 +00:00
publishAttributes.format = utils.getInputRadio("radio-publish-format");
if(publishAttributes.format == 'template' && utils.getInputChecked("#checkbox-publish-custom-template")) {
publishAttributes.customTmpl = utils.getInputValue('#textarea-publish-custom-template');
}
2013-05-29 19:55:23 +00:00
}
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") {
return html.withoutComments;
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-11-05 23:03:38 +00:00
var publishFileDesc;
var publishHTML;
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-08-13 00:03:38 +00:00
2013-06-16 10:47:35 +00:00
// Format the content
var content = getPublishContent(publishFileDesc, publishAttributes, publishHTML);
2013-10-06 14:34:40 +00:00
var title = (publishFileDesc.frontMatter || {}).title || publishFileDesc.title;
2013-05-29 19:55:23 +00:00
// Call the provider
2013-10-06 14:34:40 +00:00
publishAttributes.provider.publish(publishAttributes, publishFileDesc.frontMatter, 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);
2013-07-30 08:46:36 +00:00
eventMgr.onPublishRemoved(publishFileDesc, publishAttributes);
2013-05-29 19:55:23 +00:00
}
if(errorMsg.indexOf("|stopPublish") !== -1) {
callback(error);
return;
}
}
publishLocation(callback, errorFlag || error);
});
}
2013-08-13 00:03:38 +00:00
2013-07-24 23:20:56 +00:00
// Get the html from the onPreviewFinished callback
var currentHTML;
eventMgr.addListener("onPreviewFinished", function(htmlWithComments, htmlWithoutComments) {
currentHTML = {
withComments: htmlWithComments,
withoutComments: htmlWithoutComments
};
2013-07-24 23:20:56 +00:00
});
2013-08-04 00:53:46 +00:00
// Listen to offline status changes
var isOffline = false;
eventMgr.addListener("onOfflineChanged", function(isOfflineParam) {
isOffline = isOfflineParam;
});
2013-05-29 19:55:23 +00:00
var publishRunning = false;
publisher.publish = function() {
// If publish is running or offline
2013-08-04 00:53:46 +00:00
if(publishRunning === true || isOffline === true) {
2013-05-29 19:55:23 +00:00
return;
}
publishRunning = true;
2013-07-30 08:46:36 +00:00
eventMgr.onPublishRunning(true);
2013-06-19 20:33:46 +00:00
publishFileDesc = fileMgr.currentFile;
publishHTML = currentHTML;
2013-06-16 10:47:35 +00:00
publishAttributesList = _.values(publishFileDesc.publishLocations);
2013-05-29 19:55:23 +00:00
publishLocation(function(errorFlag) {
publishRunning = false;
2013-07-30 08:46:36 +00:00
eventMgr.onPublishRunning(false);
2013-05-29 19:55:23 +00:00
if(errorFlag === undefined) {
2013-07-30 08:46:36 +00:00
eventMgr.onPublishSuccess(publishFileDesc);
2013-05-29 19:55:23 +00:00
}
});
};
// Generate a publishIndex associated to a file and store publishAttributes
function createPublishIndex(fileDesc, publishAttributes) {
2013-11-05 23:03:38 +00:00
var publishIndex;
2013-05-29 19:55:23 +00:00
do {
publishIndex = "publish." + utils.randomString();
2013-11-05 23:03:38 +00:00
} while (_.has(storage, publishIndex));
2013-05-29 19:55:23 +00:00
publishAttributes.publishIndex = publishIndex;
2013-06-16 10:47:35 +00:00
fileDesc.addPublishLocation(publishAttributes);
2013-07-30 08:46:36 +00:00
eventMgr.onNewPublishSuccess(fileDesc, publishAttributes);
2013-05-29 19:55:23 +00:00
}
// Initialize the "New publication" dialog
2013-11-05 23:03:38 +00:00
var newLocationProvider;
2013-05-29 19:55:23 +00:00
function initNewLocation(provider) {
var defaultPublishFormat = provider.defaultPublishFormat || "markdown";
newLocationProvider = provider;
$(".publish-provider-name").text(provider.providerName);
// Show/hide controls depending on provider
2013-10-06 14:34:40 +00:00
$('.modal-publish [class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider.providerId).show();
2013-05-29 19:55:23 +00:00
// Reset fields
utils.resetModalInputs();
2013-08-22 19:10:57 +00:00
utils.setInputRadio("radio-publish-format", defaultPublishFormat);
utils.setInputChecked("#checkbox-publish-custom-template", false);
utils.setInputValue('#textarea-publish-custom-template', settings.template);
2013-05-29 19:55:23 +00:00
// 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) {
2013-08-23 23:50:14 +00:00
var publishPreferenceValue = publishPreferences[inputId];
if(_.isBoolean(publishPreferenceValue)) {
utils.setInputChecked("#input-publish-" + inputId, publishPreferenceValue);
}
else {
utils.setInputValue("#input-publish-" + inputId, publishPreferenceValue);
}
2013-05-29 19:55:23 +00:00
});
utils.setInputRadio("radio-publish-format", publishPreferences.format);
2013-08-23 23:50:14 +00:00
utils.setInputChecked("#checkbox-publish-custom-template", publishPreferences.customTmpl !== undefined);
utils.setInputValue('#textarea-publish-custom-template', publishPreferences.customTmpl || settings.template);
2013-05-29 19:55:23 +00:00
}
// Open dialog box
2013-08-11 00:52:05 +00:00
$(".modal-publish").modal();
2013-05-29 19:55:23 +00:00
}
// 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
2013-06-19 20:33:46 +00:00
var fileDesc = fileMgr.currentFile;
var content = getPublishContent(fileDesc, publishAttributes, currentHTML);
2013-10-06 14:34:40 +00:00
var title = (fileDesc.frontMatter && fileDesc.frontMatter.title) || fileDesc.title;
provider.publish(publishAttributes, fileDesc.frontMatter, 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) {
2013-08-23 23:50:14 +00:00
var inputElt = document.getElementById("input-publish-" + inputId);
if(inputElt.type == 'checkbox') {
publishPreferences[inputId] = inputElt.checked;
}
else {
publishPreferences[inputId] = inputElt.value;
}
2013-05-29 19:55:23 +00:00
});
publishPreferences.format = publishAttributes.format;
2013-08-22 19:10:57 +00:00
publishPreferences.customTmpl = publishAttributes.customTmpl;
2013-11-05 23:03:38 +00:00
storage[provider.providerId + ".publishPreferences"] = JSON.stringify(publishPreferences);
2013-05-29 19:55:23 +00:00
}
2013-08-13 00:03:38 +00:00
var initPublishButtonTmpl = [
'<li>',
' <a href="#"',
' class="action-init-publish-<%= provider.providerId %>">',
' <i class="icon-provider-<%= provider.providerId %>"></i> <%= provider.providerName %>',
' </a>',
'</li>'
].join('');
2013-08-04 00:53:46 +00:00
eventMgr.addListener("onReady", function() {
2013-11-05 23:03:38 +00:00
if(window.viewerMode === false) {
2013-08-21 00:16:10 +00:00
// Add every provider in the panel menu
2014-03-09 01:50:24 +00:00
var publishMenuElt = document.querySelector('.menu-panel .publish-on-provider-list');
2013-08-21 00:16:10 +00:00
var publishMenuHtml = _.reduce(providerMap, function(result, provider) {
return result + _.template(initPublishButtonTmpl, {
provider: provider
});
}, '');
publishMenuElt.innerHTML = publishMenuHtml;
_.each(providerMap, function(provider) {
// Click on open publish dialog
$(publishMenuElt.querySelector('.action-init-publish-' + provider.providerId)).click(function() {
initNewLocation(provider);
});
// Click on perform new publication
$(".action-publish-" + provider.providerId).click(function() {
initNewLocation(provider);
});
2013-05-29 19:55:23 +00:00
});
2013-08-21 00:16:10 +00:00
}
2013-08-23 23:50:14 +00:00
//
2013-05-29 19:55:23 +00:00
$(".action-process-publish").click(performNewLocation);
2014-03-09 01:50:24 +00:00
$(".action-update-publication").click(publisher.publish);
2013-08-23 23:50:14 +00:00
var $customTmplCollapseElt = $('.publish-custom-template-collapse').collapse({
toggle: false
});
2013-08-22 19:10:57 +00:00
var $customTmplTextareaElt = $('#textarea-publish-custom-template');
2013-08-23 23:50:14 +00:00
var doCustomTmplCollapse = _.debounce(function() {
$customTmplCollapseElt.collapse(utils.getInputRadio("radio-publish-format") == 'template' ? 'show' : 'hide');
}, 100);
2013-08-22 19:10:57 +00:00
$("#checkbox-publish-custom-template").change(function() {
$customTmplTextareaElt.prop('disabled', !this.checked);
});
$("input:radio[name=radio-publish-format]").change(function() {
2013-08-23 23:50:14 +00:00
doCustomTmplCollapse();
});
$('.modal-publish').on('hidden.bs.modal', function() {
$customTmplCollapseElt.collapse('hide');
2013-08-22 19:10:57 +00:00
});
2013-05-29 19:55:23 +00:00
// Save As menu items
$(".action-download-md").click(function() {
2013-09-10 23:51:37 +00:00
var content = fileMgr.currentFile.content;
2013-06-19 20:33:46 +00:00
var title = fileMgr.currentFile.title;
2013-05-29 19:55:23 +00:00
utils.saveAs(content, title + ".md");
});
$(".action-download-html").click(function() {
2013-06-19 20:33:46 +00:00
var title = fileMgr.currentFile.title;
utils.saveAs(currentHTML.withoutComments, title + ".html");
2013-05-29 19:55:23 +00:00
});
$(".action-download-template").click(function() {
2013-06-19 20:33:46 +00:00
var fileDesc = fileMgr.currentFile;
var content = publisher.applyTemplate(fileDesc, undefined, currentHTML);
2013-06-16 10:47:35 +00:00
utils.saveAs(content, fileDesc.title + (settings.template.indexOf("documentHTML") === -1 ? ".md" : ".html"));
2013-05-29 19:55:23 +00:00
});
2013-09-22 22:16:59 +00:00
$(".action-download-pdf").click(function() {
var fileDesc = fileMgr.currentFile;
var content = publisher.applyTemplate(fileDesc, {
customTmpl: settings.pdfTemplate
}, currentHTML);
2013-09-22 22:16:59 +00:00
var task = new AsyncTask();
2013-11-05 23:03:38 +00:00
var pdf;
2013-09-22 22:16:59 +00:00
task.onRun(function() {
if(isOffline === true) {
eventMgr.onError("Operation not available in offline mode.");
task.chain();
return;
}
var xhr = new XMLHttpRequest();
2013-11-05 23:03:38 +00:00
xhr.open('POST', constants.HTMLTOPDF_URL, true);
2013-09-22 22:16:59 +00:00
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
2013-10-27 19:19:56 +00:00
xhr.setRequestHeader('page-size', settings.pdfPageSize);
2013-09-22 22:16:59 +00:00
xhr.responseType = 'blob';
xhr.onreadystatechange = function() {
if(this.readyState == 4) {
if(this.status == 200) {
pdf = this.response;
}
else {
eventMgr.onError("Error when trying to generate PDF: " + this.status);
}
task.chain();
}
};
xhr.send(content);
});
task.onSuccess(function() {
if(pdf !== undefined) {
2014-01-13 18:57:59 +00:00
utils.saveAs(pdf, fileMgr.currentFile.title + ".pdf");
2013-09-22 22:16:59 +00:00
}
});
task.enqueue();
});
2013-05-29 19:55:23 +00:00
});
2013-07-30 08:46:36 +00:00
eventMgr.onPublisherCreated(publisher);
2013-05-29 19:55:23 +00:00
return publisher;
});