@@ -20,6 +20,8 @@ class Config {
2020 * @property {number } numColumns - Number of columns in the table
2121 * @property {string } rowsName - Name of rows in the table
2222 * @property {string } columnsName - Name of columns in the table
23+ * @property {bool } canEditRowHeader - Are the row names editable?
24+ * @property {bool } canEditColumnHeader - Are the column names editable?
2325 * @property {object } tableIds - Container for all the table's magic strings
2426 * @property {object } datumConfig - Container for default values of a cell
2527 */
@@ -34,6 +36,8 @@ class Config {
3436 this . rowsNamePlural = this . rowsName + "s" ;
3537 this . columnsNamePlural = this . columnsName + "s" ;
3638 this . wrapperDivId = clientConfig . wrapperDivId ;
39+ this . canEditRowHeader = true ;
40+ this . canEditColumnHeader = false ;
3741
3842 /**
3943 * @property {string } tableDivId - ID for the div containing the table
@@ -205,22 +209,52 @@ function createColumnHeader(config) {
205209 cell . id = cellIndexToElementId ( config . tableIds . theadElementId , 0 , colIndex )
206210 cell . classList . add ( "data-table-cell" ) ;
207211
208- let cellText = colIndex === 0 ? config . rowsNamePlural : config . columnsName + " " + colIndex ;
209- cell . appendChild ( document . createTextNode ( cellText ) ) ;
212+ cell . appendChild ( createColumnHeaderCell ( config , colIndex ) ) ;
210213 }
211214
212215 config . currNumColumns = config . defaultNumColumns + 1 ;
213216 config . currNumRows = 1 ;
214217}
215218
219+ function createColumnHeaderCell ( config , colIndex ) {
220+ if ( colIndex === 0 ) {
221+ return document . createTextNode ( config . rowsNamePlural ) ;
222+ }
223+ if ( ! config . canEditColumnHeader ) {
224+ let cellText = config . columnsName + " " + colIndex ;
225+ return document . createTextNode ( cellText ) ;
226+ }
227+
228+ // Creates the field for text input
229+ let input = document . createElement ( "INPUT" ) ;
230+ input . type = 'text' ;
231+ input . placeholder = config . columnsName ;
232+ input . classList . add ( 'table-entry-field' ) ;
233+ return input ;
234+ }
235+
236+ function createRowHeaderCell ( config , rowIndex ) {
237+ if ( ! config . canEditRowHeader ) {
238+ let cellText = config . rowsName + " " + rowIndex ;
239+ return document . createTextNode ( cellText ) ;
240+ }
241+
242+ // Creates the field for text input
243+ let input = document . createElement ( "INPUT" ) ;
244+ input . type = 'text' ;
245+ input . placeholder = config . rowsName ;
246+ input . classList . add ( 'table-entry-field' ) ;
247+ return input ;
248+ }
249+
216250/**
217251 * Adds a single column to the table
218252 * @param {object } config - Table configuration object
219253 * @param {String } [content] - Content to place in the top cell of a column (uses default if
220254 * no string is provided)
221255 * @returns {undefined } - Doesn't return anything
222256 */
223- function addSingleColumn ( config , content ) {
257+ function addSingleColumn ( config ) {
224258 let table = document . getElementById ( config . tableIds . tableElementId ) ;
225259 let numRows = config . currNumRows ;
226260 let numCols = config . currNumColumns ;
@@ -229,10 +263,7 @@ function addSingleColumn(config, content) {
229263 cell . id = cellIndexToElementId ( config . tableIds . theadElementId , 0 , numCols ) ;
230264 cell . classList . add ( "data-table-cell" ) ;
231265
232- let text = content || config . columnsName + " " + numCols ;
233- let middle = document . createTextNode ( text ) ;
234-
235- cell . appendChild ( middle ) ;
266+ cell . appendChild ( createColumnHeaderCell ( config , numCols ) ) ;
236267
237268 for ( let rowIndex = 1 ; rowIndex < numRows ; rowIndex ++ ) {
238269 createEntryCell ( config , table . rows [ rowIndex ] , rowIndex , numCols ) ;
@@ -264,15 +295,15 @@ function deleteSingleColumn(config) {
264295 * no string is provided)
265296 * @returns {undefined } - Doesn't return anything
266297 */
267- function addSingleRow ( config , content ) {
298+ function addSingleRow ( config ) {
268299 // Insert a row into the body of the table
269- let rowIndex = config . currNumRows - 1 ;
270- let row = document . getElementById ( config . tableIds . tbodyElementId ) . insertRow ( rowIndex ) ;
300+ let rowIndex = config . currNumRows ;
301+ let row = document . getElementById ( config . tableIds . tbodyElementId ) . insertRow ( rowIndex - 1 ) ;
271302 // Then for each column
272303 for ( let colIndex = 0 ; colIndex < config . currNumColumns ; colIndex ++ ) {
273304 // Create an entry cell
274305 if ( colIndex === 0 ) {
275- createRowHeader ( config , row , rowIndex , colIndex , content )
306+ createRowHeader ( config , row , rowIndex , colIndex )
276307 } else {
277308 createEntryCell ( config , row , rowIndex , colIndex )
278309 }
@@ -282,17 +313,11 @@ function addSingleRow(config, content) {
282313}
283314
284315// eslint-disable-next-line max-params
285- function createRowHeader ( config , row , rowIndex , colIndex , content ) {
286-
316+ function createRowHeader ( config , row , rowIndex , colIndex ) {
287317 let cell = row . insertCell ( colIndex ) ;
288318 cell . id = cellIndexToElementId ( config . wrapperDivId , rowIndex , colIndex )
289319 cell . classList . add ( "data-table-cell" ) ;
290-
291- if ( content === undefined ) {
292- cell . innerHTML = ( rowIndex + 1 ) ;
293- } else {
294- cell . innerHTML = content ;
295- }
320+ cell . appendChild ( createRowHeaderCell ( config , rowIndex ) ) ;
296321}
297322
298323/**
@@ -514,28 +539,14 @@ function createColumnEntryBox(config) {
514539 */
515540function createRowOrColumnInputAndBtn ( config , leftPanelInfo ) {
516541 let entryBoxDiv = document . getElementById ( config . entryIds . entryBoxDivId ) ;
517- // Creates the field for text input
518- let input = document . createElement ( "INPUT" ) ;
519- input . type = 'text' ;
520- input . id = leftPanelInfo . entryID ;
521- input . placeholder = leftPanelInfo . name ;
522- input . classList . add ( 'table-entry-field' ) ;
523-
524- // If the user hits enter while in the text box, click the addButton
525- input . addEventListener ( "onClick" , function ( ) {
526- addButton . click ( ) ;
527- } )
528- entryBoxDiv . appendChild ( input ) ;
529542
530543 // Creates the button that will take the user input and send it to addSingleColumn() when clicked
531544 let addButton = document . createElement ( "button" ) ;
532545 addButton . classList . add ( "left-panel-button" ) ;
533546 addButton . innerHTML = "+ Add " + leftPanelInfo . name . toLowerCase ( ) +
534547 " to " + leftPanelInfo . endDirection ;
535548 addButton . onclick = function ( ) {
536- let value = input . value . trim ( ) ;
537- leftPanelInfo . addEntryFunction ( config , value ) ;
538- input . value = '' ;
549+ leftPanelInfo . addEntryFunction ( config ) ;
539550 }
540551 entryBoxDiv . appendChild ( addButton ) ;
541552}
0 commit comments