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
|
/* Authors:
* Pavel Zuna <pzuna@redhat.com>
* Endi Sukma Dewata <edewata@redhat.com>
*
* Copyright (C) 2010 Red Hat
* see file 'COPYING' for use and warranty information
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; version 2 only
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* REQUIRES: ipa.js */
var IPA_ADD_POPULATE = 1;
var IPA_ADD_UPDATE = 2;
function ipa_add_field(spec) {
spec = spec || {};
var that = {};
that.name = spec.name;
that.label = spec.label;
that._entity_name = spec.entity_name;
that.init = spec.init;
that.setup = spec.setup;
that.__defineGetter__("entity_name", function(){
return that._entity_name;
});
that.__defineSetter__("entity_name", function(entity_name){
that._entity_name = entity_name;
});
return that;
}
function ipa_add_dialog(spec) {
spec = spec || {};
var that = {};
that.name = spec.name;
that.title = spec.title;
that._entity_name = spec.entity_name;
that.init = spec.init;
that.fields = [];
that.fields_by_name = {};
var dialog = $('<div/>');
that.__defineGetter__("entity_name", function(){
return that._entity_name;
});
that.__defineSetter__("entity_name", function(entity_name){
that._entity_name = entity_name;
for (var i=0; i<that.fields.length; i++) {
that.fields[i].entity_name = entity_name;
}
});
that.get_fields = function() {
return that.fields;
};
that.get_field = function(name) {
return that.fields_by_name[name];
};
that.add_field = function(field) {
that.fields.push(field);
that.fields_by_name[field.name] = field;
};
that.create_field = function(spec) {
var field = ipa_add_field(spec);
that.add_field(field);
return field;
};
that.open = function() {
dialog.empty();
dialog.attr('id', that.name);
dialog.attr('title', that.title);
for (var i = 0; i < that.fields.length; ++i) {
var field = that.fields[i];
if (field.setup) {
field.setup(dialog, IPA_ADD_POPULATE);
} else {
dialog.append('<label>' + field.label + '</label>');
dialog.append('<input type="text" name="' + field.name + '" />');
dialog.append('<br/>');
}
}
dialog.dialog({
modal: true,
buttons: {
'Add': that.add,
'Add and edit': that.add_and_edit,
'Cancel': that.cancel
}
});
};
that.add = function(evt, called_from_add_and_edit) {
var pkey = [];
var options = {};
var pkey_name = IPA.metadata[that.entity_name].primary_key;
function add_win(data, text_status, xhr) {
if (called_from_add_and_edit) {
var state = {};
state[that.entity_name + '-facet'] = 'details';
state[that.entity_name + '-pkey'] = pkey[0];
$.bbq.pushState(state);
}
}
for (var i = 0; i < that.fields.length; ++i) {
var field = that.fields[i];
if (field.setup) {
var value = field.setup(dialog, IPA_ADD_UPDATE);
if (value != null) {
if (field.name == pkey_name)
pkey = [value];
else
options[field.name] = value;
}
}
}
dialog.find('input').each(function () {
var jobj = $(this);
var attr = jobj.attr('name');
var value = jobj.val();
if (value) {
if (pkey.length == 0 && attr == pkey_name)
pkey = [jobj.val()];
else if (options[attr] == null)
options[attr] = jobj.val();
}
});
ipa_cmd('add', pkey, options, add_win, null, that.entity_name);
};
that.add_and_edit = function(evt) {
that.add(evt, true);
dialog.dialog('close');
};
that.cancel = function() {
dialog.dialog('close');
};
if (that.init) that.init();
return that;
}
|