-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspreadsheet.js
More file actions
221 lines (195 loc) · 6.07 KB
/
spreadsheet.js
File metadata and controls
221 lines (195 loc) · 6.07 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
function Table(tableId, data, headings) {
this.table = document.getElementById(tableId);
this.data = data;
this.headings = headings; // firstname: "First Name", lastname: "Last Name"...
this.order = new Array(); // 0: firstname 1: lastname...
this.sortCol = null;
this.revSort = false;
this.table.className += " spreadsheet";
this.formatCell = function (row, key, formatFunc) {
this.data[row][key].format = formatFunc;
this.update();
}
this.formatRow = function (row, formatFunc) {
for (var key in this.data[row]) {
this.data[row][key].format = formatFunc;
}
this.update();
}
this.formatColumn = function (key, formatFunc) {
for (var row in this.data) {
this.data[row][key].format = formatFunc;
}
this.update();
}
this.initStyle = function (row, key) {
if (this.data[row][key].style === undefined) {
this.data[row][key].style = new Array();
}
}
this.styleCell = function (row, key, attr, value) {
this.initStyle(row, key);
this.data[row][key].style[attr] = value;
this.update();
}
this.styleRow = function (row, attr, value) {
for (var key in this.data[row]) {
this.initStyle(row, key);
this.data[row][key].style[attr] = value;
}
this.update();
}
this.styleColumn = function (key, attr, value) {
for (var row in this.data) {
this.initStyle(row, key);
this.data[row][key].style[attr] = value;
}
this.update();
}
this.clear = function () {
for (var index=this.table.rows.length - 1; index >= 0; index--) {
this.table.deleteRow(index);
}
};
this.makeHeadings = function () {
this.order = new Array();
var row = this.table.insertRow(0);
for (var index in this.headings) {
var cell = document.createElement("th");
row.appendChild(cell);
cell.innerHTML = this.headings[index];
cell.index = index;
cell.table = this;
// Prevent editting in IE and Opera
cell.onfocus = function () {
this.blur();
}
cell.onclick = function () {
if (this.table.sortCol != this.index) {
this.table.sortCol = this.index;
this.table.revSort = false;
// Do the sorting
var sortCol = this.index;
this.table.data.sort(function (a, b) {
if (a[sortCol].content < b[sortCol].content) return -1;
if (a[sortCol].content == b[sortCol].content) return 0;
if (a[sortCol].content > b[sortCol].content) return 1;
});
} else {
this.table.revSort = !this.table.revSort;
this.table.data.reverse();
}
this.table.update();
}
if (this.sortCol == index) {
cell.className += " sorted";
if (this.revSort) {
cell.className += " reversed";
}
}
this.order.push(index);
}
}
this.update = function () {
this.clear(); // Erase what's already in the table
this.makeHeadings(); // Make the headingings
// Now add the data
for (var rowIndex in this.data) {
var row = this.table.insertRow(-1);
for (var colIndex in this.order) {
var cell = row.insertCell(colIndex);
cell.table = this;
cell.row = rowIndex;
cell.key = this.order[colIndex];
cell.column = colIndex;
this.updateCell(cell, rowIndex, this.order[colIndex]);
}
}
};
this.updateCell = function (cell, row, key) {
cell.innerHTML = "";
var editContent = document.createElement("div");
cell.appendChild(editContent);
editContent.innerHTML = this.format(row, key);
this.styleCell(cell, row, key);
// Make editable
if (!this.data[row][key].frozen) {
editContent.table = this;
editContent.cell = cell;
editContent.row = row;
editContent.key = key;
editContent.className = "spreadsheet cellcontent";
cell.className = "spreadsheet cell";
editContent.setAttribute("contenteditable", true);
editContent.cancelled = false;
editContent.onfocus = function(event) {
// Fill in just the content
this.innerHTML = this.table.data[this.row][this.key].content;
};
editContent.onblur = function(event) {
if (!this.cancelled) {
// Overwrite the content
this.table.data[this.row][this.key].content = this.innerHTML;
}
this.table.updateCell(this, this.row, this.key);
};
// One function for enter
editContent.onkeypress = function(event) {
var key = event.keyCode || event.charCode;
if (key == 13) {
// Store the new value in the data
this.blur();
return false;
}
if (key == 27) {
// Discard change
this.cancelled = true;
this.blur();
return false;
}
};
// Another for escape
editContent.onkeyup = function(event) {
var key = event.keyCode || event.charCode;
if (key == 27) {
// Discard change
this.cancelled = true;
this.blur();
return false;
}
};
}
}
this.evaluate = function (row, key) {
var content = this.data[row][key].content;
if ("string" == typeof content && content.indexOf("=") == 0) {
// This is a formula
// TODO: Fill with JISON
}
return content;
}
this.format = function (row, key) {
// Takes data at row,col and formats it
var content = this.evaluate(row, key);
if (this.data[row][key].format === undefined) {
return content;
} else {
try {
var formatted = this.data[row][key].format(this.data[row][key].content);
return formatted;
} catch (err) {
alert("#ERROR: " + err);
return content;
}
}
}
this.styleCell = function (cell, row, key) {
// Do style
if (this.data[row][key].style !== undefined) {
for (var index in this.data[row][key].style) {
cell.style[index] = this.data[row][key].style[index];
}
}
}
this.update(); // Go ahead and fill the table with data
}