From 2f10a80d233d8f672610ae182a4d2b99ad0325a4 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Thu, 2 Sep 2010 12:11:12 -0400 Subject: [PATCH] Create a new JS class for representing files in directory listings. --- build.py | 11 +++++---- web_frontend/file.js | 50 +++++++++++++++++++++++++++++++++++++++++ web_frontend/index.html | 2 +- web_frontend/main.js | 27 +++++++++++----------- 4 files changed, 71 insertions(+), 19 deletions(-) create mode 100644 web_frontend/file.js diff --git a/build.py b/build.py index dcd2ba0..0de1e2e 100755 --- a/build.py +++ b/build.py @@ -29,7 +29,8 @@ SOURCES = [ 'main.go' ] SOURCES_FE = [ - 'main.js' + 'file.js', + 'main.js', ] RESOURCES_FE = [ 'index.html', @@ -93,10 +94,12 @@ def Main(): # Compile JS. print '=== Compiling Front End ===' outfile = os.path.join(PROD_PATH, 'fe', PRODUCT_NAME + '.js') - fe_sources = map(lambda f: os.path.join(FE_PATH, f), SOURCES_FE) + fe_sources = map(lambda f: '-i' + os.path.join(FE_PATH, f), SOURCES_FE) closure_sources = os.path.join(CLOSURE_DEST, 'closure', 'goog') - args = [ CLOSURE_CALCDEPS, '-i', ' '.join(fe_sources), '-p', closure_sources, - '-o', 'compiled', '-c', CLOSURE_COMPILER, '--output_file', outfile ] + args = [ CLOSURE_CALCDEPS ] + args.extend(fe_sources) + args.extend([ '-p', closure_sources, '-o', 'compiled', '-c', CLOSURE_COMPILER, + '--output_file', outfile ]) print ' ' + ' '.join(args) handle = subprocess.Popen(args, stdout = sys.stdout, stderr = sys.stderr) handle.wait() diff --git a/web_frontend/file.js b/web_frontend/file.js new file mode 100644 index 0000000..2f44457 --- /dev/null +++ b/web_frontend/file.js @@ -0,0 +1,50 @@ +// +// Armadillo File Manager +// Copyright (c) 2010, Robert Sesek +// +// This program is free software: you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free Software +// Foundation, either version 3 of the License, or any later version. +// + +goog.provide('armadillo.File'); + +goog.require('goog.Disposable'); +goog.require('goog.dom'); + +/** + * A file in a directory listing. + * @param {string} File name. + * @constructor + */ +armadillo.File = function(name) { + goog.Disposable.call(this); + this.name_ = name; + this.isDirectory_ = app.isDirectory_(name); +}; +goog.inherits(armadillo.File, goog.Disposable); + +/** + * Disposer + * @protected + */ +goog.Disposable.prototype.disposeInternal = function() { + armadillo.File.superClass_.disposeInternal.call(this); + this.element_ = null; +}; + +/** + * Constructs the Elements that make up the UI. + * @returns {Element} An element ready for insertion into DOM. + */ +armadillo.File.prototype.draw = function() { + if (!this.element_) { + this.element_ = goog.dom.createElement('li'); + this.element_.representedObject = 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_; +}; diff --git a/web_frontend/index.html b/web_frontend/index.html index 5db29c3..c13ac2a 100644 --- a/web_frontend/index.html +++ b/web_frontend/index.html @@ -13,7 +13,7 @@ \ No newline at end of file diff --git a/web_frontend/main.js b/web_frontend/main.js index f71ded8..774f2ec 100644 --- a/web_frontend/main.js +++ b/web_frontend/main.js @@ -8,14 +8,16 @@ // goog.provide('armadillo'); +goog.provide('armadillo.App'); +goog.require('armadillo.File'); goog.require('goog.array'); goog.require('goog.dom'); goog.require('goog.fx.dom.FadeInAndShow'); goog.require('goog.net.XhrIo'); goog.require('goog.Uri.QueryData'); -armadillo = function() { +armadillo.App = function() { var start_path = '/'; if (window.location.hash) { start_path = window.location.hash.substr(1); @@ -35,7 +37,7 @@ armadillo = function() { * @param {Object} extra_data Extra data to add. * @param {Function} callback XHR callback. */ -armadillo.prototype.sendRequest_ = function(action, extra_data, callback) { +armadillo.App.prototype.sendRequest_ = function(action, extra_data, callback) { var data = new goog.Uri.QueryData(); data.set('action', action); data.extend(extra_data); @@ -46,7 +48,7 @@ armadillo.prototype.sendRequest_ = function(action, extra_data, callback) { * Updates the directory listing for a given path. * @param {string} path Path to list; relative to jail. */ -armadillo.prototype.list = function(path) { +armadillo.App.prototype.list = function(path) { var callback = function(e) { var data = e.target.getResponseJson(); if (data['error']) { @@ -74,11 +76,8 @@ armadillo.prototype.list = function(path) { // 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)); + var fileObject = new armadillo.File(file); + goog.dom.appendChild(list, fileObject.draw()); }); } this.sendRequest_('list', {'path':path}, callback); @@ -88,7 +87,7 @@ armadillo.prototype.list = function(path) { * Click handler for elements. * @param {Event} e */ -armadillo.prototype.clickHandler_ = function(e) { +armadillo.App.prototype.clickHandler_ = function(e) { var target = goog.dom.getTextContent(e.target); if (target == '../') { this.list(this.stripLastPathComponent_(this.currentPath_)); @@ -101,7 +100,7 @@ armadillo.prototype.clickHandler_ = function(e) { * Event for when the hash changes. * @param {Event} e */ -armadillo.prototype.hashChanged_ = function(e) { +armadillo.App.prototype.hashChanged_ = function(e) { if (window.location.hash.length) this.list(window.location.hash.substr(1)); }; @@ -111,7 +110,7 @@ armadillo.prototype.hashChanged_ = function(e) { * @param {string} path * @returns boolean */ -armadillo.prototype.isDirectory_ = function(path) { +armadillo.App.prototype.isDirectory_ = function(path) { return path[path.length - 1] == '/'; }; @@ -120,7 +119,7 @@ armadillo.prototype.isDirectory_ = function(path) { * @param {string} path * @returns string */ -armadillo.prototype.stripLastPathComponent_ = function(path) { +armadillo.App.prototype.stripLastPathComponent_ = function(path) { for (var i = path.length - 1; i >= 0; --i) { if (path[i] == '/') { if (i != path.length - 1) { @@ -134,7 +133,7 @@ armadillo.prototype.stripLastPathComponent_ = function(path) { /** * Clears the error message. */ -armadillo.prototype.clearError_ = function() { +armadillo.App.prototype.clearError_ = function() { this.errorEffect_.hide(); goog.dom.setTextContent(this.errorEffect_.element, ''); }; @@ -143,7 +142,7 @@ armadillo.prototype.clearError_ = function() { * Shows an error message. * @param {string} message */ -armadillo.prototype.showError_ = function(message) { +armadillo.App.prototype.showError_ = function(message) { goog.dom.setTextContent(this.errorEffect_.element, message); this.errorEffect_.show(); }; -- 2.22.5