@@ -30,18 +30,61 @@ function Node(paper, nodeName, startX) {
3030 console . log ( that ) ;
3131 that . circle . attr ( { fill : 'red' } ) ;
3232 this . paper . text ( that . startX + ARM_LEN + NODE_RADIUS , HORIZON , that . nodeName ) . attr ( { "font-size" : 12 } ) ;
33- if ( that . next != null ) {
33+ }
34+ } ;
35+
36+ function MiddlewareStack ( ) {
37+ var that = this ; // Create a handle to the current stack
38+ this . nodes = [ ] ;
39+
40+ this . addNode = function ( node ) {
41+ this . nodes . push ( node ) ;
42+ } ;
43+
44+ this . draw = function ( ) {
45+ for ( var i = 0 ; i < 4 ; i += 1 ) {
46+ this . nodes [ i ] . draw ( ) ;
47+ }
48+ } ;
49+
50+ } ;
51+
52+ function Animation ( paper ) {
53+ var that = this ; // Create a handle to the current animation
54+ this . paper = paper ;
55+ this . requestBall = this . paper . circle ( 0 , HORIZON , 5 ) . attr ( { fill : 'red' } ) ;
56+
57+ this . steps = [ ] ;
58+
59+ this . addStep = function ( node ) {
60+ this . steps . push ( node ) ;
61+ } ;
62+
63+ this . run = function ( ) {
64+ for ( var i = 0 ; i < steps . length ; i += 1 ) {
65+ this . animateRequestBall ( steps [ i ] ) ;
66+ } ;
67+ } ;
68+
69+ this . animateRequestBall = function ( destination ) {
70+ var centerOfNode = destination . startX + ARM_LEN + NODE_RADIUS ;
71+ var anim = Raphael . animation ( { cx : centerOfNode , cy : HORIZON } , SPEED , destination . highlightNode ) ;
72+ requestBall . animate ( anim ) ;
73+ } ;
74+
75+ /* if (that.next != null) {
3476 destination = that.next;
3577 } else if (that.previous != null) {
3678 destination = that.previous;
3779 } else {
3880 console.log('no where to go');
3981 }
4082 console.log('destination should be ' + destination.nodeName);
41- }
83+ */
84+
4285} ;
4386
44- function createAnimation ( ) {
87+ function displayRailsMiddlewareStack ( ) {
4588 LINE_ATTRS = { stroke : '#aaa' , 'stroke-width' : 5 } ;
4689 NODE_ATTRS = { stroke : '#aaa' , 'stroke-width' : 2 , fill : 'white' } ;
4790 NODE_RADIUS = 50 ;
@@ -50,35 +93,41 @@ function createAnimation() {
5093 ARM_LEN = 50 ;
5194
5295 var p = new Raphael ( document . getElementById ( 'canvas_container' ) , 1000 , 500 ) ;
53- var requestBall = p . circle ( 0 , HORIZON , 5 ) . attr ( { fill : 'red' } ) ;
96+ var middlewares = new MiddlewareStack ( ) ;
97+ var animation1 ;
98+ var animation2 ;
5499
55- function makeNodes ( ) {
100+ // Eventually this will be specific for each set of animations.
101+ // We will add nodes representing each of the middlewares in the stack we are displaying.
102+ // For now, just make 4 generic nodes to play with while I figure out my API.
103+ var configureMiddleware = function ( middlewares ) {
104+ var middlewares = middlewares ;
56105 for ( var i = 0 ; i < 4 ; i += 1 ) {
57106 // Create new node with paper, nodeName, and startX parameters
58- node [ i ] = new Node ( p , "NODE " + ( i + 1 ) , 200 * i ) ; // cheat and display nodes as if we started counting arrays at 1
59- // If this isn't the first node, set it's previous attribute to previous node
60- if ( i > 0 ) { node [ i ] . previous = node [ i - 1 ] ; }
61- // And update previous node to know this node as 'next'
62- if ( i > 0 ) { node [ i - 1 ] . next = node [ i ] ; }
63- node [ i ] . draw ( ) ;
107+ middlewares . addNode ( new Node ( p , "NODE " + ( i + 1 ) , 200 * i ) ) ;
64108 } ;
65- } ;
66-
67- function animateRequestBall ( destination ) {
68- var centerOfNode = destination . startX + ARM_LEN + NODE_RADIUS ;
69- var anim = Raphael . animation ( { cx : centerOfNode , cy : HORIZON } , SPEED , destination . highlightNode ) ;
70- requestBall . animate ( anim ) ;
109+ return middlewares ;
71110 }
72111
73- var node = [ ] ;
74- makeNodes ( ) ;
75- destination = node [ 0 ] ;
112+ animation1 = function ( ) {
113+ var anim = new Animation ( p ) ;
114+ anim . addStep ( middlewares . nodes [ 1 ] ) ;
115+ anim . addStep ( middlewares . nodes [ 2 ] ) ;
116+ anim . addStep ( middlewares . nodes [ 3 ] ) ;
117+ anim . addStep ( middlewares . nodes [ 2 ] ) ;
118+ anim . addStep ( middlewares . nodes [ 1 ] ) ;
119+ return anim ;
120+ } ;
76121
77- function move ( ) {
78- animateRequestBall ( destination ) ;
122+ var runAnimation = function ( ) {
123+ // CNK can't figure out how to get to animation1 to start running steps
124+ animation1 . run ( ) ;
79125 }
80126
81- $ ( '#canvas_container' ) . on ( "click" , move ) ;
127+ // OK now actually do stuff
128+ configureMiddleware ( middlewares ) ;
129+ middlewares . draw ( ) ;
130+ $ ( '#canvas_container' ) . on ( "click" , runAnimation ) ;
82131} ;
83132
84- $ ( document ) . ready ( createAnimation ) ;
133+ $ ( document ) . ready ( displayRailsMiddlewareStack ) ;
0 commit comments