2013-03-24 19:42:15 +00:00
|
|
|
function showError(msg) {
|
|
|
|
alert(msg);
|
|
|
|
}
|
2013-03-24 14:54:26 +00:00
|
|
|
|
2013-03-28 22:00:11 +00:00
|
|
|
var workingIndicator = 0;
|
|
|
|
var FLAG_GDRIVE_UPLOAD = 1;
|
|
|
|
var FLAG_SYNCHRONIZE = 2;
|
|
|
|
function setWorkingIndicator(flag) {
|
|
|
|
workingIndicator |= flag;
|
|
|
|
if(workingIndicator) {
|
|
|
|
$(".working-indicator").removeClass("hide");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function unsetWorkingIndicator(flag) {
|
|
|
|
workingIndicator &= ~flag;
|
|
|
|
if(!workingIndicator) {
|
|
|
|
$(".working-indicator").addClass("hide");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var SYNC_PROVIDER_GDRIVE = "sync.gdrive.";
|
|
|
|
var synchronizer = (function($) {
|
|
|
|
var synchronizer = {};
|
|
|
|
|
|
|
|
// A synchronization queue containing fileIndex that has to be synchronized
|
|
|
|
var syncQueue = undefined;
|
|
|
|
synchronizer.init = function() {
|
|
|
|
syncQueue = ";";
|
|
|
|
// Load the queue from localStorage in case a previous synchronization was aborted
|
|
|
|
if(localStorage["sync.queue"]) {
|
|
|
|
syncQueue = localStorage["sync.queue"];
|
|
|
|
}
|
|
|
|
if(localStorage["sync.current"]) {
|
|
|
|
this.addFile(localStorage["sync.current"]);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Add a file to the synchronization queue
|
|
|
|
synchronizer.addFile = function(fileIndex) {
|
|
|
|
if(syncQueue.indexOf(";" + fileIndex + ";") === -1) {
|
|
|
|
syncQueue += fileIndex + ";";
|
|
|
|
localStorage["sync.queue"] = syncQueue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Recursive function to run synchronization of a single file on multiple locations
|
|
|
|
function sync(fileSyncIndexList, content, title) {
|
|
|
|
if(fileSyncIndexList.length === 0) {
|
|
|
|
localStorage.removeItem("sync.current");
|
|
|
|
unsetWorkingIndicator(FLAG_SYNCHRONIZE);
|
|
|
|
running = false;
|
|
|
|
// run the next file synchronization
|
|
|
|
synchronizer.run();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var fileSyncIndex = fileSyncIndexList.pop();
|
|
|
|
|
|
|
|
// Try to find the provider
|
|
|
|
if(fileSyncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
|
|
|
|
var id = fileSyncIndex.substring(SYNC_PROVIDER_GDRIVE.length);
|
|
|
|
gdrive.updateFile(id, title, content, function() {
|
|
|
|
sync(fileSyncIndexList, content, title);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
sync(fileSyncIndexList, content, title);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var running = false;
|
|
|
|
synchronizer.run = function() {
|
|
|
|
// If synchronization is already running or nothing to synchronize
|
|
|
|
if(running || syncQueue.length === 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start synchronization of the next available in the queue
|
|
|
|
setWorkingIndicator(FLAG_SYNCHRONIZE);
|
|
|
|
running = true;
|
|
|
|
|
|
|
|
// Dequeue the fileIndex
|
|
|
|
var separatorPos = syncQueue.indexOf(";", 1);
|
|
|
|
var fileIndex = syncQueue.substring(1, separatorPos);
|
|
|
|
localStorage["sync.current"] = fileIndex;
|
|
|
|
syncQueue = syncQueue.substring(separatorPos);
|
|
|
|
localStorage["sync.queue"] = syncQueue;
|
|
|
|
|
|
|
|
var content = localStorage[fileIndex + ".content"];
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
|
|
|
|
|
|
|
// Parse the list of synchronized locations associated to the file
|
|
|
|
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
|
|
|
|
sync(fileSyncIndexList, content, title);
|
|
|
|
};
|
|
|
|
|
|
|
|
return synchronizer;
|
|
|
|
})(jQuery);
|
|
|
|
|
2013-03-24 19:42:15 +00:00
|
|
|
var fileManager = (function($) {
|
2013-03-24 14:54:26 +00:00
|
|
|
|
|
|
|
var fileManager = {};
|
|
|
|
|
2013-03-28 22:00:11 +00:00
|
|
|
var save = false;
|
2013-03-24 14:54:26 +00:00
|
|
|
fileManager.init = function() {
|
2013-03-28 22:00:11 +00:00
|
|
|
synchronizer.init();
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.selectFile();
|
2013-03-24 14:54:26 +00:00
|
|
|
window.setInterval(function() {
|
|
|
|
fileManager.saveFile();
|
2013-03-28 22:00:11 +00:00
|
|
|
synchronizer.run();
|
|
|
|
}, 3000);
|
2013-03-26 22:41:37 +00:00
|
|
|
$(".action-create-file").click(function() {
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.saveFile();
|
|
|
|
fileManager.createFile();
|
|
|
|
fileManager.selectFile();
|
|
|
|
});
|
2013-03-26 22:41:37 +00:00
|
|
|
$(".action-remove-file").click(function() {
|
|
|
|
fileManager.deleteFile();
|
|
|
|
fileManager.selectFile();
|
|
|
|
});
|
2013-03-24 23:21:55 +00:00
|
|
|
$("#file-title").click(function() {
|
|
|
|
$(this).hide();
|
|
|
|
$("#file-title-input").show().focus();
|
|
|
|
});
|
|
|
|
$("#file-title-input").blur(function() {
|
|
|
|
var title = $.trim($(this).val());
|
2013-03-26 23:32:36 +00:00
|
|
|
if (title) {
|
2013-03-25 12:03:22 +00:00
|
|
|
var fileIndex = localStorage["file.current"];
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage[fileIndex + ".title"] = title;
|
|
|
|
}
|
|
|
|
$(this).hide();
|
|
|
|
$("#file-title").show();
|
2013-03-26 22:41:37 +00:00
|
|
|
fileManager.updateFileDescList();
|
2013-03-25 12:03:22 +00:00
|
|
|
fileManager.updateFileTitleUI();
|
2013-03-28 22:00:11 +00:00
|
|
|
save = true;
|
2013-03-24 23:21:55 +00:00
|
|
|
});
|
2013-03-27 20:19:12 +00:00
|
|
|
$(".action-upload-gdrive").click(function() {
|
2013-03-27 22:09:27 +00:00
|
|
|
$(".file-sync-indicator").removeClass("hide");
|
2013-03-27 20:19:12 +00:00
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
var content = localStorage[fileIndex + ".content"];
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
2013-03-28 22:00:11 +00:00
|
|
|
(function(fileIndex) {
|
|
|
|
gdrive.createFile(title, content, function(fileSyncIndex) {
|
|
|
|
localStorage[fileIndex + ".sync"] += fileSyncIndex + ";";
|
|
|
|
});
|
|
|
|
})(fileIndex);
|
|
|
|
});
|
|
|
|
$("#wmd-input").keyup(function() {
|
|
|
|
save = true;
|
2013-03-27 20:19:12 +00:00
|
|
|
});
|
2013-03-24 14:54:26 +00:00
|
|
|
};
|
2013-03-28 22:00:11 +00:00
|
|
|
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.selectFile = function() {
|
|
|
|
// If file system does not exist
|
2013-03-28 22:00:11 +00:00
|
|
|
if (!localStorage["file.counter"] || !localStorage["file.list"]) {
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage.clear();
|
2013-03-28 22:00:11 +00:00
|
|
|
localStorage["file.counter"] = 0;
|
|
|
|
localStorage["file.list"] = ";";
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
2013-03-26 22:41:37 +00:00
|
|
|
this.updateFileDescList();
|
2013-03-24 23:21:55 +00:00
|
|
|
// If no file create one
|
2013-03-26 23:32:36 +00:00
|
|
|
if (this.fileDescList.length === 0) {
|
2013-03-24 23:21:55 +00:00
|
|
|
this.createFile();
|
2013-03-26 22:41:37 +00:00
|
|
|
this.updateFileDescList();
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
|
|
|
// If no default file take first one
|
2013-03-26 23:32:36 +00:00
|
|
|
if (!localStorage["file.current"]) {
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage["file.current"] = this.fileDescList[0].index;
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
|
|
|
// Update the editor and the file title
|
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
$("#wmd-input").val(localStorage[fileIndex + ".content"]);
|
2013-03-27 15:28:31 +00:00
|
|
|
core.createEditor();
|
2013-03-26 23:32:36 +00:00
|
|
|
this.updateFileTitleUI();
|
2013-03-24 14:54:26 +00:00
|
|
|
};
|
|
|
|
|
2013-03-24 23:21:55 +00:00
|
|
|
fileManager.createFile = function(title) {
|
2013-03-26 23:32:36 +00:00
|
|
|
if (!title) {
|
2013-03-25 23:17:34 +00:00
|
|
|
title = "Filename";
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
2013-03-28 22:00:11 +00:00
|
|
|
// Create the fileIndex
|
|
|
|
var fileCounter = parseInt(localStorage["file.counter"]);
|
|
|
|
var fileIndex = "file." + fileCounter;
|
|
|
|
// Create the file in the localStorage
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage[fileIndex + ".content"] = "";
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage[fileIndex + ".title"] = title;
|
2013-03-28 22:00:11 +00:00
|
|
|
localStorage[fileIndex + ".sync"] = ";";
|
|
|
|
localStorage["file.counter"] = fileCounter + 1;
|
|
|
|
localStorage["file.list"] += fileIndex + ";";
|
2013-03-24 23:21:55 +00:00
|
|
|
localStorage["file.current"] = fileIndex;
|
2013-03-26 22:41:37 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-26 22:41:37 +00:00
|
|
|
fileManager.deleteFile = function() {
|
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
localStorage.removeItem("file.current");
|
2013-03-28 22:00:11 +00:00
|
|
|
localStorage["file.list"] = localStorage["file.list"].replace(";" + fileIndex + ";", ";");
|
|
|
|
localStorage.removeItem(fileIndex + ".sync");
|
2013-03-26 22:41:37 +00:00
|
|
|
localStorage.removeItem(fileIndex + ".title");
|
|
|
|
localStorage.removeItem(fileIndex + ".content");
|
2013-03-24 23:21:55 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-26 22:41:37 +00:00
|
|
|
fileManager.updateFileDescList = function() {
|
|
|
|
this.fileDescList = [];
|
2013-03-24 23:21:55 +00:00
|
|
|
$("#file-selector").empty();
|
2013-03-28 22:00:11 +00:00
|
|
|
var fileIndexList = localStorage["file.list"].split(";");
|
|
|
|
for ( var i = 1; i < fileIndexList.length - 1; i++) {
|
|
|
|
var fileIndex = fileIndexList[i];
|
2013-03-24 23:21:55 +00:00
|
|
|
var title = localStorage[fileIndex + ".title"];
|
2013-03-28 22:00:11 +00:00
|
|
|
this.fileDescList.push({ "index" : fileIndex, "title" : title });
|
2013-03-25 12:03:22 +00:00
|
|
|
}
|
2013-03-26 22:41:37 +00:00
|
|
|
this.fileDescList.sort(function(a, b) {
|
|
|
|
if (a.title.toLowerCase() < b.title.toLowerCase())
|
|
|
|
return -1;
|
|
|
|
if (a.title.toLowerCase() > b.title.toLowerCase())
|
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
});
|
2013-03-25 12:03:22 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
|
|
|
fileManager.updateFileTitleUI = function() {
|
|
|
|
// Update the editor and the file title
|
2013-03-25 12:03:22 +00:00
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
var title = localStorage[fileIndex + ".title"];
|
2013-03-26 22:41:37 +00:00
|
|
|
document.title = "StackEdit - " + title;
|
|
|
|
$(".file-title").text(title);
|
2013-03-25 12:03:22 +00:00
|
|
|
$("#file-title-input").val(title);
|
|
|
|
$("#file-selector").empty();
|
2013-03-26 23:32:36 +00:00
|
|
|
|
|
|
|
for ( var i = 0; i < this.fileDescList.length; i++) {
|
2013-03-26 22:41:37 +00:00
|
|
|
var fileDesc = this.fileDescList[i];
|
|
|
|
var a = $("<a>").text(fileDesc.title);
|
|
|
|
var li = $("<li>").append(a);
|
2013-03-26 23:32:36 +00:00
|
|
|
if (fileDesc.index == fileIndex) {
|
2013-03-26 22:41:37 +00:00
|
|
|
li.addClass("disabled");
|
2013-03-26 23:32:36 +00:00
|
|
|
} else {
|
2013-03-28 22:00:11 +00:00
|
|
|
a.prop("href", "#").click(
|
2013-03-26 23:32:36 +00:00
|
|
|
(function(fileIndex) {
|
|
|
|
return function() {
|
|
|
|
localStorage["file.current"] = fileIndex;
|
|
|
|
fileManager.selectFile();
|
|
|
|
};
|
|
|
|
})(fileDesc.index));
|
2013-03-26 22:41:37 +00:00
|
|
|
}
|
|
|
|
$("#file-selector").append(li);
|
2013-03-24 23:21:55 +00:00
|
|
|
}
|
2013-03-26 23:32:36 +00:00
|
|
|
};
|
2013-03-24 14:54:26 +00:00
|
|
|
|
|
|
|
fileManager.saveFile = function() {
|
2013-03-28 22:00:11 +00:00
|
|
|
if(save) {
|
|
|
|
var content = $("#wmd-input").val();
|
|
|
|
var fileIndex = localStorage["file.current"];
|
|
|
|
localStorage[fileIndex + ".content"] = content;
|
|
|
|
synchronizer.addFile(fileIndex);
|
|
|
|
save = false;
|
|
|
|
}
|
2013-03-24 19:42:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return fileManager;
|
|
|
|
})(jQuery);
|
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
var core = (function($) {
|
|
|
|
var core = {};
|
|
|
|
|
|
|
|
core.init = function() {
|
|
|
|
this.loadSettings();
|
|
|
|
this.createLayout();
|
|
|
|
|
2013-03-28 22:00:11 +00:00
|
|
|
$(".action-load-settings").click(function() {
|
|
|
|
core.loadSettings();
|
|
|
|
});
|
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
$(".action-apply-settings").click(function() {
|
|
|
|
core.saveSettings();
|
|
|
|
fileManager.saveFile();
|
|
|
|
location.reload();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-03-28 22:00:11 +00:00
|
|
|
var settings = {
|
|
|
|
layoutOrientation: "horizontal"
|
|
|
|
};
|
2013-03-27 15:28:31 +00:00
|
|
|
core.loadSettings = function() {
|
|
|
|
if(localStorage.settings) {
|
2013-03-28 22:00:11 +00:00
|
|
|
$.extend(settings, JSON.parse(localStorage.settings));
|
2013-03-27 15:28:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Layout orientation
|
2013-03-28 22:00:11 +00:00
|
|
|
$("input:radio[name=radio-layout-orientation][value=" + settings.layoutOrientation + "]").prop("checked", true);
|
2013-03-27 15:28:31 +00:00
|
|
|
};
|
2013-03-26 23:32:36 +00:00
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
core.saveSettings = function() {
|
|
|
|
|
|
|
|
// Layout orientation
|
2013-03-28 22:00:11 +00:00
|
|
|
settings.layoutOrientation = $("input:radio[name=radio-layout-orientation]:checked").prop("value");
|
2013-03-27 15:28:31 +00:00
|
|
|
|
|
|
|
localStorage.settings = JSON.stringify(settings);
|
|
|
|
};
|
2013-03-26 22:41:37 +00:00
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
core.createLayout = function() {
|
|
|
|
var layout = undefined;
|
|
|
|
var layoutGlobalConfig = { closable : true, resizable : false,
|
|
|
|
slidable : false, livePaneResizing : true, spacing_open : 20,
|
|
|
|
spacing_closed : 20, togglerLength_open : 90,
|
|
|
|
togglerLength_closed : 90, center__minWidth : 100, center__minHeight : 100,
|
|
|
|
stateManagement__enabled : false, };
|
|
|
|
if (settings.layoutOrientation == "horizontal") {
|
2013-03-28 22:00:11 +00:00
|
|
|
$(".ui-layout-east").addClass("well").prop("id", "wmd-preview");
|
2013-03-27 15:28:31 +00:00
|
|
|
layout = $('body').layout(
|
|
|
|
$.extend(layoutGlobalConfig,
|
|
|
|
{ east__resizable : true, east__size : .5, east__minSize : 200,
|
|
|
|
south__closable : false, }));
|
|
|
|
} else if (settings.layoutOrientation == "vertical") {
|
|
|
|
$(".ui-layout-east").remove();
|
2013-03-28 22:00:11 +00:00
|
|
|
$(".ui-layout-south").addClass("well").prop("id", "wmd-preview");
|
2013-03-27 15:28:31 +00:00
|
|
|
layout = $('body').layout(
|
|
|
|
$.extend(layoutGlobalConfig, { south__resizable : true,
|
|
|
|
south__size : .5, south__minSize : 200, }));
|
|
|
|
}
|
|
|
|
$(".ui-layout-toggler-north").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$(".ui-layout-toggler-south").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$(".ui-layout-toggler-east").addClass("btn").append(
|
|
|
|
$("<b>").addClass("caret"));
|
|
|
|
$("#navbar").click(function() {
|
|
|
|
layout.allowOverflow('north');
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
core.createEditor = function() {
|
|
|
|
$("#wmd-button-bar").empty();
|
|
|
|
var converter = Markdown.getSanitizingConverter();
|
|
|
|
var editor = new Markdown.Editor(converter);
|
|
|
|
editor.run();
|
|
|
|
|
|
|
|
$(".wmd-button-row").addClass("btn-group").find("li:not(.wmd-spacer)")
|
|
|
|
.addClass("btn").css("left", 0).find("span").hide();
|
|
|
|
$("#wmd-bold-button").append($("<i>").addClass("icon-bold"));
|
|
|
|
$("#wmd-italic-button").append($("<i>").addClass("icon-italic"));
|
|
|
|
$("#wmd-link-button").append($("<i>").addClass("icon-globe"));
|
|
|
|
$("#wmd-quote-button").append($("<i>").addClass("icon-indent-left"));
|
|
|
|
$("#wmd-code-button").append($("<i>").addClass("icon-code"));
|
|
|
|
$("#wmd-image-button").append($("<i>").addClass("icon-picture"));
|
|
|
|
$("#wmd-olist-button").append($("<i>").addClass("icon-numbered-list"));
|
|
|
|
$("#wmd-ulist-button").append($("<i>").addClass("icon-list"));
|
|
|
|
$("#wmd-heading-button").append($("<i>").addClass("icon-text-height"));
|
|
|
|
$("#wmd-hr-button").append($("<i>").addClass("icon-hr"));
|
|
|
|
$("#wmd-undo-button").append($("<i>").addClass("icon-undo"));
|
|
|
|
$("#wmd-redo-button").append($("<i>").addClass("icon-share-alt"));
|
|
|
|
};
|
|
|
|
|
|
|
|
return core;
|
|
|
|
})(jQuery);
|
2013-03-25 12:03:22 +00:00
|
|
|
|
2013-03-24 19:42:15 +00:00
|
|
|
(function($) {
|
|
|
|
|
|
|
|
$(function() {
|
2013-03-25 12:03:22 +00:00
|
|
|
|
2013-03-27 15:28:31 +00:00
|
|
|
core.init();
|
2013-03-24 19:42:15 +00:00
|
|
|
if (typeof (Storage) !== "undefined") {
|
|
|
|
fileManager.init();
|
|
|
|
} else {
|
2013-03-28 22:00:11 +00:00
|
|
|
showError("Local storage is not available");
|
2013-03-26 22:41:37 +00:00
|
|
|
}
|
2013-03-24 19:42:15 +00:00
|
|
|
});
|
|
|
|
|
2013-03-24 14:54:26 +00:00
|
|
|
})(jQuery);
|