Valid HTML test
[weblabels.fsf.org.git] / www.fsf.org / 20130115 / files / referencebrowser.js
1 jq(function() {
2
3 // Move the overlay div to be a direct child
4 // of body to avoid IE7 z-index bug.
5 // TODO: load this with prepOverlay to standardize this.
6 jq('[id^=atrb_]').detach().appendTo("body");
7
8 // the overlay itself
9 jq('.addreference').overlay({
10 onBeforeLoad: function() {
11 ov = jq('div#content').data('overlay');
12 // close overlay, if there is one already
13 // we only allow one referencebrowser per time
14 if (ov) {ov.close(); }
15 var wrap = this.getOverlay().find('.overlaycontent');
16 var src = this.getTrigger().attr('src');
17 var srcfilter = src + ' >*';
18 wrap.data('srcfilter', srcfilter);
19 jq('div#content').data('overlay', this);
20 resetHistory();
21 wrap.load(srcfilter, function() {
22 var fieldname = wrap.find('input[name=fieldName]').attr('value');
23 check_referenced_items(fieldname);
24 });
25 },
26 onLoad: function() {
27 widget_id = this.getTrigger().attr('rel').substring(6);
28 disablecurrentrelations(widget_id);
29 }});
30
31 // the breadcrumb-links and the links of the 'tree'-navigati on
32 jq('[id^=atrb_] a.browsesite', jq('body')[0]).live('click', function(event) {
33 var target = jq(this);
34 var src = target.attr('href');
35 var wrap = target.parents('.overlaycontent');
36 var srcfilter = src + ' >*';
37 pushToHistory(wrap.data('srcfilter'));
38 wrap.data('srcfilter', srcfilter);
39 // the history we are constructing here is destinct from the
40 // srcfilter-history. here we construct a selection-widget, which
41 // is available, if the history_length-parameter is set on the widget
42 // the srcfilter-history is used for storing the URLs to make the
43 // 'Back'-link work.
44 var newoption = '<option value="' + src + '">' +
45 target.attr('rel') + '</option>';
46 refreshOverlay(wrap, srcfilter, newoption);
47 return false;
48 });
49
50 // the links for inserting referencens
51 jq('[id^=atrb_] input.insertreference', jq('body')[0]).live('click', function(event) {
52 var target = jq(this);
53 var wrap = target.parents('.overlaycontent');
54 var fieldname = wrap.find('input[name=fieldName]').attr('value');
55 var multi = wrap.find('input[name=multiValued]').attr('value');
56 var close_window = wrap.find('input[name=close_window]').attr('value');
57 var tablerow = target.parent().parent();
58 var title = tablerow.find('label').html();
59 var uid = target.attr('rel');
60 var messageId;
61 if (this.checked === true) {
62 refbrowser_setReference('ref_browser_' + fieldname,
63 uid, title, parseInt(multi));
64 messageId = '#messageAdded';
65 }
66 else {
67 refbrowser_delReference(fieldname, uid);
68 messageId = '#messageRemoved';
69 }
70 if (close_window === '1' && multi != '1') {
71 overlay = jq('div#content').data('overlay');
72 overlay.close();
73 } else {
74 showMessage(messageId, title);
75 };
76 });
77
78
79 // the history menu
80 jq('[id^=atrb_] form#history select[name=path]', jq('body')[0]).live('change', function(event) {
81 var target = jq(this);
82 var wrap = target.parents('.overlaycontent');
83 var src_selector = '[id^=atrb_] form#history ' +
84 'select[name=path] :selected';
85 var src = jq(src_selector).attr('value');
86 var srcfilter = src + ' >*';
87 refreshOverlay(wrap, srcfilter, '');
88 return false;
89 });
90
91 // the pagination links
92 jq('[id^=atrb_] div.listingBar a', jq('body')[0]).live('click', function(event) {
93 var target = jq(this);
94 var src = target.attr('href');
95 var wrap = target.parents('.overlaycontent');
96 var srcfilter = src + ' >*';
97 refreshOverlay(wrap, srcfilter, '');
98 return false;
99 });
100
101 // the search form
102 jq('[id^=atrb_] form#search input[name=submit]', jq('body')[0]).live('click',
103 function(event) {
104 var target = jq(this);
105 var src = target.parents('form').attr('action');
106 var wrap = target.parents('.overlaycontent');
107 var fieldname = wrap.find('input[name=fieldName]').attr('value');
108 var fieldrealname = wrap.find('input[name=fieldRealName]').attr('value');
109 var at_url = wrap.find('input[name=at_url]').attr('value');
110 var searchvalue = encodeURI(wrap.find('input[name=searchValue]').attr('value'));
111 var multi = wrap.find('input[name=multiValued]').attr('value');
112 var close_window = wrap.find('input[name=close_window]').attr('value');
113 qs = 'searchValue=' + searchvalue + '&fieldRealName=' + fieldrealname +
114 '&fieldName=' + fieldname + '&multiValued=' + multi +
115 '&close_window' + close_window + '&at_url=' + at_url;
116 var srcfilter = src + '?' + qs + ' >*';
117 pushToHistory(wrap.data('srcfilter'));
118 wrap.data('srcfilter', srcfilter);
119 refreshOverlay(wrap, srcfilter, '');
120 return false;
121 });
122
123 });
124
125 function disablecurrentrelations (widget_id) {
126 jq('ul#' + widget_id + ' :input').each(
127 function (intIndex) {
128 uid = jq(this).attr('value');
129 cb = jq('input[rel=' + uid + ']');
130 cb.attr('disabled', 'disabled');
131 cb.attr('checked', 'checked');
132 });
133 }
134
135 // function to return a reference from the popup window back into the widget
136 function refbrowser_setReference(widget_id, uid, label, multi)
137 {
138 var element = null,
139 label_element = null,
140 current_values = null,
141 i = null,
142 list = null,
143 li = null,
144 input = null,
145 up_element = null,
146 down_element = null,
147 container = null,
148 fieldname = null;
149 // differentiate between the single and mulitselect widget
150 // since the single widget has an extra label field.
151 if (multi === 0) {
152 jq('#' + widget_id).attr('value', uid);
153 jq('#' + widget_id + '_label').attr('value', label);
154 } else {
155 // check if the item isn't already in the list
156 current_values = jq('#' + widget_id + ' input');
157 for (i = 0; i < current_values.length; i++) {
158 if (current_values[i].value === uid) {
159 return false;
160 }
161 }
162 // now add the new item
163 var fieldname = widget_id.substr('ref_browser_'.length);
164 list = document.getElementById(widget_id);
165 // add ul-element to DOM, if it is not there
166 if (list === null) {
167 container = jq('#archetypes-fieldname-' + fieldname +
168 ' input + div');
169 if (!container.length) {
170 // fix for Plone 3.3 collections, with a weird widget-id
171 container = jq('#archetypes-fieldname-value input + div');
172 }
173 container.after(
174 '<ul class="visualNoMarker" id="' + widget_id + '"></ul>');
175 list = document.getElementById(widget_id);
176 }
177 li = document.createElement('li');
178 label_element = document.createElement('label');
179 input = document.createElement('input');
180 input.type = 'checkbox';
181 input.value = uid;
182 input.checked = true;
183 input.name = fieldname + ':list';
184 label_element.appendChild(input);
185 label_element.appendChild(document.createTextNode(' ' + label));
186 li.appendChild(label_element);
187 li.id = 'ref-' + widget_id + '-' + current_values.length;
188
189 sortable = jq('input[name=' + fieldname + '-sortable]').attr('value');
190 if (sortable === '1') {
191 up_element = document.createElement('a');
192 up_element.title = 'Move Up';
193 up_element.innerHTML = '&#x25b2;';
194 up_element.onclick = function () {
195 refbrowser_moveReferenceUp(this);
196 return false;
197 };
198
199 li.appendChild(up_element);
200
201 down_element = document.createElement('a');
202 down_element.title = 'Move Down';
203 down_element.innerHTML = '&#x25bc;';
204 down_element.onclick = function () {
205 refbrowser_moveReferenceDown(this);
206 return false;
207 };
208
209 li.appendChild(down_element);
210 }
211 list.appendChild(li);
212
213 // fix on IE7 - check *after* adding to DOM
214 input.checked = true;
215 }
216 }
217
218 // remove the item for the uid from the reference widget
219 function refbrowser_delReference(fieldname, uid) {
220 var selector = 'input[value="' + uid + '"][name="' + fieldname + ':list"]',
221 inputs = jq(selector);
222 inputs.closest('li').remove();
223 }
224
225 // function to clear the reference field or remove items
226 // from the multivalued reference list.
227 function refbrowser_removeReference(widget_id, multi)
228 {
229 var x = null,
230 element = null,
231 label_element = null,
232 list = null;
233
234 if (multi) {
235 list = document.getElementById(widget_id);
236 for (x = list.length - 1; x >= 0; x--) {
237 if (list[x].selected) {
238 list[x] = null;
239 }
240 }
241 for (x = 0; x < list.length; x++) {
242 list[x].selected = 'selected';
243 }
244 } else {
245 jq('#' + widget_id).attr('value', "");
246 jq('#' + widget_id + '_label').attr('value', "");
247 }
248 }
249
250 function refbrowser_moveReferenceUp(self)
251 {
252 var elem = self.parentNode,
253 eid = null,
254 pos = null,
255 widget_id = null,
256 newelem = null,
257 prevelem = null,
258 arrows = null,
259 cbs = null;
260 if (elem === null) {
261 return false;
262 }
263 eid = elem.id.split('-');
264 pos = eid.pop();
265 if (pos === "0") {
266 return false;
267 }
268 widget_id = eid.pop();
269 newelem = elem.cloneNode(true);
270
271 //Fix: (IE keep the standard value)
272 cbs = newelem.getElementsByTagName("input");
273 if (cbs.length > 0) {
274 cbs[0].checked = elem.getElementsByTagName("input")[0].checked;
275 }
276
277 prevelem = document.getElementById('ref-' + widget_id + '-' + (pos - 1));
278
279 // up arrow
280 arrows = newelem.getElementsByTagName("a");
281 arrows[0].onclick = function () {
282 refbrowser_moveReferenceUp(this);
283 return false;
284 };
285 // down arrow
286 arrows[1].onclick = function () {
287 refbrowser_moveReferenceDown(this);
288 return false;
289 };
290
291 elem.parentNode.insertBefore(newelem, prevelem);
292 elem.parentNode.removeChild(elem);
293 newelem.id = 'ref-' + widget_id + '-' + (pos - 1);
294 prevelem.id = 'ref-' + widget_id + '-' + pos;
295 }
296
297 function refbrowser_moveReferenceDown(self)
298 {
299 var elem = self.parentNode,
300 eid = null,
301 pos = null,
302 widget_id = null,
303 current_values = null,
304 newelem = null,
305 nextelem = null,
306 cbs = null,
307 arrows = null;
308 if (elem === null) {
309 return false;
310 }
311 eid = elem.id.split('-');
312 pos = parseInt(eid.pop(), 10);
313 widget_id = eid.pop();
314 current_values = jq('#ref_browser_items_' + widget_id + ' input');
315 if ((pos + 1) === current_values.length) {
316 return false;
317 }
318
319 newelem = elem.cloneNode(true);
320 //Fix: (IE keep the standard value)
321 cbs = newelem.getElementsByTagName("input");
322 if (cbs.length > 0) {
323 cbs[0].checked = elem.getElementsByTagName("input")[0].checked;
324 }
325
326 // up img
327 arrows = newelem.getElementsByTagName("a");
328 arrows[0].onclick = function () {
329 refbrowser_moveReferenceUp(this);
330 return false;
331 };
332 // down img
333 arrows[1].onclick = function () {
334 refbrowser_moveReferenceDown(this);
335 return false;
336 };
337
338 nextelem = document.getElementById('ref-' + widget_id + '-' + (pos + 1));
339
340 elem.parentNode.insertBefore(newelem, nextelem.nextSibling);
341 elem.parentNode.removeChild(elem);
342 newelem.id = 'ref-' + widget_id + '-' + (pos + 1);
343 nextelem.id = 'ref-' + widget_id + '-' + pos;
344 }
345
346 function showMessage(messageId, text) {
347 var template = jq(messageId).parent(),
348 message_div = template.clone(),
349 message = message_div.children(),
350 old_message = jq('#message'),
351 message_wrapper = jq('#messageWrapper');
352
353 // insert a new, cloned message
354 message_wrapper.prepend(message_div);
355 message.find('dd').text(text);
356 message.css({opacity: 0}).show();
357 message.attr('id', 'message');
358
359 // animate message switch and remove old message
360 message_wrapper.animate({height: message.height() + 20 }, 400);
361 message.fadeTo(400, 1);
362 old_message.fadeTo(400, 0, function() {
363 old_message.parent().remove();
364 });
365 };
366
367 function submitHistoryForm() {
368 var form = document.history;
369 var path = form.path.options[form.path.selectedIndex].value;
370 form.action = path;
371 form.submit();
372 }
373
374 function pushToHistory(url) {
375 var history = jq(document).data('atrb_history');
376 history.push(url);
377 jq(document).data('atrb_history', history);
378 }
379
380 function resetHistory() {
381 jq(document).data('atrb_history', []);
382 }
383
384 function popFromHistory() {
385 var history = jq(document).data('atrb_history');
386 value = history.pop();
387 jq(document).data('atrb_history', history);
388 return value;
389 }
390
391 function refreshOverlay(wrap, srcfilter, newoption) {
392 var oldhistory = jq('[id^=atrb_] form#history select');
393 wrap.load(srcfilter, function() {
394 jq('[id^=atrb_] form#history select').append(newoption +
395 oldhistory.html());
396 ov = jq('div#content').data('overlay');
397 widget_id = ov.getTrigger().attr('rel').substring(6);
398 disablecurrentrelations(widget_id);
399 var fieldname = wrap.find('input[name=fieldName]').attr('value');
400 check_referenced_items(fieldname);
401 });
402 }
403
404 // check all references in the overlay that are present in the widget
405 function check_referenced_items(fieldname) {
406 var refs_in_overlay = jq('input.insertreference'),
407 uid_selector = "input[name='" + fieldname + ":list']",
408 current = jq(uid_selector), // the widget in the form
409 current_uids = current.map(function () {
410 if (jq(this).attr('checked') === true) {
411 return jq(this).attr('value');
412 }
413 return null;
414 });
415
416 refs_in_overlay.each(function () {
417 var overlay_ref = jq(this),
418 uid = jq(overlay_ref).attr('rel'),
419 i;
420
421 for (i = 0; i < current_uids.length; i++) {
422 if (uid === current_uids[i]) {
423 overlay_ref.attr('checked', true);
424 return true; // break jq.each
425 }
426 }
427 });
428 }