mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2025-01-11 18:00:26 +00:00
Handle files being deleted from Dropbox
This commit is contained in:
parent
0187f3c3bc
commit
7ee48626ab
@ -12,8 +12,16 @@ Startup the Dropbox wiki app
|
|||||||
/*global $tw: false */
|
/*global $tw: false */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
exports.startup = function() {
|
exports.startup = function(loggedIn) {
|
||||||
// Check that we've been loaded from the dropbox
|
// 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(loggedIn) {
|
||||||
|
// Figure out the wiki name
|
||||||
var url = (window.location.protocol + "//" + window.location.host + window.location.pathname),
|
var url = (window.location.protocol + "//" + window.location.host + window.location.pathname),
|
||||||
wikiName;
|
wikiName;
|
||||||
if(url.indexOf($tw.plugins.dropbox.userInfo.publicAppUrl) === 0) {
|
if(url.indexOf($tw.plugins.dropbox.userInfo.publicAppUrl) === 0) {
|
||||||
@ -26,15 +34,8 @@ exports.startup = function() {
|
|||||||
// Save the wiki name for later
|
// Save the wiki name for later
|
||||||
$tw.plugins.dropbox.wikiName = wikiName;
|
$tw.plugins.dropbox.wikiName = wikiName;
|
||||||
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleWikiName, text: $tw.plugins.dropbox.wikiName},true);
|
$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
|
// Check for later versions of files on Dropbox
|
||||||
$tw.plugins.dropbox.loadTiddlerFiles("/" + $tw.plugins.dropbox.wikiName + "/tiddlers",function(hadChanges) {
|
$tw.plugins.dropbox.refreshTiddlerFiles("/" + $tw.plugins.dropbox.wikiName + "/tiddlers",function(hadChanges) {
|
||||||
// Save the tiddler index if we had changes
|
// Save the tiddler index if we had changes
|
||||||
if(hadChanges) {
|
if(hadChanges) {
|
||||||
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",function(error) {
|
$tw.plugins.dropbox.saveTiddlerIndex("/" + $tw.plugins.dropbox.wikiName + "/index.html",function(error) {
|
||||||
@ -45,6 +46,7 @@ exports.startup = function() {
|
|||||||
} else {
|
} else {
|
||||||
alert("This TiddlyWiki file must be in Dropbox");
|
alert("This TiddlyWiki file must be in Dropbox");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -12,13 +12,15 @@ Startup the Dropbox main app
|
|||||||
/*global $tw: false */
|
/*global $tw: false */
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
exports.startup = function() {
|
exports.startup = function(loggedIn) {
|
||||||
|
if(loggedIn) {
|
||||||
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "no"},true);
|
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "no"},true);
|
||||||
// Load tiddlers
|
// Load tiddlers
|
||||||
$tw.plugins.dropbox.loadWikiFiles("/",function() {
|
$tw.plugins.dropbox.loadWikiFiles("/",function() {
|
||||||
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "yes"},true);
|
$tw.wiki.addTiddler({title: $tw.plugins.dropbox.titleLoadedWikis, text: "yes"},true);
|
||||||
console.log("Loaded all wikis",$tw.wiki.tiddlers);
|
console.log("Loaded all wikis",$tw.wiki.tiddlers);
|
||||||
});
|
});
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
@ -48,12 +48,17 @@ $tw.plugins.dropbox.login = function() {
|
|||||||
// Get user information
|
// Get user information
|
||||||
$tw.plugins.dropbox.getUserInfo(function() {
|
$tw.plugins.dropbox.getUserInfo(function() {
|
||||||
// Invoke any dropbox-startup modules
|
// Invoke any dropbox-startup modules
|
||||||
|
$tw.plugins.dropbox.invokeDropboxStartupModules(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
// Invoke any dropbox-startup modules
|
||||||
|
$tw.plugins.dropbox.invokeDropboxStartupModules = function(loggedIn) {
|
||||||
var mods = $tw.modules.types["dropbox-startup"];
|
var mods = $tw.modules.types["dropbox-startup"];
|
||||||
for(var m=0; m<mods.length; m++) {
|
for(var m=0; m<mods.length; m++) {
|
||||||
mods[m].startup();
|
mods[m].startup(loggedIn);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get user information
|
// Get user information
|
||||||
@ -103,13 +108,27 @@ $tw.plugins.dropbox.loadWikiFiles = function(path,callback) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// Load tiddler files from a folder
|
// Synchronise the local state with the files in Dropbox
|
||||||
$tw.plugins.dropbox.loadTiddlerFiles = function(path,callback) {
|
$tw.plugins.dropbox.refreshTiddlerFiles = function(path,callback) {
|
||||||
// First get the list of tiddler files
|
// First get the list of tiddler files
|
||||||
$tw.plugins.dropbox.client.stat(path,{readDir: true},function(error,stat,stats) {
|
$tw.plugins.dropbox.client.stat(path,{readDir: true},function(error,stat,stats) {
|
||||||
if(error) {
|
if(error) {
|
||||||
return $tw.plugins.dropbox.showError(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
|
// Process the files via an asynchronous queue, with concurrency set to 2 at a time
|
||||||
var q = async.queue(function(task,callback) {
|
var q = async.queue(function(task,callback) {
|
||||||
$tw.plugins.dropbox.loadTiddlerFile(task.path,task.type,task.stats,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 we didn't queue anything for loading we'll have to manually trigger our callback
|
||||||
if(q.length() === 0) {
|
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 {
|
} else {
|
||||||
tiddlers = $tw.wiki.deserializeTiddlers(mimeType,data,{title: defaultTitle});
|
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
|
// Check to see if there's a metafile
|
||||||
var metafilePath = path + ".meta",
|
var metafilePath = path + ".meta",
|
||||||
metafileIndex = null;
|
metafileIndex = null;
|
||||||
@ -197,21 +214,25 @@ console.log("loading tiddler from",path);
|
|||||||
}
|
}
|
||||||
// Process the metafile if it's there
|
// Process the metafile if it's there
|
||||||
if(tiddlers.length === 1 && metafileIndex !== null) {
|
if(tiddlers.length === 1 && metafileIndex !== null) {
|
||||||
|
var mainStat = stat;
|
||||||
$tw.plugins.dropbox.client.readFile(metafilePath,function(error,data,stat) {
|
$tw.plugins.dropbox.client.readFile(metafilePath,function(error,data,stat) {
|
||||||
if(error) {
|
if(error) {
|
||||||
callback(error);
|
callback(error);
|
||||||
return $tw.plugins.dropbox.showError(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
|
// Extract the metadata and add the tiddlers
|
||||||
tiddlers = [$tw.utils.parseFields(data,tiddlers[0])];
|
tiddlers = [$tw.utils.parseFields(data,tiddlers[0])];
|
||||||
$tw.wiki.addTiddlers(tiddlers);
|
$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();
|
callback();
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
// Add the tiddlers
|
// Add the tiddlers
|
||||||
$tw.wiki.addTiddlers(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();
|
callback();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -342,6 +363,8 @@ exports.startup = function() {
|
|||||||
// Authenticate ourselves if the marker is in the document query string
|
// Authenticate ourselves if the marker is in the document query string
|
||||||
if(document.location.search.indexOf(queryLoginMarker) !== -1) {
|
if(document.location.search.indexOf(queryLoginMarker) !== -1) {
|
||||||
$tw.plugins.dropbox.login();
|
$tw.plugins.dropbox.login();
|
||||||
|
} else {
|
||||||
|
$tw.plugins.dropbox.invokeDropboxStartupModules(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user