4831: Rearrange classes more.
authorTom Clegg <tom@curoverse.com>
Tue, 20 Jan 2015 03:02:24 +0000 (22:02 -0500)
committerTom Clegg <tom@curoverse.com>
Tue, 20 Jan 2015 03:02:24 +0000 (22:02 -0500)
apps/backstage/app/backstage-layout.js
apps/backstage/app/backstage-routes.js
apps/backstage/app/base-layout.js
apps/backstage/app/component.arv-api-directory.js
apps/backstage/app/component.arv-index.js

index 746daf8d4772c1dbf2f024e0085c302eb0af10db..818daa85b83a81b7ba60d287c58dfa1a58d9d1e7 100644 (file)
@@ -4,15 +4,15 @@ var m = require('mithril');
 var _ = require('lodash');
 var Layout = require('./base-layout');
 
 var _ = require('lodash');
 var Layout = require('./base-layout');
 
-function BackstageLayout(innerModules) {
-    return _.extend(this, {
-        controller: BackstageLayout.controller.bind(this, innerModules),
-        view: BackstageLayout.view,
+function BackstageLayout(opts) {
+    _.extend(this, {
+        controller: this.controller.bind(this, opts),
     });
 }
     });
 }
-BackstageLayout.prototype = new Layout();
-BackstageLayout.controller = Layout.controller;
-BackstageLayout.view = function view(ctrl) {
+_.extend(BackstageLayout.prototype, Layout.prototype, {
+    view: view
+});
+function view(ctrl) {
     return [
         m('.navbar.navbar-default', {role: 'navigation'}, [
             m('.container-fluid', [
     return [
         m('.navbar.navbar-default', {role: 'navigation'}, [
             m('.container-fluid', [
index 4f6676556782e5855344ad2a3cea053da4c1c008..1d4961f109ba048e9c31d81a05e6f08ccc4edabb 100644 (file)
@@ -20,12 +20,18 @@ var connections = m.prop('4xphq qr1hi 9tee4 su92l tb05z'.split(' ').map(
 m.route(document.body, '/', {
     '/login-callback': new BackstageLoginComponent(),
     '/': new BackstageLayout({
 m.route(document.body, '/', {
     '/login-callback': new BackstageLoginComponent(),
     '/': new BackstageLayout({
-        content: new ArvApiDirectoryComponent(connections)
+        modules: {
+            content: new ArvApiDirectoryComponent({connections: connections}),
+        },
     }),
     '/list/:connection/:modelName': new BackstageLayout({
     }),
     '/list/:connection/:modelName': new BackstageLayout({
-        content: ArvIndexComponent
+        modules: {
+            content: ArvIndexComponent
+        },
     }),
     '/show/:uuid': new BackstageLayout({
     }),
     '/show/:uuid': new BackstageLayout({
-        content: ArvShowComponent
+        modules: {
+            content: ArvShowComponent
+        },
     }),
 });
     }),
 });
index cba62c94d6618d66b1a77ca5c3c59dff9bc0f643..acab495fead562331ff5d1f73a303d79de0a67a3 100644 (file)
@@ -1,37 +1,50 @@
-// Layout class. Instances are suitable for passing to m.route().
+// Layout class. Instances of subclasses are suitable for passing to
+// m.route().
 //
 // Usage:
 //
 // Usage:
-// new Layout(viewFunction, {main: FooModuleClass, nav: NavComponent})
+// new LayoutSubclass({modules: {main: FooModuleClass, nav: NavComponent}})
 //
 //
-// viewFunction is expected to include this.views.main() and
-// this.views.nav() somewhere in its return value.
-//
-// Content can be given as a class (in which case instances are made
-// with new FooModuleClass()) or as a component instance (i.e., an
-// object with a function named 'controller').
+// Content can be given as:
+// * a class -- instances are made with `new class()`
+// * an array of [cls, arg] -- instances are made with `new cls.controller(arg)`
+// * a component instance (i.e., an object with a function named 'controller')
 //
 // The layout is responsible for creating and unloading controllers.
 
 module.exports = Layout;
 
 //
 // The layout is responsible for creating and unloading controllers.
 
 module.exports = Layout;
 
-var BaseController = require('app/base-ctrl');
+var BaseController = require('./base-ctrl');
 var _ = require('lodash');
 
 var _ = require('lodash');
 
-function Layout(innerModules) {
+function Layout(opts) {
     return _.extend(this, {
     return _.extend(this, {
-        controller: Layout.controller.bind(this, innerModules),
+        controller: this.controller.bind(this, opts),
     });
 }
     });
 }
-Layout.controller = function controller(innerModules) {
+_.extend(Layout.prototype, {
+    controller: controller,
+});
+controller.prototype = new BaseController();
+function controller(opts) {
+    _.extend(this, {modules: {}}, opts);
     this.views = {};
     this.controllers = [];
     this.views = {};
     this.controllers = [];
-    Object.keys(innerModules).map(function(key) {
-        var module = innerModules[key];
-        var component = (module.controller instanceof Function) ? module : new module();
-        var ctrl = new component.controller();
+    Object.keys(this.modules).map(function(key) {
+        var module = this.modules[key];
+        var component;
+        var ctrl;
+        if (module instanceof Array) {
+            component = module[0];
+            ctrl = new component.controller(module[1]);
+        } else if (module.controller instanceof Function) {
+            component = module;
+            ctrl = new component.controller();
+        } else {
+            component = new module();
+            ctrl = new component.controller();
+        }
         var view = component.view.bind(component.view, ctrl);
         this.controllers.push(ctrl);
         this.views[key] = view;
     }, this);
         var view = component.view.bind(component.view, ctrl);
         this.controllers.push(ctrl);
         this.views[key] = view;
     }, this);
-};
-Layout.controller.prototype = new BaseController();
+}
index 9d1360c04d7eee2505ee4d42a112c9b1a5b1e881..b79765b9877081c418414aed8f81d724c3efad6b 100644 (file)
@@ -1,43 +1,50 @@
 module.exports = ArvApiDirectoryComponent;
 
 module.exports = ArvApiDirectoryComponent;
 
-var m = require('mithril')
-, BaseController = require('app/base-ctrl')
-, ArvApiStatusComponent = require('app/component.arv-api-status');
+var m = require('mithril');
+var _ = require('lodash');
+var BaseController = require('./base-ctrl');
+var ArvApiStatusComponent = require('./component.arv-api-status');
 
 
-function ArvApiDirectoryComponent(connections) {
-    this.controller = Controller;
-    Controller.prototype = new BaseController();
-    function Controller(vm) {
-        this.vm = vm || {};
-        this.vm.widgets = connections().map(function(conn) {
-            var component = new ArvApiStatusComponent(conn);
-            return {
-                view: component.view,
-                controller: new component.controller(),
-            };
-        });
-        // Give BaseController a list of components to unload.
-        this.controllers = function() {
-            return this.vm.widgets.map(function(widget) {
-                return widget.controller;
-            });
-        }.bind(this);
-
-        this.redrawTimer = setInterval(function() {
-            // If redraw is really really cheap, we can do this to make
-            // "#seconds old" timers count in real time.
-            m.redraw();
-        }, 1000);
-        this.onunload = function() {
-            clearTimeout(this.redrawTimer);
+function ArvApiDirectoryComponent(opts) {
+    _.extend(this, {
+        controller: this.controller.bind(this, opts),
+    });
+}
+_.extend(ArvApiDirectoryComponent.prototype, {
+    controller: controller,
+    view: view,
+});
+function controller(opts) {
+    _.extend(this, {connections: m.prop([])}, opts);
+    this.redrawTimer = setInterval(function() {
+        // If redraw is really really cheap, we can do this to make
+        // "#seconds old" timers count in real time.
+        m.redraw();
+    }, 1000);
+    this.widgets = this.connections().map(function(conn) {
+        var component = new ArvApiStatusComponent(conn);
+        return {
+            view: component.view,
+            controller: new component.controller(),
         };
         };
-    };
-    this.view = View;
-    function View(ctrl) {
-        return m('div', [
-            ctrl.vm.widgets.map(function(widget) {
-                return widget.view(widget.controller);
-            })
-        ]);
-    };
+    });
+}
+_.extend(controller.prototype, BaseController.prototype, {
+    // Give BaseController a list of components to unload.
+    controllers: function controllers() {
+        return this.widgets.map(function(widget) {
+            return widget.controller;
+        });
+    },
+    onunload: function onunload() {
+        clearTimeout(this.redrawTimer);
+        BaseController.prototype.onunload.call(this);
+    },
+});
+function view(ctrl) {
+    return m('div', [
+        ctrl.widgets.map(function(widget) {
+            return widget.view(widget.controller);
+        })
+    ]);
 }
 }
index 8b79f2e6092d085b544bbfd34e1b28514493e753..1eded1ac6488458c91c1e341ce9e10ccbeed49fc 100644 (file)
@@ -1,13 +1,18 @@
 module.exports = ArvIndexComponent;
 
 module.exports = ArvIndexComponent;
 
-var m = require('mithril')
-, BaseController = require('app/base-ctrl')
-, ArvListComponent = require('app/component.arv-list')
-, ArvObjectRowComponent = require('app/component.arv-object-row')
-, InfiniteScroll = require('app/infinitescroll');
+var m = require('mithril');
+var _ = require('lodash');
+var BaseController = require('./base-ctrl');
+var ArvListComponent = require('./component.arv-list')
+var ArvObjectRowComponent = require('./component.arv-object-row')
+var InfiniteScroll = require('./infinitescroll');
 
 function ArvIndexComponent() {}
 
 function ArvIndexComponent() {}
-ArvIndexComponent.controller = function controller() {
+_.extend(ArvIndexComponent.prototype, {
+    controller: controller,
+    view: view,
+});
+function controller() {
     this.list =
         new ArvListComponent(null, null, ArvObjectRowComponent);
     this.listCtrl =
     this.list =
         new ArvListComponent(null, null, ArvObjectRowComponent);
     this.listCtrl =
@@ -16,10 +21,12 @@ ArvIndexComponent.controller = function controller() {
         new InfiniteScroll(this.listCtrl, this.list.view, {pxThreshold: 200});
     this.scrollerCtrl =
         new this.scroller.controller();
         new InfiniteScroll(this.listCtrl, this.list.view, {pxThreshold: 200});
     this.scrollerCtrl =
         new this.scroller.controller();
-};
-ArvIndexComponent.controller.prototype.controllers = function controllers() {
-    return [this.listCtrl, this.scrollerCtrl];
-};
-ArvIndexComponent.view = function view(ctrl) {
+}
+_.extend(controller.prototype, BaseController.prototype, {
+    controllers: function controllers() {
+        return [this.listCtrl, this.scrollerCtrl];
+    },
+});
+function view(ctrl) {
     return ctrl.scroller.view(ctrl.scrollerCtrl);
     return ctrl.scroller.view(ctrl.scrollerCtrl);
-};
+}