1+ var $table = null ;
2+
3+ function Array2DTracer ( module ) {
4+ if ( Tracer . call ( this , module || Array2DTracer ) ) {
5+ initTable ( ) ;
6+ return true ;
7+ }
8+ return false ;
9+ }
10+
11+ Array2DTracer . prototype = Object . create ( Tracer . prototype ) ;
12+ Array2DTracer . prototype . constructor = Array2DTracer ;
13+
14+ // Override
15+ Array2DTracer . prototype . resize = function ( ) {
16+ Tracer . prototype . resize . call ( this ) ;
17+
18+ var $parent = $table . parent ( ) ;
19+ $table . css ( 'margin-top' , $parent . height ( ) / 2 - $table . height ( ) / 2 ) ;
20+ } ;
21+
22+ // Override
23+ Array2DTracer . prototype . clear = function ( ) {
24+ Tracer . prototype . clear . call ( this ) ;
25+
26+ clearTableColor ( ) ;
27+ } ;
28+
29+ Array2DTracer . prototype . createRandomData = function ( N , M , min , max ) {
30+ if ( ! N ) N = 10 ;
31+ if ( ! M ) M = 10 ;
32+ if ( min === undefined ) min = 1 ;
33+ if ( max === undefined ) max = 9 ;
34+ var D = [ ] ;
35+ for ( var i = 0 ; i < N ; i ++ ) {
36+ D . push ( [ ] ) ;
37+ for ( var j = 0 ; j < M ; j ++ ) {
38+ D [ i ] . push ( ( Math . random ( ) * ( max - min + 1 ) | 0 ) + min ) ;
39+ }
40+ }
41+ return D ;
42+ } ;
43+
44+ // Override
45+ Array2DTracer . prototype . setData = function ( D ) {
46+ if ( Tracer . prototype . setData . call ( this , arguments ) ) return true ;
47+
48+ $table . empty ( ) ;
49+ for ( var i = 0 ; i < D . length ; i ++ ) {
50+ var $row = $ ( '<div class="mtbl-row">' ) ;
51+ $table . append ( $row ) ;
52+ for ( var j = 0 ; j < D [ i ] . length ; j ++ ) {
53+ var $cell = $ ( '<div class="mtbl-cell">' ) . text ( D [ i ] [ j ] ) ;
54+ $row . append ( $cell ) ;
55+ }
56+ }
57+ this . resize ( ) ;
58+
59+ return false ;
60+ } ;
61+
62+ Array2DTracer . prototype . _change = function ( sx , sy , ex , ey ) {
63+ if ( sx instanceof Array ) {
64+ this . pushStep ( { type : 'select' , coords : sx , color : tableColor . changed } , true ) ;
65+ this . pushStep ( { type : 'deselect' , coords : sx } , false ) ;
66+ } else if ( ex !== undefined && ey !== undefined ) {
67+ this . pushStep ( { type : 'select' , sx : sx , sy : sy , ex : ex , ey : ey , color : tableColor . changed } , true ) ;
68+ this . pushStep ( { type : 'deselect' , sx : sx , sy : sy , ex : ex , ey : ey } , false ) ;
69+ } else {
70+ this . pushStep ( { type : 'select' , x : sx , y : sy , color : tableColor . changed } , true ) ;
71+ this . pushStep ( { type : 'deselect' , x : sx , y : sy } , false ) ;
72+ }
73+ } ;
74+
75+ Array2DTracer . prototype . _changeRow = function ( x , sy , ey ) {
76+ this . pushStep ( { type : 'select' , x : x , sy : sy , ey : ey , color : tableColor . changed } , true ) ;
77+ this . pushStep ( { type : 'deselect' , x : x , sy : sy , ey : ey } , false ) ;
78+ } ;
79+
80+ Array2DTracer . prototype . _changeCol = function ( y , sx , ex ) {
81+ this . pushStep ( { type : 'select' , y : y , sx : sx , ex : ex , color : tableColor . changed } , true ) ;
82+ this . pushStep ( { type : 'deselect' , y : y , sx : sx , ex : ex } , false ) ;
83+ } ;
84+
85+ Array2DTracer . prototype . _select = function ( sx , sy , ex , ey ) {
86+ if ( sx instanceof Array ) {
87+ this . pushStep ( { type : 'select' , coords : sx } , true ) ;
88+ } else if ( ex !== undefined && ey !== undefined ) {
89+ this . pushStep ( { type : 'select' , sx : sx , sy : sy , ex : ex , ey : ey } , true ) ;
90+ } else {
91+ this . pushStep ( { type : 'select' , x : sx , y : sy } , true ) ;
92+ }
93+ } ;
94+
95+ Array2DTracer . prototype . _selectRow = function ( x , sy , ey ) {
96+ this . pushStep ( { type : 'select' , x : x , sy : sy , ey : ey } , true ) ;
97+ } ;
98+
99+ Array2DTracer . prototype . _selectCol = function ( y , sx , ex ) {
100+ this . pushStep ( { type : 'select' , y : y , sx : sx , ex : ex } , true ) ;
101+ } ;
102+
103+ Array2DTracer . prototype . _deselect = function ( sx , sy , ex , ey ) {
104+ if ( sx instanceof Array ) {
105+ this . pushStep ( { type : 'deselect' , coords : sx } , true ) ;
106+ } else if ( ex !== undefined && ey !== undefined ) {
107+ this . pushStep ( { type : 'deselect' , sx : sx , sy : sy , ex : ex , ey : ey } , true ) ;
108+ } else {
109+ this . pushStep ( { type : 'deselect' , x : sx , y : sy } , true ) ;
110+ }
111+ } ;
112+
113+ Array2DTracer . prototype . _deselectRow = function ( x , sy , ey ) {
114+ this . pushStep ( { type : 'deselect' , x : x , sy : sy , ey : ey } , true ) ;
115+ } ;
116+
117+ Array2DTracer . prototype . _deselectCol = function ( y , sx , ex ) {
118+ this . pushStep ( { type : 'deselect' , y : y , sx : sx , ex : ex } , true ) ;
119+ } ;
120+
121+ Array2DTracer . prototype . processStep = function ( step , options ) {
122+ switch ( step . type ) {
123+ case 'select' :
124+ case 'deselect' :
125+ var select = step . type == 'select' ;
126+ var color = select ? step . color !== undefined ? step . color : tableColor . selected : tableColor . default ;
127+ if ( step . coords ) {
128+ step . coords . forEach ( function ( coord ) {
129+ var x = coord . x ;
130+ var y = coord . y ;
131+ paintColor ( x , y , x , y , color ) ;
132+ } ) ;
133+ } else {
134+ var sx = step . sx ;
135+ var sy = step . sy ;
136+ var ex = step . ex ;
137+ var ey = step . ey ;
138+ if ( sx === undefined ) sx = step . x ;
139+ if ( sy === undefined ) sy = step . y ;
140+ if ( ex === undefined ) ex = step . x ;
141+ if ( ey === undefined ) ey = step . y ;
142+ paintColor ( sx , sy , ex , ey , color ) ;
143+ }
144+ break ;
145+ }
146+ } ;
147+
148+ // Override
149+ Array2DTracer . prototype . prevStep = function ( ) {
150+ this . clear ( ) ;
151+ $ ( '#tab_trace .wrapper' ) . empty ( ) ;
152+ var finalIndex = this . traceIndex - 1 ;
153+ if ( finalIndex < 0 ) {
154+ this . traceIndex = - 1 ;
155+ return ;
156+ }
157+ for ( var i = 0 ; i < finalIndex ; i ++ ) {
158+ this . step ( i , { virtual : true } ) ;
159+ }
160+ this . step ( finalIndex ) ;
161+ } ;
162+
163+ var initTable = function ( ) {
164+ $ ( '.module_container' ) . empty ( ) ;
165+ $table = $ ( '<div class="mtbl-table">' ) ;
166+ $ ( '.module_container' ) . append ( $table ) ;
167+ } ;
168+
169+ var tableColor = {
170+ selected : '#0ff' ,
171+ changed : '#f00' ,
172+ default : ''
173+ } ;
174+
175+ var paintColor = function ( sx , sy , ex , ey , color ) {
176+ for ( var i = sx ; i <= ex ; i ++ ) {
177+ var $row = $table . find ( '.mtbl-row' ) . eq ( i ) ;
178+ for ( var j = sy ; j <= ey ; j ++ ) {
179+ $row . find ( '.mtbl-cell' ) . eq ( j ) . css ( 'background' , color ) ;
180+ }
181+ }
182+ } ;
183+
184+ var clearTableColor = function ( ) {
185+ $table . find ( '.mtbl-cell' ) . css ( 'background' , '' ) ;
186+ } ;
0 commit comments