Fixed title editing issue on IE

This commit is contained in:
benweet 2014-09-06 01:09:35 +01:00
parent d33037e18d
commit f3b32e8d03

View File

@ -1,209 +1,219 @@
define([ define([
"jquery", "jquery",
"underscore", "underscore",
"constants", "constants",
"core", "core",
"utils", "utils",
"storage", "storage",
"settings", "settings",
"eventMgr", "eventMgr",
"fileSystem", "fileSystem",
"classes/FileDescriptor", "classes/FileDescriptor",
"text!WELCOME.md" "text!WELCOME.md"
], function($, _, constants, core, utils, storage, settings, eventMgr, fileSystem, FileDescriptor, welcomeContent) { ], function($, _, constants, core, utils, storage, settings, eventMgr, fileSystem, FileDescriptor, welcomeContent) {
var fileMgr = {}; var fileMgr = {};
// Defines the current file // Defines the current file
fileMgr.currentFile = undefined; fileMgr.currentFile = undefined;
// Set the current file and refresh the editor // Set the current file and refresh the editor
fileMgr.selectFile = function(fileDesc) { fileMgr.selectFile = function(fileDesc) {
fileDesc = fileDesc || fileMgr.currentFile; fileDesc = fileDesc || fileMgr.currentFile;
if(fileDesc === undefined) { if(fileDesc === undefined) {
var fileSystemSize = _.size(fileSystem); var fileSystemSize = _.size(fileSystem);
if(fileSystemSize === 0) { if(fileSystemSize === 0) {
// If fileSystem empty create one file // If fileSystem empty create one file
fileDesc = fileMgr.createFile(constants.WELCOME_DOCUMENT_TITLE, welcomeContent); fileDesc = fileMgr.createFile(constants.WELCOME_DOCUMENT_TITLE, welcomeContent);
} }
else { else {
// Select the last selected file // Select the last selected file
fileDesc = _.max(fileSystem, function(fileDesc) { fileDesc = _.max(fileSystem, function(fileDesc) {
return fileDesc.selectTime || 0; return fileDesc.selectTime || 0;
}); });
} }
} }
if(fileMgr.currentFile !== fileDesc) { if(fileMgr.currentFile !== fileDesc) {
fileMgr.currentFile = fileDesc; fileMgr.currentFile = fileDesc;
fileDesc.selectTime = new Date().getTime(); fileDesc.selectTime = new Date().getTime();
// Notify extensions // Notify extensions
eventMgr.onFileSelected(fileDesc); eventMgr.onFileSelected(fileDesc);
// Hide the viewer pencil button // Hide the viewer pencil button
$(".action-edit-document").toggleClass("hide", fileDesc.fileIndex != constants.TEMPORARY_FILE_INDEX); $(".action-edit-document").toggleClass("hide", fileDesc.fileIndex != constants.TEMPORARY_FILE_INDEX);
} }
// Refresh the editor (even if it's the same file) // Refresh the editor (even if it's the same file)
core.initEditor(fileDesc); core.initEditor(fileDesc);
}; };
fileMgr.createFile = function(title, content, discussionListJSON, syncLocations, isTemporary) { fileMgr.createFile = function(title, content, discussionListJSON, syncLocations, isTemporary) {
content = content !== undefined ? content : settings.defaultContent; content = content !== undefined ? content : settings.defaultContent;
if(!title) { if(!title) {
// Create a file title // Create a file title
title = constants.DEFAULT_FILE_TITLE; title = constants.DEFAULT_FILE_TITLE;
var indicator = 2; var indicator = 2;
var checkTitle = function (fileDesc) { var checkTitle = function(fileDesc) {
return fileDesc.title == title; return fileDesc.title == title;
}; };
while (_.some(fileSystem, checkTitle)) { while(_.some(fileSystem, checkTitle)) {
title = constants.DEFAULT_FILE_TITLE + indicator++; title = constants.DEFAULT_FILE_TITLE + indicator++;
} }
} }
// Generate a unique fileIndex // Generate a unique fileIndex
var fileIndex = constants.TEMPORARY_FILE_INDEX; var fileIndex = constants.TEMPORARY_FILE_INDEX;
if(!isTemporary) { if(!isTemporary) {
do { do {
fileIndex = "file." + utils.randomString(); fileIndex = "file." + utils.randomString();
} while (_.has(fileSystem, fileIndex)); } while(_.has(fileSystem, fileIndex));
} }
// syncIndex associations // syncIndex associations
syncLocations = syncLocations || {}; syncLocations = syncLocations || {};
var sync = _.reduce(syncLocations, function(sync, syncAttributes) { var sync = _.reduce(syncLocations, function(sync, syncAttributes) {
utils.storeAttributes(syncAttributes); utils.storeAttributes(syncAttributes);
return sync + syncAttributes.syncIndex + ";"; return sync + syncAttributes.syncIndex + ";";
}, ";"); }, ";");
storage[fileIndex + ".title"] = title; storage[fileIndex + ".title"] = title;
storage[fileIndex + ".content"] = content; storage[fileIndex + ".content"] = content;
storage[fileIndex + ".sync"] = sync; storage[fileIndex + ".sync"] = sync;
storage[fileIndex + ".publish"] = ";"; storage[fileIndex + ".publish"] = ";";
// Create the file descriptor // Create the file descriptor
var fileDesc = new FileDescriptor(fileIndex, title, syncLocations); var fileDesc = new FileDescriptor(fileIndex, title, syncLocations);
discussionListJSON && (fileDesc.discussionListJSON = discussionListJSON); discussionListJSON && (fileDesc.discussionListJSON = discussionListJSON);
// Add the index to the file list // Add the index to the file list
if(!isTemporary) { if(!isTemporary) {
utils.appendIndexToArray("file.list", fileIndex); utils.appendIndexToArray("file.list", fileIndex);
fileSystem[fileIndex] = fileDesc; fileSystem[fileIndex] = fileDesc;
eventMgr.onFileCreated(fileDesc); eventMgr.onFileCreated(fileDesc);
} }
return fileDesc; return fileDesc;
}; };
fileMgr.deleteFile = function(fileDesc) { fileMgr.deleteFile = function(fileDesc) {
fileDesc = fileDesc || fileMgr.currentFile; fileDesc = fileDesc || fileMgr.currentFile;
// Unassociate file from folder // Unassociate file from folder
if(fileDesc.folder) { if(fileDesc.folder) {
fileDesc.folder.removeFile(fileDesc); fileDesc.folder.removeFile(fileDesc);
eventMgr.onFoldersChanged(); eventMgr.onFoldersChanged();
} }
// Remove the index from the file list // Remove the index from the file list
utils.removeIndexFromArray("file.list", fileDesc.fileIndex); utils.removeIndexFromArray("file.list", fileDesc.fileIndex);
delete fileSystem[fileDesc.fileIndex]; delete fileSystem[fileDesc.fileIndex];
// Don't bother with fields in localStorage, they will be removed on next page load // Don't bother with fields in localStorage, they will be removed on next page load
if(fileMgr.currentFile === fileDesc) { if(fileMgr.currentFile === fileDesc) {
// Unset the current fileDesc // Unset the current fileDesc
fileMgr.currentFile = undefined; fileMgr.currentFile = undefined;
// Refresh the editor with another file // Refresh the editor with another file
fileMgr.selectFile(); fileMgr.selectFile();
} }
eventMgr.onFileDeleted(fileDesc); eventMgr.onFileDeleted(fileDesc);
}; };
// Get the file descriptor associated to a syncIndex // Get the file descriptor associated to a syncIndex
fileMgr.getFileFromSyncIndex = function(syncIndex) { fileMgr.getFileFromSyncIndex = function(syncIndex) {
return _.find(fileSystem, function(fileDesc) { return _.find(fileSystem, function(fileDesc) {
return _.has(fileDesc.syncLocations, syncIndex); return _.has(fileDesc.syncLocations, syncIndex);
}); });
}; };
// Get the file descriptor associated to a publishIndex // Get the file descriptor associated to a publishIndex
fileMgr.getFileFromPublishIndex = function(publishIndex) { fileMgr.getFileFromPublishIndex = function(publishIndex) {
return _.find(fileSystem, function(fileDesc) { return _.find(fileSystem, function(fileDesc) {
return _.has(fileDesc.publishLocations, publishIndex); return _.has(fileDesc.publishLocations, publishIndex);
}); });
}; };
eventMgr.addListener("onReady", function() { eventMgr.addListener("onReady", function() {
var $editorElt = $("#wmd-input"); var $editorElt = $("#wmd-input");
fileMgr.selectFile(); fileMgr.selectFile();
var $fileTitleElt = $('.file-title-navbar'); var $fileTitleElt = $('.file-title-navbar');
var $fileTitleInputElt = $(".input-file-title"); var $fileTitleInputElt = $(".input-file-title");
$(".action-create-file").click(function() { $(".action-create-file").click(function() {
setTimeout(function() { setTimeout(function() {
var fileDesc = fileMgr.createFile(); var fileDesc = fileMgr.createFile();
fileMgr.selectFile(fileDesc); fileMgr.selectFile(fileDesc);
$fileTitleElt.click(); $fileTitleElt.click();
}, 400); }, 400);
}); });
$('.action-remove-file-confirm').click(function() { $('.action-remove-file-confirm').click(function() {
$('.modal-remove-file-confirm').modal('show'); $('.modal-remove-file-confirm').modal('show');
}); });
$(".action-remove-file").click(function() { $(".action-remove-file").click(function() {
fileMgr.deleteFile(); fileMgr.deleteFile();
}); });
$fileTitleElt.click(function() { var titleEditing;
if(window.viewerMode === true) { $fileTitleElt.click(function() {
return; if(window.viewerMode === true) {
} return;
$fileTitleElt.addClass('hide'); }
var fileTitleInput = $fileTitleInputElt.removeClass('hide'); $fileTitleElt.addClass('hide');
setTimeout(function() { var fileTitleInput = $fileTitleInputElt.removeClass('hide');
fileTitleInput.focus().get(0).select(); titleEditing = true;
}, 10); setTimeout(function() {
}); fileTitleInput.focus().get(0).select();
function applyTitle() { }, 10);
$fileTitleInputElt.addClass('hide'); });
$fileTitleElt.removeClass('hide'); function applyTitle() {
var title = $.trim($fileTitleInputElt.val()); if(!titleEditing) {
var fileDesc = fileMgr.currentFile; return;
if(title && title != fileDesc.title) { }
fileDesc.title = title; $fileTitleInputElt.addClass('hide');
eventMgr.onTitleChanged(fileDesc); $fileTitleElt.removeClass('hide');
} var title = $.trim($fileTitleInputElt.val());
$fileTitleInputElt.val(fileDesc.title); var fileDesc = fileMgr.currentFile;
$editorElt.focus(); if(title && title != fileDesc.title) {
} fileDesc.title = title;
$fileTitleInputElt.blur(function() { eventMgr.onTitleChanged(fileDesc);
applyTitle(); }
}).keyup(function(e) { $fileTitleInputElt.val(fileDesc.title);
if(e.keyCode == 13) { $editorElt.focus();
applyTitle(); titleEditing = false;
} }
if(e.keyCode == 27) {
$fileTitleInputElt.val("");
applyTitle();
}
});
$(".action-open-stackedit").click(function() {
window.location.href = "editor";
});
$(".action-edit-document").click(function() {
var content = $editorElt.val();
var title = fileMgr.currentFile.title;
var fileDesc = fileMgr.createFile(title, content);
fileMgr.selectFile(fileDesc);
window.location.href = "editor";
});
$(".action-welcome-file").click(function() {
var fileDesc = fileMgr.createFile(constants.WELCOME_DOCUMENT_TITLE, welcomeContent);
fileMgr.selectFile(fileDesc);
});
});
eventMgr.onFileMgrCreated(fileMgr); $fileTitleInputElt.blur(function() {
return fileMgr; setTimeout(function() {
applyTitle();
}, 0);
}).keypress(function(e) {
if(e.keyCode == 13) {
applyTitle();
e.preventDefault();
}
if(e.keyCode == 27) {
$fileTitleInputElt.val("");
applyTitle();
}
});
$(".action-open-stackedit").click(function() {
window.location.href = "editor";
});
$(".action-edit-document").click(function() {
var content = $editorElt.val();
var title = fileMgr.currentFile.title;
var fileDesc = fileMgr.createFile(title, content);
fileMgr.selectFile(fileDesc);
window.location.href = "editor";
});
$(".action-welcome-file").click(function() {
var fileDesc = fileMgr.createFile(constants.WELCOME_DOCUMENT_TITLE, welcomeContent);
fileMgr.selectFile(fileDesc);
});
});
eventMgr.onFileMgrCreated(fileMgr);
return fileMgr;
}); });