diff --git a/css/main.css b/css/main.css index c4d453e6..24a40c12 100644 --- a/css/main.css +++ b/css/main.css @@ -120,7 +120,7 @@ code { } h1 { - margin: 50px 0 20px; + margin: 40px 0 20px; } h2 { diff --git a/index.html b/index.html index e1f72fea..7e52c586 100644 --- a/index.html +++ b/index.html @@ -88,6 +88,8 @@
NOTE: You can add locations using - sub-menu "Publish on".
+ "Publish on" sub-menu. -Copyright 2013 Benoit Schweblin
diff --git a/js/blogger-provider.js b/js/blogger-provider.js
new file mode 100644
index 00000000..8d8134cc
--- /dev/null
+++ b/js/blogger-provider.js
@@ -0,0 +1,45 @@
+define(["jquery", "google-helper"], function($, googleHelper) {
+
+ // Dependencies
+ var core = undefined;
+
+ var bloggerProvider = {
+ providerType: PROVIDER_TYPE_PUBLISH_FLAG,
+ providerId: PROVIDER_BLOGGER,
+ providerName: "Blogger",
+ defaultPublishFormat: "html"
+ };
+
+ bloggerProvider.publish = function(publishAttributes, title, content, callback) {
+ googleHelper.uploadBlogger(publishAttributes.blogUrl,
+ publishAttributes.blogId, publishAttributes.postId, title, content,
+ function(blogId, postId) {
+ if(blogId === undefined || postId === undefined) {
+ callback(true);
+ return;
+ }
+ publishAttributes.blogId = blogId;
+ publishAttributes.postId = postId;
+ callback();
+ });
+ };
+
+ bloggerProvider.newPublishAttributes = function(event) {
+ var publishAttributes = {};
+ publishAttributes.blogUrl = core.getInputValue($("#input-publish-blogger-url"), event);
+ var postId = $("#input-publish-blogger-postid").val();
+ if(postId) {
+ publishAttributes.postId = postId;
+ }
+ if(event.isPropagationStopped()) {
+ return undefined;
+ }
+ return publishAttributes;
+ };
+
+ bloggerProvider.init = function(coreModule) {
+ core = coreModule;
+ };
+
+ return bloggerProvider;
+});
\ No newline at end of file
diff --git a/js/file-manager.js b/js/file-manager.js
index 50869c32..d6927471 100644
--- a/js/file-manager.js
+++ b/js/file-manager.js
@@ -370,49 +370,6 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
});
}
- // Initialize the "New publication" dialog
- var newPublishProvider = undefined;
- function initNewPublish(provider, defaultPublishFormat) {
- defaultPublishFormat = defaultPublishFormat || "markdown";
- newPublishProvider = provider;
-
- // Show/hide controls depending on provider
- $('div[class*=" modal-publish-"]').hide().filter(".modal-publish-" + provider).show();
-
- // Reset fields
- core.resetModalInputs();
- $("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
-
- // Open dialog box
- $("#modal-publish").modal();
- }
-
- // Create a new publication on GitHub
- function newPublishGithub(event) {
- var publishAttributes = {};
- publishAttributes.repository = core.getInputValue($("#input-publish-github-reponame"), event);
- publishAttributes.branch = core.getInputValue($("#input-publish-github-branch"), event);
- publishAttributes.path = core.getInputValue($("#input-publish-github-path"), event);
- publishAttributes.provider = newPublishProvider;
- if(event.isPropagationStopped()) {
- return;
- }
- publisher.newLocation(publishAttributes);
- }
-
- // Create a new publication on Blogger
- function newPublishBlogger(event) {
- var blogUrl = core.getInputValue($("#input-publish-blogger-url"), event);
- if(event.isPropagationStopped()) {
- return;
- }
-
- googleHelper.getBlogByUrl(blogUrl, function(blog) {
- console.log(blog);
- });
-
- }
-
fileManager.init = function(coreModule) {
core = coreModule;
@@ -497,22 +454,6 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni
var path = core.getInputValue($("#manual-dropbox-path"), event);
manualDropbox(path);
});
-
- // Publish actions
- $(".action-publish-github").click(function() {
- initNewPublish(PROVIDER_GITHUB);
- });
- $(".action-publish-blogger").click(function() {
- initNewPublish(PROVIDER_BLOGGER, "html");
- });
- $(".action-process-publish").click(function(e) {
- if(newPublishProvider == PROVIDER_GITHUB) {
- newPublishGithub(e);
- }
- else if(newPublishProvider == PROVIDER_BLOGGER) {
- newPublishBlogger(e);
- }
- });
};
return fileManager;
diff --git a/js/github-helper.js b/js/github-helper.js
index 4de5c277..44e6b8ca 100644
--- a/js/github-helper.js
+++ b/js/github-helper.js
@@ -161,7 +161,9 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
callback(error);
};
asyncTask.onError = function() {
- console.error(error);
+ if(error !== undefined) {
+ console.error(error);
+ }
var errorMsg = "Could not publish on GitHub.";
if(error === 401 || error === 403) {
github = undefined;
diff --git a/js/github-provider.js b/js/github-provider.js
index e9768166..635cbde9 100644
--- a/js/github-provider.js
+++ b/js/github-provider.js
@@ -3,21 +3,32 @@ define(["jquery", "github-helper"], function($, githubHelper) {
// Dependencies
var core = undefined;
- var publishGithub = {
+ var githubProvider = {
providerType: PROVIDER_TYPE_PUBLISH_FLAG,
providerId: PROVIDER_GITHUB,
providerName: "GitHub"
};
- publishGithub.publish = function(publishAttributes, title, content, callback) {
+ githubProvider.publish = function(publishAttributes, title, content, callback) {
var commitMsg = core.settings.commitMsg;
githubHelper.upload(publishAttributes.repository, publishAttributes.branch,
publishAttributes.path, content, commitMsg, callback);
};
- publishGithub.init = function(coreModule) {
+ githubProvider.newPublishAttributes = function(event) {
+ var publishAttributes = {};
+ publishAttributes.repository = core.getInputValue($("#input-publish-github-reponame"), event);
+ publishAttributes.branch = core.getInputValue($("#input-publish-github-branch"), event);
+ publishAttributes.path = core.getInputValue($("#input-publish-github-path"), event);
+ if(event.isPropagationStopped()) {
+ return undefined;
+ }
+ return publishAttributes;
+ };
+
+ githubProvider.init = function(coreModule) {
core = coreModule;
};
- return publishGithub;
+ return githubProvider;
});
\ No newline at end of file
diff --git a/js/google-helper.js b/js/google-helper.js
index a418fd03..08bc6e0e 100644
--- a/js/google-helper.js
+++ b/js/google-helper.js
@@ -355,7 +355,8 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
});
};
- function handleError(error, asyncTask, callback) {
+ function handleError(error, asyncTask, callback, serviceName) {
+ serviceName = serviceName || "Google Drive";
var errorMsg = undefined;
asyncTask.onError = function() {
if (errorMsg !== undefined) {
@@ -370,19 +371,19 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
errorMsg = error;
}
else if (error.code >= 500 && error.code < 600) {
- errorMsg = "Google Drive is not accessible.";
+ errorMsg = serviceName + " is not accessible.";
// Retry as described in Google's best practices
asyncTask.retry();
return;
} else if (error.code === 401 || error.code === 403) {
authenticated = false;
- errorMsg = "Access to Google Drive is not authorized.";
+ errorMsg = "Access to " + serviceName + " is not authorized.";
} else if (error.code <= 0) {
connected = false;
authenticated = false;
core.setOffline();
} else {
- errorMsg = "Google Drive error (" + error.code + ": "
+ errorMsg = serviceName + " error (" + error.code + ": "
+ error.message + ").";
}
}
@@ -493,40 +494,91 @@ define(["jquery", "async-runner"], function($, asyncTaskRunner) {
});
};
- googleHelper.getBlogByUrl = function(url, callback) {
+ googleHelper.uploadBlogger = function(blogUrl, blogId, postId, title, content, callback) {
authenticate(function() {
if (connected === false) {
callback();
return;
}
- var result = undefined;
var asyncTask = {};
asyncTask.run = function() {
var token = gapi.auth.getToken();
var headers = {
Authorization : token ? "Bearer " + token.access_token: null
};
- $.ajax({
- url : "https://www.googleapis.com/blogger/v3/blogs/byurl",
- data: { url: url },
- headers : headers,
- dataType : "json",
- timeout : AJAX_TIMEOUT
- }).done(function(blog, textStatus, jqXHR) {
- result = blog;
- asyncTask.success();
- }).fail(function(jqXHR) {
- var error = {
- code: jqXHR.status,
- message: jqXHR.statusText
+
+ function getBlogId(localCallback) {
+ $.ajax({
+ url : "https://www.googleapis.com/blogger/v3/blogs/byurl",
+ data: { url: blogUrl },
+ headers : headers,
+ dataType : "json",
+ timeout : AJAX_TIMEOUT
+ }).done(function(blog, textStatus, jqXHR) {
+ blogId = blog.id;
+ localCallback();
+ }).fail(function(jqXHR) {
+ var error = {
+ code: jqXHR.status,
+ message: jqXHR.statusText
+ };
+ // Handle error
+ if(error.code === 404) {
+ error = 'Blog "' + blogUrl + '" not found on Blogger.';
+ }
+ handleError(error, asyncTask, callback, "Blogger");
+ });
+ }
+
+ function publish() {
+ var url = "https://www.googleapis.com/blogger/v3/blogs/" + blogId + "/posts/";
+ var data = {
+ kind: "blogger#post",
+ blog: { id: blogId },
+ title: title,
+ content: content
};
- // Handle error
- handleError(error, asyncTask, callback);
- });
+ var type = "POST";
+ // If it's an update
+ if(postId !== undefined) {
+ url += postId;
+ data.id = postId;
+ type = "PUT";
+ }
+ $.ajax({
+ url : url,
+ data: JSON.stringify(data),
+ headers : headers,
+ type: type,
+ contentType: "application/json",
+ dataType : "json",
+ timeout : AJAX_TIMEOUT
+ }).done(function(post, textStatus, jqXHR) {
+ postId = post.id;
+ asyncTask.success();
+ }).fail(function(jqXHR) {
+ var error = {
+ code: jqXHR.status,
+ message: jqXHR.statusText
+ };
+ // Handle error
+ if(error.code === 404 && postId !== undefined) {
+ error = 'Post ' + postId + ' not found on Blogger.';
+ }
+ handleError(error, asyncTask, callback, "Blogger");
+ });
+ }
+
+ if(blogId === undefined) {
+ getBlogId(publish);
+ }
+ else {
+ publish();
+ }
};
asyncTask.onSuccess = function() {
- callback(result);
+ callback(blogId, postId);
};
asyncTask.onError = function() {
callback();
diff --git a/js/publisher.js b/js/publisher.js
index 96dd66fd..76479d9f 100644
--- a/js/publisher.js
+++ b/js/publisher.js
@@ -1,4 +1,4 @@
-define(["jquery", "github-provider", "underscore"], function($) {
+define(["jquery", "github-provider", "blogger-provider", "underscore"], function($) {
// Dependencies
var core = undefined;
@@ -91,7 +91,7 @@ define(["jquery", "github-provider", "underscore"], function($) {
// Call the provider
var provider = providerMap[publishAttributes.provider];
provider.publish(publishAttributes, publishTitle, content, function(error) {
- publishLocation(callback, errorFlag);
+ publishLocation(callback, errorFlag || error );
});
}
@@ -126,21 +126,43 @@ define(["jquery", "github-provider", "underscore"], function($) {
localStorage[fileIndex + ".publish"] += publishIndex + ";";
}
+ // Initialize the "New publication" dialog
+ var newLocationProvider = undefined;
+ function initNewLocation(provider) {
+ var defaultPublishFormat = provider.defaultPublishFormat || "markdown";
+ newLocationProvider = provider;
+
+ // 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();
+ }
+
// Add a new publish location to a local document
- publisher.newLocation = function(publishAttributes) {
+ function performNewLocation(event) {
+ var provider = newLocationProvider;
+ var publishAttributes = provider.newPublishAttributes(event);
+ if(publishAttributes === undefined) {
+ return;
+ }
var fileIndex = fileManager.getCurrentFileIndex();
var title = localStorage[fileIndex + ".title"];
var content = getPublishContent(publishAttributes);
- var provider = providerMap[publishAttributes.provider];
provider.publish(publishAttributes, title, content, function(error) {
if(error === undefined) {
+ publishAttributes.provider = provider.providerId;
createPublishIndex(fileIndex, publishAttributes);
publisher.notifyPublish();
core.showMessage('"' + title
- + '" will now be published on GitHub.');
+ + '" is now published on ' + provider.providerName + '.');
}
});
- };
+ }
// Used to populate the "Manage publication" dialog
var lineTemplate = ['