1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-10-02 00:40:47 +00:00

Handle files being deleted from Dropbox

This commit is contained in:
Jeremy Ruston 2012-10-14 00:21:44 +01:00
parent 0187f3c3bc
commit 7ee48626ab
3 changed files with 75 additions and 48 deletions

View File

@ -12,38 +12,40 @@ Startup the Dropbox wiki app
/*global $tw: false */
"use strict";
exports.startup = function() {
// Check that we've been loaded from the dropbox
var url = (window.location.protocol + "//" + window.location.host + window.location.pathname),
wikiName;
if(url.indexOf($tw.plugins.dropbox.userInfo.publicAppUrl) === 0) {
var p = url.indexOf("/",$tw.plugins.dropbox.userInfo.publicAppUrl.length + 1);
if(p !== -1 && url.substr(p) === "/index.html") {
wikiName = decodeURIComponent(url.substring($tw.plugins.dropbox.userInfo.publicAppUrl.length + 1,p));
}
exports.startup = function(loggedIn) {
// Load any tiddlers embedded in the index file
var index = $tw.wiki.getTiddlerData($tw.plugins.dropbox.titleTiddlerIndex);
if(index) {
$tw.wiki.addTiddlers(index.tiddlers);
$tw.wiki.addTiddlers(index.shadows,true);
$tw.plugins.dropbox.fileInfo = index.fileInfo;
}
if(wikiName) {
// Save the wiki name for later
$tw.plugins.dropbox.wikiName = wikiName;
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleWikiName, text: $tw.plugins.dropbox.wikiName},true);
// Load any tiddlers embedded in the index file
var index = $tw.wiki.getTiddlerData($tw.plugins.dropbox.titleTiddlerIndex);
if(index) {
$tw.wiki.addTiddlers(index.tiddlers);
$tw.wiki.addTiddlers(index.shadows,true);
$tw.plugins.dropbox.fileInfo = index.fileInfo;
}
// Check for later versions of files on Dropbox
$tw.plugins.dropbox.loadTiddlerFiles("/" + $tw.plugins.dropbox.wikiName + "/tiddlers",function(hadChanges) {
// Save the tiddler index if we had changes
if(hadChanges) {
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",function(error) {
console.log("Saved tiddler index");
});
if(loggedIn) {
// Figure out the wiki name
var url = (window.location.protocol + "//" + window.location.host + window.location.pathname),
wikiName;
if(url.indexOf($tw.plugins.dropbox.userInfo.publicAppUrl) === 0) {
var p = url.indexOf("/",$tw.plugins.dropbox.userInfo.publicAppUrl.length + 1);
if(p !== -1 && url.substr(p) === "/index.html") {
wikiName = decodeURIComponent(url.substring($tw.plugins.dropbox.userInfo.publicAppUrl.length + 1,p));
}
});
} else {
alert("This TiddlyWiki file must be in Dropbox");
}
if(wikiName) {
// Save the wiki name for later
$tw.plugins.dropbox.wikiName = wikiName;
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleWikiName, text: $tw.plugins.dropbox.wikiName},true);
// Check for later versions of files on Dropbox
$tw.plugins.dropbox.refreshTiddlerFiles("/" + $tw.plugins.dropbox.wikiName + "/tiddlers",function(hadChanges) {
// Save the tiddler index if we had changes
if(hadChanges) {
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",function(error) {
console.log("Saved tiddler index");
});
}
});
} else {
alert("This TiddlyWiki file must be in Dropbox");
}
}
};

View File

@ -12,13 +12,15 @@ Startup the Dropbox main app
/*global $tw: false */
"use strict";
exports.startup = function() {
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "no"},true);
// Load tiddlers
$tw.plugins.dropbox.loadWikiFiles("/",function() {
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "yes"},true);
console.log("Loaded all wikis",$tw.wiki.tiddlers);
});
exports.startup = function(loggedIn) {
if(loggedIn) {
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "no"},true);
// Load tiddlers
$tw.plugins.dropbox.loadWikiFiles("/",function() {
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "yes"},true);
console.log("Loaded all wikis",$tw.wiki.tiddlers);
});
}
};
})();

View File

@ -48,14 +48,19 @@ $tw.plugins.dropbox.login = function() {
// Get user information
$tw.plugins.dropbox.getUserInfo(function() {
// Invoke any dropbox-startup modules
var mods = $tw.modules.types["dropbox-startup"];
for(var m=0; m<mods.length; m++) {
mods[m].startup();
}
$tw.plugins.dropbox.invokeDropboxStartupModules(true);
});
});
};
// Invoke any dropbox-startup modules
$tw.plugins.dropbox.invokeDropboxStartupModules = function(loggedIn) {
var mods = $tw.modules.types["dropbox-startup"];
for(var m=0; m<mods.length; m++) {
mods[m].startup(loggedIn);
}
};
// Get user information
$tw.plugins.dropbox.getUserInfo = function(callback) {
$tw.plugins.dropbox.client.getUserInfo(function(error,userInfo) {
@ -103,13 +108,27 @@ $tw.plugins.dropbox.loadWikiFiles = function(path,callback) {
});
};
// Load tiddler files from a folder
$tw.plugins.dropbox.loadTiddlerFiles = function(path,callback) {
// Synchronise the local state with the files in Dropbox
$tw.plugins.dropbox.refreshTiddlerFiles = function(path,callback) {
// First get the list of tiddler files
$tw.plugins.dropbox.client.stat(path,{readDir: true},function(error,stat,stats) {
if(error) {
return $tw.plugins.dropbox.showError(error);
}
// Make a hashmap of each of the file names
var filenames = {},f,hadDeletions;
for(f=0; f<stats.length; f++) {
filenames[stats[f].name] = true;
}
console.log("filenames",filenames);
console.log("fileinfo",$tw.plugins.dropbox.fileInfo)
// Check to see if any files have been deleted, and remove the associated tiddlers
for(f in $tw.plugins.dropbox.fileInfo) {
if(!$tw.utils.hop(filenames,f)) {
$tw.wiki.deleteTiddler($tw.plugins.dropbox.fileInfo[f].title);
hadDeletions = true;
}
}
// Process the files via an asynchronous queue, with concurrency set to 2 at a time
var q = async.queue(function(task,callback) {
$tw.plugins.dropbox.loadTiddlerFile(task.path,task.type,task.stats,callback);
@ -143,7 +162,7 @@ $tw.plugins.dropbox.loadTiddlerFiles = function(path,callback) {
}
// If we didn't queue anything for loading we'll have to manually trigger our callback
if(q.length() === 0) {
callback(false); // And tell it that there weren't any changes
callback(hadDeletions); // And tell it that there are changes if there were deletions
}
});
};
@ -185,8 +204,6 @@ console.log("loading tiddler from",path);
} else {
tiddlers = $tw.wiki.deserializeTiddlers(mimeType,data,{title: defaultTitle});
}
// Save the revision of this file so we can detect changes
$tw.plugins.dropbox.fileInfo[stat.name] = {versionTag: stat.versionTag};
// Check to see if there's a metafile
var metafilePath = path + ".meta",
metafileIndex = null;
@ -197,21 +214,25 @@ console.log("loading tiddler from",path);
}
// Process the metafile if it's there
if(tiddlers.length === 1 && metafileIndex !== null) {
var mainStat = stat;
$tw.plugins.dropbox.client.readFile(metafilePath,function(error,data,stat) {
if(error) {
callback(error);
return $tw.plugins.dropbox.showError(error);
}
// Save the revision of the metafile so we can detect changes later
$tw.plugins.dropbox.fileInfo[stat.name] = {versionTag: stat.versionTag};
// Extract the metadata and add the tiddlers
tiddlers = [$tw.utils.parseFields(data,tiddlers[0])];
$tw.wiki.addTiddlers(tiddlers);
// Save the revision of the files so we can detect changes later
$tw.plugins.dropbox.fileInfo[mainStat.name] = {versionTag: mainStat.versionTag,title: tiddlers[0].title};
$tw.plugins.dropbox.fileInfo[stat.name] = {versionTag: stat.versionTag,title: tiddlers[0].title};
callback();
});
} else {
// Add the tiddlers
$tw.wiki.addTiddlers(tiddlers);
// Save the revision of this file so we can detect changes
$tw.plugins.dropbox.fileInfo[stat.name] = {versionTag: stat.versionTag,title: tiddlers[0].title};
callback();
}
});
@ -342,6 +363,8 @@ exports.startup = function() {
// Authenticate ourselves if the marker is in the document query string
if(document.location.search.indexOf(queryLoginMarker) !== -1) {
$tw.plugins.dropbox.login();
} else {
$tw.plugins.dropbox.invokeDropboxStartupModules(false);
}
};