From bc5f03f91296caf6f815dc2924dd41a2ae2dfaca Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 31 Aug 2010 10:19:55 -0400 Subject: [PATCH] * Restructure WebFE to have an app controller object. * Add a click handler for the entries. --- web_frontend/index.html | 2 +- web_frontend/main.js | 42 +++++++++++++++++++++++++++++++++++------ 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/web_frontend/index.html b/web_frontend/index.html index ab951eb..2ca064c 100644 --- a/web_frontend/index.html +++ b/web_frontend/index.html @@ -10,7 +10,7 @@ \ No newline at end of file diff --git a/web_frontend/main.js b/web_frontend/main.js index 27a695e..7b61aa0 100644 --- a/web_frontend/main.js +++ b/web_frontend/main.js @@ -5,9 +5,10 @@ goog.require('goog.dom'); goog.require('goog.net.XhrIo'); goog.require('goog.Uri.QueryData'); -armadillo.Main = function() { - armadillo.List('/'); -}; +armadillo = function() { + this.list('/'); + this.listeners_ = new Array(); +} /** * Starts a new XHR service request from the backend. @@ -15,7 +16,7 @@ armadillo.Main = function() { * @param {Object} extra_data Extra data to add. * @param {Function} callback XHR callback. */ -armadillo.Request = function(action, extra_data, callback) { +armadillo.prototype.sendRequest_ = function(action, extra_data, callback) { var data = new goog.Uri.QueryData(); data.set('action', 'list'); data.extend(extra_data); @@ -26,20 +27,49 @@ armadillo.Request = function(action, extra_data, callback) { * Updates the directory listing for a given path. * @param {string} path Path to list; relative to jail. */ -armadillo.List = function(path) { +armadillo.prototype.list = function(path) { var callback = function(e) { var data = e.target.getResponseJson(); if (data['status']) { return; // Error. } + // Unlisten all current listeners. + goog.array.forEach(app.listeners_, function(e) { + goog.events.unlistenByKey(e); + }); + + // Update the listing. goog.dom.setTextContent(goog.dom.getElement('pwd'), path); var list = goog.dom.getElement('ls'); goog.dom.removeChildren(list); + + // Add items for each entry. goog.array.forEach(data, function(file) { var elm = goog.dom.createElement('li'); goog.dom.setTextContent(elm, file); goog.dom.appendChild(list, elm); + app.listeners_.push(goog.events.listen(elm, + goog.events.EventType.CLICK, app.clickHandler_, false, app)); }); } - armadillo.Request('list', {'path':path}, callback); + this.sendRequest_('list', {'path':path}, callback); +}; + +/** + * Click handler for elements. + * @param {Event} e + */ +armadillo.prototype.clickHandler_ = function(e) { + if (this.isDirectory_(goog.dom.getTextContent(e.target))) { + alert('this is a dir'); + } +}; + +/** + * Checks whether a path is a directory. + * @param {string} path + * @returns boolean + */ +armadillo.prototype.isDirectory_ = function(path) { + return path[path.length - 1] == '/'; }; -- 2.43.5