Publish support
This commit is contained in:
parent
fb848a9f81
commit
85528c92a9
@ -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, input, .add-on {
|
||||
.btn, .navbar-inner, .ui-layout-east, .ui-layout-south, textarea, .add-on {
|
||||
border: none !important;
|
||||
}
|
||||
|
||||
@ -38,10 +38,9 @@ div, span, a, ul, li, textarea, input, button {
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.input-prepend input,
|
||||
input,
|
||||
.input-prepend .btn,
|
||||
.input-prepend .add-on
|
||||
{
|
||||
.input-prepend .add-on {
|
||||
border: 1px solid #ebebeb !important;
|
||||
}
|
||||
|
||||
|
181
index.html
181
index.html
@ -5,8 +5,10 @@
|
||||
<link rel="icon" href="img/stackedit-32.ico" type="image/x-icon">
|
||||
<link rel="shortcut icon" href="img/stackedit-32.ico"
|
||||
type="image/x-icon">
|
||||
<meta name="keywords" content="Markdown, Editor, PageDown, Stack Overflow, Stack Exchange">
|
||||
<meta name="description" content="StackEdit is a free, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites.">
|
||||
<meta name="keywords"
|
||||
content="Markdown, Editor, PageDown, Stack Overflow, Stack Exchange">
|
||||
<meta name="description"
|
||||
content="StackEdit is a free, open-source Markdown editor based on PageDown, the Markdown library used by Stack Overflow and the other Stack Exchange sites.">
|
||||
<meta name="author" content="Benoit Schweblin">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<link href="css/main.css" rel="stylesheet" media="screen">
|
||||
@ -23,10 +25,10 @@
|
||||
<li class="btn-group"><button class="btn action-force-sync"
|
||||
title="Synchronize">
|
||||
<i class="icon-refresh"></i>
|
||||
</button><button class="btn action-publish"
|
||||
title="Publish">
|
||||
<i class="icon-share"></i>
|
||||
</button></li>
|
||||
</button>
|
||||
<button class="btn action-publish" title="Publish">
|
||||
<i class="icon-share"></i>
|
||||
</button></li>
|
||||
<li class="btn-group"><button class="btn action-create-file"
|
||||
title="New local document">
|
||||
<i class="icon-file"></i>
|
||||
@ -70,22 +72,22 @@
|
||||
data-target="#modal-manage-sync"><i class="icon-refresh"></i>
|
||||
Manage synchronization</a></li>
|
||||
<li class="divider"></li>
|
||||
<li class="dropdown-submenu"><a href="#"><i
|
||||
class="icon-share"></i> Publish</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="#" class="action-publish-github"><i
|
||||
class="icon-github"></i> GitHub</a></li>
|
||||
<li><a href="#" class="action-publish-blogger"><i
|
||||
class="icon-blogger"></i> Blogger</a></li>
|
||||
<li><a href="#" class="action-publish-wordpress"><i
|
||||
class="icon-wordpress"></i> WordPress</a></li>
|
||||
<li><a href="#" class="action-publish-tumblr"><i
|
||||
class="icon-tumblr"></i> Tumblr</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#" data-toggle="modal"
|
||||
data-target="#modal-manage-publication"><i class="icon-share"></i>
|
||||
Manage publication</a></li>
|
||||
<li class="divider"></li>
|
||||
<li class="dropdown-submenu"><a href="#"><i
|
||||
class="icon-share"></i> Publish on</a>
|
||||
<ul class="dropdown-menu">
|
||||
<li><a href="#" class="action-publish-github"><i
|
||||
class="icon-github"></i> GitHub</a></li>
|
||||
<li><a href="#" class="action-publish-blogger"><i
|
||||
class="icon-blogger"></i> Blogger</a></li>
|
||||
<li><a href="#" class="action-publish-wordpress"><i
|
||||
class="icon-wordpress"></i> WordPress</a></li>
|
||||
<li><a href="#" class="action-publish-tumblr"><i
|
||||
class="icon-tumblr"></i> Tumblr</a></li>
|
||||
</ul></li>
|
||||
<li><a href="#" data-toggle="modal"
|
||||
data-target="#modal-manage-publication"><i class="icon-share"></i>
|
||||
Manage publication</a></li>
|
||||
<li class="divider"></li>
|
||||
<li><a href="#" data-toggle="modal"
|
||||
data-target="#modal-settings" class="action-load-settings"><i
|
||||
class="icon-cog"></i> Settings</a></li>
|
||||
@ -256,55 +258,47 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="modal-publish-existing" class="modal hide">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal"
|
||||
aria-hidden="true">×</button>
|
||||
<h3>Publish</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<dl class="dl-horizontal">
|
||||
<dt>Would you like to:</dt>
|
||||
<dd>
|
||||
<label class="radio"> <input type="radio"
|
||||
name="radio-publish-existing" value="new">
|
||||
Create a new Post
|
||||
</label> <label class="radio"> <input type="radio"
|
||||
name="radio-publish-existing" value="existing"> Edit an existing post
|
||||
</label>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="#" class="btn" data-dismiss="modal">Cancel</a> <a href="#"
|
||||
data-dismiss="modal" class="btn btn-primary action-publish-next">Next</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="modal-publish-format" class="modal hide">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal"
|
||||
aria-hidden="true">×</button>
|
||||
<h3>Publish</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<dl class="dl-horizontal">
|
||||
<dt>Publish as:</dt>
|
||||
<dd>
|
||||
<label class="radio"> <input type="radio"
|
||||
name="radio-publish-format" value="markdown">
|
||||
Markdown format
|
||||
</label> <label class="radio"> <input type="radio"
|
||||
name="radio-publish-format" value="html"> HTML format
|
||||
</label>
|
||||
</dd>
|
||||
</dl>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="#" class="btn" data-dismiss="modal">Cancel</a> <a href="#"
|
||||
data-dismiss="modal" class="btn btn-primary action-publish-finish">OK</a>
|
||||
</div>
|
||||
</div>
|
||||
<div id="modal-publish" class="modal hide">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal"
|
||||
aria-hidden="true">×</button>
|
||||
<h3>Publish</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="form-horizontal">
|
||||
<div class="control-group control-publish-blogger">
|
||||
<label class="control-label" for="input-publish-blogger-url">Blog URL</label>
|
||||
<div class="controls">
|
||||
<input type="text" id="input-publish-blogger-url"
|
||||
placeholder="http://exemple.blogger.com/">
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group control-publish-blogger">
|
||||
<label class="control-label" for="input-publish-blogger-postid">Update existing post ID (optional)</label>
|
||||
<div class="controls">
|
||||
<input type="text" id="input-publish-blogger-postid"
|
||||
placeholder="Post ID">
|
||||
</div>
|
||||
</div>
|
||||
<div class="control-group">
|
||||
<div class="control-label">Publish as</div>
|
||||
<div class="controls">
|
||||
<label class="radio"> <input type="radio"
|
||||
name="radio-publish-format" value="markdown"> Markdown
|
||||
format
|
||||
</label> <label class="radio"> <input type="radio"
|
||||
name="radio-publish-format" value="html"> HTML format
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="#" class="btn" data-dismiss="modal">Cancel</a>
|
||||
<a href="#" data-dismiss="modal"
|
||||
class="btn btn-primary action-publish-ok">OK</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="modal-settings" class="modal hide">
|
||||
<div class="modal-header">
|
||||
@ -313,17 +307,20 @@
|
||||
<h3>Settings</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<dl class="dl-horizontal">
|
||||
<dt>Layout orientation:</dt>
|
||||
<dd>
|
||||
<label class="radio"> <input type="radio"
|
||||
name="radio-layout-orientation" value="horizontal">
|
||||
Horizontal
|
||||
</label> <label class="radio"> <input type="radio"
|
||||
name="radio-layout-orientation" value="vertical"> Vertical
|
||||
</label>
|
||||
</dd>
|
||||
</dl>
|
||||
<div class="form-horizontal">
|
||||
<div class="control-group">
|
||||
<div class="control-label">Layout orientation</div>
|
||||
<div class="controls">
|
||||
<label class="radio"> <input type="radio"
|
||||
name="radio-layout-orientation" value="horizontal">
|
||||
Horizontal
|
||||
</label> <label class="radio"> <input type="radio"
|
||||
name="radio-layout-orientation" value="vertical">
|
||||
Vertical
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="#" class="btn" data-dismiss="modal">Cancel</a> <a href="#"
|
||||
@ -376,7 +373,7 @@
|
||||
<a target="_blank" href="http://twitter.github.com/bootstrap/">Bootstrap</a>
|
||||
</dd>
|
||||
<dd>
|
||||
<a target="_blank" href="http://glyphicons.com/">GLYPHICONS</a>
|
||||
<a target="_blank" href="http://glyphicons.com/">Glyphicons</a>
|
||||
</dd>
|
||||
<dd>
|
||||
<a target="_blank" href="https://github.com/stanlemon/jGrowl/">jGrowl</a>
|
||||
@ -403,8 +400,24 @@
|
||||
<div class="modal-footer">
|
||||
<a href="#" class="btn btn-primary" data-dismiss="modal">Close</a>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
<div id="modal-non-unique" class="modal hide">
|
||||
<div class="modal-header">
|
||||
<h3>Stopped...</h3>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<p>StackEdit has stopped because another instance was running on
|
||||
the same browser.</p>
|
||||
<p class="muted">If you want to reopen StackEdit, click on
|
||||
"Reload".</p>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<a href="javascript:window.location.reload();"
|
||||
class="btn btn-primary">Reload</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="dropboxjs" data-app-key="x0k2l8puemfvg0o"></div>
|
||||
</body>
|
||||
</html>
|
||||
|
@ -6,17 +6,20 @@
|
||||
* - an optional onError() function
|
||||
* - an optional timeout field (default is 30000)
|
||||
*/
|
||||
define(["core"], function(core) {
|
||||
define(function() {
|
||||
|
||||
var asyncTaskRunner = {};
|
||||
|
||||
// Dependencies
|
||||
var core = undefined;
|
||||
|
||||
var asyncTaskQueue = [];
|
||||
var currentTask = undefined;
|
||||
var currentTaskRunning = false;
|
||||
var currentTaskStartTime = core.currentTime;
|
||||
var currentTaskStartTime = 0;
|
||||
|
||||
// Run the next task in the queue if any and no other is running
|
||||
asyncTaskRunner.runTask = function() {
|
||||
function runTask() {
|
||||
|
||||
// If there is a task currently running
|
||||
if(currentTaskRunning === true) {
|
||||
@ -42,17 +45,6 @@ define(["core"], function(core) {
|
||||
// Set task attributes and functions
|
||||
currentTask.finished = false;
|
||||
currentTask.retryCounter = 0;
|
||||
currentTask.finish = function() {
|
||||
this.finished = true;
|
||||
currentTask = undefined;
|
||||
currentTaskRunning = false;
|
||||
if(asyncTaskQueue.length === 0) {
|
||||
core.showWorkingIndicator(false);
|
||||
}
|
||||
else {
|
||||
asyncTaskRunner.runTask();
|
||||
}
|
||||
};
|
||||
currentTask.success = function() {
|
||||
runSafe(this.onSuccess);
|
||||
};
|
||||
@ -68,8 +60,7 @@ define(["core"], function(core) {
|
||||
return;
|
||||
}
|
||||
// Implement an exponential backoff
|
||||
var delay = (Math.pow(2, currentTask.retryCounter++) + Math.random()) * 1000;
|
||||
console.log(delay);
|
||||
var delay = Math.pow(2, currentTask.retryCounter++) * 1000;
|
||||
currentTaskStartTime = core.currentTime + delay;
|
||||
currentTaskRunning = false;
|
||||
asyncTaskRunner.runTask();
|
||||
@ -81,6 +72,11 @@ define(["core"], function(core) {
|
||||
currentTaskRunning = true;
|
||||
currentTask.run();
|
||||
}
|
||||
}
|
||||
|
||||
asyncTaskRunner.runTask = function() {
|
||||
// Use setTimeout to avoid stack overflow
|
||||
setTimeout(runTask, 0);
|
||||
};
|
||||
|
||||
function runSafe(func) {
|
||||
@ -104,12 +100,16 @@ define(["core"], function(core) {
|
||||
}
|
||||
}
|
||||
|
||||
// Add a task in the queue
|
||||
// Add a task into the queue
|
||||
asyncTaskRunner.addTask = function(asyncTask) {
|
||||
asyncTaskQueue.push(asyncTask);
|
||||
asyncTaskRunner.runTask();
|
||||
};
|
||||
|
||||
asyncTaskRunner.init = function(coreModule) {
|
||||
core = coreModule;
|
||||
};
|
||||
|
||||
return asyncTaskRunner;
|
||||
});
|
||||
|
||||
|
12
js/config.js
12
js/config.js
@ -1,5 +1,6 @@
|
||||
var GOOGLE_SCOPES = [ 'https://www.googleapis.com/auth/drive.install',
|
||||
'https://www.googleapis.com/auth/drive' ];
|
||||
var GOOGLE_SCOPES = [ "https://www.googleapis.com/auth/drive.install",
|
||||
"https://www.googleapis.com/auth/drive",
|
||||
"https://www.googleapis.com/auth/blogger" ];
|
||||
var GOOGLE_DRIVE_APP_ID = "241271498917";
|
||||
var DROPBOX_APP_KEY = "lq6mwopab8wskas";
|
||||
var DROPBOX_APP_SECRET = "851fgnucpezy84t";
|
||||
@ -10,13 +11,14 @@ var AJAX_TIMEOUT = 10000;
|
||||
var ASYNC_TASK_DEFAULT_TIMEOUT = 30000;
|
||||
var AUTH_POPUP_TIMEOUT = 90000;
|
||||
var SYNC_PERIOD = 180000;
|
||||
var USER_IDLE_THRESHOLD = 300000;
|
||||
var SYNC_PROVIDER_GDRIVE = "sync.gdrive.";
|
||||
var SYNC_PROVIDER_DROPBOX = "sync.dropbox.";
|
||||
|
||||
// Use by Google's client.js
|
||||
var delayedFunction = undefined;
|
||||
function runDelayedFunction() {
|
||||
if (delayedFunction !== undefined) {
|
||||
delayedFunction();
|
||||
}
|
||||
if (delayedFunction !== undefined) {
|
||||
delayedFunction();
|
||||
}
|
||||
}
|
||||
|
113
js/core.js
113
js/core.js
@ -1,16 +1,58 @@
|
||||
define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function($) {
|
||||
define(
|
||||
[ "jquery", "file-manager", "google-helper", "dropbox-helper",
|
||||
"synchronizer", "publisher", "async-runner", "bootstrap", "jgrowl",
|
||||
"layout", "Markdown.Editor", "config", "custo" ],
|
||||
function($, fileManager, googleHelper, dropboxHelper, synchronizer,
|
||||
publisher, asyncTaskRunner) {
|
||||
|
||||
var core = {};
|
||||
|
||||
// Time shared by others modules
|
||||
core.currentTime = new Date().getTime();
|
||||
core.updateCurrentTime = function() {
|
||||
core.currentTime = new Date().getTime();
|
||||
};
|
||||
|
||||
// Usage: callback = callback || core.doNothing;
|
||||
core.doNothing = function() {};
|
||||
|
||||
// Time shared by others modules
|
||||
core.currentTime = new Date().getTime();
|
||||
function updateCurrentTime() {
|
||||
core.currentTime = new Date().getTime();
|
||||
}
|
||||
|
||||
// Used to detect user activity
|
||||
var userReal = false;
|
||||
var userActive = false;
|
||||
var windowUnique = true;
|
||||
var userLastActivity = 0;
|
||||
function setUserActive() {
|
||||
userReal = true;
|
||||
userActive = true;
|
||||
userLastActivity = core.currentTime;
|
||||
};
|
||||
function isUserActive() {
|
||||
if(userActive === true
|
||||
&& core.currentTime - userLastActivity > USER_IDLE_THRESHOLD) {
|
||||
userActive = false;
|
||||
}
|
||||
return userActive && windowUnique;
|
||||
}
|
||||
|
||||
// Used to only have 1 window of the application in the same browser
|
||||
var windowId = undefined;
|
||||
core.checkWindowUnique = function() {
|
||||
if(userReal === false || windowUnique === false) {
|
||||
return;
|
||||
}
|
||||
if(windowId === undefined) {
|
||||
windowId = Math.random().toString(36);
|
||||
localStorage["frontWindowId"] = windowId;
|
||||
}
|
||||
var frontWindowId = localStorage["frontWindowId"];
|
||||
if(frontWindowId != windowId) {
|
||||
windowUnique = false;
|
||||
$('#modal-non-unique').modal({
|
||||
backdrop: "static",
|
||||
keyboard: false
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// Useful function
|
||||
core.getInputValue = function(element, event) {
|
||||
@ -58,10 +100,6 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
var offlineTime = core.currentTime;
|
||||
var offlineListeners = [];
|
||||
|
||||
core.addOfflineListener = function(listener) {
|
||||
offlineListeners.push(listener);
|
||||
};
|
||||
|
||||
core.setOffline = function() {
|
||||
offlineTime = core.currentTime;
|
||||
if(core.isOffline === false) {
|
||||
@ -89,7 +127,7 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
}
|
||||
};
|
||||
|
||||
core.checkOnline = function() {
|
||||
function checkOnline() {
|
||||
// Try to reconnect if we are offline but we have some network
|
||||
if (core.isOffline === true && navigator.onLine === true
|
||||
&& offlineTime + CHECK_ONLINE_PERIOD < core.currentTime) {
|
||||
@ -102,7 +140,7 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
core.setOnline();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Setting management
|
||||
var settings = { layoutOrientation : "horizontal" };
|
||||
@ -338,10 +376,10 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
|
||||
// Used to setup an empty localStorage
|
||||
function setupLocalStorage() {
|
||||
if (localStorage["file.counter"] === undefined
|
||||
|| localStorage["file.list"] === undefined) {
|
||||
if (localStorage["file.counter"] === undefined) {
|
||||
localStorage["file.counter"] = "0";
|
||||
localStorage["file.list"] = ";";
|
||||
localStorage["version"] = "v1";
|
||||
}
|
||||
}
|
||||
|
||||
@ -351,19 +389,22 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
|
||||
// from v0 to v1
|
||||
if(version === undefined) {
|
||||
|
||||
// Synchronization queue not used anymore
|
||||
localStorage.removeItem("sync.queue");
|
||||
localStorage.removeItem("sync.current");
|
||||
|
||||
var fileIndexList = localStorage["file.list"].split(";");
|
||||
for ( var i = 1; i < fileIndexList.length - 1; i++) {
|
||||
var fileIndex = fileIndexList[i];
|
||||
localStorage[fileIndex + ".publish"] = ";";
|
||||
var titleCRC = core.crc32(localStorage[fileIndex + ".title"]);
|
||||
var contentCRC = core.crc32(localStorage[fileIndex + ".content"]);
|
||||
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
|
||||
for ( var j = 1; j < fileSyncIndexList.length - 1; j++) {
|
||||
var fileSyncIndex = fileSyncIndexList[j];
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = contentCRC;
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = "0";
|
||||
// We store title CRC only for Google Drive synchronization
|
||||
if(localStorage[fileSyncIndex + ".etag"] !== undefined) {
|
||||
localStorage[fileSyncIndex + ".titleCRC"] = titleCRC;
|
||||
localStorage[fileSyncIndex + ".titleCRC"] = "0";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -389,6 +430,9 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
core.setOffline();
|
||||
}
|
||||
|
||||
// Detect user activity
|
||||
$(document).mousemove(setUserActive).keypress(setUserActive);
|
||||
|
||||
// Avoid dropdown to close when clicking on submenu
|
||||
$('.dropdown-submenu > a').click(function(e) {
|
||||
e.stopPropagation();
|
||||
@ -421,8 +465,37 @@ define(["jquery", "bootstrap", "jgrowl", "layout", "Markdown.Editor"], function(
|
||||
|
||||
$(".action-apply-settings").click(function() {
|
||||
core.saveSettings();
|
||||
location.reload();
|
||||
window.location.reload();
|
||||
});
|
||||
|
||||
// Init asyncTaskRunner
|
||||
asyncTaskRunner.init(core);
|
||||
|
||||
// Init helpers
|
||||
googleHelper.init(core, fileManager);
|
||||
dropboxHelper.init(core, fileManager);
|
||||
|
||||
// Init publisher
|
||||
publisher.init(core, fileManager);
|
||||
|
||||
// Init synchronizer
|
||||
synchronizer.init(core, fileManager);
|
||||
offlineListeners.push(synchronizer.updateSyncButton);
|
||||
|
||||
// Init file manager
|
||||
fileManager.init(core);
|
||||
|
||||
// Do periodic tasks
|
||||
window.setInterval(function() {
|
||||
updateCurrentTime();
|
||||
core.checkWindowUnique();
|
||||
if(isUserActive() === false) {
|
||||
return;
|
||||
}
|
||||
synchronizer.sync();
|
||||
asyncTaskRunner.runTask();
|
||||
checkOnline();
|
||||
}, 1000);
|
||||
};
|
||||
|
||||
return core;
|
||||
|
@ -1,6 +1,7 @@
|
||||
define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
|
||||
define(["jquery", "async-runner"], function($, asyncTaskRunner) {
|
||||
|
||||
// Dependencies
|
||||
var core = undefined;
|
||||
var fileManager = undefined;
|
||||
|
||||
var client = undefined;
|
||||
@ -372,7 +373,8 @@ define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
|
||||
});
|
||||
};
|
||||
|
||||
dropboxHelper.init = function(fileManagerModule) {
|
||||
dropboxHelper.init = function(coreModule, fileManagerModule) {
|
||||
core = coreModule;
|
||||
fileManager = fileManagerModule;
|
||||
};
|
||||
|
||||
|
@ -1,115 +1,11 @@
|
||||
define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "publisher", "async-runner"],
|
||||
function($, core, googleHelper, dropboxHelper, synchronizer, publisher, asyncTaskRunner) {
|
||||
define(["jquery", "google-helper", "dropbox-helper", "synchronizer", "publisher"],
|
||||
function($, googleHelper, dropboxHelper, synchronizer, publisher) {
|
||||
|
||||
var fileManager = {};
|
||||
|
||||
fileManager.init = function() {
|
||||
googleHelper.init(fileManager);
|
||||
dropboxHelper.init(fileManager);
|
||||
publisher.init(fileManager);
|
||||
|
||||
var changeSyncButtonState = function() {
|
||||
if(synchronizer.isRunning() || synchronizer.isQueueEmpty() || core.isOffline) {
|
||||
$(".action-force-sync").addClass("disabled");
|
||||
}
|
||||
else {
|
||||
$(".action-force-sync").removeClass("disabled");
|
||||
}
|
||||
};
|
||||
core.addOfflineListener(changeSyncButtonState);
|
||||
synchronizer.init(fileManager, {
|
||||
onSyncBegin : changeSyncButtonState,
|
||||
onSyncEnd : changeSyncButtonState,
|
||||
onQueueChanged : changeSyncButtonState
|
||||
});
|
||||
$(".action-force-sync").click(function() {
|
||||
if(!$(this).hasClass("disabled")) {
|
||||
synchronizer.forceSync();
|
||||
}
|
||||
});
|
||||
|
||||
fileManager.selectFile();
|
||||
|
||||
// Do periodic tasks
|
||||
window.setInterval(function() {
|
||||
core.updateCurrentTime();
|
||||
synchronizer.sync();
|
||||
asyncTaskRunner.runTask();
|
||||
core.checkOnline();
|
||||
}, 1000);
|
||||
|
||||
$(".action-create-file").click(function() {
|
||||
var fileIndex = fileManager.createFile();
|
||||
fileManager.selectFile(fileIndex);
|
||||
$("#file-title").click();
|
||||
});
|
||||
$(".action-remove-file").click(function() {
|
||||
fileManager.deleteFile();
|
||||
fileManager.selectFile();
|
||||
});
|
||||
$("#file-title").click(function() {
|
||||
$(this).hide();
|
||||
$("#file-title-input").show().focus();
|
||||
});
|
||||
$("#file-title-input").blur(function() {
|
||||
var title = $.trim($(this).val());
|
||||
if (title) {
|
||||
var fileIndexTitle = localStorage["file.current"] + ".title";
|
||||
if (title != localStorage[fileIndexTitle]) {
|
||||
localStorage[fileIndexTitle] = title;
|
||||
fileManager.updateFileTitles();
|
||||
fileManager.saveFile();
|
||||
}
|
||||
}
|
||||
$(this).hide();
|
||||
$("#file-title").show();
|
||||
});
|
||||
$(".action-download-md").click(
|
||||
function() {
|
||||
var content = $("#wmd-input").val();
|
||||
var uriContent = "data:application/octet-stream;base64,"
|
||||
+ core.encodeBase64(content);
|
||||
window.open(uriContent, 'file');
|
||||
});
|
||||
$(".action-download-html").click(
|
||||
function() {
|
||||
var content = $("#wmd-preview").html();
|
||||
var uriContent = "data:application/octet-stream;base64,"
|
||||
+ core.encodeBase64(content);
|
||||
window.open(uriContent, 'file');
|
||||
});
|
||||
$(".action-upload-gdrive-root").click(function() {
|
||||
uploadGdrive();
|
||||
});
|
||||
$(".action-upload-gdrive-select").click(function() {
|
||||
// This action is not available because picker does not support
|
||||
// folder selection yet
|
||||
googleHelper.picker(function(ids) {
|
||||
if(ids !== undefined && ids.length !== 0) {
|
||||
uploadGdrive(undefined, ids[0]);
|
||||
}
|
||||
}, true);
|
||||
});
|
||||
$(".action-download-gdrive").click(function() {
|
||||
googleHelper.picker(importGdrive);
|
||||
});
|
||||
$(".action-manual-gdrive").click(function(event) {
|
||||
var fileId = core.getInputValue($("#manual-gdrive-fileid"), event);
|
||||
manualGdrive(fileId);
|
||||
});
|
||||
$(".action-download-dropbox").click(function() {
|
||||
dropboxHelper.picker(importDropbox);
|
||||
});
|
||||
$(".action-upload-dropbox").click(function(event) {
|
||||
var path = core.getInputValue($("#upload-dropbox-path"), event);
|
||||
manualDropbox(path);
|
||||
});
|
||||
$(".action-manual-dropbox").click(function(event) {
|
||||
var path = core.getInputValue($("#manual-dropbox-path"), event);
|
||||
manualDropbox(path);
|
||||
});
|
||||
};
|
||||
|
||||
// Dependencies
|
||||
var core = undefined;
|
||||
|
||||
// Caution: this function recreate the editor (reset undo operations)
|
||||
var fileDescList = [];
|
||||
fileManager.selectFile = function(fileIndex) {
|
||||
@ -118,8 +14,9 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
fileIndex = this.createFile();
|
||||
}
|
||||
|
||||
fileIndex = fileIndex || localStorage["file.current"];
|
||||
if(fileIndex !== undefined) {
|
||||
// Since we are going to modify current file
|
||||
core.checkWindowUnique();
|
||||
localStorage["file.current"] = fileIndex;
|
||||
}
|
||||
|
||||
@ -128,7 +25,7 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
refreshManageSync();
|
||||
|
||||
// Recreate the editor
|
||||
var fileIndex = localStorage["file.current"];
|
||||
fileIndex = localStorage["file.current"];
|
||||
$("#wmd-input").val(localStorage[fileIndex + ".content"]);
|
||||
core.createEditor(function() {
|
||||
fileManager.saveFile();
|
||||
@ -174,6 +71,8 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
var fileIndexCurrent = localStorage["file.current"];
|
||||
fileIndex = fileIndex || fileIndexCurrent;
|
||||
if(fileIndex == fileIndexCurrent) {
|
||||
// Since we are going to modify current file
|
||||
core.checkWindowUnique();
|
||||
localStorage.removeItem("file.current");
|
||||
}
|
||||
|
||||
@ -195,7 +94,7 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
var content = $("#wmd-input").val();
|
||||
var fileIndex = localStorage["file.current"];
|
||||
localStorage[fileIndex + ".content"] = content;
|
||||
synchronizer.addFileForUpload(fileIndex);
|
||||
synchronizer.notifyChange(fileIndex);
|
||||
};
|
||||
|
||||
fileManager.updateFileTitles = function() {
|
||||
@ -218,6 +117,8 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
var fileIndex = localStorage["file.current"];
|
||||
// If no default file take first one
|
||||
if (!fileIndex) {
|
||||
// Since we are going to modify current file
|
||||
core.checkWindowUnique();
|
||||
fileIndex = fileDescList[0].index;
|
||||
localStorage["file.current"] = fileIndex;
|
||||
}
|
||||
@ -256,6 +157,8 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
} else {
|
||||
a.prop("href", "#").click((function(fileIndex) {
|
||||
return function() {
|
||||
// Since we are going to modify current file
|
||||
core.checkWindowUnique();
|
||||
localStorage["file.current"] = fileIndex;
|
||||
fileManager.selectFile();
|
||||
};
|
||||
@ -445,5 +348,82 @@ define(["jquery", "core", "google-helper", "dropbox-helper", "synchronizer", "pu
|
||||
}
|
||||
}
|
||||
|
||||
fileManager.init = function(coreModule) {
|
||||
core = coreModule;
|
||||
|
||||
fileManager.selectFile();
|
||||
|
||||
$(".action-create-file").click(function() {
|
||||
var fileIndex = fileManager.createFile();
|
||||
fileManager.selectFile(fileIndex);
|
||||
$("#file-title").click();
|
||||
});
|
||||
$(".action-remove-file").click(function() {
|
||||
fileManager.deleteFile();
|
||||
fileManager.selectFile();
|
||||
});
|
||||
$("#file-title").click(function() {
|
||||
$(this).hide();
|
||||
$("#file-title-input").show().focus();
|
||||
});
|
||||
$("#file-title-input").blur(function() {
|
||||
var title = $.trim($(this).val());
|
||||
if (title) {
|
||||
var fileIndexTitle = localStorage["file.current"] + ".title";
|
||||
if (title != localStorage[fileIndexTitle]) {
|
||||
localStorage[fileIndexTitle] = title;
|
||||
fileManager.updateFileTitles();
|
||||
fileManager.saveFile();
|
||||
}
|
||||
}
|
||||
$(this).hide();
|
||||
$("#file-title").show();
|
||||
});
|
||||
$(".action-download-md").click(
|
||||
function() {
|
||||
var content = $("#wmd-input").val();
|
||||
var uriContent = "data:application/octet-stream;base64,"
|
||||
+ core.encodeBase64(content);
|
||||
window.open(uriContent, 'file');
|
||||
});
|
||||
$(".action-download-html").click(
|
||||
function() {
|
||||
var content = $("#wmd-preview").html();
|
||||
var uriContent = "data:application/octet-stream;base64,"
|
||||
+ core.encodeBase64(content);
|
||||
window.open(uriContent, 'file');
|
||||
});
|
||||
$(".action-upload-gdrive-root").click(function() {
|
||||
uploadGdrive();
|
||||
});
|
||||
$(".action-upload-gdrive-select").click(function() {
|
||||
// This action is not available because picker does not support
|
||||
// folder selection
|
||||
googleHelper.picker(function(ids) {
|
||||
if(ids !== undefined && ids.length !== 0) {
|
||||
uploadGdrive(undefined, ids[0]);
|
||||
}
|
||||
}, true);
|
||||
});
|
||||
$(".action-download-gdrive").click(function() {
|
||||
googleHelper.picker(importGdrive);
|
||||
});
|
||||
$(".action-manual-gdrive").click(function(event) {
|
||||
var fileId = core.getInputValue($("#manual-gdrive-fileid"), event);
|
||||
manualGdrive(fileId);
|
||||
});
|
||||
$(".action-download-dropbox").click(function() {
|
||||
dropboxHelper.picker(importDropbox);
|
||||
});
|
||||
$(".action-upload-dropbox").click(function(event) {
|
||||
var path = core.getInputValue($("#upload-dropbox-path"), event);
|
||||
manualDropbox(path);
|
||||
});
|
||||
$(".action-manual-dropbox").click(function(event) {
|
||||
var path = core.getInputValue($("#manual-dropbox-path"), event);
|
||||
manualDropbox(path);
|
||||
});
|
||||
};
|
||||
|
||||
return fileManager;
|
||||
});
|
||||
|
@ -1,6 +1,7 @@
|
||||
define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
|
||||
define(["jquery", "async-runner"], function($, asyncTaskRunner) {
|
||||
|
||||
// Dependencies
|
||||
var core = undefined;
|
||||
var fileManager = undefined;
|
||||
|
||||
var connected = false;
|
||||
@ -464,7 +465,8 @@ define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
|
||||
});
|
||||
};
|
||||
|
||||
googleHelper.init = function(fileManagerModule) {
|
||||
googleHelper.init = function(coreModule, fileManagerModule) {
|
||||
core = coreModule;
|
||||
fileManager = fileManagerModule;
|
||||
var state = localStorage["sync.gdrive.state"];
|
||||
if(state === undefined) {
|
||||
|
@ -18,10 +18,10 @@ requirejs.config({
|
||||
}
|
||||
});
|
||||
|
||||
require(["jquery", "core", "file-manager", "config", "custo"], function($, core, fileManager) {
|
||||
require(["jquery", "core"], function($, core) {
|
||||
$(function() {
|
||||
|
||||
// If browser detected a new application cache.
|
||||
// If browser has detected a new application cache.
|
||||
if (window.applicationCache
|
||||
&& window.applicationCache.status === window.applicationCache.UPDATEREADY) {
|
||||
window.applicationCache.swapCache();
|
||||
@ -30,6 +30,5 @@ require(["jquery", "core", "file-manager", "config", "custo"], function($, core,
|
||||
}
|
||||
|
||||
core.init();
|
||||
fileManager.init();
|
||||
});
|
||||
});
|
||||
|
@ -1,37 +1,44 @@
|
||||
define(["jquery", "core", "async-runner"], function($, core, asyncTaskRunner) {
|
||||
define(["jquery"], function($) {
|
||||
|
||||
// Dependencies
|
||||
var core = undefined;
|
||||
var fileManager = undefined;
|
||||
|
||||
var publisher = {};
|
||||
|
||||
var wizardProvider = undefined;
|
||||
|
||||
function initWizard(provider) {
|
||||
function initWizard(provider, defaultPublishFormat) {
|
||||
defaultPublishFormat = defaultPublishFormat || "markdown";
|
||||
wizardProvider = provider;
|
||||
$("input:radio[name=radio-publish-existing][value=new]").prop("checked", true);
|
||||
$("input:radio[name=radio-publish-format][value=markdown]").prop("checked", true);
|
||||
|
||||
// Show/hide controls depending on provider
|
||||
$('div[class*=" control-publish-"]').hide().filter(".control-publish-" + provider).show();
|
||||
|
||||
// Reset fields
|
||||
$("#modal-publish input[type=text]").val("");
|
||||
$("input:radio[name=radio-publish-format][value=" + defaultPublishFormat + "]").prop("checked", true);
|
||||
|
||||
// Open dialog box
|
||||
$("#modal-publish").modal();
|
||||
}
|
||||
|
||||
publisher.init = function(fileManagerModule) {
|
||||
publisher.init = function(coreModule, fileManagerModule) {
|
||||
core = coreModule;
|
||||
fileManager = fileManagerModule;
|
||||
$("#action-publish-github").click(function() {
|
||||
$(".action-publish-github").click(function() {
|
||||
initWizard("github");
|
||||
});
|
||||
$("#action-publish-blogger").click(function() {
|
||||
initWizard("blogger");
|
||||
$(".action-publish-blogger").click(function() {
|
||||
initWizard("blogger", "html");
|
||||
});
|
||||
$("#action-publish-wordpress").click(function() {
|
||||
$(".action-publish-wordpress").click(function() {
|
||||
initWizard("wordpress");
|
||||
});
|
||||
$("#action-publish-tumblr").click(function() {
|
||||
$(".action-publish-tumblr").click(function() {
|
||||
initWizard("tumblr");
|
||||
});
|
||||
};
|
||||
|
||||
publisher.initWizard = function() {
|
||||
|
||||
};
|
||||
|
||||
return publisher;
|
||||
});
|
@ -1,132 +1,141 @@
|
||||
define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core, googleHelper, dropboxHelper) {
|
||||
define(["jquery", "google-helper", "dropbox-helper"], function($, googleHelper, dropboxHelper) {
|
||||
var synchronizer = {};
|
||||
|
||||
// Dependencies
|
||||
var core = undefined;
|
||||
var fileManager = undefined;
|
||||
|
||||
// Used to know the providers we are connected to
|
||||
synchronizer.useGoogleDrive = false;
|
||||
synchronizer.useDropbox = false;
|
||||
|
||||
var onSyncBegin = undefined;
|
||||
var onSyncEnd = undefined;
|
||||
var onQueueChanged = undefined;
|
||||
|
||||
// A synchronization queue containing fileIndex that has to be synchronized
|
||||
var syncUpQueue = undefined;
|
||||
// Used to know if user can force synchronization
|
||||
var uploadPending = false;
|
||||
|
||||
synchronizer.init = function(fileManagerModule, options) {
|
||||
fileManager = fileManagerModule;
|
||||
onSyncBegin = options.onSyncBegin || core.doNothing;
|
||||
onSyncEnd = options.onSyncEnd || core.doNothing;
|
||||
onQueueChanged = options.onQueueChanged || core.doNothing;
|
||||
|
||||
syncUpQueue = ";";
|
||||
// Load the queue from localStorage in case a previous synchronization
|
||||
// was aborted
|
||||
if (localStorage["sync.queue"]) {
|
||||
syncUpQueue = localStorage["sync.queue"];
|
||||
onQueueChanged();
|
||||
}
|
||||
if (localStorage["sync.current"]) {
|
||||
this.addFileForUpload(localStorage["sync.current"]);
|
||||
}
|
||||
};
|
||||
|
||||
// Add a file to the synchronization queue
|
||||
synchronizer.addFileForUpload = function(fileIndex) {
|
||||
synchronizer.notifyChange = function(fileIndex) {
|
||||
// Check that file has synchronized locations
|
||||
if(localStorage[fileIndex + ".sync"].length === 1) {
|
||||
return;
|
||||
}
|
||||
// Check that file is not in the queue
|
||||
if (syncUpQueue.indexOf(";" + fileIndex + ";") !== -1) {
|
||||
return;
|
||||
}
|
||||
syncUpQueue += fileIndex + ";";
|
||||
localStorage["sync.queue"] = syncUpQueue;
|
||||
onQueueChanged();
|
||||
uploadPending = true;
|
||||
synchronizer.updateSyncButton();
|
||||
};
|
||||
|
||||
// Recursive function to upload a single file on multiple locations
|
||||
function fileUp(fileSyncIndexList, content, contentCRC, title, titleCRC, callback) {
|
||||
if (fileSyncIndexList.length === 0) {
|
||||
localStorage.removeItem("sync.current");
|
||||
// run the next file synchronization
|
||||
syncUp(callback);
|
||||
var uploadFileSyncIndexList = [];
|
||||
var uploadContent = undefined;
|
||||
var uploadContentCRC = undefined;
|
||||
var uploadTitle = undefined;
|
||||
var uploadTitleCRC = undefined;
|
||||
function locationUp(callback) {
|
||||
|
||||
// No more synchronized location for this document
|
||||
if (uploadFileSyncIndexList.length === 0) {
|
||||
fileUp(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
// Dequeue a synchronized location
|
||||
var fileSyncIndex = uploadFileSyncIndexList.pop();
|
||||
if(!fileSyncIndex) {
|
||||
locationUp(callback);
|
||||
return;
|
||||
}
|
||||
var fileSyncIndex = fileSyncIndexList.pop();
|
||||
|
||||
// Skip if CRC has not changed
|
||||
var syncContentCRC = localStorage[fileSyncIndex + ".contentCRC"];
|
||||
var syncTitleCRC = localStorage[fileSyncIndex + ".titleCRC"];
|
||||
if(contentCRC == syncContentCRC && (syncTitleCRC === undefined || titleCRC == syncTitleCRC)) {
|
||||
fileUp(fileSyncIndexList, content, contentCRC, title, titleCRC, callback);
|
||||
if(uploadContentCRC == syncContentCRC && (syncTitleCRC === undefined || uploadTitleCRC == syncTitleCRC)) {
|
||||
locationUp(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
// If upload is going to run, go for an other upload cycle at the end
|
||||
uploadCycle = true;
|
||||
// When page is refreshed, this flag is false but should be true here
|
||||
uploadPending = true;
|
||||
|
||||
// Try to find the provider
|
||||
if (fileSyncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
|
||||
var id = fileSyncIndex.substring(SYNC_PROVIDER_GDRIVE.length);
|
||||
googleHelper.upload(id, undefined, title, content, function(result) {
|
||||
googleHelper.upload(id, undefined, uploadTitle, uploadContent, function(result) {
|
||||
if (result !== undefined) {
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = contentCRC;
|
||||
localStorage[fileSyncIndex + ".titleCRC"] = titleCRC;
|
||||
fileUp(fileSyncIndexList, content, contentCRC, title, titleCRC, callback);
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = uploadContentCRC;
|
||||
localStorage[fileSyncIndex + ".titleCRC"] = uploadTitleCRC;
|
||||
locationUp(callback);
|
||||
return;
|
||||
}
|
||||
// If error we put the fileIndex back in the queue
|
||||
synchronizer.addFileForUpload(localStorage["sync.current"]);
|
||||
localStorage.removeItem("sync.current");
|
||||
callback();
|
||||
|
||||
// If error we abort the synchronization (retry later)
|
||||
callback("abort");
|
||||
return;
|
||||
});
|
||||
} else if (fileSyncIndex.indexOf(SYNC_PROVIDER_DROPBOX) === 0) {
|
||||
var path = fileSyncIndex.substring(SYNC_PROVIDER_DROPBOX.length);
|
||||
path = decodeURIComponent(path);
|
||||
dropboxHelper.upload(path, content, function(result) {
|
||||
dropboxHelper.upload(path, uploadContent, function(result) {
|
||||
if (result !== undefined) {
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = contentCRC;
|
||||
fileUp(fileSyncIndexList, content, contentCRC, title, titleCRC, callback);
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = uploadContentCRC;
|
||||
locationUp(callback);
|
||||
return;
|
||||
}
|
||||
// If error we put the fileIndex back in the queue
|
||||
synchronizer.addFileForUpload(localStorage["sync.current"]);
|
||||
localStorage.removeItem("sync.current");
|
||||
callback();
|
||||
|
||||
// If error we abort the synchronization (retry later)
|
||||
callback("abort");
|
||||
return;
|
||||
});
|
||||
} else {
|
||||
fileUp(fileSyncIndexList, content, contentCRC, title, titleCRC, callback);
|
||||
// This should never happen
|
||||
console.error("Invalid fileSyncIndex: " + fileSyncIndex);
|
||||
callback("error");
|
||||
}
|
||||
}
|
||||
|
||||
function syncUp(callback) {
|
||||
// If nothing to synchronize
|
||||
if (syncUpQueue.length === 1) {
|
||||
callback();
|
||||
// Recursive function to upload multiple files
|
||||
var uploadFileIndexList = [];
|
||||
function fileUp(callback) {
|
||||
|
||||
// No more fileIndex to synchronize
|
||||
if (uploadFileIndexList.length === 0) {
|
||||
syncUp(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
// Dequeue a fileIndex
|
||||
var fileIndex = uploadFileIndexList.pop();
|
||||
var fileSyncIndexes = localStorage[fileIndex + ".sync"];
|
||||
if(!fileIndex || fileSyncIndexes.length === 1) {
|
||||
fileUp(callback);
|
||||
return;
|
||||
}
|
||||
|
||||
// Dequeue the fileIndex
|
||||
var separatorPos = syncUpQueue.indexOf(";", 1);
|
||||
var fileIndex = syncUpQueue.substring(1, separatorPos);
|
||||
localStorage["sync.current"] = fileIndex;
|
||||
syncUpQueue = syncUpQueue.substring(separatorPos);
|
||||
localStorage["sync.queue"] = syncUpQueue;
|
||||
onQueueChanged();
|
||||
// Get document title/content
|
||||
uploadContent = localStorage[fileIndex + ".content"];
|
||||
uploadContentCRC = core.crc32(uploadContent);
|
||||
uploadTitle = localStorage[fileIndex + ".title"];
|
||||
uploadTitleCRC = core.crc32(uploadTitle);
|
||||
|
||||
var content = localStorage[fileIndex + ".content"];
|
||||
var title = localStorage[fileIndex + ".title"];
|
||||
var contentCRC = core.crc32(content);
|
||||
var titleCRC = core.crc32(title);
|
||||
// Parse the list of synchronized locations associated to the document
|
||||
uploadFileSyncIndexList = fileSyncIndexes.split(";");
|
||||
locationUp(callback);
|
||||
}
|
||||
|
||||
// Parse the list of synchronized locations associated to the file
|
||||
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
|
||||
fileUp(fileSyncIndexList, content, contentCRC, title, titleCRC, callback);
|
||||
};
|
||||
// Used to upload document changes from local storage
|
||||
var uploadCycle = false;
|
||||
function syncUp(callback) {
|
||||
if(uploadCycle === true) {
|
||||
// New upload cycle
|
||||
uploadCycle = false;
|
||||
uploadFileIndexList = localStorage["file.list"].split(";");
|
||||
fileUp(callback);
|
||||
}
|
||||
else {
|
||||
callback();
|
||||
}
|
||||
}
|
||||
|
||||
// Used to download file changes from Google Drive
|
||||
function syncDownGdrive(callback) {
|
||||
if (synchronizer.useGoogleDrive === false) {
|
||||
callback();
|
||||
@ -136,12 +145,12 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
+ "lastChangeId"]);
|
||||
googleHelper.checkUpdates(lastChangeId, function(changes, newChangeId) {
|
||||
if (changes === undefined) {
|
||||
callback();
|
||||
callback("error");
|
||||
return;
|
||||
}
|
||||
googleHelper.downloadContent(changes, function(changes) {
|
||||
if (changes === undefined) {
|
||||
callback();
|
||||
callback("error");
|
||||
return;
|
||||
}
|
||||
var updateFileTitles = false;
|
||||
@ -155,32 +164,35 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
localStorage.removeItem(fileSyncIndex + ".etag");
|
||||
continue;
|
||||
}
|
||||
var title = localStorage[fileIndex + ".title"];
|
||||
var localTitle = localStorage[fileIndex + ".title"];
|
||||
// File deleted
|
||||
if (change.deleted === true) {
|
||||
fileManager.removeSync(fileSyncIndex);
|
||||
updateFileTitles = true;
|
||||
core.showMessage('"' + title + '" has been removed from Google Drive.');
|
||||
core.showMessage('"' + localTitle + '" has been removed from Google Drive.');
|
||||
continue;
|
||||
}
|
||||
var content = localStorage[fileIndex + ".content"];
|
||||
var localTitleChanged = localStorage[fileSyncIndex + ".titleCRC"] == core.crc32(localTitle);
|
||||
var localContent = localStorage[fileIndex + ".content"];
|
||||
var localContentChanged = localStorage[fileSyncIndex + ".contentCRC"] == core.crc32(localContent);
|
||||
var file = change.file;
|
||||
var titleChanged = title != file.title;
|
||||
var contentChanged = content != file.content;
|
||||
// If file is in the upload queue we have a conflict
|
||||
if ((titleChanged || contentChanged) && syncUpQueue.indexOf(";" + fileIndex + ";") !== -1) {
|
||||
fileManager.createFile(title + " (backup)", content);
|
||||
var fileTitleChanged = localTitle != file.title;
|
||||
var fileContentChanged = localContent != file.content;
|
||||
// Conflict detection
|
||||
if ((fileTitleChanged === true && localTitleChanged === true)
|
||||
|| (fileContentChanged === true && localContentChanged === true)) {
|
||||
fileManager.createFile(localTitle + " (backup)", localContent);
|
||||
updateFileTitles = true;
|
||||
core.showMessage('Conflict detected on "' + title + '". A backup has been created locally.');
|
||||
core.showMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
|
||||
}
|
||||
// If file title changed
|
||||
if(titleChanged) {
|
||||
if(fileTitleChanged) {
|
||||
localStorage[fileIndex + ".title"] = file.title;
|
||||
updateFileTitles = true;
|
||||
core.showMessage('"' + title + '" has been renamed to "' + file.title + '" on Google Drive.');
|
||||
core.showMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
|
||||
}
|
||||
// If file content changed
|
||||
if(contentChanged) {
|
||||
if(fileContentChanged) {
|
||||
localStorage[fileIndex + ".content"] = file.content;
|
||||
core.showMessage('"' + file.title + '" has been updated from Google Drive.');
|
||||
if(fileIndex == localStorage["file.current"]) {
|
||||
@ -190,13 +202,11 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
}
|
||||
// Update file etag and CRCs
|
||||
localStorage[fileSyncIndex + ".etag"] = file.etag;
|
||||
var contentCRC = core.crc32(file.content);
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = contentCRC;
|
||||
var titleCRC = core.crc32(file.title);
|
||||
localStorage[fileSyncIndex + ".titleCRC"] = titleCRC;
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = core.crc32(file.content);
|
||||
localStorage[fileSyncIndex + ".titleCRC"] = core.crc32(file.title);
|
||||
|
||||
// Synchronize file with others locations
|
||||
synchronizer.addFileForUpload(fileIndex);
|
||||
uploadPending = true;
|
||||
}
|
||||
if(updateFileTitles) {
|
||||
fileManager.updateFileTitles();
|
||||
@ -208,6 +218,7 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
});
|
||||
}
|
||||
|
||||
// Used to download file changes from Dropbox
|
||||
function syncDownDropbox(callback) {
|
||||
if (synchronizer.useDropbox === false) {
|
||||
callback();
|
||||
@ -216,12 +227,12 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
var lastChangeId = localStorage[SYNC_PROVIDER_DROPBOX + "lastChangeId"];
|
||||
dropboxHelper.checkUpdates(lastChangeId, function(changes, newChangeId) {
|
||||
if (changes === undefined) {
|
||||
callback();
|
||||
callback("error");
|
||||
return;
|
||||
}
|
||||
dropboxHelper.downloadContent(changes, function(changes) {
|
||||
if (changes === undefined) {
|
||||
callback();
|
||||
callback("error");
|
||||
return;
|
||||
}
|
||||
var updateFileTitles = false;
|
||||
@ -235,27 +246,28 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
localStorage.removeItem(fileSyncIndex + ".version");
|
||||
continue;
|
||||
}
|
||||
var title = localStorage[fileIndex + ".title"];
|
||||
var localTitle = localStorage[fileIndex + ".title"];
|
||||
// File deleted
|
||||
if (change.wasRemoved === true) {
|
||||
fileManager.removeSync(fileSyncIndex);
|
||||
updateFileTitles = true;
|
||||
core.showMessage('"' + title + '" has been removed from Dropbox.');
|
||||
core.showMessage('"' + localTitle + '" has been removed from Dropbox.');
|
||||
continue;
|
||||
}
|
||||
var content = localStorage[fileIndex + ".content"];
|
||||
var localContent = localStorage[fileIndex + ".content"];
|
||||
var localContentChanged = localStorage[fileSyncIndex + ".contentCRC"] == core.crc32(localContent);
|
||||
var file = change.stat;
|
||||
var contentChanged = content != file.content;
|
||||
// If file is in the upload queue we have a conflict
|
||||
if (contentChanged && syncUpQueue.indexOf(";" + fileIndex + ";") !== -1) {
|
||||
fileManager.createFile(title + " (backup)", content);
|
||||
var fileContentChanged = localContent != file.content;
|
||||
// Conflict detection
|
||||
if (fileContentChanged === true && localContentChanged === true) {
|
||||
fileManager.createFile(localTitle + " (backup)", localContent);
|
||||
updateFileTitles = true;
|
||||
core.showMessage('Conflict detected on "' + title + '". A backup has been created locally.');
|
||||
core.showMessage('Conflict detected on "' + localTitle + '". A backup has been created locally.');
|
||||
}
|
||||
// If file content changed
|
||||
if(contentChanged) {
|
||||
if(fileContentChanged) {
|
||||
localStorage[fileIndex + ".content"] = file.content;
|
||||
core.showMessage('"' + title + '" has been updated from Dropbox.');
|
||||
core.showMessage('"' + localTitle + '" has been updated from Dropbox.');
|
||||
if(fileIndex == localStorage["file.current"]) {
|
||||
updateFileTitles = false; // Done by next function
|
||||
fileManager.selectFile(); // Refresh editor
|
||||
@ -263,11 +275,10 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
}
|
||||
// Update file version and CRC
|
||||
localStorage[fileSyncIndex + ".version"] = file.versionTag;
|
||||
var contentCRC = core.crc32(file.content);
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = contentCRC;
|
||||
localStorage[fileSyncIndex + ".contentCRC"] = core.crc32(file.content);
|
||||
|
||||
// Synchronize file with others locations
|
||||
synchronizer.addFileForUpload(fileIndex);
|
||||
uploadPending = true;
|
||||
}
|
||||
if(updateFileTitles) {
|
||||
fileManager.updateFileTitles();
|
||||
@ -293,29 +304,58 @@ define(["jquery", "core", "google-helper", "dropbox-helper"], function($, core,
|
||||
return;
|
||||
}
|
||||
syncRunning = true;
|
||||
uploadCycle = true;
|
||||
lastSync = core.currentTime;
|
||||
onSyncBegin();
|
||||
|
||||
syncDown(function() {
|
||||
syncUp(function() {
|
||||
synchronizer.updateSyncButton();
|
||||
|
||||
function isError(error) {
|
||||
if(error !== undefined) {
|
||||
syncRunning = false;
|
||||
onSyncEnd();
|
||||
synchronizer.updateSyncButton();
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
syncDown(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncUp(function(error) {
|
||||
if(isError(error)) {
|
||||
return;
|
||||
}
|
||||
syncRunning = false;
|
||||
uploadPending = false;
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
synchronizer.forceSync = function() {
|
||||
lastSync = 0;
|
||||
this.sync();
|
||||
synchronizer.sync();
|
||||
};
|
||||
|
||||
synchronizer.isRunning = function() {
|
||||
return syncRunning;
|
||||
synchronizer.updateSyncButton = function() {
|
||||
if(syncRunning === true || uploadPending === false || core.isOffline) {
|
||||
$(".action-force-sync").addClass("disabled");
|
||||
}
|
||||
else {
|
||||
$(".action-force-sync").removeClass("disabled");
|
||||
}
|
||||
};
|
||||
|
||||
synchronizer.init = function(coreModule, fileManagerModule) {
|
||||
core = coreModule;
|
||||
fileManager = fileManagerModule;
|
||||
|
||||
synchronizer.updateSyncButton();
|
||||
$(".action-force-sync").click(function() {
|
||||
if(!$(this).hasClass("disabled")) {
|
||||
synchronizer.forceSync();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
synchronizer.isQueueEmpty = function() {
|
||||
return syncUpQueue.length === 1;
|
||||
};
|
||||
|
||||
return synchronizer;
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user