Stackedit/js/main.js

517 lines
16 KiB
JavaScript
Raw Normal View History

2013-03-30 11:56:17 +00:00
var currentTime = new Date().getTime();
2013-03-24 19:42:15 +00:00
function showError(msg) {
2013-03-30 11:56:17 +00:00
showMessage(msg, "icon-warning-sign");
}
function showMessage(msg, iconClass, options) {
options = options || {};
2013-03-31 01:00:03 +00:00
iconClass = iconClass || "icon-info-sign";
2013-03-30 11:56:17 +00:00
$.jGrowl("<i class='icon-white " + iconClass + "'></i> " + msg, options);
2013-03-24 19:42:15 +00:00
}
2013-03-24 14:54:26 +00:00
2013-03-30 11:56:17 +00:00
function showWorkingIndicator(show) {
if (show === false) {
$(".working-indicator").addClass("hide");
} else {
2013-03-28 22:00:11 +00:00
$(".working-indicator").removeClass("hide");
}
}
2013-03-30 11:56:17 +00:00
var offline = false;
var offlineTime = currentTime;
function onOffline() {
offline = true;
offlineTime = currentTime;
if ($(".msg-offline").length === 0)
showMessage("You are offline.", "icon-exclamation-sign msg-offline", {
sticky : true, close : function() {
showMessage("You are back online!", "icon-signal");
} });
}
function onOnline() {
offline = false;
$(".msg-offline").parents(".jGrowl-notification").trigger(
'jGrowl.beforeClose');
}
function autoClean() {
// Try to reconnect if we are offline but we have some network
if (offline === true && navigator.onLine === true
&& offlineTime + 60000 < currentTime) {
offlineTime = currentTime;
// Try to download anything to test the connection
$.ajax(
{ url : "https://apis.google.com/js/client.js", timeout : 5000,
dataType : "script" }).done(function() {
onOnline();
});
2013-03-28 22:00:11 +00:00
}
}
var SYNC_PROVIDER_GDRIVE = "sync.gdrive.";
var synchronizer = (function($) {
var synchronizer = {};
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
// A synchronization queue containing fileIndex that has to be synchronized
var syncQueue = undefined;
synchronizer.init = function() {
syncQueue = ";";
2013-03-29 19:37:52 +00:00
// Load the queue from localStorage in case a previous synchronization
// was aborted
if (localStorage["sync.queue"]) {
2013-03-28 22:00:11 +00:00
syncQueue = localStorage["sync.queue"];
}
2013-03-29 19:37:52 +00:00
if (localStorage["sync.current"]) {
2013-03-28 22:00:11 +00:00
this.addFile(localStorage["sync.current"]);
}
};
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
// Add a file to the synchronization queue
synchronizer.addFile = function(fileIndex) {
2013-03-29 19:37:52 +00:00
if (syncQueue.indexOf(";" + fileIndex + ";") === -1) {
2013-03-28 22:00:11 +00:00
syncQueue += fileIndex + ";";
localStorage["sync.queue"] = syncQueue;
}
};
2013-03-29 19:37:52 +00:00
// Recursive function to run synchronization of a single file on multiple
// locations
2013-03-28 22:00:11 +00:00
function sync(fileSyncIndexList, content, title) {
2013-03-29 19:37:52 +00:00
if (fileSyncIndexList.length === 0) {
2013-03-28 22:00:11 +00:00
localStorage.removeItem("sync.current");
running = false;
// run the next file synchronization
synchronizer.run();
return;
}
var fileSyncIndex = fileSyncIndexList.pop();
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
// Try to find the provider
2013-03-29 19:37:52 +00:00
if (fileSyncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
2013-03-28 22:00:11 +00:00
var id = fileSyncIndex.substring(SYNC_PROVIDER_GDRIVE.length);
2013-03-28 23:06:03 +00:00
gdrive.updateFile(id, title, content, function(result) {
2013-03-30 11:56:17 +00:00
if (!result && offline) {
// If we detect offline mode we put the fileIndex back in
// the queue
synchronizer.addFile(localStorage["sync.current"]);
localStorage.removeItem("sync.current");
running = false;
return;
2013-03-28 23:06:03 +00:00
}
2013-03-28 22:00:11 +00:00
sync(fileSyncIndexList, content, title);
});
} else {
sync(fileSyncIndexList, content, title);
}
}
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
var running = false;
synchronizer.run = function() {
// If synchronization is already running or nothing to synchronize
2013-03-30 11:56:17 +00:00
if (running || syncQueue.length === 1 || offline) {
2013-03-28 22:00:11 +00:00
return;
}
running = true;
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
// 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;
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
var content = localStorage[fileIndex + ".content"];
var title = localStorage[fileIndex + ".title"];
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
// Parse the list of synchronized locations associated to the file
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
sync(fileSyncIndexList, content, title);
};
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
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-30 11:56:17 +00:00
gdrive.init();
2013-03-28 22:00:11 +00:00
synchronizer.init();
2013-03-24 23:21:55 +00:00
fileManager.selectFile();
2013-03-29 19:37:52 +00:00
// Save file automatically and synchronize
2013-03-24 14:54:26 +00:00
window.setInterval(function() {
2013-03-30 11:56:17 +00:00
currentTime = new Date().getTime();
2013-03-24 14:54:26 +00:00
fileManager.saveFile();
2013-03-28 22:00:11 +00:00
synchronizer.run();
2013-03-30 11:56:17 +00:00
asyncTaskRunner.runTask();
autoClean();
2013-03-29 19:37:52 +00:00
}, 1000);
$(".action-create-file").click(function() {
2013-03-24 23:21:55 +00:00
fileManager.saveFile();
fileManager.createFile();
fileManager.selectFile();
});
$(".action-remove-file").click(function() {
fileManager.deleteFile();
fileManager.selectFile();
});
2013-03-31 01:00:03 +00:00
$(".action-refresh-manage-sync").click(refreshManageSync);
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-31 01:00:03 +00:00
var fileIndexTitle = localStorage["file.current"] + ".title";
if (title != localStorage[fileIndexTitle]) {
localStorage[fileIndexTitle] = title;
updateFileDescList();
updateFileTitleUI();
save = true;
}
2013-03-24 23:21:55 +00:00
}
$(this).hide();
$("#file-title").show();
});
2013-03-29 19:37:52 +00:00
$(".action-download-md").click(
function() {
var content = $("#wmd-input").val();
2013-03-31 01:00:03 +00:00
var uriContent = "data:application/octet-stream;base64,"
+ base64.encode(content);
2013-03-29 19:37:52 +00:00
window.open(uriContent, 'file');
});
$(".action-download-html").click(
function() {
var content = $("#wmd-preview").html();
2013-03-31 01:00:03 +00:00
var uriContent = "data:application/octet-stream;base64,"
+ base64.encode(content);
2013-03-29 19:37:52 +00:00
window.open(uriContent, 'file');
});
2013-03-31 01:00:03 +00:00
$(".action-upload-gdrive").click(uploadGdrive);
2013-03-24 14:54:26 +00:00
};
2013-03-29 19:37:52 +00:00
2013-03-31 01:00:03 +00:00
var fileDescList = [];
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-31 01:00:03 +00:00
updateFileDescList();
2013-03-24 23:21:55 +00:00
// If no file create one
2013-03-31 01:00:03 +00:00
if (fileDescList.length === 0) {
2013-03-24 23:21:55 +00:00
this.createFile();
2013-03-31 01:00:03 +00:00
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-31 01:00:03 +00:00
localStorage["file.current"] = 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-28 23:12:15 +00:00
core.createEditor(function() {
save = true;
});
2013-03-31 01:00:03 +00:00
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"] = "";
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 23:32:36 +00:00
fileManager.deleteFile = function() {
var fileIndex = localStorage["file.current"];
2013-03-31 01:33:31 +00:00
// Remove synchronized locations
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
for ( var i = 1; i < fileSyncIndexList.length - 1; i++) {
var sync = fileSyncIndexList[i];
fileManager.removeSync(sync);
}
localStorage.removeItem(fileIndex + ".sync");
localStorage.removeItem("file.current");
2013-03-29 19:37:52 +00:00
localStorage["file.list"] = localStorage["file.list"].replace(";"
2013-03-31 01:33:31 +00:00
+ fileIndex + ";", ";");
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-31 01:00:03 +00:00
fileManager.saveFile = function() {
if (save) {
var content = $("#wmd-input").val();
var fileIndex = localStorage["file.current"];
localStorage[fileIndex + ".content"] = content;
synchronizer.addFile(fileIndex);
save = false;
}
};
// Remove a synchronization location associated to the file
fileManager.removeSync = function(sync) {
var fileIndexSync = localStorage["file.current"] + ".sync";
localStorage[fileIndexSync] = localStorage[fileIndexSync].replace(";"
+ sync + ";", ";");
2013-03-31 01:33:31 +00:00
localStorage.removeItem(sync + ".etag");
2013-03-31 01:00:03 +00:00
};
function uploadGdrive() {
$(".file-sync-indicator").removeClass("hide");
var fileIndex = localStorage["file.current"];
var content = localStorage[fileIndex + ".content"];
var title = localStorage[fileIndex + ".title"];
gdrive.createFile(title, content, function(fileSyncIndex) {
if (fileSyncIndex) {
localStorage[fileIndex + ".sync"] += fileSyncIndex + ";";
updateFileTitleUI();
showMessage('The file "' + title
+ '" will now be synchronized on Google Drive.');
} else {
showError("Error while creating file on Google Drive.");
}
});
}
function updateFileDescList() {
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-31 01:00:03 +00:00
fileDescList.push({ index : fileIndex, title : title });
2013-03-25 12:03:22 +00:00
}
2013-03-31 01:00:03 +00:00
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-31 01:00:03 +00:00
}
;
function updateFileTitleUI() {
function composeTitle(fileIndex) {
var result = localStorage[fileIndex + ".title"];
var sync = localStorage[fileIndex + ".sync"];
if (sync.indexOf(SYNC_PROVIDER_GDRIVE) !== -1) {
result = '<i class="icon-gdrive"></i> ' + result;
}
return result;
}
2013-03-26 23:32:36 +00:00
// 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"];
document.title = "StackEdit - " + title;
2013-03-31 01:00:03 +00:00
$("#file-title").html(composeTitle(fileIndex));
$(".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
2013-03-31 01:00:03 +00:00
for ( var i = 0; i < fileDescList.length; i++) {
var fileDesc = fileDescList[i];
var a = $("<a>").html(composeTitle(fileDesc.index));
var li = $("<li>").append(a);
2013-03-26 23:32:36 +00:00
if (fileDesc.index == fileIndex) {
li.addClass("disabled");
2013-03-26 23:32:36 +00:00
} else {
2013-03-29 19:37:52 +00:00
a.prop("href", "#").click((function(fileIndex) {
return function() {
localStorage["file.current"] = fileIndex;
fileManager.selectFile();
};
})(fileDesc.index));
}
$("#file-selector").append(li);
2013-03-24 23:21:55 +00:00
}
2013-03-31 01:00:03 +00:00
}
;
2013-03-24 14:54:26 +00:00
2013-03-31 01:00:03 +00:00
function refreshManageSync() {
var fileIndex = localStorage["file.current"];
var fileSyncIndexList = localStorage[fileIndex + ".sync"].split(";");
$(".msg-no-sync, .msg-sync-list").addClass("hide");
$("#manage-sync-list .input-append").remove();
if (fileSyncIndexList.length > 2) {
$(".msg-sync-list").removeClass("hide");
} else {
$(".msg-no-sync").removeClass("hide");
2013-03-28 22:00:11 +00:00
}
2013-03-31 01:00:03 +00:00
for ( var i = 1; i < fileSyncIndexList.length - 1; i++) {
var sync = fileSyncIndexList[i];
(function(sync) {
var line = $("<div>").addClass("input-append");
if (sync.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
line.append($("<input>").prop("type", "text").prop(
"disabled", true).addClass("span5").val(
"Google Drive, FileID="
+ sync.substring(SYNC_PROVIDER_GDRIVE.length)));
line.append($("<a>").addClass("btn").html(
'<i class="icon-trash"></i>').prop("title", "Remove this synchronized location").click(function() {
fileManager.removeSync(sync);
updateFileTitleUI();
refreshManageSync();
}));
}
$("#manage-sync-list").append(line);
})(sync);
}
}
2013-03-24 19:42:15 +00:00
return fileManager;
})(jQuery);
2013-03-27 15:28:31 +00:00
var core = (function($) {
var core = {};
2013-03-29 19:37:52 +00:00
2013-03-27 15:28:31 +00:00
core.init = function() {
this.loadSettings();
this.createLayout();
2013-03-29 19:37:52 +00:00
2013-03-28 22:00:11 +00:00
$(".action-load-settings").click(function() {
core.loadSettings();
});
2013-03-29 19:37:52 +00:00
2013-03-27 15:28:31 +00:00
$(".action-apply-settings").click(function() {
core.saveSettings();
fileManager.saveFile();
location.reload();
});
};
2013-03-29 19:37:52 +00:00
var settings = { layoutOrientation : "horizontal" };
2013-03-27 15:28:31 +00:00
core.loadSettings = function() {
2013-03-29 19:37:52 +00:00
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
}
2013-03-29 19:37:52 +00:00
2013-03-27 15:28:31 +00:00
// Layout orientation
2013-03-29 19:37:52 +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() {
2013-03-29 19:37:52 +00:00
2013-03-27 15:28:31 +00:00
// Layout orientation
2013-03-29 19:37:52 +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-27 15:28:31 +00:00
core.createLayout = function() {
var layout = undefined;
var layoutGlobalConfig = { closable : true, resizable : false,
2013-03-30 11:56:17 +00:00
slidable : false, livePaneResizing : true,
enableCursorHotkey : false, spacing_open : 15, spacing_closed : 15,
togglerLength_open : 90, togglerLength_closed : 90,
center__minWidth : 100, center__minHeight : 100,
stateManagement__enabled : false, };
2013-03-27 15:28:31 +00:00
if (settings.layoutOrientation == "horizontal") {
2013-03-29 19:37:52 +00:00
$(".ui-layout-south").remove();
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(
2013-03-29 19:37:52 +00:00
$.extend(layoutGlobalConfig, { east__resizable : true,
east__size : .5, east__minSize : 200 }));
2013-03-27 15:28:31 +00:00
} 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,
2013-03-29 19:37:52 +00:00
south__size : .5, south__minSize : 200 }));
2013-03-27 15:28:31 +00:00
}
$(".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');
});
};
2013-03-28 23:35:32 +00:00
core.createEditor = function(onTextChange) {
2013-03-27 15:28:31 +00:00
$("#wmd-button-bar").empty();
var converter = Markdown.getSanitizingConverter();
2013-03-28 23:35:32 +00:00
var firstChange = true;
2013-03-29 19:37:52 +00:00
converter.hooks.chain("preConversion", function(text) {
if (!firstChange) {
2013-03-28 23:35:32 +00:00
onTextChange();
}
2013-03-29 19:37:52 +00:00
return text;
});
2013-03-27 15:28:31 +00:00
var editor = new Markdown.Editor(converter);
editor.run();
2013-03-28 23:35:32 +00:00
firstChange = false;
2013-03-27 15:28:31 +00:00
$(".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"));
};
2013-03-29 19:37:52 +00:00
2013-03-27 15:28:31 +00:00
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-30 11:56:17 +00:00
$.jGrowl.defaults.closer = false;
$.jGrowl.defaults.closeTemplate = '';
$.jGrowl.defaults.position = 'bottom-right';
2013-03-27 15:28:31 +00:00
core.init();
2013-03-31 01:00:03 +00:00
2013-03-30 11:56:17 +00:00
// listen to online/offline events
$(window).on('offline', onOffline);
$(window).on('online', onOnline);
if (navigator.onLine === false) {
onOffline();
}
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-24 19:42:15 +00:00
});
2013-03-24 14:54:26 +00:00
})(jQuery);