From d34f163dbc153dec2aca8c21b7b906c8735d36c6 Mon Sep 17 00:00:00 2001 From: Jeremy Ruston Date: Thu, 5 Apr 2012 18:25:39 +0100 Subject: [PATCH] Added deletion support to HttpSync and LocalFileSync --- js/HttpSync.js | 9 ++++--- js/LocalFileSync.js | 60 +++++++++++++++++++++++++++++++-------------- tiddlywiki.js | 5 ++++ 3 files changed, 53 insertions(+), 21 deletions(-) diff --git a/js/HttpSync.js b/js/HttpSync.js index ae2b668ce..32189bee0 100644 --- a/js/HttpSync.js +++ b/js/HttpSync.js @@ -4,7 +4,7 @@ title: js/HttpSync.js \*/ (function(){ -/*jslint node: true */ +/*jslint node: true, browser: true */ "use strict"; function HttpSync(store) { @@ -12,7 +12,8 @@ function HttpSync(store) { this.changeCounts = {}; store.addEventListener("",function(changes) { for(var title in changes) { - var tiddler = store.getTiddler(title); + var x = new XMLHttpRequest(), + tiddler = store.getTiddler(title); if(tiddler) { var fieldStrings = tiddler.getFieldStrings(), fields = {}, @@ -21,10 +22,12 @@ function HttpSync(store) { fields[fieldStrings[t].name] = fieldStrings[t].value; } fields.text = tiddler.text; - var x = new XMLHttpRequest(); x.open("PUT",window.location.toString() + encodeURIComponent(title),true); x.setRequestHeader("Content-type", "application/json"); x.send(JSON.stringify(fields)); + } else { + x.open("DELETE",window.location.toString() + encodeURIComponent(title),true); + x.send(); } } }); diff --git a/js/LocalFileSync.js b/js/LocalFileSync.js index 9363dca92..25aaf2e63 100644 --- a/js/LocalFileSync.js +++ b/js/LocalFileSync.js @@ -15,19 +15,26 @@ var retrieveFile = require("./FileRetriever.js").retrieveFile, util = require("util"), async = require("async"); + + function LocalFileSync(dirpath,store,callback) { this.dirpath = dirpath; this.store = store; this.callback = callback; - this.changeCounts = {}; // A hashmap of : - var self = this; - // Set up a queue for loading tiddler files + this.tiddlers = {}; // A hashmap of : {changeCount: , files: [name]} + var self = this, + sanitizeFilepath = function(filepath) { + return filepath.replace(/\//mg,"-"); + }; + // Set up a queue for loading tiddler files, tasks are {filepath:,callback:} this.loadQueue = async.queue(function(task,callback) { + task.files = []; retrieveFile(task.filepath,self.dirpath,function(err,data) { if(err) { callback(err); } else { - // Use the filepath as the default title and src for the tiddler + task.files.push(task.filepath); + // Use the filepath as the default title for the tiddler var fields = { title: data.path }; @@ -39,6 +46,7 @@ function LocalFileSync(dirpath,store,callback) { if(err && err.code !== "ENOENT" && err.code !== "404") { callback(err); } else { + task.files.push(metafile); var fields = tiddlers[0]; if(!err) { var text = data.text.split("\n\n")[0]; @@ -70,14 +78,17 @@ function LocalFileSync(dirpath,store,callback) { for(var t=0; t lastChangeCount) { - files = self.store.serializeTiddlers([tiddler],"application/x-tiddler"); - for(var t=0; t tiddlerInfo.changeCount) { + files = self.store.serializeTiddlers([tiddler],"application/x-tiddler"); + for(t=0; t