-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
495 lines (361 loc) · 15.2 KB
/
script.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
let defaultprop = {
"text": "",
"font weight": "",
"font-style": "",
"text-decoration": "",
"text-align": "left",
"background-color": "#ffffff",
"color": "#000000",
"font-family": "Noto Sans",
"font-size": "14px"
}
//used for storing update data
let cellData = {
"Sheet1": {
}
}
let selectedSheet = "Sheet1";
let totalSheet = 1;
let lastaddsheet = 1;
$(document).ready(function() {
let cellContainer = $('input-area');
for (let i = 1; i <= 100; i++) {
let ans = "";
let n = i;
while (n > 0) {
let rem = n % 26;
if (rem == 0) {
ans = "Z" + ans;
n = Math.floor(n / 26) - 1;
} else {
ans = String.fromCharCode(rem - 1 + 65) + ans;
n = Math.floor(n / 26);
}
}
let column = $(`<div class="column-name colid-${i} " id="colcod-${ans} ">${ans}</div>`);
$(".column").append(column);
let row = $(`<div class="row-name rowid-${i}">${i}</div>`)
$(".row").append(row);
}
for (let i = 1; i <= 100; i++) {
let row = $(`<div class="cell-row "></div?>`) //create a row
for (let j = 1; j <= 100; j++) {
let colcode = $(`.colid-${j}`).attr("id").split("-")[1];
let column = $(`<div class="input-cell " contenteditable="true" id="row-${i}-col-${j}" data="code-${colcode}"></div>`)
row.append(column);
}
$(".input-area").append(row);
}
$(".align-icon").click(function() {
$(".align-icon.selected").removeClass("selected");
$(this).addClass("selected");
// for left format/right/center
});
$(".on-icon").click(function() {
$(this).toggleClass("selected"); //on h to off vice versa
// $(this).addClass("selected");
// for bold italic underline on and off
});
$(".input-cell").click(function(e) {
if (e.ctrlKey) {
let [rowId, colId] = getRowCol(this);
if (rowId > 1) {
let topCellSelected = $(`#row-${rowId-1}-col-${colId}`).hasClass("selected");
if (topCellSelected) {
// console.log(rowId - 1, colId);
$(this).addClass("top-cell-selected");
$(`#row-${rowId-1}-col-${colId}`).addClass("bottom-cell-selected");
}
}
if (rowId < 100) {
let bottomCellSelected = $(`#row-${rowId+1}-col-${colId}`).hasClass("selected");
if (bottomCellSelected) {
$(this).addClass("bottom-cell-selected");
$(`#row-${rowId+1}-col-${colId}`).addClass("top-cell-selected");
}
}
if (colId > 1) {
let leftCellSelected = $(`#row-${rowId}-col-${colId-1}`).hasClass("selected");
if (leftCellSelected) {
$(this).addClass("left-cell-selected");
$(`#row-${rowId}-col-${colId-1}`).addClass("right-cell-selected");
}
}
if (colId < 100) {
let rightCellSelected = $(`#row-${rowId}-col-${colId+1}`).hasClass("selected");
if (rightCellSelected) {
$(this).addClass("right-cell-selected");
$(`#row-${rowId}-col-${colId+1}`).addClass("left-cell-selected");
}
}
} else {
$(".input-cell.selected").removeClass("selected");
}
$(this).addClass("selected");
changeHeader(this);
});
//cell change krne pe header m property change like bold italic selected
function changeHeader(ele) {
let [rowId, colId] = getRowCol(ele);
//check if properties present or not
let cellInfo = defaultprop;
if (cellData[selectedSheet][rowId] && cellData[selectedSheet][rowId][colId]) {
cellInfo = cellData[selectedSheet][rowId][colId];
}
//cell info nikalne k baad usa lga do agr nhi lgi to
cellInfo['font-weight'] ? $(".icon-bold").addClass("selected") : $(".icon-bold").removeClass("selected");
cellInfo['font-style'] ? $(".icon-italic").addClass("selected") : $(".icon-italic").removeClass("selected");
cellInfo['text-decoration'] ? $(".icon-underline").addClass("selected") : $(".icon-underline").removeClass("selected");
let aligment = cellInfo["text-align"];
$(".align-icon.selected").removeClass("selected");
$("icon-align-" + aligment).addClass("selected");
$(".background-color-picker").val(cellInfo["background-color"]);
$(".text-color-picker").val(cellInfo["color"]);
$(".font-family-selector").val(cellInfo["font-family"]);
$(".font-family-selctor").css("font-family", cellInfo["font-family"]);
$(".font-size-selector").val(cellInfo["font-size"]);
}
//content editable
$(".input-cell").dblclick(function() {
$(".input-cell.selected").removeClass("selected");
$(this).attr("contenteditable", "true");
$(this).focus();
});
$(".input-cell").blur(function() {
$(".input-cell.selected").attr("contenteditable", "false");
//storing text
updateCell("text", $(this).text());
})
//scroll left -> x axis
// scroll up down ->y axis
$(".input-area").scroll(function() {
console.log(this.scrollLeft);
$(".column").scrollLeft(this.scrollLeft);
$(".row").scrollTop(this.scrollTop);
});
});
// gives current row-col id
function getRowCol(ele) {
let idArray = $(ele).attr("id").split("-");
let rowId = parseInt(idArray[1]);
let colId = parseInt(idArray[3]);
return [rowId, colId];
}
function updateCell(property, value, defaultPossible) {
$(".input-cell.selected").each(function() {
$(this).css(property, value);
let [rowId, colId] = getRowCol(this);
if (cellData[selectedSheet][rowId]) {
if (cellData[selectedSheet][rowId][colId]) {
cellData[selectedSheet][rowId][colId][property] = value;
} else {
cellData[selectedSheet][rowId][colId] = {...defaultprop };
cellData[selectedSheet][rowId][colId][property] = value;
}
} else {
cellData[selectedSheet][rowId] = {};
cellData[selectedSheet][rowId][colId] = {...defaultprop };
cellData[selectedSheet][rowId][colId][property] = value;
}
if (defaultPossible && (JSON.stringify(cellData[selectedSheet][rowId][colId]) === JSON.stringify(defaultPossible))) {
delete cellData[selectedSheet][rowId][colId];
if (Object.keys(cellData[selectedSheet][rowId]).length == 0) {
delete cellData[selectedSheet][rowId];
}
}
});
console.log(cellData);
}
$(".icon-bold").click(function() {
if ($(this).hasClass("selected")) {
updateCell("font-weight", "", true); //phle s bold h to remove
} else {
updateCell("font-weight", "bold", false); //bold kr dya
}
})
$(".icon-italic").click(function() {
if ($(this).hasClass("selected")) {
updateCell("font-style", "", true); //phle s bold h to remove
} else {
updateCell("font-style", "italic", false); //bold kr dya
}
})
$(".icon-underline").click(function() {
if ($(this).hasClass("selected")) {
updateCell("text-decoration", "", true); //phle s bold h to remove
} else {
updateCell("text-decoration", "underline", false); //bold kr dya
}
});
$(".icon-align-left").click(function() {
if (!$(this).hasClass("selected ")) {
updateCell("text-align", "left", true);
}
})
$(".icon-align-center").click(function() {
if (!$(this).hasClass("selected ")) {
updateCell("text-align", "center", true);
}
})
$(".icon-align-right").click(function() {
if (!$(this).hasClass("selected ")) {
updateCell("text-align", "right", true);
}
});
$(".color-fill-icon").click(function() {
$(".background-color-picker").click();
})
$(".color-text-icon").click(function() {
$(".text-color-picker").click();
});
$(".background-color-picker").change(function() {
updateCell("background-color", $(this).val());
})
$(".text-color-picker").change(function() {
updateCell("color", $(this).val());
});
$(".font-family-selector").change(function() {
updateCell("font-family", $(this).val());
$("font-family-selctor").css("font-family", $(this).val());
})
$(".font-size-selector").change(function() {
updateCell("font-size", $(this).val());
});
//for new sheet -->empty data in new sheet
function emptySheet() {
let sheetInfo = cellData[selectedSheet];
for (let i of Object.keys(sheetInfo)) {
for (let j of Object.keys(sheetInfo[i])) {
$(`#row-${i}-col-${j}`).text("");
$(`#row-${i}-col-${j}`).css("background-color", "#ffffff");
$(`#row-${i}-col-${j}`).css("color", "#000000");
$(`#row-${i}-col-${j}`).css("text-align", "left");
$(`#row-${i}-col-${j}`).css("font-weight", "");
$(`#row-${i}-col-${j}`).css("font-style", "");
$(`#row-${i}-col-${j}`).css("text-decoration", "");
$(`#row-${i}-col-${j}`).css("font-family", "Noto Sans");
$(`#row-${i}-col-${j}`).css("font-size", "14px");
}
}
}
//add data in sheet
function loadSheet() {
let sheetInfo = cellData[selectedSheet];
for (let i of Object.keys(sheetInfo)) {
for (let j of Object.keys(sheetInfo[i])) {
let cellInfo = cellData[selectedSheet][i][j];
$(`#row-${i}-col-${j}`).text(cellInfo["text"]);
$(`#row-${i}-col-${j}`).css("background-color", cellInfo["background-color"]);
$(`#row-${i}-col-${j}`).css("color", cellInfo["color"]);
$(`#row-${i}-col-${j}`).css("text-align", cellInfo["text-align"]);
$(`#row-${i}-col-${j}`).css("font-weight", cellInfo["font-weight"]);
$(`#row-${i}-col-${j}`).css("font-style", cellInfo["font-style"]);
$(`#row-${i}-col-${j}`).css("text-decoration", cellInfo["text-decoration"]);
$(`#row-${i}-col-${j}`).css("font-family", cellInfo["font-family"]);
$(`#row-${i}-col-${j}`).css("font-size", cellInfo["font-size"]);
}
}
}
//add sheet in ui
$(".icon-add").click(function() {
emptySheet(); //empty sheet
$(".sheet-tab.selected").removeClass("selected"); //remove current selected sheet
let sheetName = "Sheet" + (lastaddsheet + 1);
cellData[sheetName] = {};
totalSheet += 1;
lastaddsheet += 1;
selectedSheet = sheetName;
$(".sheet-tab-container").append(` <div class="sheet-tab selected">${sheetName}</div>`);
addSheetEvents();
});
function addSheetEvents() {
$(".sheet-tab.selected").click(function() {
if (!$(this).hasClass("selected ")) {
selectSheet(this);
}
});
$(".sheet-tab.selected").contextmenu(function(e) {
e.preventDefault();
//default right click off
// console.log("hello");
// add custom modal
selectSheet(this);
if ($("sheet-option-modal").length == 0) {
$(".lower-bar").append(` <div class="sheet-option-modal">
<div class="sheet-rename">Rename</div>
<div class="sheet-delete">Delete</div> </div>`);
// add rename box
$(".sheet-rename").click(function() {
$(".lower-bar").append(`<div class="sheet-rename-modal">
<h4 class="modal-title">Rename Sheet To: </h4>
<input type="text" class="new-sheet-name" placeholder="Sheet Name" />
<div class="action-button">
<div class="submit-button">Submit</div>
<div class="cancel-button">Cancel</div>
</div>
</div>`);
$(".cancel-button").click(function() {
$(".sheet-rename-modal").remove();
});
$(".submit-button").click(function() {
let newSheetname = $(".new-sheet-name").val();
// console.log(newSheetname);
$(".sheet-tab.selected").text(newSheetname);
let newCellData = {};
for (let key in cellData) {
if (key != selectedSheet) {
newCellData[key] = cellData[key];
} else {
newCellData[newSheetname] = cellData[key];
}
}
cellData = newCellData;
selectedSheet = newSheetname;
$(".sheet-rename-modal").remove();
})
});
//delete sheet
$(".sheet-delete").click(function() {
$(".lower-bar").append(`<div class="sheet-rename-modal delete-sh">
<h4 class="modal-title text-align:center">Are you want to sure delete these Sheet ?</h4>
<div class="action-button">
<div class="submit-delete-button">Delete</div>
<div class="cancel-delete-button">Cancel</div>
</div>
</div>`);
$(".cancel-delete-button").click(function() {
$(".sheet-rename-modal").remove();
});
$(".submit-delete-button").click(function() {
if (Object.keys(cellData).length > 1) {
let currSheetName = selectedSheet;
let currSheet = $(".sheet-tab.selected");
let currSheetIndex = Object.keys(cellData).indexOf(selectedSheet);
if (currSheetIndex == 0) {
$(".sheet-tab.selected").next().click();
} else {
$(".sheet-tab.selected").prev().click();
}
delete cellData[currSheetName];
currSheet.remove();
} else {
alert("Sorry, there is only one sheet. So, it's not possible");
}
$(".sheet-rename-modal").remove();
})
});
}
$(".sheet-option-modal").css("left", e.pageX + "px");
});
}
$(".container").click(function() {
$(".sheet-option-modal").remove();
})
addSheetEvents();
function selectSheet(ele) {
$(".sheet-tab.selectd").removeClass("selected");
$(ele).addClass("selected");
emptySheet();
selectedSheet = $(ele).text();
loadSheet();
}