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
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This Source Code Form is "Incompatible With Secondary Licenses", as
* defined by the Mozilla Public License, v. 2.0. */
function EditTable(parent_el, table_data) {
this.parent_el = YAHOO.util.Dom.get(parent_el);
this.table_data = table_data;
this.field_count = table_data.fields.length;
if (!JSON) JSON = YAHOO.lang.JSON;
this.render = function() {
// create table
this.parent_el.innerHTML = '';
var table = document.createElement('table');
// header
var tr = document.createElement('tr');
for (var i = 0; i < this.field_count; i++) {
var th = document.createElement('th');
th.appendChild(document.createTextNode(this.table_data.fields[i]));
tr.appendChild(th);
}
var td = document.createElement('td');
td.innerHTML = ' ';
tr.appendChild(td);
table.appendChild(tr);
// rows
for (var i = 0; i < table_data.data.length; i++) {
// skip deleted rows
if (this.table_data.data[i][0] < 0)
continue;
var tr = document.createElement('tr');
for (var j = 0; j < this.field_count; j++) {
var td = document.createElement('td');
td.appendChild(document.createTextNode(this.table_data.data[i][j]));
tr.appendChild(td);
if (this.table_data.fields[j] != this.table_data.id_field) {
td.className = 'editable';
td.contentEditable = true;
YAHOO.util.Event.addListener(td, 'keydown', this._edit_keydown, this);
YAHOO.util.Event.addListener(td, 'blur', this._save, this);
d = td;
}
}
var td = document.createElement('td');
var a = document.createElement('a');
a.href = '#';
a.innerHTML = 'x';
YAHOO.util.Event.addListener(a, 'click', this._remove_row, this);
td.appendChild(a);
td.className = 'action';
tr.appendChild(td);
table.appendChild(tr);
}
this.parent_el.appendChild(table);
var add_btn = document.createElement('button');
add_btn.innerHTML = 'Add';
YAHOO.util.Event.addListener(add_btn, 'click', this._add_row, this);
this.parent_el.appendChild(add_btn);
},
this.to_json = function(target) {
YAHOO.util.Dom.get(target).value = JSON.stringify(this.table_data);
},
this._add_row = function(event, obj) {
var row = [];
for (var i = 0; i < obj.field_count; i++) {
row.push(obj.table_data.fields[i] == obj.table_data.id_field ? '-' : '');
}
obj.table_data.data.push(row);
obj.render();
YAHOO.util.Dom.removeClass('commit_btn', 'bz_default_hidden');
event.preventDefault();
},
this._remove_row = function(event, obj) {
var row = event.target.parentElement.parentElement.rowIndex - 1;
if (obj.table_data.data[row][0] == '-') {
// removing a newly added row
obj.table_data.data.splice(row, 1);
}
else {
// to remove a db row we set its id to negative
// it'll be skipped by render, and the update script knows which id to delete
obj.table_data.data[row][0] = -obj.table_data.data[row][0];
}
obj.render();
YAHOO.util.Dom.removeClass('commit_btn', 'bz_default_hidden');
event.preventDefault();
},
this._save = function(event, obj) {
var row = event.target.parentElement.rowIndex - 1;
var col = event.target.cellIndex;
var value = event.target.textContent;
if (obj.table_data.data[row][col] != event.target.textContent) {
obj.table_data.data[row][col] = event.target.textContent;
YAHOO.util.Dom.removeClass('commit_btn', 'bz_default_hidden');
}
},
this._revert = function(event, obj) {
var row = event.target.parentElement.rowIndex - 1;
var col = event.target.cellIndex;
event.target.replaceChild(
document.createTextNode(obj.table_data.data[row][col]),
event.target.firstChild
);
},
this._edit_keydown = function(event, obj) {
if (event.keyCode == 13) {
event.preventDefault();
obj._save(event, obj);
document.activeElement.blur(event.target);
}
else if (event.keyCode == 27) {
event.preventDefault();
obj._revert(event, obj);
}
}
};
|