From 8ef4977323adb61298644c668459bc94021a6197 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Mon, 6 Sep 2010 09:33:52 -0400 Subject: [PATCH] Move click handling into the File object. --- web_frontend/file.js | 17 ++++++++++++++--- web_frontend/main.js | 17 +++++------------ 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/web_frontend/file.js b/web_frontend/file.js index 2f44457..8c281a1 100644 --- a/web_frontend/file.js +++ b/web_frontend/file.js @@ -20,7 +20,7 @@ goog.require('goog.dom'); armadillo.File = function(name) { goog.Disposable.call(this); this.name_ = name; - this.isDirectory_ = app.isDirectory_(name); + this.isDirectory_ = app.isDirectory(name); }; goog.inherits(armadillo.File, goog.Disposable); @@ -31,6 +31,7 @@ goog.inherits(armadillo.File, goog.Disposable); goog.Disposable.prototype.disposeInternal = function() { armadillo.File.superClass_.disposeInternal.call(this); this.element_ = null; + goog.events.unlistenByKey(this.clickListener_); }; /** @@ -41,10 +42,20 @@ armadillo.File.prototype.draw = function() { if (!this.element_) { this.element_ = goog.dom.createElement('li'); this.element_.representedObject = this; + this.clickListener_ = goog.events.listen(this.element_, + goog.events.EventType.CLICK, this.clickHandler_, false, this); } goog.dom.removeChildren(this.element_); goog.dom.setTextContent(this.element_, this.name_); - app.listeners_.push(goog.events.listen(this.element_, - goog.events.EventType.CLICK, app.clickHandler_, false, app)); return this.element_; }; + +/** + * Click handler for the ist element. + * @param {Event} e + */ +armadillo.File.prototype.clickHandler_ = function(e) { + if (this.isDirectory_) { + app.navigate(this.name_); + } +}; diff --git a/web_frontend/main.js b/web_frontend/main.js index 774f2ec..cfee7ec 100644 --- a/web_frontend/main.js +++ b/web_frontend/main.js @@ -23,7 +23,6 @@ armadillo.App = function() { start_path = window.location.hash.substr(1); } this.list(start_path); - this.listeners_ = new Array(); this.errorEffect_ = new goog.fx.dom.FadeInAndShow(goog.dom.getElement('error'), 2.0); this.errorEffect_.hide(); @@ -58,11 +57,6 @@ armadillo.App.prototype.list = function(path) { app.clearError_(); } - // 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); app.currentPath_ = path; @@ -84,14 +78,13 @@ armadillo.App.prototype.list = function(path) { }; /** - * Click handler for elements. - * @param {Event} e + * Navigates to a subpath. Can only handle directories. + * @param {string} target Relative path to |currentPath_|. */ -armadillo.App.prototype.clickHandler_ = function(e) { - var target = goog.dom.getTextContent(e.target); +armadillo.App.prototype.navigate = function(target) { if (target == '../') { this.list(this.stripLastPathComponent_(this.currentPath_)); - } else if (this.isDirectory_(target)) { + } else { this.list(this.currentPath_ + target); } }; @@ -110,7 +103,7 @@ armadillo.App.prototype.hashChanged_ = function(e) { * @param {string} path * @returns boolean */ -armadillo.App.prototype.isDirectory_ = function(path) { +armadillo.App.prototype.isDirectory = function(path) { return path[path.length - 1] == '/'; }; -- 2.22.5