@@ -7,7 +7,7 @@ $(function() {
77 styles : {
88 dashed : "5,5" ,
99 solid : "0" ,
10- dotted : "2,1"
10+ dotted : "2,1" ,
1111 }
1212 } ,
1313
@@ -20,23 +20,24 @@ $(function() {
2020 this . x_table = d3 . select ( "#x-axis-lines" ) ;
2121 this . selected_element = null ;
2222 this . offset = { x : 0 , y : 0 } ;
23-
2423 this . main_plot = $ ( "#main-plot" ) . main_plot ( "instance" ) ;
2524
25+ //Attach drag event handlers
2626 d3 . select ( "#main-plot-div" ) . on ( "mousemove" , function ( e ) {
2727 self . drag_plot_element ( e ) ;
2828 } ) ;
2929 d3 . select ( "#main-plot-div" ) . on ( "mouseup" , function ( ) {
3030 self . end_dragging ( ) ;
3131 } ) ;
32-
3332 this . y_table . append ( "tbody" ) ;
3433 this . x_table . append ( "tbody" ) ;
35- console . log ( this . _elements . styles . dashed ) ;
3634 this . add_row ( 0 , "#FF0000" , this . _elements . styles . dashed , this . y_table ) ;
3735 this . add_row ( 0 , "#FF0000" , this . _elements . styles . dashed , this . x_table ) ;
38- d3 . select ( "#reference-axes-layer" ) . attr ( "display" , "none" ) ;
3936
37+ //Hide axes until tab is selected
38+ d3 . select ( "#reference-axes-layer" )
39+ . selectAll ( "*" )
40+ . remove ( ) ;
4041 this . attach_event_handlers ( ) ;
4142 } ,
4243
@@ -50,21 +51,24 @@ $(function() {
5051 } ,
5152
5253 start_dragging : function ( e , element ) {
53- //Update plot stats and assign selected element
54+ //Assign selected element
5455 this . selected_element = element ;
5556 this . offset = this . get_mouse_pos ( e ) ;
5657 } ,
5758
5859 drag_plot_element : function ( e ) {
5960 self = this ;
61+ //If element is being dragged
6062 if ( self . selected_element ) {
6163 var mousePos = this . get_mouse_pos ( e ) ;
6264 if ( this . selected_element . getAttribute ( "class" ) . includes ( "x-reference" ) ) {
6365 var currentX = parseFloat ( this . selected_element . getAttribute ( "x1" ) ) ;
66+ //Check that line is still on plot
6467 if ( this . main_plot . margins . left < currentX && currentX < this . main_plot . width - this . main_plot . margins . right ) {
6568 var newX = currentX + ( mousePos . x - this . offset . x ) ;
6669 this . selected_element . setAttribute ( "x1" , newX + "px" ) ;
6770 this . selected_element . setAttribute ( "x2" , newX + "px" ) ;
71+ //Find the corresponding element in the array and update coordinate
6872 var array = this . _elements . x_lines . find ( x => {
6973 if ( x !== undefined ) {
7074 return x . number === parseInt ( self . selected_element . getAttribute ( "number" ) ) ;
@@ -73,13 +77,13 @@ $(function() {
7377 array . coordinate = parseInt ( this . main_plot . xscale . invert ( newX ) ) ;
7478 }
7579 } else if ( this . selected_element . getAttribute ( "class" ) . includes ( "y-reference" ) ) {
76- console . log ( this . _elements . y_lines ) ;
77-
7880 var currentY = parseFloat ( this . selected_element . getAttribute ( "y2" ) ) ;
81+ //Check that line is still on plot
7982 if ( currentY > this . main_plot . margins . top && currentY < this . main_plot . height - this . main_plot . margins . bottom ) {
8083 var newY = currentY + ( mousePos . y - this . offset . y ) ;
8184 this . selected_element . setAttribute ( "y1" , newY + "px" ) ;
8285 this . selected_element . setAttribute ( "y2" , newY + "px" ) ;
86+ //Find the corresponding element in the array and update coordinate
8387 var array = this . _elements . y_lines . find ( y => {
8488 if ( y !== undefined ) {
8589 return y . number === parseInt ( self . selected_element . getAttribute ( "number" ) ) ;
@@ -88,6 +92,7 @@ $(function() {
8892 array . coordinate = this . main_plot . yscale . invert ( Math . abs ( newY ) ) . toFixed ( 2 ) ;
8993 }
9094 }
95+ //Update offset, tables, and add numbers to plot
9196 this . offset = this . get_mouse_pos ( e ) ;
9297 this . update_tables ( ) ;
9398 this . add_plot_numbers ( ) ;
@@ -101,6 +106,7 @@ $(function() {
101106
102107 attach_event_handlers : function ( ) {
103108 let self = this ;
109+ //Add default rows
104110 this . y_plus . on ( "click" , function ( ) {
105111 self . add_row ( 0 , "#FF0000" , self . _elements . styles . dashed , self . y_table ) ;
106112 } )
@@ -109,21 +115,25 @@ $(function() {
109115 } )
110116 } ,
111117
118+ //Adds a row to x or y table and plots line
112119 add_row : function ( coord , col , style , table ) {
113120 let self = this ;
114121 let lines ;
115-
122+ //Use proper array
116123 if ( table == this . y_table ) {
117124 lines = this . _elements . y_lines ;
118125 } else {
119126 lines = this . _elements . x_lines ;
120127 }
121128 num_rows = table . selectAll ( "tr" ) . size ( ) ;
129+ //Limited to 4 rows so that style selectors are positioned properly
122130 if ( num_rows <= 3 ) {
131+ //Assign array values
123132 let row_number = lines . length ;
124133 lines . push ( { coordinate : coord , color : col , style : style , number : row_number } ) ;
125134 let row = table . append ( "tr" )
126135 . attr ( "number" , row_number ) ;
136+ //Append text input
127137 row . append ( "td" )
128138 . style ( "width" , "30%" )
129139 . append ( "input" )
@@ -135,6 +145,7 @@ $(function() {
135145 lines [ row_number ] . coordinate = parseFloat ( this . value ) ;
136146 self . plot_lines ( ) ;
137147 } ) ;
148+ //Append color input
138149 row . append ( "td" )
139150 . style ( "width" , "30%" )
140151 . append ( "input" )
@@ -144,6 +155,7 @@ $(function() {
144155 lines [ row_number ] . color = this . value ;
145156 self . plot_lines ( ) ;
146157 } ) ;
158+ //Append style column with given style parameter
147159 style_col = row . append ( "td" )
148160 . style ( "width" , "30%" ) ;
149161 style_div = style_col . append ( "div" )
@@ -169,6 +181,7 @@ $(function() {
169181 . attr ( "stroke-width" , "1.5px" )
170182 . attr ( "fill" , "none" )
171183 . attr ( "id" , "diagonal-line" ) ;
184+ //Append style selector with all styles
172185 let style_selector = style_div . append ( "div" )
173186 . classed ( "style-selector" , true ) ;
174187 for ( let s in self . _elements . styles ) {
@@ -206,6 +219,7 @@ $(function() {
206219 style_svg . on ( "click" , function ( ) {
207220 style_selector . style ( "display" , "block" ) ;
208221 } ) ;
222+ //Append remove button
209223 remove = row . append ( "td" )
210224 . style ( "width" , "10%" )
211225 . append ( "svg" )
@@ -237,11 +251,10 @@ $(function() {
237251 self . x_plus . style ( "display" , "block" ) ;
238252 }
239253 }
240- console . log ( number_rows ) ;
241254 self . plot_lines ( ) ;
242255 } ) ;
256+ //If there are more than 4 rows, hide new row button
243257 if ( num_rows >= 3 ) {
244- console . log ( num_rows ) ;
245258 if ( lines == self . _elements . y_lines ) {
246259 self . y_plus . style ( "display" , "none" ) ;
247260 } else if ( lines === self . _elements . x_lines ) {
@@ -254,8 +267,10 @@ $(function() {
254267
255268
256269 plot_lines :function ( ) {
270+ //Remove old lines
257271 d3 . select ( "#reference-axes-layer" ) . selectAll ( "*" ) . remove ( ) ;
258272 let self = this ;
273+ //Add x-axis lines to plot with drag event handlers and labels
259274 for ( let line of self . _elements . x_lines ) {
260275 if ( line != null && ! isNaN ( line . coordinate ) ) {
261276 plotted_line = d3 . select ( "#reference-axes-layer" ) . append ( "line" )
@@ -272,15 +287,9 @@ $(function() {
272287 self . selected_element = this ;
273288 self . start_dragging ( e , d3 . select ( this ) . node ( ) ) ;
274289 } ) ;
275- d3 . select ( "#reference-axes-layer" ) . append ( "text" )
276- . attr ( "x" , self . main_plot . xscale ( line . coordinate ) - 4 )
277- . attr ( "y" , self . main_plot . yscale ( self . main_plot . ymin ) + 8 )
278- . style ( "text-align" , "middle" )
279- . style ( "fill" , line . color )
280- . attr ( "font-size" , "8px" )
281- . text ( line . coordinate ) ;
282290 }
283291 }
292+ //Add y-axis lines to plot with event handlers and labels
284293 for ( let line of self . _elements . y_lines ) {
285294 if ( line != null && ! isNaN ( line . coordinate ) ) {
286295 plotted_line = d3 . select ( "#reference-axes-layer" ) . append ( "line" )
@@ -297,21 +306,16 @@ $(function() {
297306 self . selected_element = this ;
298307 self . start_dragging ( e , d3 . select ( this ) . node ( ) ) ;
299308 } ) ;
300- d3 . select ( "#reference-axes-layer" ) . append ( "text" )
301- . attr ( "x" , self . main_plot . xscale ( self . main_plot . xmax ) + 5 )
302- . attr ( "y" , self . main_plot . yscale ( line . coordinate ) + 4 )
303- . style ( "text-align" , "middle" )
304- . style ( "fill" , line . color )
305- . attr ( "font-size" , "8px" )
306- . text ( line . coordinate ) ;
307309 }
308310 }
309- //Fix the rest of the nucleosome slider to not save variables
311+ this . add_plot_numbers ( ) ;
310312 } ,
311313
312314 add_plot_numbers : function ( ) {
315+ self = this ;
316+ //Adds coord labels to x-axis lines
313317 d3 . select ( "#reference-axes-layer" ) . selectAll ( "text" ) . remove ( ) ;
314- for ( var line of self . _elements . x_lines ) {
318+ for ( let line of self . _elements . x_lines ) {
315319 if ( line !== undefined ) {
316320 d3 . select ( "#reference-axes-layer" ) . append ( "text" )
317321 . attr ( "x" , self . main_plot . xscale ( line . coordinate ) - 4 )
@@ -322,7 +326,8 @@ $(function() {
322326 . text ( line . coordinate ) ;
323327 }
324328 }
325- for ( var line of self . _elements . y_lines ) {
329+ //Adds occupancy labels to y-axis lines
330+ for ( let line of self . _elements . y_lines ) {
326331 if ( line !== undefined ) {
327332 d3 . select ( "#reference-axes-layer" ) . append ( "text" )
328333 . attr ( "x" , self . main_plot . xscale ( self . main_plot . xmax ) + 5 )
@@ -336,28 +341,28 @@ $(function() {
336341 } ,
337342
338343 update_tables :function ( ) {
344+ //Updates tables based on line's position on plot
339345 for ( var line of this . _elements . x_lines ) {
340346 if ( line !== undefined ) {
341- row = this . x_table . selectAll ( "tr" ) . filter ( function ( ) { return parseInt ( d3 . select ( this ) . attr ( "number" ) ) == parseInt ( line . number ) ; } ) ;
347+ row = this . x_table . selectAll ( "tr" ) . filter ( function ( ) { return parseInt ( d3 . select ( this ) . attr ( "number" ) ) == parseInt ( line . number ) ; } ) ;
342348 row . select ( ".coord_input" ) . attr ( "value" , line . coordinate ) ;
343349 }
344350 }
345351 for ( var line of this . _elements . y_lines ) {
346352 if ( line !== undefined ) {
347- row = this . y_table . selectAll ( "tr" ) . filter ( function ( ) { return parseInt ( d3 . select ( this ) . attr ( "number" ) ) == parseInt ( line . number ) ; } ) ;
353+ row = this . y_table . selectAll ( "tr" ) . filter ( function ( ) { return parseInt ( d3 . select ( this ) . attr ( "number" ) ) == parseInt ( line . number ) ; } ) ;
348354 row . select ( ".coord_input" ) . attr ( "value" , line . coordinate ) ;
349355 }
350356 }
351357 } ,
352358
353359 update_all :function ( ) {
354- this . plot_lines ( ) ;
355- this . update_tables ( ) ;
356- this . add_plot_numbers ( ) ;
360+ if ( d3 . select ( "#keep-reference-lines" ) . property ( "checked" ) || d3 . select ( "#reference-axes-tab" ) . classed ( "selected-tab" ) ) {
361+ this . plot_lines ( ) ;
362+ this . update_tables ( ) ;
363+ this . add_plot_numbers ( ) ;
364+ }
357365 }
358366 } ) ;
359-
360-
361-
362367 $ ( "#reference-axes-pane" ) . reference_axes ( ) ;
363368} )
0 commit comments