-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathgenericadmin.js
executable file
·330 lines (283 loc) · 11.9 KB
/
genericadmin.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
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
/*
genericadmin - Weston Nielson ([email protected])
updated by Jan Schrewe ([email protected])
updated by Troy Melhase ([email protected])
updated by Jonathan Ellenberger ([email protected])
*/
(function($) {
var GenericAdmin = {
url_array: null,
fields: null,
obj_url: "../../obj-data/",
admin_media_url: window.__admin_media_prefix__,
popup: '_popup',
prepareSelect: function(select) {
var that = this,
opt_keys = [],
opt_dict = {},
no_value,
opt_group_css = 'style="font-style:normal; font-weight:bold; color:#999; padding-left: 2px;"';
// polish the look of the select
select.find('option').each(function() {
var key, opt;
if (this.value) {
if (that.url_array[this.value]) {
key = that.url_array[this.value][0].split('/')[0];
opt = $(this).clone();
opt.text(that.capFirst(opt.text()));
if ($.inArray(key, opt_keys) < 0) {
opt_keys.push(key);
// if it's the first time in array
// it's the first time in dict
opt_dict[key] = [opt];
} else {
opt_dict[key].push(opt);
}
}
} else {
no_value = $(this).clone();
}
});
select.empty().append(no_value);
opt_keys = opt_keys.sort();
$.each(opt_keys, function(index, key) {
var opt_group = $('<optgroup label="' + that.capFirst(key) + '" ' + opt_group_css + '></optgroup>');
$.each(opt_dict[key], function(index, value) {
opt_group.append(value).css('color', '#000');
});
select.append(opt_group);
});
return select;
},
getLookupUrlParams: function(cID) {
var q = this.url_array[cID][1] || {},
str = [];
for(var p in q) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(q[p]));
}
x = str.join("&");
url = x ? ("?" + x) : "";
return url;
},
getLookupUrl: function(cID) {
return '../../../../' + this.url_array[cID][0] + '/' + this.getLookupUrlParams(cID);
},
getFkId: function() {
if (this.fields.inline === false) {
return 'id_' + this.fields.fk_field;
} else {
return ['id_', this.fields.prefix, '-', this.fields.number, '-', this.fields.fk_field].join('');
}
},
getCtId: function() {
if (this.fields.inline === false) {
return 'id_' + this.fields.ct_field;
} else {
return ['id_', this.fields.prefix, '-', this.fields.number, '-', this.fields.ct_field].join('');
}
},
capFirst: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
hideLookupLink: function() {
var this_id = this.getFkId();
$('#lookup_' + this_id).unbind().remove();
$('#lookup_text_' + this_id + ' a').remove();
$('#lookup_text_' + this_id + ' span').remove();
},
showLookupLink: function() {
var that = this,
url = this.getLookupUrl(this.cID),
id = 'lookup_' + this.getFkId(),
link = '<a class="related-lookup" id="' + id + '" href="' + url + '"> </a>';
link = link + '<strong id="lookup_text_'+ this.getFkId() +'" margin-left: 5px"><a target="_new" href="#"></a><span></span></strong>';
// insert link html after input element
this.object_input.after(link);
return id;
},
pollInputChange: function(window) {
var that = this,
interval_id = setInterval(function() {
if (window.closed === true) {
clearInterval(interval_id);
that.updateObjectData()();
return true;
}
},
150);
},
popRelatedObjectLookup: function(link) {
var name = id_to_windowname(this.getFkId()),
url_parts = [],
href,
win;
if (link.href.search(/\?/) >= 0) {
url_parts[0] = '&';
//href = link.href + '&pop=1';
} else {
url_parts[0] = '?';
//href = link.href + '?pop=1';
}
url_parts[1] = this.popup;
url_parts[2] = '=1';
href = link.href + url_parts.join('');
var left = window.screen.width / 2 - 400,
top = window.screen.height / 2 - 250;
win = window.open(href, name, 'left='+left+',top='+top+',height=500,width=800,resizable=yes,scrollbars=yes');
// wait for popup to be closed and load object data
this.pollInputChange(win);
win.focus();
return false;
},
updateObjectData: function() {
var that = this;
return function() {
var value = that.object_input.val();
if (!value) {
return
}
//var this_id = that.getFkId();
$('#lookup_text_' + that.getFkId() + ' span').text('loading...');
$.ajax({
url: that.obj_url,
dataType: 'json',
data: {
object_id: value,
content_type: that.cID
},
success: function(item) {
if (item && item.content_type_text && item.object_text) {
var url = that.getLookupUrl(that.cID);
$('#lookup_text_' + that.getFkId() + ' a')
.text(item.content_type_text + ': ' + item.object_text)
.attr('href', url + item.object_id);
// run a callback to do other stuff like prepopulating url fields
// can't be done with normal django admin prepopulate
if (that.updateObjectDataCallback) {
that.updateObjectDataCallback(item);
}
}
$('#lookup_text_' + that.getFkId() + ' span').text('');
},
error: function(xhr, status, error) {
$('#lookup_text_' + that.getFkId() + ' span').text('')
.html('Error: ' + xhr.status + ' – ' + that.capFirst(xhr.statusText.toLowerCase()));
if (xhr.status === 404) {
that.object_input.val('');
} else {
$('#lookup_text_' + that.getFkId() + ' span').css('color', '#f00');
}
}
});
};
},
install: function(fields, url_array, popup_var) {
var that = this;
this.url_array = url_array;
this.fields = fields;
this.popup = popup_var || this.popup;
// store the base element
this.object_input = $("#" + this.getFkId());
// find the select we need to change
this.object_select = this.prepareSelect($("#" + this.getCtId()));
// install event handler for select
this.object_select.change(function() {
// reset the object input to the associated select (this one)
var link_id;
//(this).css('color', 'red'); // uncomment for testing
that.hideLookupLink();
// Set our objectId when the content_type is changed
if (this.value) {
that.cID = this.value;
link_id = that.showLookupLink();
$('#' + link_id).click(function(e) {
e.preventDefault();
that.popRelatedObjectLookup(this);
});
}
});
// fire change event if something is already selected
if (this.object_select.val()) {
this.object_select.trigger('change');
}
// Bind to the onblur of the object_id input.
this.object_input.blur(that.updateObjectData());
// Fire once for initial link.
this.updateObjectData()();
}
};
var InlineAdmin = {
sub_admins: null,
url_array: null,
fields: null,
popup: '_popup',
install: function(fields, url_array, popup_var) {
var inline_count = $('#id_' + fields.prefix + '-TOTAL_FORMS').val(),
admin;
this.url_array = url_array;
this.fields = fields;
this.sub_admins = [];
this.popup = popup_var || this.popup;
for (var j = 0; j < inline_count; j++) {
f = $.extend({}, this.fields);
f.number = j;
admin = $.extend({}, GenericAdmin);
admin.install(f, this.url_array, popup_var);
this.sub_admins.push(admin);
}
$('#' + this.fields.prefix + '-group .add-row a').click(this.addHandler());
},
addHandler: function() {
var that = this;
return function(e) {
e.preventDefault();
var added_fields = $.extend({}, that.fields),
admin = $.extend({}, GenericAdmin);
added_fields.number = ($('#id_' + that.fields.prefix + '-TOTAL_FORMS').val() - 1);
admin.install(added_fields, that.url_array, that.popup);
that.sub_admins.push(admin);
$('#' + that.fields.prefix + '-' + added_fields.number + ' .inline-deletelink').click(
that.removeHandler(that)
);
}
},
removeHandler: function(that) {
return function(e) {
var parent_id,
deleted_num,
sub_admin;
e.preventDefault();
parent_id = $(e.currentTarget).parents('.dynamic-' + that.fields.prefix).first().attr('id');
deleted_num = parseInt(parent_id.charAt(parent_id.length - 1), 10);
for (var i = (that.sub_admins.length - 1); i >= 0; i--) {
sub_admin = that.sub_admins[i];
if (sub_admin.fields.number === deleted_num) {
that.sub_admins.splice(i, 1);
} else if (sub_admin.fields.number > deleted_num) {
sub_admin.fields.number = sub_admin.fields.number - 1;
}
}
}
}
};
$(document).ready(function() {
$.ajax({
url: '../../genericadmin-init/',
dataType: 'json',
success: function(data) {
var url_array = data.url_array,
ct_fields = data.fields,
popup_var = data.popup_var,
fields;
for (var i = 0; i < ct_fields.length; i++) {
fields = ct_fields[i];
if (fields.inline === false) {
$.extend({}, GenericAdmin).install(fields, url_array, popup_var);
} else {
$.extend({}, InlineAdmin).install(fields, url_array, popup_var);
}
}
}
});
});
} (django.jQuery || jQuery));