This repository was archived by the owner on Dec 26, 2023. It is now read-only.
forked from feincms/feincms
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathitem_editor.js
More file actions
332 lines (273 loc) · 11.1 KB
/
Copy pathitem_editor.js
File metadata and controls
332 lines (273 loc) · 11.1 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
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
331
332
if(!Array.indexOf) {
Array.prototype.indexOf = function(obj) {
for(var i=0; i<this.length; i++) {
if(this[i]==obj) {
return i;
}
}
return -1;
}
}
function region_append(region, obj, modname) {
var wrp = [];
wrp.push('<fieldset class="module aligned order-item">');
wrp.push('<h2><img class="item-delete" src="'+IMG_DELETELINK_PATH+'" /><span class="handle"></span> '+modname+' (<span class="collapse">'+gettext('Hide')+'</span>)</h2>');
wrp.push('<div class="item-content"></div>');
wrp.push('</fieldset>');
$("#"+REGION_MAP[region]+"_body").children("div.order-machine").append(wrp.join(""))
.children("fieldset.order-item:last").children(".item-content").append(obj);
}
function create_new_spare_form(form, modvar, last_id) {
// create new spare form
var new_form = form.html().replace(
new RegExp(modvar+'-'+last_id, 'g'),
modvar+'-'+(last_id+1));
new_form = '<div id="'+modvar+'_set_item_'+(last_id+1)+'">'+new_form+'</div>';
$("#"+modvar+"_set").append(new_form);
}
function set_item_field_value(item, field, value) {
// item: DOM object created by 'region_append' function
// field: "order-field" | "delete-field" | "region-choice-field"
if (field=="delete-field")
item.find("."+field).attr("checked",value);
else if (field=="region-choice-field") {
var old_region_id = REGION_MAP.indexOf(item.find("."+field).val());
item.find("."+field).val(REGION_MAP[value]);
old_region_item = $("#"+REGION_MAP[old_region_id]+"_body");
if (old_region_item.children("div.order-machine").children().length == 0)
old_region_item.children("div.empty-machine-msg").show();
else
old_region_item.children("div.empty-machine-msg").hide();
new_region_item = $("#"+REGION_MAP[value]+"_body");
new_region_item.children("div.empty-machine-msg").hide();
}
else
item.find("."+field).val(value);
}
function move_item (region_id, item) {
poorify_rich(item);
$("#"+REGION_MAP[region_id]+"_body").children("div.order-machine").append(item);
set_item_field_value(item, "region-choice-field", region_id);
richify_poor(item);
}
function poorify_rich(item){
item.children(".item-content").hide();
item.find('div.item-content textarea[class^=item-richtext-]').each(function(){
var field = $(this);
var classes = field.attr('class').split(' ');
$.each(classes, function() {
if(this.match('^item-richtext-')) {
var remove_func = undefined;
try { remove_func = eval('feincms_richtext_remove_' + this.substr(14)); } catch(e) {}
if(typeof(remove_func) == 'function'){
remove_func(field);
}
}
});
});
$('input[type=radio][checked]', item).addClass('radiochecked');
}
function richify_poor(item){
item.children(".item-content").show();
item.find('div.item-content textarea[class^=item-richtext-]').each(function(){
var field = $(this);
var classes = field.attr('class').split(' ');
$.each(classes, function() {
if(this.match('^item-richtext-')) {
var add_func = undefined;
try { add_func = eval('feincms_richtext_add_' + this.substr(14)); } catch(e) {}
if(typeof(add_func) == 'function'){
add_func(field);
}
}
});
});
$('input.radiochecked', item).removeClass('radiochecked').trigger('click');
}
function sort_by_ordering(e1, e2) {
var v1 = parseInt($('.order-field', e1).val()) || 0;
var v2 = parseInt($('.order-field', e2).val()) || 0;
return v1 > v2 ? 1 : -1;
};
function give_ordering_to_content_types() {
for (var i=0; i<REGION_MAP.length;i++) {
var container = $("#"+REGION_MAP[i]+"_body div.order-machine");
for (var j=0; j<container.children().length; j++) {
set_item_field_value(container.find("fieldset.order-item:eq("+j+")"), "order-field", j);
}
}
}
function order_content_types_in_regions() {
for (var i=0; i<REGION_MAP.length;i++) {
var container = $("#"+REGION_MAP[i]+"_body div.order-machine");
container.children().sort(sort_by_ordering).each(function() {
container.append(this);
});
}
}
function attach_dragdrop_handlers() {
// hide content on drag n drop
$("#main h2 span.handle").mousedown(function(){
poorify_rich($(this).parents("fieldset.order-item"));
});
$("#main h2 span.handle").mouseup(function(){
richify_poor($(this).parents("fieldset.order-item"));
});
}
function init_contentblocks() {
for(var i=0; i<contentblock_init_handlers.length; i++)
contentblock_init_handlers[i]();
}
// global variable holding the current template key
var current_template;
$(document).ready(function(){
$("#main_wrapper > .navi_tab").click(function(){
var elem = $(this);
$("#main_wrapper > .navi_tab").removeClass("tab_active");
elem.addClass("tab_active");
$("#main > div:visible, #main > fieldset:visible").hide();
var tab_str = elem.attr("id").substr(0, elem.attr("id").length-4);
$('#'+tab_str+'_body').show();
ACTIVE_REGION = REGION_MAP.indexOf(tab_str);
if (tab_str == "settings")
$(".machine-control").hide();
else
$(".machine-control").show();
// make it possible to open current tab on page reload
window.location.hash = '#tab_'+tab_str;
});
$("input.order-machine-add-button").click(function(){
var select = $(this).prev();
var modvar = select.val();
var modname = select.children("option:selected").html();
var total_forms = $('#id_'+modvar+'-TOTAL_FORMS');
var last_id = parseInt(total_forms.val()) - 1;
var form = $("#"+modvar+"_set_item_"+last_id);
// update formset bookkeeping value
total_forms.val(last_id+2);
create_new_spare_form(form, modvar, last_id);
region_append(ACTIVE_REGION, form, modname, modvar);
set_item_field_value(form, "region-choice-field", ACTIVE_REGION);
attach_dragdrop_handlers();
init_contentblocks();
});
$("input.order-machine-move-button").click(function(){
var moveTo = $(this).prev().val();
var active_item = $("#main div.order-machine fieldset.active-item");
if (!active_item.length) {
jAlert(NO_ITEM_SELECTED_MESSAGE);
return false;
}
move_item(REGION_MAP.indexOf(moveTo), active_item);
});
$("h2 img.item-delete").live('click', function(){
popup_bg = '<div class="popup_bg"></div>';
$("body").append(popup_bg);
var item = $(this).parents(".order-item");
jConfirm(DELETE_MESSAGES[0], DELETE_MESSAGES[1], function(r) {
if (r==true) {
set_item_field_value(item,"delete-field","checked");
item.fadeOut(200);
}
$(".popup_bg").remove();
});
});
$('h2 span.collapse').live('click', function(){
var node = this;
$(this.parentNode.parentNode).children('.item-content').slideToggle(function(){
$(node).text(gettext($(this).is(':visible') ? 'Hide' : 'Show'));
});
return false;
});
current_template = $('input[name=template_key][checked], select[name=template_key]').val();
function on_template_key_changed(){
var input_element = this;
var new_template = this.value;
if(current_template==new_template)
// Selected template did not change
return false;
current_regions = template_regions[current_template];
new_regions = template_regions[new_template];
not_in_new = [];
for(var i=0; i<current_regions.length; i++)
if(new_regions.indexOf(current_regions[i])==-1)
not_in_new.push(current_regions[i]);
popup_bg = '<div id="popup_bg"></div>';
$("body").append(popup_bg);
var msg = CHANGE_TEMPLATE_MESSAGES[1];
if(not_in_new.length) {
msg = interpolate(CHANGE_TEMPLATE_MESSAGES[2], {
'source_regions': not_in_new,
'target_region': new_regions[0]
}, true);
}
jConfirm(msg, CHANGE_TEMPLATE_MESSAGES[0], function(ret) {
if(ret) {
for(var i=0; i<not_in_new.length; i++) {
var items = $('#'+not_in_new[i]+'_body div.order-machine').children();
move_item(0, items);
}
input_element.checked = true;
$('form').append('<input type="hidden" name="_continue" value="1" />').submit();
} else {
$("div#popup_bg").remove();
}
});
return false;
}
$('input[type=radio][name=template_key]').click(on_template_key_changed);
$('select[name=template_key]').change(on_template_key_changed);
$("fieldset.order-item").live('click', function(){
if(!$(this).hasClass('active-item')) {
$("fieldset.active-item").removeClass("active-item");
$(this).addClass("active-item");
}
});
$('form').submit(function(){
give_ordering_to_content_types();
var form = $(this);
form.attr('action', form.attr('action')+window.location.hash);
return true;
});
// move contents into their corresponding regions and do some simple formatting
$("div[id$=_set]").children().each(function(){
var elem = $(this);
if (!(elem.hasClass("header"))) {
elem.find("input[name$=-region]").addClass("region-choice-field");
elem.find("input[name$=-DELETE]").addClass("delete-field").parents("div.form-row").hide();
elem.find("input[name$=-ordering]").addClass("order-field");
var region_id = elem.find(".region-choice-field").val();
region_id = REGION_MAP.indexOf(region_id);
var content_type = elem.attr("id").substr(0, elem.attr("id").indexOf("_"));
region_append(region_id,elem, CONTENT_NAMES[content_type]);
set_item_field_value(elem,"region-choice-field",region_id)
}
});
// register regions as sortable for drag N drop
$(".order-machine").sortable({
handle: '.handle',
helper: 'clone',
placeholder: 'highlight',
stop: function(event, ui) {
richify_poor($(ui.item));
}
});
attach_dragdrop_handlers();
order_content_types_in_regions();
$('#inlines').hide();
var errors = $('#main div.errors');
if(errors.length) {
var id = errors.parents('fieldset[id$=_body], div[id$=_body]').attr('id');
$('#'+id.replace('_body', '_tab')).trigger('click');
} else {
if(window.location.hash) {
var tab = $('#'+window.location.hash.substr(5)+'_tab');
if(tab.length) {
tab.trigger('click');
return;
}
}
$('#main_wrapper>div.navi_tab:first-child').trigger('click');
}
});
$(window).load(function(){init_contentblocks()});