2013-04-20 00:14:20 +00:00
|
|
|
define(["jquery", "github-provider", "blogger-provider", "dropbox-provider", "gdrive-provider", "underscore"], function($) {
|
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-14 13:24:29 +00:00
|
|
|
// Create a map with providerName: providerObject
|
|
|
|
var providerMap = _.chain(arguments)
|
|
|
|
.map(function(argument) {
|
2013-04-20 17:40:05 +00:00
|
|
|
return argument && argument.providerId && [argument.providerId, argument];
|
2013-04-14 13:24:29 +00:00
|
|
|
}).compact().object().value();
|
|
|
|
|
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
|
2013-04-14 13:24:29 +00:00
|
|
|
publisher.notifyPublish = function() {
|
2013-04-13 18:11:54 +00:00
|
|
|
var fileIndex = fileManager.getCurrentFileIndex();
|
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();
|
2013-04-14 13:24:29 +00:00
|
|
|
publisher.refreshManagePublish();
|
2013-04-10 23:13:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// 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-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-04-14 13:24:29 +00:00
|
|
|
var fileIndex = fileManager.getCurrentFileIndex();
|
2013-04-14 21:15:40 +00:00
|
|
|
try {
|
|
|
|
return _.template(core.settings.template, {
|
|
|
|
documentTitle: localStorage[fileIndex + ".title"],
|
|
|
|
documentMarkdown: $("#wmd-input").val(),
|
|
|
|
documentHTML: $("#wmd-preview").html(),
|
|
|
|
publishAttributes: publishAttributes
|
|
|
|
});
|
|
|
|
} catch(e) {
|
|
|
|
core.showError(e);
|
|
|
|
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
|
|
|
|
var publishIndexList = [];
|
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
|
|
|
|
if (publishIndexList.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
|
|
|
|
var publishIndex = publishIndexList.pop();
|
2013-04-14 21:15:40 +00:00
|
|
|
var publishAttributes = JSON.parse(localStorage[publishIndex]);
|
|
|
|
var content = getPublishContent(publishAttributes);
|
2013-04-14 13:24:29 +00:00
|
|
|
|
|
|
|
// Call the provider
|
2013-04-14 21:15:40 +00:00
|
|
|
var provider = providerMap[publishAttributes.provider];
|
|
|
|
provider.publish(publishAttributes, publishTitle, content, function(error) {
|
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;
|
|
|
|
publisher.updatePublishButton();
|
2013-04-13 18:11:54 +00:00
|
|
|
var fileIndex = fileManager.getCurrentFileIndex();
|
2013-04-14 13:24:29 +00:00
|
|
|
publishTitle = localStorage[fileIndex + ".title"];
|
|
|
|
publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";"));
|
|
|
|
publishLocation(function(errorFlag) {
|
2013-04-11 22:38:41 +00:00
|
|
|
publishRunning = false;
|
|
|
|
publisher.updatePublishButton();
|
2013-04-14 13:24:29 +00:00
|
|
|
if(errorFlag === undefined) {
|
2013-04-14 21:15:40 +00:00
|
|
|
core.showMessage('"' + publishTitle + '" successfully published.');
|
2013-04-11 22:38:41 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2013-04-13 18:11:54 +00:00
|
|
|
|
2013-04-14 21:15:40 +00:00
|
|
|
// Generate a publishIndex associated to a fileIndex and store publishAttributes
|
|
|
|
function createPublishIndex(fileIndex, publishAttributes) {
|
2013-04-14 13:24:29 +00:00
|
|
|
var publishIndex = undefined;
|
|
|
|
do {
|
|
|
|
publishIndex = "publish." + core.randomString();
|
|
|
|
} while(_.has(localStorage, publishIndex));
|
2013-04-14 21:15:40 +00:00
|
|
|
localStorage[publishIndex] = JSON.stringify(publishAttributes);
|
2013-04-14 13:24:29 +00:00
|
|
|
localStorage[fileIndex + ".publish"] += publishIndex + ";";
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
core.resetModalInputs();
|
|
|
|
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
2013-04-13 18:11:54 +00:00
|
|
|
var fileIndex = fileManager.getCurrentFileIndex();
|
|
|
|
var title = localStorage[fileIndex + ".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-04-14 21:15:40 +00:00
|
|
|
createPublishIndex(fileIndex, publishAttributes);
|
2013-04-14 13:24:29 +00:00
|
|
|
publisher.notifyPublish();
|
|
|
|
core.showMessage('"' + title
|
2013-04-15 21:15:15 +00:00
|
|
|
+ '" is now published on ' + provider.providerName + '.');
|
2013-04-14 13:24:29 +00:00
|
|
|
}
|
|
|
|
});
|
2013-04-15 21:15:15 +00:00
|
|
|
}
|
2013-04-14 13:24:29 +00:00
|
|
|
|
|
|
|
// Used to populate the "Manage publication" dialog
|
|
|
|
var lineTemplate = ['<div class="input-prepend input-append">',
|
|
|
|
'<span class="add-on" title="<%= provider.providerName %>">',
|
|
|
|
'<i class="icon-<%= provider.providerId %>"></i></span>',
|
|
|
|
'<input class="span5" type="text" value="<%= publishDesc %>" disabled />',
|
|
|
|
'</div>'].join("");
|
|
|
|
var removeButtonTemplate = '<a class="btn" title="Remove this location"><i class="icon-trash"></i></a>';
|
|
|
|
publisher.refreshManagePublish = function() {
|
|
|
|
var fileIndex = fileManager.getCurrentFileIndex();
|
|
|
|
var publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";"));
|
|
|
|
$(".msg-no-publish, .msg-publish-list").addClass("hide");
|
|
|
|
$("#manage-publish-list .input-append").remove();
|
|
|
|
if (publishIndexList.length > 0) {
|
|
|
|
$(".msg-publish-list").removeClass("hide");
|
|
|
|
} else {
|
|
|
|
$(".msg-no-publish").removeClass("hide");
|
2013-04-13 18:11:54 +00:00
|
|
|
}
|
2013-04-14 13:24:29 +00:00
|
|
|
_.each(publishIndexList, function(publishIndex) {
|
2013-04-20 00:14:20 +00:00
|
|
|
var publishAttributes = JSON.parse(localStorage[publishIndex]);
|
2013-04-15 21:15:15 +00:00
|
|
|
var publishDesc = JSON.stringify(publishAttributes).replace(/{|}|"/g, "");
|
2013-04-14 13:24:29 +00:00
|
|
|
lineElement = $(_.template(lineTemplate, {
|
2013-04-14 21:15:40 +00:00
|
|
|
provider: providerMap[publishAttributes.provider],
|
2013-04-14 13:24:29 +00:00
|
|
|
publishDesc: publishDesc
|
|
|
|
}));
|
|
|
|
lineElement.append($(removeButtonTemplate).click(function() {
|
|
|
|
fileManager.removePublish(publishIndex);
|
|
|
|
}));
|
|
|
|
$("#manage-publish-list").append(lineElement);
|
|
|
|
});
|
|
|
|
};
|
2013-04-13 18:11:54 +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-13 18:11:54 +00:00
|
|
|
|
2013-04-15 21:15:15 +00:00
|
|
|
// Init each provider
|
2013-04-13 18:11:54 +00:00
|
|
|
_.each(providerMap, function(provider) {
|
2013-04-20 00:14:20 +00:00
|
|
|
provider.init(core, fileManager);
|
2013-04-15 21:15:15 +00:00
|
|
|
// Publish provider button
|
|
|
|
$(".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-04-11 22:38:41 +00:00
|
|
|
$(".action-force-publish").click(function() {
|
|
|
|
if(!$(this).hasClass("disabled")) {
|
|
|
|
publisher.publish();
|
|
|
|
}
|
|
|
|
});
|
2013-04-14 16:58:35 +00:00
|
|
|
|
|
|
|
$(".tooltip-template").tooltip({
|
2013-04-14 21:15:40 +00:00
|
|
|
html: true,
|
|
|
|
container: '#modal-settings',
|
|
|
|
placement: 'right',
|
|
|
|
trigger: 'manual',
|
|
|
|
title: ['Available variables:<br>',
|
|
|
|
'<ul><li><b>documentTitle</b>: document title</li>',
|
|
|
|
'<li><b>documentMarkdown</b>: document in Markdown format</li>',
|
|
|
|
'<li><b>documentHTML</b>: document in HTML format</li>',
|
|
|
|
'<li><b>publishAttributes</b>: attributes of the publish location (undefined when using "Save")</li></ul>',
|
|
|
|
'Examples:<br>',
|
|
|
|
_.escape('<title><%= documentTitle %></title>'),
|
|
|
|
'<br>',
|
|
|
|
_.escape('<div><%- documentHTML %></div>'),
|
|
|
|
'<br>',
|
|
|
|
_.escape('<% if(publishAttributes.provider == "github") print(documentMarkdown); %>'),
|
|
|
|
'<br><br><a target="_blank" href="http://underscorejs.org/#template">More info</a>',
|
|
|
|
].join("")
|
|
|
|
}).click(function(e) {
|
|
|
|
$(this).tooltip('show');
|
|
|
|
e.stopPropagation();
|
|
|
|
});
|
|
|
|
|
|
|
|
$(document).click(function(e) {
|
|
|
|
$(".tooltip-template").tooltip('hide');
|
2013-04-14 16:58:35 +00:00
|
|
|
});
|
2013-04-09 07:58:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return publisher;
|
|
|
|
});
|