From ded5975c14bbe5f0edca15b3dbf272b356eb20d4 Mon Sep 17 00:00:00 2001 From: benweet Date: Mon, 15 Apr 2013 22:15:15 +0100 Subject: [PATCH] Support for Blogger --- css/main.css | 2 +- index.html | 53 ++++++++++++++--------- js/blogger-provider.js | 45 +++++++++++++++++++ js/file-manager.js | 59 ------------------------- js/github-helper.js | 4 +- js/github-provider.js | 19 ++++++-- js/google-helper.js | 98 ++++++++++++++++++++++++++++++++---------- js/publisher.js | 44 +++++++++++++++---- 8 files changed, 207 insertions(+), 117 deletions(-) create mode 100644 js/blogger-provider.js 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 @@
  • + name="radio-publish-format" value="template"> Template + @@ -344,7 +346,7 @@ is not published.

    NOTE: You can add locations using - sub-menu "Publish on".

    + "Publish on" sub-menu.

    -
    - -
    - -
    -
    +
    + +
    + +
    +
    @@ -412,7 +414,9 @@
    + for="textarea-settings-publish-template">Template (?) +
    @@ -494,16 +498,23 @@
    PageDown + / Pagedown-extra +
    +
    + Prettify
    RequireJS
    -
    - UI Layout -
    -
    - Underscore.js -
    +
    + UI + Layout +
    +
    + Underscore.js +

    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 = ['

    ', @@ -162,7 +184,7 @@ define(["jquery", "github-provider", "underscore"], function($) { _.each(publishIndexList, function(publishIndex) { var serializedObject = localStorage[publishIndex]; var publishAttributes = JSON.parse(serializedObject); - var publishDesc = JSON.stringify(_.omit(publishAttributes, 'provider')).replace(/{|}|"/g, ""); + var publishDesc = JSON.stringify(publishAttributes).replace(/{|}|"/g, ""); lineElement = $(_.template(lineTemplate, { provider: providerMap[publishAttributes.provider], publishDesc: publishDesc @@ -178,11 +200,17 @@ define(["jquery", "github-provider", "underscore"], function($) { core = coreModule; fileManager = fileManagerModule; - // Init providers + // Init each provider _.each(providerMap, function(provider) { provider.init(core); + // Publish provider button + $(".action-publish-" + provider.providerId).click(function() { + initNewLocation(provider); + }); }); + $(".action-process-publish").click(performNewLocation); + $(".action-force-publish").click(function() { if(!$(this).hasClass("disabled")) { publisher.publish();