-
Notifications
You must be signed in to change notification settings - Fork 821
/
TableField.js
executable file
·209 lines (186 loc) · 5.72 KB
/
TableField.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
/**
* Javascript for TableField. allows the deletion of records via
* AJAX, and the addition of rows via javasript.
*
* TODO relies on include-order at the moment to override actions :/
*/
Effect.FadeOut = function(element,callback) {
element = $(element);
var oldOpacity = Element.getInlineOpacity(element);
var options = Object.extend({
from: Element.getOpacity(element) || 1.0,
to: 0.0,
afterFinishInternal: function(effect) {
effect.element.parentNode.removeChild(effect.element);
}
}, arguments[1] || {});
return new Effect.Opacity(element,options);
}
TableField = Class.create();
TableField.prototype = {
newRowID: 1,
/**
* Applies behaviour to the delete button for deleting objects via ajax.
*/
initialize: function() {
var rules = {};
rules['#'+this.id+' table.data a.deletelink'] = {
onclick: this.deleteRecord.bind(this)
};
rules['#'+this.id+' table.data a.addrow'] = {
onclick: this.addRow.bind(this)
};
Behaviour.register('TableField_'+this.id,rules);
},
/**
* Deletes the given dataobject record via an ajax request. If the record doesn't have any
* information in it, it just removes it from the form.
* to tablefield->Delete()
* @param {Object} e
*/
deleteRecord: function(e) {
var img = Event.element(e);
var link = Event.findElement(e,"a");
var row = Event.findElement(e,"tr");
var params = link.getAttribute("href").toQueryParams();
var isEmpty = true;
var recordID = row.getRecordId();
var self = this;
// Check to see if there is a dataobject to delete first, otherwise remove the row.
// or: Check if a childID is set (not present on new items)
if(
(this.hasNoValues(row,"input") && this.hasNoValues(row,"select") && this.hasNoValues(row,"textarea"))
|| params["childID"] <= 0 || (recordID <= 0 || recordID == false)
){
if( row.parentNode.getElementsByTagName('tr').length > 1 ) {
Effect.FadeOut(row);
} else {
// clear all fields in the row
var fields = row.getElementsByTagName('input');
if( fields )
for( var i = 0; i < fields.length; i++ ) {
fields[i].value = '';
}
}
Event.stop(e);
return false;
}
// TODO ajaxErrorHandler and loading-image are dependent on cms, but formfield is in sapphire
var confirmed = confirm(ss.i18n._t('TABLEFIELD.DELETECONFIRMMESSAGE', 'Are you sure you want to delete this record?'));
if(confirmed){
img.setAttribute("src",'sapphire/images/network-save.gif'); // TODO doesn't work
jQuery.ajax({
'url': link.getAttribute("href"),
'method': 'post',
'data': {ajax: 1, 'SecurityID': $('SecurityID') ? $('SecurityID').value : null},
'success': function(response){
Effect.Fade(
row,
{
afterFinish: function(obj) {
// remove row from DOM
obj.element.parentNode.removeChild(obj.element);
// recalculate summary if needed (assumes that TableListField.js is present)
// TODO Proper inheritance
if(self._summarise) self._summarise();
// custom callback
if(self.callback_deleteRecord) self.callback_deleteRecord(e);
}
}
);
},
'error': ajaxErrorHandler
});
}
Event.stop(e);
return false;
},
/**
*
*
* @param {Object} element
* @param {Object} tagName
*/
hasNoValues: function(element,tagName){
elements = element.getElementsByTagName(tagName);
if(elements.length >= 1){
var isEmpty = true;
for(var i = 0; i < elements.length;i++){
if(elements[i].type != "hidden"){
if(elements[i].value != null && elements[i].value != ""){
isEmpty = false;
}
}
}
return isEmpty;
}else{
return true;
}
},
/**
* Appends a new row to the DOM.
*
* @param {Object} tableid
*/
addRow: function (e){
var table = Event.findElement(e,"table");
if(table){
// Clone the last TR
var tbody = table.tBodies[0];
var numRows = tbody.rows.length;
var newRow = tbody.rows[0].cloneNode(true);
// Get the input elements in this new row
var inputs = newRow.getElementsByTagName('input');
// For every input, set it's value to blank if it is not hidden
for(var i = 0; i < inputs.length; i++) {
if(inputs[i].type != 'hidden') {
inputs[i].value = ""
};
}
this.newRowID++;
if(newRow.id != "new"){
this.resetNames(newRow);
}
// Make sure all inputs have unique IDs
for(var i = 0; i < inputs.length; i++) {
inputs[i].id += "-" + this.newRowID;
}
// Change the ID to a unique one
newRow.id = "New_" + this.newRowID;
// Append the new row to the DOM
table.tBodies[0].appendChild(newRow);
Behaviour.apply(table);
}
Event.stop(e);
},
/**
* resets the names for all elements inside a row.
* @param {Object} row
*/
resetNames: function(row){
// Support for addressing the ID's appropriately.
for(i = 0; i < row.cells.length;i++){
for(b=0; b < row.cells[i].childNodes.length;b++){
inputElement = row.cells[i].childNodes[b];
if(inputElement.type != 'hidden') inputElement.value = "";
if(inputElement.name != null){
if(inputElement.name.substr(inputElement.name.length - 2,inputElement.name.length) != "[]"){
inputElement.name =
inputElement.name.substr(0,inputElement.name.indexOf('[')+1) + "new" +
inputElement.name.substr(inputElement.name.indexOf(']'),inputElement.name.length) + "[]";
}else{
inputElement.name =
inputElement.name.substr(0,inputElement.name.indexOf('[')+1) + "new" +
inputElement.name.substr(inputElement.name.indexOf(']'),inputElement.name.length);
}
}
}
}
}
}
TableField.applyTo('div.TableField');
if(typeof ajaxErrorHandler == 'undefined'){
ajaxErrorHandler = function(response) {
alert(response.responseText);
}
}