New extension pattern

This commit is contained in:
benweet 2013-05-27 00:38:13 +01:00
parent 1f0fea1428
commit 56fa0c5873
7 changed files with 85 additions and 88 deletions

View File

@ -68,7 +68,7 @@ define(["core", "utils", "extension-manager", "dropbox-helper"], function(core,
var importPaths = [];
_.each(paths, function(path) {
var syncIndex = createSyncIndex(path);
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
core.showError('"' + fileDesc.title + '" was already imported');
return;
@ -87,7 +87,7 @@ define(["core", "utils", "extension-manager", "dropbox-helper"], function(core,
}
// Check that file is not synchronized with an other one
var syncIndex = createSyncIndex(path);
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
var existingTitle = fileDesc.title;
core.showError('File path is already synchronized with "' + existingTitle + '"');
@ -101,7 +101,7 @@ define(["core", "utils", "extension-manager", "dropbox-helper"], function(core,
}
var syncAttributes = createSyncAttributes(result.path, result.versionTag, content);
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
callback(undefined, syncIndex, syncAttributes);
callback(undefined, syncAttributes);
});
}
@ -168,7 +168,7 @@ define(["core", "utils", "extension-manager", "dropbox-helper"], function(core,
_.each(changes, function(change) {
var syncAttributes = change.syncAttributes;
var syncIndex = syncAttributes.syncIndex;
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
// No file corresponding (file may have been deleted locally)
if(fileDesc === undefined) {
return;
@ -177,10 +177,10 @@ define(["core", "utils", "extension-manager", "dropbox-helper"], function(core,
// File deleted
if (change.wasRemoved === true) {
core.showError('"' + localTitle + '" has been removed from Dropbox.');
core.fileManager.removeSync(syncIndex);
core.fileManager.removeSync(syncAttributes);
return;
}
var localContent = localStorage[fileDesc.index + ".content"];
var localContent = localStorage[fileDesc.fileIndex + ".content"];
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
var file = change.stat;
var remoteContentCRC = utils.crc32(file.content);
@ -194,7 +194,7 @@ define(["core", "utils", "extension-manager", "dropbox-helper"], function(core,
}
// If file content changed
if(fileContentChanged && remoteContentChanged === true) {
localStorage[fileDesc.index + ".content"] = file.content;
localStorage[fileDesc.fileIndex + ".content"] = file.content;
core.showMessage('"' + localTitle + '" has been updated from Dropbox.');
if(core.fileManager.isCurrentFile(fileDesc)) {
updateFileTitles = false; // Done by next function

View File

@ -36,7 +36,7 @@ define( [ "jquery", "underscore" ], function($) {
$("#file-selector li:not(.stick)").empty();
_.each(sortedDescriptor, function(fileDescToPrint) {
var a = $("<a>").html(composeTitle(fileDescToPrint.index));
var a = $("<a>").html(composeTitle(fileDescToPrint.fileIndex));
var li = $("<li>").append(a);
if (fileDescToPrint === fileDesc) {
li.addClass("disabled");

View File

@ -46,7 +46,7 @@ define( [ "jquery", "underscore" ], function($) {
publishDesc: publishDesc
}));
lineElement.append($(removeButtonTemplate).click(function() {
fileManager.removePublish(publishIndex);
fileManager.removePublish(publishAttributes);
}));
publishList.append(lineElement);
});

View File

@ -18,7 +18,7 @@ define([
.reduce(function(fileSystemDescriptor, fileIndex) {
var title = localStorage[fileIndex + ".title"];
var fileDesc = {
index : fileIndex,
fileIndex : fileIndex,
title : title,
syncLocations: {},
publishLocations: {}
@ -52,8 +52,8 @@ define([
if(fileDesc === undefined) {
localStorage.removeItem("file.current");
}
else if(fileDesc.index != TEMPORARY_FILE_INDEX) {
localStorage["file.current"] = fileDesc.index;
else if(fileDesc.fileIndex != TEMPORARY_FILE_INDEX) {
localStorage["file.current"] = fileDesc.fileIndex;
}
};
@ -78,7 +78,7 @@ define([
extensionManager.onFileSelected(fileDesc);
// Hide the viewer pencil button
if(fileDesc.index == TEMPORARY_FILE_INDEX) {
if(fileDesc.fileIndex == TEMPORARY_FILE_INDEX) {
$(".action-edit-document").removeClass("hide");
}
else {
@ -86,7 +86,7 @@ define([
}
// Recreate the editor
$("#wmd-input").val(localStorage[fileDesc.index + ".content"]);
$("#wmd-input").val(localStorage[fileDesc.fileIndex + ".content"]);
core.createEditor(function() {
// Callback to save content when textarea changes
fileManager.saveFile();
@ -128,7 +128,7 @@ define([
// Create the file descriptor
var fileDesc = {
index : fileIndex,
fileIndex : fileIndex,
title : title,
syncLocations: syncLocations,
publishLocations: {}
@ -151,17 +151,17 @@ define([
}
// Remove synchronized locations
_.each(fileDesc.syncLocations, function(syncAttributes, syncIndex) {
fileManager.removeSync(syncIndex, true);
_.each(fileDesc.syncLocations, function(syncAttributes) {
fileManager.removeSync(syncAttributes, true);
});
// Remove publish locations
_.each(fileDesc.publishLocations, function(publishAttributes, publishIndex) {
fileManager.removePublish(publishIndex);
_.each(fileDesc.publishLocations, function(publishAttributes) {
fileManager.removePublish(publishAttributes, true);
});
// Remove the index from the file list
var fileIndex = fileDesc.index;
var fileIndex = fileDesc.fileIndex;
localStorage["file.list"] = localStorage["file.list"].replace(";"
+ fileIndex + ";", ";");
localStorage.removeItem(fileIndex + ".title");
@ -176,29 +176,28 @@ define([
fileManager.saveFile = function() {
var content = $("#wmd-input").val();
var fileDesc = fileManager.getCurrentFile();
localStorage[fileDesc.index + ".content"] = content;
localStorage[fileDesc.fileIndex + ".content"] = content;
extensionManager.onFileChanged(fileDesc);
synchronizer.notifyChange(fileDesc);
};
// Add a syncIndex (synchronized location) to a file
fileManager.addSync = function(fileDesc, syncIndex, syncAttributes) {
localStorage[fileDesc.index + ".sync"] += syncIndex + ";";
fileDesc.syncLocations[syncIndex] = syncAttributes;
// Add a synchronized location to a file
fileManager.addSync = function(fileDesc, syncAttributes) {
localStorage[fileDesc.fileIndex + ".sync"] += syncAttributes.syncIndex + ";";
fileDesc.syncLocations[syncAttributes.syncIndex] = syncAttributes;
// addSync is only used for export, not for import
extensionManager.onSyncExportSuccess(fileDesc, syncIndex, syncAttributes);
extensionManager.onSyncExportSuccess(fileDesc, syncAttributes);
};
// Remove a syncIndex (synchronized location)
fileManager.removeSync = function(syncIndex, skipExtensions) {
var fileDesc = fileManager.getFileFromSync(syncIndex);
// Remove a synchronized location
fileManager.removeSync = function(syncAttributes, skipExtensions) {
var fileDesc = fileManager.getFileFromSyncIndex(syncAttributes.syncIndex);
if(fileDesc !== undefined) {
localStorage[fileDesc.index + ".sync"] = localStorage[fileDesc.index + ".sync"].replace(";"
+ syncIndex + ";", ";");
localStorage[fileDesc.fileIndex + ".sync"] = localStorage[fileDesc.fileIndex + ".sync"].replace(";"
+ syncAttributes.syncIndex + ";", ";");
}
// Remove sync attributes
localStorage.removeItem(syncIndex);
var syncAttributes = fileDesc.syncLocations[syncIndex];
localStorage.removeItem(syncAttributes.syncIndex);
fileDesc.syncLocations.removeItem(syncIndex);
if(!skipExtensions) {
extensionManager.onSyncRemoved(fileDesc, syncAttributes);
@ -206,7 +205,7 @@ define([
};
// Get the file descriptor associated to a syncIndex
fileManager.getFileFromSync = function(syncIndex) {
fileManager.getFileFromSyncIndex = function(syncIndex) {
return _.find(fileSystemDescriptor, function(fileDesc) {
return _.has(fileDesc.syncLocations, syncIndex);
});
@ -214,7 +213,7 @@ define([
// Get syncAttributes from syncIndex
fileManager.getSyncAttributes = function(syncIndex) {
var fileDesc = fileManager.getFileFromSync(syncIndex);
var fileDesc = fileManager.getFileFromSyncIndex(syncIndex);
return fileDesc && fileDesc.syncLocations[syncIndex];
};
@ -228,25 +227,24 @@ define([
};
// Add a publishIndex (publish location) to a file
fileManager.addPublish = function(fileDesc, publishIndex, publishAttributes) {
localStorage[fileDesc.index + ".publish"] += publishIndex + ";";
fileDesc.publishLocations[publishIndex] = publishAttributes;
extensionManager.onNewPublishSuccess(fileDesc, publishIndex, publishAttributes);
fileManager.addPublish = function(fileDesc, publishAttributes) {
localStorage[fileDesc.fileIndex + ".publish"] += publishAttributes.publishIndex + ";";
fileDesc.publishLocations[publishAttributes.publishIndex] = publishAttributes;
extensionManager.onNewPublishSuccess(fileDesc, publishAttributes);
};
// Remove a publishIndex (publish location)
fileManager.removePublish = function(publishIndex, skipExtensions) {
var fileDesc = fileManager.getFileFromPublish(publishIndex);
fileManager.removePublish = function(publishAttributes, skipExtensions) {
var fileDesc = fileManager.getFileFromPublish(publishAttributes.publishIndex);
if(fileDesc !== undefined) {
localStorage[fileDesc.index + ".publish"] = localStorage[fileDesc.index + ".publish"].replace(";"
+ publishIndex + ";", ";");
localStorage[fileDesc.fileIndex + ".publish"] = localStorage[fileDesc.fileIndex + ".publish"].replace(";"
+ publishAttributes.publishIndex + ";", ";");
if(fileManager.isCurrentFile(fileDesc)) {
publisher.notifyPublish();
}
}
// Remove publish attributes
localStorage.removeItem(publishIndex);
var publishAttributes = fileDesc.publishLocations[publishIndex];
localStorage.removeItem(publishAttributes.publishIndex);
fileDesc.publishLocations.removeItem(publishIndex);
if(!skipExtensions) {
extensionManager.onPublishRemoved(fileDesc, publishAttributes);
@ -308,7 +306,7 @@ define([
$("#file-title").show();
var title = $.trim(input.val());
var fileDesc = fileManager.getCurrentFile();
var fileIndexTitle = fileDesc.index + ".title";
var fileIndexTitle = fileDesc.fileIndex + ".title";
if (title) {
if (title != localStorage[fileIndexTitle]) {
localStorage[fileIndexTitle] = title;

View File

@ -9,6 +9,10 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
exportPreferencesInputIds: ["gdrive-parentid"]
};
function createSyncIndex(id) {
return "sync." + PROVIDER_GDRIVE + "." + id;
}
function createSyncAttributes(id, etag, content, title) {
var syncAttributes = {};
syncAttributes.provider = gdriveProvider;
@ -16,13 +20,10 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
syncAttributes.etag = etag;
syncAttributes.contentCRC = utils.crc32(content);
syncAttributes.titleCRC = utils.crc32(title);
syncAttributes.syncIndex = createSyncIndex(id);
return syncAttributes;
}
function createSyncIndex(id) {
return "sync." + PROVIDER_GDRIVE + "." + id;
}
function importFilesFromIds(ids) {
googleHelper.downloadMetadata(ids, function(error, result) {
if(error) {
@ -35,10 +36,9 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
var fileDescList = [];
_.each(result, function(file) {
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = utils.serializeAttributes(syncAttributes);
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
var syncLocations = {};
syncLocations[syncIndex] = syncAttributes;
syncLocations[syncAttributes.syncIndex] = syncAttributes;
var fileDesc = core.fileManager.createFile(file.title, file.content, syncLocations);
core.fileManager.selectFile(fileDesc);
fileDescList.push(fileDesc);
@ -56,7 +56,7 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
var importIds = [];
_.each(ids, function(id) {
var syncIndex = createSyncIndex(id);
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
core.showError('"' + fileDesc.title + '" was already imported');
return;
@ -75,9 +75,8 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = utils.serializeAttributes(syncAttributes);
callback(undefined, syncIndex, syncAttributes);
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
callback(undefined, syncAttributes);
});
};
@ -88,7 +87,7 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
}
// Check that file is not synchronized with an other one
var syncIndex = createSyncIndex(id);
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
core.showError('File ID is already synchronized with "' + fileDesc.title + '"');
callback(true);
@ -100,9 +99,8 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
return;
}
var syncAttributes = createSyncAttributes(result.id, result.etag, content, title);
var syncIndex = createSyncIndex(syncAttributes.id);
localStorage[syncIndex] = utils.serializeAttributes(syncAttributes);
callback(undefined, syncIndex, syncAttributes);
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
callback(undefined, syncAttributes);
});
};
@ -140,8 +138,8 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
if(syncAttributes === undefined) {
return;
}
// Store syncIndex to avoid 2 times formating
change.syncIndex = syncIndex;
// Store syncAttributes to avoid 2 times searching
change.syncAttributes = syncAttributes;
// Delete
if(change.deleted === true) {
interestingChanges.push(change);
@ -150,8 +148,6 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
// Modify
if(syncAttributes.etag != change.file.etag) {
interestingChanges.push(change);
// Store syncAttributes to avoid 2 times searching
change.syncAttributes = syncAttributes;
}
});
googleHelper.downloadContent(interestingChanges, function(error, changes) {
@ -161,8 +157,9 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
}
var updateFileTitles = false;
_.each(changes, function(change) {
var syncIndex = change.syncIndex;
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var syncAttributes = change.syncAttributes;
var syncIndex = syncAttributes.syncIndex;
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
// No file corresponding (file may have been deleted locally)
if(fileDesc === undefined) {
return;
@ -171,12 +168,11 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
// File deleted
if (change.deleted === true) {
core.showError('"' + localTitle + '" has been removed from Google Drive.');
core.fileManager.removeSync(syncIndex);
core.fileManager.removeSync(syncAttributes);
return;
}
var syncAttributes = change.syncAttributes;
var localTitleChanged = syncAttributes.titleCRC != utils.crc32(localTitle);
var localContent = localStorage[fileDesc.index + ".content"];
var localContent = localStorage[fileDesc.fileIndex + ".content"];
var localContentChanged = syncAttributes.contentCRC != utils.crc32(localContent);
var file = change.file;
var remoteTitleCRC = utils.crc32(file.title);
@ -194,14 +190,14 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
}
// If file title changed
if(fileTitleChanged && remoteTitleChanged === true) {
localStorage[fileDesc.index + ".title"] = file.title;
localStorage[fileDesc.fileIndex + ".title"] = file.title;
fileDesc.title = file.title;
updateFileTitles = true;
core.showMessage('"' + localTitle + '" has been renamed to "' + file.title + '" on Google Drive.');
}
// If file content changed
if(fileContentChanged && remoteContentChanged === true) {
localStorage[fileDesc.index + ".content"] = file.content;
localStorage[fileDesc.fileIndex + ".content"] = file.content;
core.showMessage('"' + file.title + '" has been updated from Google Drive.');
if(core.fileManager.isCurrentFile(fileDesc)) {
updateFileTitles = false; // Done by next function
@ -264,8 +260,11 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
if(error) {
return;
}
var syncIndex = createSyncAttributes(file.id, file.etag, file.content, file.title);
var fileDesc = core.fileManager.createFile(file.title, file.content, [syncIndex]);
var syncAttributes = createSyncAttributes(file.id, file.etag, file.content, file.title);
localStorage[syncAttributes.syncIndex] = utils.serializeAttributes(syncAttributes);
var syncLocations = {};
syncLocations[syncAttributes.syncIndex] = syncAttributes;
var fileDesc = core.fileManager.createFile(file.title, file.content, syncAttributes);
core.fileManager.selectFile(fileDesc);
core.showMessage('"' + file.title + '" created successfully on Google Drive.');
});
@ -274,7 +273,7 @@ define(["core", "utils", "extension-manager", "google-helper", "underscore"], fu
var importIds = [];
_.each(state.ids, function(id) {
var syncIndex = createSyncIndex(id);
var fileDesc = core.fileManager.getFileFromSync(syncIndex);
var fileDesc = core.fileManager.getFileFromSyncIndex(syncIndex);
if(fileDesc !== undefined) {
core.fileManager.selectFile(fileDesc);
}

View File

@ -96,8 +96,7 @@ define([
}
// Dequeue a synchronized location
var publishIndex = publishAttributesList.pop();
var publishAttributes = JSON.parse(localStorage[publishIndex]);
var publishAttributes = publishAttributesList.pop();
var content = getPublishContent(publishAttributes);
// Call the provider
@ -106,7 +105,7 @@ define([
if(error !== undefined) {
var errorMsg = error.toString();
if(errorMsg.indexOf("|removePublish") !== -1) {
core.fileManager.removePublish(publishIndex);
core.fileManager.removePublish(publishAttributes);
}
if(errorMsg.indexOf("|stopPublish") !== -1) {
callback(error);
@ -144,8 +143,9 @@ define([
do {
publishIndex = "publish." + utils.randomString();
} while(_.has(localStorage, publishIndex));
publishAttributes.publishIndex = publishIndex;
localStorage[publishIndex] = JSON.stringify(publishAttributes);
core.fileManager.addPublish(fileDesc, publishIndex, publishAttributes);
core.fileManager.addPublish(fileDesc, publishAttributes);
}
// Initialize the "New publication" dialog
@ -210,7 +210,7 @@ define([
// Retrieve file's publish locations from localStorage
publisher.populatePublishLocations = function(fileDesc) {
_.chain(localStorage[fileDesc.index + ".publish"].split(";"))
_.chain(localStorage[fileDesc.fileIndex + ".publish"].split(";"))
.compact()
.each(function(publishIndex) {
var publishAttributes = JSON.parse(localStorage[publishIndex]);

View File

@ -108,7 +108,7 @@ define([
}
// Get document title/content
uploadContent = localStorage[fileDesc.index + ".content"];
uploadContent = localStorage[fileDesc.fileIndex + ".content"];
uploadContentCRC = utils.crc32(uploadContent);
uploadTitle = fileDesc.title;
uploadTitleCRC = utils.crc32(uploadTitle);
@ -202,7 +202,7 @@ define([
// Retrieve file's sync locations from localStorage
publisher.populateSyncLocations = function(fileDesc) {
_.chain(localStorage[fileDesc.index + ".sync"].split(";"))
_.chain(localStorage[fileDesc.fileIndex + ".sync"].split(";"))
.compact()
.each(function(syncIndex) {
var syncAttributes = JSON.parse(localStorage[syncIndex]);
@ -249,12 +249,12 @@ define([
// Perform the provider's export
var fileDesc = core.fileManager.getCurrentFile();
var title = fileDesc.title;
var content = localStorage[fileDesc.index + ".content"];
provider.exportFile(event, title, content, function(error, syncIndex, syncAttributes) {
var content = localStorage[fileDesc.fileIndex + ".content"];
provider.exportFile(event, title, content, function(error, syncAttributes) {
if(error) {
return;
}
core.fileManager.addSync(fileDesc, syncIndex, syncAttributes);
core.fileManager.addSync(fileDesc, syncAttributes);
});
// Store input values as preferences for next time we open the export dialog
@ -268,12 +268,12 @@ define([
$(".action-sync-manual-" + provider.providerId).click(function(event) {
var fileDesc = core.fileManager.getCurrentFile();
var title = fileDesc.title;
var content = localStorage[fileDesc.index + ".content"];
provider.exportManual(event, title, content, function(error, syncIndex, syncAttributes) {
var content = localStorage[fileDesc.fileIndex + ".content"];
provider.exportManual(event, title, content, function(error, syncAttributes) {
if(error) {
return;
}
core.fileManager.addSync(fileDesc, syncIndex, syncAttributes);
core.fileManager.addSync(fileDesc, syncAttributes);
});
});
});