-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.js
704 lines (584 loc) · 25.7 KB
/
renderer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
const $ = jQuery = require('jquery');
const fsp = require('fs').promises;
const electron = require('electron');
// to communicate with main.js (where Electron window is initialised)
const { ipcRenderer } = electron;
// to display native system dialogs for opening and saving files, alerting, etc.
const dialog = electron.remote.dialog;
// receive messages from main.js file and perform required function
ipcRenderer.on('open-file', () => { openFile() })
.on('save-file', () => { saveFile() })
.on('select-all-cells', () => { $('#tl-cell').trigger('click') })
// rows is an array of cell objects which have their data and properties
let rows = [], preventKeyDown = false;
$(function() {
// scrolling behaviour like professional spreadsheet applications (controlling the row and column headers)
$('.content').on('scroll', function() {
$('.first-row').css('top', $(this).scrollTop());
$('.first-col').css('left', $(this).scrollLeft());
$('#tl-cell').css('top', $(this).scrollTop());
$('#tl-cell').css('left', $(this).scrollLeft());
})
deselectAll();
// prepare a default spreadsheet
$('.grid').find('.row').each(function() {
let cells = [];
$(this).find('.data-cell').each(function() {
let cell = getDefaultCell();
cells.push(cell);
prepareCellSpan(cell, this);
})
rows.push(cells);
})
// ResizeObserver API observe changes to the dimensions of row headers and column headers
// and accordingly change the width or height of cells in a particular row or column
var resizeObserver = new ResizeObserver(function(entries) {
for(let entry of entries) {
let item = entry.target;
if($(item).attr("id")) {
// if item is a column header (A, B, C, D etc.), update width of all cells in this column
if($(item).attr("id") >= "A") {
let ci = parseInt($(item).attr("id").charCodeAt(0) - "A".charCodeAt(0));
$(`.data-cell[data-y=${ci}]`).css('width', $(item).width());
for(let i = 0; i < 100; i++) {
rows[i][ci]["width"] = $(item).width();
}
} else { // if item is a row header (1, 2, 3, 4 etc.), update height of all cells in this row
let ri = parseInt($(item).attr("id"));
$(`.data-cell[data-x=${ri}]`).css('height', $(item).height());
for(let j = 0; j < 26; j++) {
rows[ri][j]["height"] = $(item).height();
}
}
}
}
});
// get all the headers
let headers = document.querySelectorAll('.column-header, .row-header');
// observe resize on these headers
headers.forEach((header) => resizeObserver.observe(header));
/* event handlers to detect change and update properties of a cell */
//--------------------------------------------------------------------------------------------------
$('#font-family').on('change', function() {
let fontFamily = $(this).val();
applyProperty('font-family', fontFamily, 'fontFamily', fontFamily);
})
$('#font-size').on('change', function() {
let fontSize = $(this).val();
applyProperty('font-size', fontSize, 'fontSize', fontSize);
})
$('#bold').on('click', function() {
$(this).toggleClass('selected');
let bold = $(this).hasClass('selected');
applyProperty('font-weight', bold ? 'bold' : 'normal', 'bold', bold);
})
$('#italic').on('click', function() {
$(this).toggleClass('selected');
let italic = $(this).hasClass('selected');
applyProperty('font-style', italic ? 'italic' : 'normal', 'italic', italic);
})
$('#underline').on('click', function() {
$(this).toggleClass('selected');
let underline = $(this).hasClass('selected');
applyProperty('text-decoration', underline ? 'underline' : 'none', 'underline', underline);
})
$('#bg-color').on('change', function() {
let bgColor = $(this).val();
applyProperty('background-color', bgColor, 'bgColor', bgColor);
})
$('#text-color').on('change', function() {
let textColor = $(this).val();
applyProperty('color', textColor, 'textColor', textColor);
})
$('.valign').on('click', function() {
// if already selected, deselect and set default value
if($(this).hasClass('selected')) {
$(this).removeClass('selected');
applyProperty('-webkit-box-align', 'end', 'valign', 'end');
return;
}
$('.valign').removeClass('selected');
$(this).addClass('selected');
let valign = $(this).attr('prop-val');
applyProperty('-webkit-box-align', valign, 'valign', valign);
})
$('.halign').on('click', function() {
// if already selected, deselect and set default value
if($(this).hasClass('selected')) {
$(this).removeClass('selected');
applyProperty('-webkit-box-pack', 'start', 'halign', 'start');
return;
}
$('.halign').removeClass('selected');
$(this).addClass('selected');
let halign = $(this).attr('prop-val');
applyProperty('-webkit-box-pack', halign, 'halign', halign);
})
//--------------------------------------------------------------------------------------------------
// detect keypress in cell range input box, basically, keypress is used here to detect when enter key is pressed
$('#range').on('keypress', function(event) {
if(event.type === 'keypress' && event.which === 13) {
if($(this).val().length <= 5) {
let rh = $(this).val().substr(1), ch = $(this).val().split('')[0].toUpperCase();
let [ri, ci] = getCellPositionFromName(rh, ch);
// click the cell whose position is entered in the range input box
$(`.data-cell[data-x=${ri}][data-y=${ci}]`).trigger('click');
}
}
})
// detect focus or input in formula bar to maintain link between the content of selected cell
// and the content of formula bar
$('#formula').on('focus input', function() {
let rh = parseInt($('#range').val().substr(1));
let ch = $('#range').val().split('')[0].toUpperCase();
let [ri, ci] = getCellPositionFromName(rh, ch);
let cellSpan = `.data-cell[data-x=${ri}][data-y=${ci}]`;
let cell = rows[ri][ci];
let formula = $('#formula').val();
$(cellSpan).trigger('dblclick', false);
$(cellSpan).text(formula);
cell.val = $(cellSpan).text();
}).on('blur keypress', function(event) { // on blur or enter key press in formula bar, apply the formula to selected cell
if(event.type === 'blur' || (event.type === 'keypress' && event.which === 13)) {
let formula = $(this).val();
// if text does not begin with '=', donot process it as a formula, simply return.
if(formula[0] !== '=') {
return;
}
// apply the formula to the cells which are to be edited or if multiple cells are selected
$('.data-cell[contenteditable=true], .data-cell.multiple').each(function() {
let [ri, ci] = getCellPosition(this)
// remove any previous formula from the given cell, remove all its dependencies on other cells and vice versa
if(rows[ri][ci].formula) {
removeFormula(ri, ci);
}
// setup formula property of the cell and setup dependencies with other cells depending upon the formula
setupFormula(ri, ci, formula);
// evaluate the cell formula using its property setup in the above step
let nval = evaluateFormula(ri, ci);
// update the cell values and all its dependencies
updateVal(ri, ci, nval, true);
})
}
})
// the leftmost corner cell, when clicked selects all the cells
$('#tl-cell').on('dblclick', function() {
$('.data-cell.multiple').removeClass('multiple');
$('.first-row .cell').addClass('selected');
$('.first-col .row-header').addClass('selected');
$('.data-cell').addClass('multiple');
})
// select all cells of a row when its row header is clicked
$('.row-header').on('click', function(event) {
deselectAll();
// if Ctrl key is not pressed, then deselect the previous selected cells
if(!event.ctrlKey) {
$('.data-cell.multiple').removeClass('multiple');
$(this).addClass('selected');
$('.first-row .cell').addClass('selected');
}
// select the given row cells
let ri = parseInt($(this).attr("id"));
$(`.data-cell[data-x=${ri}]`).addClass('multiple');
$(`.data-cell[data-x=${ri}]:first`).addClass('selected');
})
// select all cells of a column when its column header is clicked
$('.column-header').on('click', function(event) {
deselectAll();
// if Ctrl key is not pressed, then deselect the previous selected cells
if(!event.ctrlKey) {
$('.data-cell.multiple').removeClass('multiple');
$(this).addClass('selected');
$('.first-row .row-header').addClass('selected');
}
// select the given column cells
let ci = parseInt($(this).attr("id").charCodeAt(0) - "A".charCodeAt(0));
$(`.data-cell[data-y=${ci}]`).addClass('multiple');
$(`.data-cell[data-y=${ci}]:first`).addClass('selected');
})
// handle click and double click event on a cell
$('.data-cell').on('click dblclick', function(event, focus, keydown) {
// if Ctrl key is not pressed and the cell is editable, then prevent cell movement with arrow keys
if(!event.ctrlKey && isCellEditable(this)) {
if(keydown) {
preventKeyDown = false;
} else {
preventKeyDown = true;
}
return;
}
// make editable cells non editable (editability of a cell can be switched any time)
$('.data-cell[contenteditable=true]').css('cursor', 'cell');
$('.data-cell[contenteditable=true]').attr('contenteditable', 'false');
// get the cell which is clicked
let [ri, ci] = getCellPosition(this);
let [rh, ch] = getCellNameAttributes(ri, ci);
let cell = rows[ri][ci];
// handle single click
if(event.type === 'click') {
cellSelectionHandler(event, this, cell, rh, ch);
}
// handle double click
if(event.type === 'dblclick') {
cellFocusHandler(this, focus, keydown);
}
// update range input box
$('#range').val(ch + rh);
// update formula bar with the value of the selected cell or its formula (if any)
if(cell.formula) {
$('#formula').val('=' + cell.formula);
} else {
$('#formula').val(cell.val);
}
})
// handle focus and input events of a cell
$('.data-cell').on('focus input', function(event) {
// if cell is editable, then remove its border selection
if(isCellEditable(this)) {
$(this).removeClass('selected');
}
// get the current cell from rows[]
let [ri, ci] = getCellPosition(this);
let cell = rows[ri][ci];
// update formula bar simultaneously
if(cell.formula) {
$('#formula').val('=' + cell.formula);
} else {
$('#formula').val($(this).text());
}
// before any input is given, clear previous data of this cell
if(event.type === 'focus') {
if(isCellEditable(this)) {
if (document.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(this);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(this);
selection.removeAllRanges();
selection.addRange(range);
}
}
}
if(event.type === 'input') {
// remove the formula immediately (if any) and update the new cell value
if(cell.formula) {
$('#formula').val('');
removeFormula(ri, ci);
}
// update all the other cells dependent on the current cell
updateVal(ri, ci, $(this).text(), false);
}
}).on('blur', function() {
let [ri, ci] = getCellPosition(this);
let val = $(this).text();
// if cell content starts with '=', then process it as a formula and place the final result into the cell
if(val[0] == '=') {
setupFormula(ri, ci, val);
let nval = evaluateFormula(ri, ci);
updateVal(ri, ci, nval, true);
}
})
// detect key events on a selected cell
$('.data-cell').on('keydown', function(event) {
let [ri, ci] = getCellPosition(this);
let key = event.which;
// if key pressed is Tab, Enter, Escape, left arrow, right arrow, up arrow, down arrow
if([9, 13, 27, 37, 38, 39, 40].includes(key)) {
// if cell is currently in editable mode, then do not allow keypress events to interfere
// unless key pressed is either Enter or Tab
if(isCellEditable(this) && key !== 13 && key !== 9 && preventKeyDown) {
return;
}
// So, if the key pressed is either Enter or Tab, make the cell non editable
if(isCellEditable(this)) {
makeCellNonEditable(this);
}
// prevent default behaviour of the keys
event.preventDefault();
// move the cell depending on the key pressed, arrow key movement is obvious, Enter shifts selection down
// Tab shifts selection right
if(key === 37) {
ci -= 1;
} else if(key === 38) {
ri -= 1;
} else if(key === 9 || key === 39) {
ci += 1;
} else {
ri += 1;
}
// if new position is not a valid position, return
if(ri < 0 || ri > 1000 || ci < 0 || ci > 25) {
return;
}
// click the required cell to according to movement
$(`.data-cell[data-x=${ri}][data-y=${ci}]`).trigger('click');
}
// alphanumeric keys when pressed make the current cell to start taking input
else if(key === 32 || (key >= 48 && key <= 57) || (key >= 65 && key <= 120) || (key >= 186 && key <= 222)) {
// if not Ctrl key is pressed, start taking the input. Double click is triggered here as the action is to
// make the cell editable
if(!event.ctrlKey) {
$(`.data-cell[data-x=${ri}][data-y=${ci}]`).trigger('dblclick', true);
}
}
})
// when the application is opened, the first cell (A1) is selected by default
$('.data-cell:first').trigger('click');
})
// to handle task - open an existing file
async function openFile() {
// show a open file dialog where user can choose a file
let dialogObj = await dialog.showOpenDialog();
// if operation is cancelled, return
if(dialogObj.canceled)
return;
// read the file selected
let data = await fsp.readFile(dialogObj.filePaths[0]);
// convert the file into JSON data, as the file contains cell objects
rows = JSON.parse(data);
// fill the data in the spreadsheet by rendering data of each cell
let ri = 0;
$('.grid').find('.row').each(function() {
let ci = 0;
$(this).find('.data-cell').each(function() {
let cell = rows[ri][ci];
prepareCellSpan(cell, this);
ci++;
})
ri++;
})
$('.data-cell:first').trigger('click');
}
// to handle task - save a file
async function saveFile() {
// show a save file dialog where user can type a filename and save it
let dialogObj = await dialog.showSaveDialog();
if(dialogObj.canceled)
return;
// write the rows[] to file
await fsp.writeFile(dialogObj.filePath, JSON.stringify(rows));
}
// apply a property to a cell or group of cells
function applyProperty(property, val, propertyAttr, attrVal) {
$('.data-cell.selected, .data-cell[contenteditable=true], .data-cell.multiple').css(property, val);
$('.data-cell.selected, .data-cell[contenteditable=true], .data-cell.multiple').each(function() {
let [ri, ci] = getCellPosition(this);
rows[ri][ci][propertyAttr] = attrVal;
})
}
// parse and set the formula of a cell and create dependencies in the form of a directed graph data structure
// we can check and remove circular dependencies through toplogical sort (not implemented here)
function setupFormula(ri, ci, formula) {
let cell = rows[ri][ci];
formula = formula.substr(1);
cell.formula = formula;
// remove all the parenthesis
formula = formula.replace('(', '').replace(')', '');
// get components in the formula (dependencies, cell nodes)
let comps = formula.split(' ');
// for each component
for(let i = 0; i < comps.length; i++) {
comps[i][0] = comps[i][0].toUpperCase();
// if the component is valid
if(comps[i].charCodeAt(0) >= "A".charCodeAt(0) && comps[i].charCodeAt(0) <= "Z".charCodeAt(0)) {
let [uri, uci] = getCellPositionFromName(comps[i].substr(1), comps[i][0]);
// upstream property of a cell refers to an array of cells on which a given cell depends
// these components are going to define the value of this cell, so add them in upstream
cell.upstream.push({
ri: uri, ci: uci
})
let upstCell = rows[uri][uci];
// downstream property of a cell refers to an array of cells which are dependent on a given cell
// the current cell is dependent on the components of the formula, so add the current cell to the
// downstream of each component
upstCell.downstream.push({
ri: ri, ci: ci
})
}
}
}
// remove any pre-existing formula and dependencies from a cell
function removeFormula(ri, ci) {
let cell = rows[ri][ci];
cell.formula = '';
// for each upstream cell, remove the current cell from their downstreams
for(let i = 0; i < cell.upstream.length; i++) {
let upstObj = cell.upstream[i];
let upstCell = rows[upstObj.ri][upstObj.ci];
for(let j = 0; j < cell.downstream.length; j++) {
let dwnstObj = upstCell.downstream[j];
if(dwnstObj.ri == ri && dwnstObj.ci == ci) {
upstCell.downstream.splice(j, 1);
break;
}
}
}
// clear the upstream of the current cell
cell.upstream = [];
}
// evaluate the value of a cell formula
function evaluateFormula(ri, ci) {
let cell = rows[ri][ci];
let formula = cell.formula;
// replace the components in the formula by their values
for(let i = 0; i < cell.upstream.length; i++) {
let upstrObj = cell.upstream[i];
let upstCell = rows[upstrObj.ri][upstrObj.ci];
let [rh, ch] = getCellNameAttributes(upstrObj.ri, upstrObj.ci);
formula = formula.replace(ch + rh, upstCell.val || 0);
}
// evaluate the converted expression using inbuilt eval() function
// we can also calculate the result using infix evaluation method (uses a stack)
return eval(formula);
}
// reflect the changed values in the cell dependencies
function updateVal(ri, ci, val, render) {
let cell = rows[ri][ci];
cell.val = val;
// is the value is to be rendered
if(render) {
$(`.data-cell[data-x=${ri}][data-y=${ci}]`).text(val);
}
// update the value in each downstream cell
for(let i = 0; i < cell.downstream.length; i++) {
let dwnstObj = cell.downstream[i];
// work recursively to reach all nodes
let nval = evaluateFormula(dwnstObj.ri, dwnstObj.ci);
updateVal(dwnstObj.ri, dwnstObj.ci, nval, true);
}
}
// deselect row header, column header and cell
function deselectAll() {
$('.column-header.selected, .row-header.selected').removeClass('selected');
$('.data-cell.selected').removeClass('selected');
}
// return the default cell object
function getDefaultCell() {
return {
val: '',
fontFamily: 'Product Sans Regular',
fontSize: '14',
bold: false,
italic: false,
underline: false,
bgColor: '#FFFFFF',
textColor: '#000000',
valign: 'end',
halign: 'left',
formula: '',
upstream: [],
downstream: [],
height: 20,
width: 90
}
}
// prepare a cell <span></span> by setting all the properties in UI
function prepareCellSpan(cell, cellSpan) {
$(cellSpan).html(cell.val);
$(cellSpan).css({'font-family':cell.fontFamily,
'font-size':cell.fontSize + 'px',
'font-weight':(cell.bold ? 'bold' : 'normal'),
'font-style':(cell.italic ? 'italic' : 'normal'),
'text-decoration':(cell.underline ? 'underline' : 'none'),
'background-color':cell.bgColor,
'color':cell.textColor,
'-webkit-box-pack':cell.halign,
'-webkit-box-align': cell.valign,
'height': cell.height,
'width': cell.width
});
let [ri, ci] = getCellPosition(cellSpan);
// set the height and width of row and column headers according to the cells
$( `.row-header[id=${ri}]`).css('height', cell.height);
$(`.column-header[data-y=${ci}]`).css('width', cell.width);
}
// return cell indices if cell HTML element is provided
function getCellPosition(cellSpan) {
let ri = parseInt($(cellSpan).attr('data-x'));
let ci = parseInt($(cellSpan).attr('data-y'));
return [ri, ci];
}
// convert cell name to cell indices (parameters in terms of row header and column header, ex - 10, E)
function getCellPositionFromName(rh, ch) {
return [parseInt(rh) - 1, parseInt(ch.charCodeAt(0) - "A".charCodeAt(0))]
}
// convert cell indices to cell name
function getCellNameAttributes(ri, ci) {
return [(parseInt(ri) + 1).toString(),
String.fromCharCode("A".charCodeAt(0) + parseInt(ci))];
}
// return true if cell can be edited, otherwise false
function isCellEditable(cellSpan) {
return $(cellSpan).attr('contenteditable') === 'true';
}
// to make the cell non editable temporarily
function makeCellNonEditable(cellSpan) {
$(cellSpan).attr('contenteditable', 'false');
$(cellSpan).css('cursor', 'cell');
}
// when a cell is clicked single time
function cellSelectionHandler(event, cellSpan, cell, rh, ch) {
deselectAll();
// add border selection to the cell
$(cellSpan).addClass('selected');
// Focus the cell to listen for keydown and other events
$(cellSpan).trigger('focus');
// if Ctrl key is not pressed, deselect the previous multiple selection
if(!event.ctrlKey) {
$('.data-cell.multiple').removeClass('multiple');
} else {
// otherwise, add the current cell into multiple selection. If already a part of multiple selection, deselect it
$(cellSpan).toggleClass('multiple');
}
// if Ctrl key is not pressed or there is only one cell in multiple selection, highlight the row header and column header.
// When a cell is clicked, its row header and column header are shown as selected
// filter function helps in finding out the matching row and column headers
if(!event.ctrlKey || $('.data-cell.multiple').length === 1) {
$(`.first-row .cell:contains(${ch})`).filter(function() {
return $(this).text() === ch;
}).addClass('selected');
$(`.first-col .row-header:contains(${rh})`).filter(function() {
return $(this).text() === rh;
}).addClass('selected');
}
/* show the cell properties in UI */
//--------------------------------------------------------------------------------------------------
$('#font-family').val(cell.fontFamily);
$('#font-size').val(cell.fontSize);
if(cell.bold) {
$('#bold').addClass('selected');
} else {
$('#bold').removeClass('selected');
}
if(cell.italic) {
$('#italic').addClass('selected');
} else {
$('#italic').removeClass('selected');
}
if(cell.underline) {
$('#underline').addClass('selected');
} else {
$('#underline').removeClass('selected');
}
$('#bg-color').val(cell.bgColor);
$('#text-color').val(cell.textColor);
$('.valign').removeClass('selected');
$(`.valign[prop-val=${cell.valign}`).addClass('selected');
$('.halign').removeClass('selected');
$(`.halign[prop-val=${cell.halign}`).addClass('selected');
//--------------------------------------------------------------------------------------------------
}
// when the cell is focus (double clicked)
function cellFocusHandler(cellSpan, focus) {
// remove cell selection and make it editable
$(cellSpan).removeClass('selected');
$(cellSpan).attr('contenteditable', 'true');
$(cellSpan).css('cursor', 'text');
// focus is an additional parameter to provide focus to the cell explicitly if required
if(focus) {
$(cellSpan).trigger('focus');
}
}