From 0420be3a7dc03f2904a3eab4bb6a81b835fc1ec3 Mon Sep 17 00:00:00 2001 From: benweet Date: Sun, 14 Apr 2013 14:24:29 +0100 Subject: [PATCH] Support for templating --- css/main.css | 11 ++- index.html | 13 ++- js/async-runner.js | 6 +- js/config.js | 5 +- js/core.js | 17 +++- js/file-manager.js | 194 +++++++++++++----------------------------- js/github-provider.js | 23 +++++ js/publisher.js | 124 +++++++++++++++++++-------- 8 files changed, 214 insertions(+), 179 deletions(-) create mode 100644 js/github-provider.js diff --git a/css/main.css b/css/main.css index 6d8f52c3..a46e102c 100644 --- a/css/main.css +++ b/css/main.css @@ -29,7 +29,7 @@ div, span, a, ul, li, textarea, input, button { text-shadow: none !important; } -.btn, .navbar-inner, .ui-layout-east, .ui-layout-south, textarea, .add-on { +.btn, .navbar-inner, .ui-layout-east, .ui-layout-south, .add-on { border: none !important; } @@ -39,12 +39,13 @@ div, span, a, ul, li, textarea, input, button { } input, +textarea, .input-prepend .btn, .input-prepend .add-on { border: 1px solid #ebebeb !important; } -input.error { +.error { border-color: #ff6661 !important; } @@ -165,6 +166,7 @@ hr { #wmd-input { font-family: "Courier New", Courier, monospace; resize: none; + border: none !important; } #wmd-preview { @@ -325,3 +327,8 @@ hr { .picker-dialog { z-index: 1050 !important; } + +#modal-settings textarea { + max-width: 206px; + height: 150px; +} diff --git a/index.html b/index.html index bd19a52c..dadb120a 100644 --- a/index.html +++ b/index.html @@ -58,6 +58,8 @@ class="icon-download-alt"> Save as MD
  • Save as HTML
  • +
  • Save using template
  • ").append(a); if (fileDesc.index == fileIndex) { @@ -176,20 +173,19 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni }; })(fileDesc.index)); } - $("#file-selector").append(li); - } + $("#file-selector").append(li); + }); synchronizer.useGoogleDrive = useGoogleDrive; synchronizer.useDropbox = useDropbox; }; - // Remove a synchronized location + // Remove a syncIndex (synchronized location) fileManager.removeSync = function(syncIndex) { - var currentFileIndex = fileManager.getCurrentFileIndex(); - var fileIndex = this.getFileIndexFromSync(syncIndex); + var fileIndex = fileManager.getFileIndexFromSync(syncIndex); if(fileIndex !== undefined) { localStorage[fileIndex + ".sync"] = localStorage[fileIndex + ".sync"].replace(";" + syncIndex + ";", ";"); - if(fileIndex == currentFileIndex) { + if(fileManager.isCurrentFileIndex(fileIndex)) { refreshManageSync(); } } @@ -200,46 +196,36 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni localStorage.removeItem(syncIndex + ".titleCRC"); }; - // Look for local file associated to a synchronized location + // Get the fileIndex associated to a syncIndex fileManager.getFileIndexFromSync = function(syncIndex) { - var fileIndexList = localStorage["file.list"].split(";"); - for ( var i = 1; i < fileIndexList.length - 1; i++) { - var fileIndex = fileIndexList[i]; - var sync = localStorage[fileIndex + ".sync"]; - if (sync.indexOf(";" + syncIndex + ";") !== -1) { - return fileIndex; - } - } - return undefined; + return _.chain(localStorage["file.list"].split(";")).compact() + .find(function(fileIndex) { + var sync = localStorage[fileIndex + ".sync"]; + return sync.indexOf(";" + syncIndex + ";") !== -1; + }).value(); }; - // Remove a publish location + // Remove a publishIndex (publish location) fileManager.removePublish = function(publishIndex) { - var currentFileIndex = fileManager.getCurrentFileIndex(); - var fileIndex = this.getFileIndexFromPublish(publishIndex); + var fileIndex = fileManager.getFileIndexFromPublish(publishIndex); if(fileIndex !== undefined) { localStorage[fileIndex + ".publish"] = localStorage[fileIndex + ".publish"].replace(";" + publishIndex + ";", ";"); - if(fileIndex == currentFileIndex) { - refreshManagePublish(); + if(fileManager.isCurrentFileIndex(fileIndex)) { + publisher.notifyPublish(); } } // Remove publish object localStorage.removeItem(publishIndex); - publisher.notifyCurrentFile(); }; - // Look for local file associated to a publish location + // Get the fileIndex associated to a publishIndex fileManager.getFileIndexFromPublish = function(publishIndex) { - var fileIndexList = localStorage["file.list"].split(";"); - for ( var i = 1; i < fileIndexList.length - 1; i++) { - var fileIndex = fileIndexList[i]; - var publish = localStorage[fileIndex + ".publish"]; - if (publish.indexOf(";" + publishIndex + ";") !== -1) { - return fileIndex; - } - } - return undefined; + return _.chain(localStorage["file.list"].split(";")).compact() + .find(function(fileIndex) { + var sync = localStorage[fileIndex + ".publish"]; + return sync.indexOf(";" + publishIndex + ";") !== -1; + }).value(); }; function uploadGdrive(fileId, folderId) { @@ -350,80 +336,38 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni function refreshManageSync() { var fileIndex = fileManager.getCurrentFileIndex(); - var syncIndexList = localStorage[fileIndex + ".sync"].split(";"); + var syncIndexList = _.compact(localStorage[fileIndex + ".sync"].split(";")); $(".msg-no-sync, .msg-sync-list").addClass("hide"); $("#manage-sync-list .input-append").remove(); - if (syncIndexList.length > 2) { + if (syncIndexList.length > 0) { $(".msg-sync-list").removeClass("hide"); } else { $(".msg-no-sync").removeClass("hide"); } - for ( var i = 1; i < syncIndexList.length - 1; i++) { - var syncIndex = syncIndexList[i]; - (function(syncIndex) { - var line = $("
    ").addClass("input-prepend input-append"); - if (syncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) { - line.append($("").addClass("add-on").prop("title", "Google Drive").html( - '')); - line.append($("").prop("type", "text").prop( - "disabled", true).addClass("span5").val( - syncIndex.substring(SYNC_PROVIDER_GDRIVE.length))); - } - else if (syncIndex.indexOf(SYNC_PROVIDER_DROPBOX) === 0) { - line.append($("").addClass("add-on").prop("title", "Dropbox").html( - '')); - line.append($("").prop("type", "text").prop( - "disabled", true).addClass("span5").val( - decodeURIComponent(syncIndex.substring(SYNC_PROVIDER_DROPBOX.length)))); - } - line.append($("").addClass("btn").html( - '').prop("title", - "Remove this location").click(function() { - fileManager.removeSync(syncIndex); - fileManager.updateFileTitles(); - })); - $("#manage-sync-list").append(line); - })(syncIndex); - } - } - - function refreshManagePublish() { - var fileIndex = fileManager.getCurrentFileIndex(); - var publishIndexList = localStorage[fileIndex + ".publish"].split(";"); - $(".msg-no-publish, .msg-publish-list").addClass("hide"); - $("#manage-publish-list .input-append").remove(); - if (publishIndexList.length > 2) { - $(".msg-publish-list").removeClass("hide"); - } else { - $(".msg-no-publish").removeClass("hide"); - } - for ( var i = 1; i < publishIndexList.length - 1; i++) { - var publishIndex = publishIndexList[i]; - var serializedObject = localStorage[publishIndex]; - (function(publishIndex, publishObject, serializedObject) { - var line = $("
    ").addClass("input-prepend input-append"); - if (publishObject.provider == PUBLISH_PROVIDER_GITHUB) { - line.append($("").addClass("add-on").prop("title", "GitHub").html( - '')); - line.append($("").prop("type", "text").prop( - "disabled", true).addClass("span5").val( - serializedObject)); - } - else if (publishObject.provider == PUBLISH_PROVIDER_BLOGGER) { - line.append($("").addClass("add-on").prop("title", "Blogger").html( - '')); - line.append($("").prop("type", "text").prop( - "disabled", true).addClass("span5").val( - serializedObject)); - } - line.append($("").addClass("btn").html( + _.each(syncIndexList, function(syncIndex) { + var line = $("
    ").addClass("input-prepend input-append"); + if (syncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) { + line.append($("").addClass("add-on").prop("title", "Google Drive").html( + '')); + line.append($("").prop("type", "text").prop( + "disabled", true).addClass("span5").val( + syncIndex.substring(SYNC_PROVIDER_GDRIVE.length))); + } + else if (syncIndex.indexOf(SYNC_PROVIDER_DROPBOX) === 0) { + line.append($("").addClass("add-on").prop("title", "Dropbox").html( + '')); + line.append($("").prop("type", "text").prop( + "disabled", true).addClass("span5").val( + decodeURIComponent(syncIndex.substring(SYNC_PROVIDER_DROPBOX.length)))); + } + line.append($("").addClass("btn").html( '').prop("title", "Remove this location").click(function() { - fileManager.removePublish(publishIndex); - })); - $("#manage-publish-list").append(line); - })(publishIndex, JSON.parse(serializedObject), serializedObject.replace(/{|}|"/g, "")); - } + fileManager.removeSync(syncIndex); + fileManager.updateFileTitles(); + })); + $("#manage-sync-list").append(line); + }); } // Initialize the "New publication" dialog @@ -443,16 +387,6 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni $("#modal-publish").modal(); } - // Generate a publishIndex, store a publishObject and associate it to a fileIndex - function createPublishIndex(publishObject, fileIndex) { - var publishIndex = undefined; - do { - publishIndex = "publish." + core.randomString(); - } while(localStorage[publishIndex] !== undefined); - localStorage[publishIndex] = JSON.stringify(publishObject); - localStorage[fileIndex + ".publish"] += publishIndex + ";"; - } - // Create a new publication on GitHub function newPublishGithub(event) { var publishObject = {}; @@ -463,22 +397,7 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni if(event.isPropagationStopped()) { return; } - - var fileIndex = fileManager.getCurrentFileIndex(); - var title = localStorage[fileIndex + ".title"]; - var content = publisher.getPublishContent(publishObject); - var commitMsg = core.settings.commitMsg; - githubHelper.upload(publishObject.repository, - publishObject.branch, publishObject.path, content, commitMsg, - function(error) { - if(error === undefined) { - createPublishIndex(publishObject, fileIndex); - refreshManagePublish(); - publisher.notifyCurrentFile(); - core.showMessage('"' + title - + '" will now be published on GitHub.'); - } - }); + publisher.newLocation(publishObject); } // Create a new publication on Blogger @@ -538,6 +457,13 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni var uriContent = "data:application/octet-stream;base64," + core.encodeBase64(content); window.open(uriContent, 'file'); + }); + $(".action-download-template").click( + function() { + var content = publisher.applyTemplate(); + var uriContent = "data:application/octet-stream;base64," + + core.encodeBase64(content); + window.open(uriContent, 'file'); }); // Synchronize actions @@ -574,16 +500,16 @@ define(["jquery", "google-helper", "dropbox-helper", "github-helper", "synchroni // Publish actions $(".action-publish-github").click(function() { - initNewPublish(PUBLISH_PROVIDER_GITHUB); + initNewPublish(PROVIDER_GITHUB); }); $(".action-publish-blogger").click(function() { - initNewPublish(PUBLISH_PROVIDER_BLOGGER, "html"); + initNewPublish(PROVIDER_BLOGGER, "html"); }); $(".action-process-publish").click(function(e) { - if(newPublishProvider == PUBLISH_PROVIDER_GITHUB) { + if(newPublishProvider == PROVIDER_GITHUB) { newPublishGithub(e); } - else if(newPublishProvider == PUBLISH_PROVIDER_BLOGGER) { + else if(newPublishProvider == PROVIDER_BLOGGER) { newPublishBlogger(e); } }); diff --git a/js/github-provider.js b/js/github-provider.js new file mode 100644 index 00000000..061b8aac --- /dev/null +++ b/js/github-provider.js @@ -0,0 +1,23 @@ +define(["jquery", "github-helper"], function($, githubHelper) { + + // Dependencies + var core = undefined; + + var publishGithub = { + providerType: PROVIDER_TYPE_PUBLISH_FLAG, + providerId: PROVIDER_GITHUB, + providerName: "GitHub" + }; + + publishGithub.publish = function(publishObject, title, content, callback) { + var commitMsg = core.settings.commitMsg; + githubHelper.upload(publishObject.repository, publishObject.branch, + publishObject.path, content, commitMsg, callback); + }; + + publishGithub.init = function(coreModule) { + core = coreModule; + }; + + return publishGithub; +}); \ No newline at end of file diff --git a/js/publisher.js b/js/publisher.js index fe381ed5..e3baa362 100644 --- a/js/publisher.js +++ b/js/publisher.js @@ -1,4 +1,4 @@ -define(["jquery", "google-helper", "github-helper", "publish-github", "underscore"], function($, googleHelper, githubHelper) { +define(["jquery", "github-provider", "underscore"], function($) { // Dependencies var core = undefined; @@ -6,14 +6,17 @@ define(["jquery", "google-helper", "github-helper", "publish-github", "underscor var publisher = {}; - // Providers - var providerMap = {}; - + // Create a map with providerName: providerObject + var providerMap = _.chain(arguments) + .map(function(argument) { + return argument && argument.providerType & PROVIDER_TYPE_PUBLISH_FLAG && [argument.providerId, argument]; + }).compact().object().value(); + // Used to know if the current file has publications var hasPublications = false; // Allows external modules to update hasPublications flag - publisher.notifyCurrentFile = function() { + publisher.notifyPublish = function() { var fileIndex = fileManager.getCurrentFileIndex(); // Check that file has publications @@ -24,6 +27,7 @@ define(["jquery", "google-helper", "github-helper", "publish-github", "underscor hasPublications = true; } publisher.updatePublishButton(); + publisher.refreshManagePublish(); }; // Used to enable/disable the publish button @@ -36,6 +40,16 @@ define(["jquery", "google-helper", "github-helper", "publish-github", "underscor } }; + // Apply template to the current document + publisher.applyTemplate = function() { + var fileIndex = fileManager.getCurrentFileIndex(); + return _.template(core.settings.template, { + documentTitle: localStorage[fileIndex + ".title"], + documentMarkdown: $("#wmd-input").val(), + documentHTML: $("#wmd-preview").html() + }); + }; + // Used to get content to publish function getPublishContent(publishObject) { if(publishObject.format === undefined) { @@ -44,37 +58,35 @@ define(["jquery", "google-helper", "github-helper", "publish-github", "underscor if(publishObject.format == "markdown") { return $("#wmd-input").val(); } - return $("#wmd-preview").html(); + else if(publishObject.format == "html") { + return $("#wmd-preview").html(); + } + else { + return publisher.applyTemplate(); + } } // Recursive function to publish a file on multiple locations var publishIndexList = []; - function publishLocation(callback, error) { + var publishTitle = undefined; + function publishLocation(callback, errorFlag) { // No more publish location for this document if (publishIndexList.length === 0) { - callback(error); + callback(errorFlag); return; } // Dequeue a synchronized location var publishIndex = publishIndexList.pop(); - if(!publishIndex) { - publishLocation(callback, error); - return; - } - var publishObject = JSON.parse(localStorage[publishIndex]); var content = getPublishContent(publishObject); - var commitMsg = core.settings.commitMsg; - - // Try to find the provider - if(publishObject.provider == PUBLISH_PROVIDER_GITHUB) { - githubHelper.upload(publishObject.repository, publishObject.branch, publishObject.path, content, commitMsg, - function(error) { - publishLocation(callback, error); - }); - } + + // Call the provider + var provider = providerMap[publishObject.provider]; + provider.publish(publishObject, publishTitle, content, function(error) { + publishLocation(callback, errorFlag); + }); } var publishRunning = false; @@ -87,32 +99,74 @@ define(["jquery", "google-helper", "github-helper", "publish-github", "underscor publishRunning = true; publisher.updatePublishButton(); var fileIndex = fileManager.getCurrentFileIndex(); - var title = localStorage[fileIndex + ".title"]; - publishIndexList = localStorage[fileIndex + ".publish"].split(";");; - publishLocation(function(error) { + publishTitle = localStorage[fileIndex + ".title"]; + publishIndexList = _.compact(localStorage[fileIndex + ".publish"].split(";")); + publishLocation(function(errorFlag) { publishRunning = false; publisher.updatePublishButton(); - if(error === undefined) { + if(errorFlag === undefined) { core.showMessage('"' + title + '" successfully published.'); } }); }; + // Generate a publishIndex associated to a fileIndex and store a publishObject + function createPublishIndex(fileIndex, publishObject) { + var publishIndex = undefined; + do { + publishIndex = "publish." + core.randomString(); + } while(_.has(localStorage, publishIndex)); + localStorage[publishIndex] = JSON.stringify(publishObject); + localStorage[fileIndex + ".publish"] += publishIndex + ";"; + } + // Add a new publish location to a local document - publisher.newLocation = function(publishObject, callback) { + publisher.newLocation = function(publishObject) { var fileIndex = fileManager.getCurrentFileIndex(); var title = localStorage[fileIndex + ".title"]; var content = getPublishContent(publishObject); var provider = providerMap[publishObject.provider]; - provider.publishNew(publishObject, title, content); + provider.publish(publishObject, title, content, function(error) { + if(error === undefined) { + createPublishIndex(fileIndex, publishObject); + publisher.notifyPublish(); + core.showMessage('"' + title + + '" will now be published on GitHub.'); + } + }); }; - - // Associate publish provider to publisher - _.each(arguments, function(argument) { - if(argument !== undefined && argument.publishProvider !== undefined) { - providerMap[argument.publishProvider] = argument; + + // Used to populate the "Manage publication" dialog + var lineTemplate = ['
    ', + '', + '', + '', + '
    '].join(""); + var removeButtonTemplate = '
    '; + 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"); } - }); + _.each(publishIndexList, function(publishIndex) { + var serializedObject = localStorage[publishIndex]; + var publishObject = JSON.parse(serializedObject); + var publishDesc = JSON.stringify(_.omit(publishObject, 'provider')).replace(/{|}|"/g, ""); + lineElement = $(_.template(lineTemplate, { + provider: providerMap[publishObject.provider], + publishDesc: publishDesc + })); + lineElement.append($(removeButtonTemplate).click(function() { + fileManager.removePublish(publishIndex); + })); + $("#manage-publish-list").append(lineElement); + }); + }; publisher.init = function(coreModule, fileManagerModule) { core = coreModule; @@ -120,7 +174,7 @@ define(["jquery", "google-helper", "github-helper", "publish-github", "underscor // Init providers _.each(providerMap, function(provider) { - provider.init(); + provider.init(core); }); $(".action-force-publish").click(function() {