Stackedit/js/main.js

467 lines
14 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-04-01 01:06:52 +00:00
var AJAX_TIMEOUT = 5000;
var CHECK_ONLINE_PERIOD = 60000;
2013-03-30 11:56:17 +00:00
var offline = false;
var offlineTime = currentTime;
function onOffline() {
offlineTime = currentTime;
2013-04-01 16:46:48 +00:00
if(offline === false) {
offline = true;
2013-03-30 11:56:17 +00:00
showMessage("You are offline.", "icon-exclamation-sign msg-offline", {
sticky : true, close : function() {
showMessage("You are back online!", "icon-signal");
} });
2013-04-01 16:46:48 +00:00
}
2013-03-30 11:56:17 +00:00
}
function onOnline() {
$(".msg-offline").parents(".jGrowl-notification").trigger(
'jGrowl.beforeClose');
2013-04-01 16:46:48 +00:00
offline = false;
2013-03-30 11:56:17 +00:00
}
2013-04-01 01:06:52 +00:00
function checkOnline() {
2013-03-30 11:56:17 +00:00
// Try to reconnect if we are offline but we have some network
if (offline === true && navigator.onLine === true
2013-04-01 01:06:52 +00:00
&& offlineTime + CHECK_ONLINE_PERIOD < currentTime) {
2013-03-30 11:56:17 +00:00
offlineTime = currentTime;
// Try to download anything to test the connection
$.ajax(
2013-04-01 01:06:52 +00:00
{ url : "https://apis.google.com/js/client.js",
timeout : AJAX_TIMEOUT, dataType : "script" }).done(function() {
2013-03-30 11:56:17 +00:00
onOnline();
});
2013-03-28 22:00:11 +00:00
}
}
2013-04-01 16:46:48 +00:00
var DEFAULT_FILE_TITLE = "Filename";
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
2013-04-01 01:06:52 +00:00
// Do periodic stuff
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-04-01 16:46:48 +00:00
synchronizer.sync();
2013-03-30 11:56:17 +00:00
asyncTaskRunner.runTask();
2013-04-01 01:06:52 +00:00
checkOnline();
2013-03-29 19:37:52 +00:00
}, 1000);
$(".action-create-file").click(function() {
2013-03-24 23:21:55 +00:00
fileManager.saveFile();
2013-04-01 16:46:48 +00:00
var fileIndex = fileManager.createFile();
fileManager.selectFile(fileIndex);
2013-04-01 01:06:52 +00:00
$("#file-title").click();
2013-03-24 23:21:55 +00:00
});
$(".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;
2013-04-01 01:06:52 +00:00
fileManager.updateFileTitles();
2013-03-31 01:00:03 +00:00
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-04-01 16:46:48 +00:00
$(".action-upload-dropbox").click(function() {
showMessage("Sorry, Dropbox synchronization is not yet available.");
});
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-04-01 16:46:48 +00:00
fileManager.selectFile = function(fileIndex) {
2013-03-24 23:21:55 +00:00
// 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
}
// If no file create one
2013-04-01 01:06:52 +00:00
if (localStorage["file.list"].length === 1) {
2013-04-01 16:46:48 +00:00
fileIndex = this.createFile();
}
fileIndex = fileIndex || localStorage["file.current"];
if(fileIndex !== undefined) {
localStorage["file.current"] = fileIndex;
2013-03-24 23:21:55 +00:00
}
2013-04-01 16:46:48 +00:00
2013-04-01 01:06:52 +00:00
// Update the file titles
this.updateFileTitles();
// Update the editor
2013-03-24 23:21:55 +00:00
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-24 14:54:26 +00:00
};
2013-04-01 16:46:48 +00:00
fileManager.createFile = function(title, content) {
content = content || "";
2013-03-26 23:32:36 +00:00
if (!title) {
2013-04-01 16:46:48 +00:00
// Create a file title
title = DEFAULT_FILE_TITLE;
function exists(title) {
for ( var i = 0; i < fileDescList.length; i++) {
if(fileDescList[i].title == title) {
return true;
}
}
}
var indicator = 2;
while(exists(title)) {
title = DEFAULT_FILE_TITLE + indicator++;
}
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-04-01 16:46:48 +00:00
localStorage[fileIndex + ".content"] = 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-04-01 16:46:48 +00:00
return fileIndex;
};
2013-03-26 23:32:36 +00:00
fileManager.deleteFile = function() {
var fileIndex = localStorage["file.current"];
2013-04-01 01:06:52 +00:00
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++) {
2013-04-01 01:06:52 +00:00
var fileSyncIndex = fileSyncIndexList[i];
fileManager.removeSync(fileSyncIndex);
}
2013-03-31 01:33:31 +00:00
localStorage.removeItem(fileIndex + ".sync");
localStorage.removeItem("file.current");
2013-03-29 19:37:52 +00:00
localStorage["file.list"] = localStorage["file.list"].replace(";"
2013-04-01 01:06:52 +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;
2013-04-01 16:46:48 +00:00
synchronizer.addFileForUpload(fileIndex);
2013-03-31 01:00:03 +00:00
save = false;
}
};
2013-04-01 01:06:52 +00:00
fileManager.updateFileTitles = function() {
2013-03-31 01:00:03 +00:00
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
2013-04-01 16:46:48 +00:00
var fileIndex = localStorage["file.current"];
2013-04-01 01:06:52 +00:00
// If no default file take first one
2013-04-01 16:46:48 +00:00
if (!fileIndex) {
fileIndex = fileDescList[0].index;
localStorage["file.current"] = fileIndex;
2013-04-01 01:06:52 +00:00
}
syncGoogleDrive = false;
2013-03-31 01:00:03 +00:00
function composeTitle(fileIndex) {
var result = localStorage[fileIndex + ".title"];
var sync = localStorage[fileIndex + ".sync"];
2013-04-01 01:06:52 +00:00
if (sync.indexOf(";" + SYNC_PROVIDER_GDRIVE) !== -1) {
syncGoogleDrive = true;
2013-03-31 01:00:03 +00:00
result = '<i class="icon-gdrive"></i> ' + result;
}
return result;
}
2013-03-26 23:32:36 +00:00
2013-04-01 16:46:48 +00:00
// Update the file title
2013-03-25 12:03:22 +00:00
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);
2013-04-01 16:46:48 +00:00
// Update the file selector
2013-03-25 12:03:22 +00:00
$("#file-selector").empty();
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-04-01 01:06:52 +00:00
};
// Remove a synchronized location
fileManager.removeSync = function(fileSyncIndex) {
var fileIndexList = localStorage["file.list"].split(";");
// Look for local files associated to this synchronized location
for ( var i = 1; i < fileIndexList.length - 1; i++) {
var fileIndexSync = fileIndexList[i] + ".sync";
localStorage[fileIndexSync] = localStorage[fileIndexSync].replace(";"
+ fileSyncIndex + ";", ";");
}
// Remove etag
localStorage.removeItem(fileSyncIndex + ".etag");
};
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 + ";";
fileManager.updateFileTitles();
showMessage('The file "' + title
+ '" will now be synchronized on Google Drive.');
} else {
showError("Error while creating file on Google Drive.");
}
});
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++) {
2013-04-01 01:06:52 +00:00
var fileSyncIndex = fileSyncIndexList[i];
(function(fileSyncIndex) {
2013-03-31 01:00:03 +00:00
var line = $("<div>").addClass("input-append");
2013-04-01 01:06:52 +00:00
if (fileSyncIndex.indexOf(SYNC_PROVIDER_GDRIVE) === 0) {
2013-03-31 01:00:03 +00:00
line.append($("<input>").prop("type", "text").prop(
"disabled", true).addClass("span5").val(
"Google Drive, FileID="
2013-04-01 01:06:52 +00:00
+ fileSyncIndex.substring(SYNC_PROVIDER_GDRIVE.length)));
2013-03-31 01:00:03 +00:00
line.append($("<a>").addClass("btn").html(
2013-04-01 01:06:52 +00:00
'<i class="icon-trash"></i>').prop("title",
"Remove this synchronized location").click(function() {
fileManager.removeSync(fileSyncIndex);
fileManager.updateFileTitles();
2013-03-31 01:00:03 +00:00
refreshManageSync();
}));
}
$("#manage-sync-list").append(line);
2013-04-01 01:06:52 +00:00
})(fileSyncIndex);
2013-03-31 01:00:03 +00:00
}
}
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-04-01 16:46:48 +00:00
// jGrowl configuration
2013-04-01 01:06:52 +00:00
$.jGrowl.defaults.life = 5000;
2013-03-30 11:56:17 +00:00
$.jGrowl.defaults.closer = false;
$.jGrowl.defaults.closeTemplate = '';
$.jGrowl.defaults.position = 'bottom-right';
2013-04-01 16:46:48 +00:00
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);