-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.js
More file actions
376 lines (287 loc) · 10.6 KB
/
render.js
File metadata and controls
376 lines (287 loc) · 10.6 KB
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
// jquery to maipulate dom
const dialog=require('electron').remote.dialog;
const fs=require("fs");
const $=require("jquery");
$(document).ready(function () {
window.db=[];
init();
$("#font-family-container").on("change", function () {
let fontFamily = $(this).val();
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
db[rid][cid].fontFamily = fontFamily;
$(`.cell[rid=${rid}][cid=${cid}]`).css("font-family", fontFamily);
})
$("#font-size").on("change", function () {
let fontSize = $(this).val();
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
db[rid][cid].fontSize = fontSize;
$(`.cell[rid=${rid}][cid=${cid}]`).css("font-size", fontSize + "px");
})
$('#underline').on("click", function () {
$(this).toggleClass('selected');
let underline = $(this).hasClass('selected');
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
db[rid][cid].underline = underline;
$(`.cell[rid=${rid}][cid=${cid}]`).css("text-decoration", underline ? "underline" : "none");
})
$('#italic').on("click", function () {
$(this).toggleClass('selected');
let address = $("#address").val();
let italic = $(this).hasClass('selected');
let { rid, cid } = getrcidfromaddress(address);
db[rid][cid].italic = italic
$(`.cell[rid=${rid}][cid=${cid}]`).css("font-style", italic ? "italic" : "normal");
})
$('#bold').on("click", function () {
$(this).toggleClass('selected');
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
let bold = $(this).hasClass('selected');
$(`.cell[rid=${rid}][cid=${cid}]`).css("font-weight", bold ? "bolder" : "normal");
db[rid][cid].bold = bold;
})
$("#bg-color").on("change", function () {
let bgColor = $(this).val();
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
db[rid][cid].bgColor = bgColor;
$(`.cell[rid=${rid}][cid=${cid}]`).css("background-color", bgColor);
})
$("#text-color").on("change", function () {
let color = $(this).val();
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
db[rid][cid].color = color;
$(`.cell[rid=${rid}][cid=${cid}]`).css("color", color);
})
$('[halign]').on('click', function () {
$('[halign]').removeClass('selected');
$(this).addClass('selected');
let address = $("#address").val();
let { rid, cid } = getrcidfromaddress(address);
let halign = $(this).attr('halign');
db[rid][cid].halign = halign;
$(`.cell[rid=${rid}][cid=${cid}]`).css("text-align", halign);
})
$(".grid .row .cell").on("click", function () {
let clickedcell = this;
let {rid,cid}=getrcidfromcell(clickedcell);
let address=getaddressfromridcid(rid,cid);
//let row = Number(rid) + 1;
//let col = String.fromCharCode(Number(cid) + 65);
//let address = col + row;
//console.log(address, "was clicked");
//address display
$("#address").val(address);
//formula change
let isEmpty=db[rid][cid].isEmpty;
let val=db[rid][cid].val;
$("#formula").val(db[rid][cid].formula);
})
//get row id and col id from cell
function getrcidfromcell(clickedcell)
{
let rid = $(clickedcell).attr("rid");
let cid = $(clickedcell).attr("cid");
return{rid:rid, cid:cid};
}
//update database
$(".grid .row .cell").on("blur", function () {
let clickedcell = this;
let {rid,cid}=getrcidfromcell(clickedcell);
if(db[rid][cid].val==$(clickedcell).text())
return;
if(db[rid][cid].formula)
deleteformula(db[rid][cid].formula,rid,cid);
let value=$(clickedcell).text();
updateUI(value,rid,cid);
let pixel=$("#font-size").val();
let cell=$(`.cell[rid=${rid}][cid=${cid}]`);
cell.css("font-size", pixel + 'px');
//console.log(address, "was blurred");
})
/*formula*///////////////////////////////////////////////
$("#formula").on("blur",function(){
let formulale=$(this);
if(formulale.val()=="")
return;
let formula=$("#formula").val();
let address=$("#address").val();
let{rid,cid}= getrcidfromaddress(address);
if(db[rid][cid].formula)
deleteformula(db[rid][cid].formula,rid,cid);
// set the formula at which formula is placed
//db>update formula
setformula(rid,cid,formula,address);
//evaluate formula
let value=evaluateformula(formula);
//console.log(value);
//UI update
db[rid][cid]
updateUI(value,rid,cid);
});
function evaluateformula(formula)
{ // (A1+A2) A1-10 ,A2-20
let formularr=formula.split(" ");
for(let i=0;i<formularr.length;i++)
{
let comp=formularr[i];
if(iscompvalid(comp))
{
//replace logic
let{rid,cid}=getrcidfromaddress(comp);
let value= db[rid][cid].val;
formula=formula.replace(comp,value);
//console.log(formula);
}
}
return eval(formula);
}
function deleteformula(formula,rid,cid)
{
let address=getaddressfromridcid(rid,cid);
let formularr=formula.split(" ");
//get yourself removed from your parent array
for(let i=0;i<formularr.length;i++)
{
let comp=formularr[i];
if(iscompvalid(comp))
{
let pob=getrcidfromaddress(comp);
let pchildrenarray= db[pob.rid][pob.cid].children;
let filteredpchildarray=pchildrenarray.filter(test);
function test(elem)
{
return elem!==address;
}
db[pob.rid][pob.cid].children=filteredpchildarray;
}
}
}
function getaddressfromridcid(rid,cid){
let col=String.fromCharCode(Number(cid)+65);
let row=Number(rid)+1;
let address=col+row;
return address;
}
function iscompvalid(comp)
{
let ascii=comp.charCodeAt(0);
return (ascii>=65 && ascii<=90);
}
function updateUI(value,rid,cid)
{
// set evaluated answer of the formula
$(`.cell[rid=${rid}][cid=${cid}]`).text(value);
// update in db also
db[rid][cid].val=value;
db[rid][cid].isEmpty=false;
// loop on dependencies
let childrens=db[rid][cid].children;
for(let i=0;i<childrens.length;i++)
{
let childadd=childrens[i];
let childRC=getrcidfromaddress(childadd);
let child=db[childRC.rid][childRC.cid];
let nval=evaluateformula(child.formula);
updateUI(nval,childRC.rid,childRC.cid);
}
}
function getrcidfromaddress(address)
{
// B2 > 1,2
let cid = Number(address.charCodeAt(0))-65;
let rid = Number(address=address.slice(1))-1;
return {rid,cid};
}
function setformula(rid,cid,formula,address)
{
db[rid][cid].formula=formula;
//dependency-add/update > gotoparent and then add yourself to their children array(dependencies)
let formularr=formula.split(" ");
for(let i=0;i<formularr.length;i++)
{
let comp=formularr[i];
if(iscompvalid(comp))
{
// get yourself added
let pob=getrcidfromaddress(comp);
db[pob.rid][pob.cid].children.push(address);
db[rid][cid].parent.push(comp);
}
}
}
// new, open ,save *************************************
// new file
$("#new").on("click",init);
//save file
$("#save").on("click",function(){
//dialog box-open up and save
let sfilepath=dialog.showSaveDialogSync();
let data=JSON.stringify(db);
fs.writeFileSync(sfilepath,data);
});
//open file
$("#open").on("click",function(){
// file data read and update the UI
let filearr=dialog.showOpenDialogSync();
let binaryc=fs.readFileSync(filearr[0]);
db=JSON.parse(binaryc);
//console.log(db);
init();
setUI(db);
});
//setUI
function setUI(db)
{
let allrows= $(".grid .row");
for(let i=0;i<allrows.length;i++){
let cols=$(allrows[i]).find(".cell");
for(let j=0;j<cols.length;j++){
let val=db[i][j].val;
let isEmpty=db[i][j].isEmpty;
$(`.cell[rid=${i}][cid=${j}]`).text(isEmpty ? "" : val);
}
}
}
//initially
function init(){
let allrows= $(".grid .row");
for(let i=0;i<allrows.length;i++){
let cols=$(allrows[i]).find(".cell");
let colsarr=[];
for(let j=0;j<cols.length;j++){
let cellObject={
val:0,
formula: "",
isEmpty:true,
children: [],
parent:[],
fontFamily: 'Arial',
fontSize: 12,
bold: false,
underline: false,
italic: false,
bgColor: '#FFFFFF',
textColor: '#000000',
halign: 'left'
}
let cell=$(`.cell[rid=${i}][cid=${j}]`);
cell.text("");
cell.css('font-family', cellObject.fontFamily);
cell.css("font-size", cellObject.fontSize + 'px');
cell.css("font-weight", cellObject.bold ? "bolder" : "normal");
cell.css("text-decoration", cellObject.underline ? "underline" : "none");
cell.css("font-style", cellObject.italic ? "italic" : "normal");
cell.css("background-color", cellObject.bgColor);
cell.css("color", cellObject.textColor);
cell.css("text-align", cellObject.halign);
colsarr.push(cellObject);
}
db.push(colsarr);
}
}
})