From 7afb183b3187b75c98f4eb1f0251a4bdfe34d5ff Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 17 Jul 2010 19:34:16 -0400 Subject: [PATCH] Add dirtag.xul -> web.py data transfer. Now the Dirtag instance in web.py carries out request made from dirtag.xul. TODO: for some reason rdf refreshing isn't working/visible quite yet... --- dirtag/__init__.py | 4 ++++ dirtag/web.py | 22 ++++++++++++++++++---- static/dirtag.js | 40 ++++++++++++++++++++++++++++++++++------ 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/dirtag/__init__.py b/dirtag/__init__.py index cb69eeb..2270104 100644 --- a/dirtag/__init__.py +++ b/dirtag/__init__.py @@ -183,6 +183,10 @@ class Dirtag (object): # TODO: assumes unique basenames. Check? return os.path.join(*([self.tag_dir] + tag + target[-1:])) + def new_tag(self, tag): + for i in range(len(tag)): + os.mkdir(os.path.join(self.tag_dir, *tag[:i+1])) + def add_tag(self, target, tag): tag_path = self.tag_path(target, tag) target_path = os.path.abspath(os.path.join(*([self.raw_dir]+target))) diff --git a/dirtag/web.py b/dirtag/web.py index fee3bb8..0d33ebf 100755 --- a/dirtag/web.py +++ b/dirtag/web.py @@ -17,9 +17,9 @@ class WebInterface: """Initialize the bug repository for this web interface.""" self.dirtag = dirtag self.env = Environment(loader=FileSystemLoader(template_dir)) - self.repository_name = repository_name + self.repository_name = repository_name self.rdf_root = 'http://dirtag.com/' - + @cherrypy.expose def index(self): template = self.env.get_template('dirtag.xul') @@ -115,8 +115,22 @@ class WebInterface: """ return len([x for x,y in zip(a,b) if x==y]) == len(a) - #raise cherrypy.HTTPRedirect('/static/dirtag.xul', status=302) - + @cherrypy.expose + def new_tag(self, tag): + self.dirtag.new_tag(tag.split('/')) + return '

New tag %s added

' % tag + + @cherrypy.expose + def add_tag(self, path, tag): + self.dirtag.add_tag(path.split('/'), tag.split('/')) + return '

Added tag %s to %s

' % (tag, path) + + @cherrypy.expose + def remove_tag(self, path, tag): + self.dirtag.remove_tag(path.split('/'), tag.split('/')) + return '

Removed tag %s from %s

' % (tag, path) + + if __name__ == '__main__': parser = OptionParser('%prog [options]', epilog=WebInterface.__doc__) parser.add_option('-r', '--raw-dir', dest='raw_dir', metavar='DIR', diff --git a/static/dirtag.js b/static/dirtag.js index 554f9ae..47f63d4 100644 --- a/static/dirtag.js +++ b/static/dirtag.js @@ -17,17 +17,44 @@ function tree_selection(id) { /* Functions driving the CherryPy backend */ -function new_tag(path) { - alert('Adding tag: '+path.toString()); +function reload_rdf(e) { /* https://developer.mozilla.org/en/RDF_in_Mozilla_FAQ */ + if (e.readyState == 4) { /* 4: request finished and response is ready */ + if(e.status != 200) { + alert("Error loading page\n\n"+e.responseText); + return; + } + // Get the RDF service + var RDF = + Components + .classes['@mozilla.org/rdf/rdf-service;1'] + .getService(Components.interfaces.nsIRDFService); + // Get the datasources and refresh them + RDF.GetDataSource('raw.rdf').Refresh('non-blocking'); + RDF.GetDataSource('tag.rdf').Refresh('non-blocking'); + } +} + +function push_data(url) { + /* https://developer.mozilla.org/en/using_xmlhttprequest */ + xmlDoc = new XMLHttpRequest(); + xmlDoc.open('GET', url, true); + xmlDoc.onreadystatechange = function() { reload_rdf(xmlDoc); }; + xmlDoc.send(null); +} + +function new_tag(tag) { + push_data('/new_tag?tag='+escape(tag.join('/'))); } function add_tag(path, tag) { - alert('Add tag '+tag+' to '+path.toString()); + push_data('/add_tag?path='+escape(path.join('/')) + +'&tag='+escape(tag.join('/'))); } function remove_tag(path, tag) { - alert('Remove tag '+tag+' from '+path.toString()); + push_data('/remove_tag?path='+escape(path.join('/')) + +'&tag='+escape(tag.join('/'))); } /* The bindings themselves */ @@ -50,7 +77,6 @@ function new_tag_button() { function set_selected_element(path) { element = document.getElementById('element'); element.file_path = path; - alert('Set selected element: '+path.toString()); } function raw_tree_select() { @@ -65,7 +91,9 @@ function checkbox_activity(tree, row, col, value) { var path = document.getElementById('element').file_path; var name = tree.columns.getPrimaryColumn(); id = tree_path(tree, row); - if (value == true || value == "true") { + if (path == 'UNDEFINED') { + return; + } else if (value == true || value == 'true') { add_tag(path, id); } else { remove_tag(path, id); -- 2.26.2