source: products/quintagroup.referencedatagridfield/branches/collective_ns/collective/referencedatagridfield/skins/referencedatagridfield/referencebrowser.js @ 2363

Last change on this file since 2363 was 2363, checked in by mylan, 14 years ago

Fixed clashes of ReferenceDataGridField? widget functionality with standard ReferenceBrowserWidget? one

File size: 12.7 KB
Line 
1function prepareRefPopup(context) {
2    jq(function() {
3
4      // the overlay itself
5      jq('.addreference', context).overlay({
6           closeOnClick: false,
7           onBeforeLoad: function() {
8               ov = jq('div#content').data('overlay');
9               // close overlay, if there is one already
10               // we only allow one referencebrowser per time
11               if (ov) {ov.close(); }
12               var wrap = this.getContent().find('.overlaycontent');
13               var src = this.getTrigger().attr('src');
14               var srcfilter = src + ' >*';
15               wrap.data('srcfilter', srcfilter);
16               jq('div#content').data('overlay', this);
17               resetHistory();
18               wrap.load(srcfilter);
19               },
20           onLoad: function() {
21               widget_id = this.getTrigger().attr('rel').substring(6);
22               disablecurrentrelations(widget_id);
23           }});
24
25      // the breadcrumb-links and the links of the 'tree'-navigation
26      jq('[id^=atrb_] a.browsesite', context).live('click', function(event) {
27          var target = jq(this);
28          var src = target.attr('href');
29          var wrap = target.parents('.overlaycontent');
30          var srcfilter = src + ' >*';
31          pushToHistory(wrap.data('srcfilter'));
32          wrap.data('srcfilter', srcfilter);
33          // the history we are constructing here is destinct from the
34          // srcfilter-history. here we construct a selection-widget, which
35          // is available, if the history_length-parameter is set on the widget
36          // the srcfilter-history is used for storing the URLs to make the
37          // 'Back'-link work.
38          var newoption = '<option value="' + src + '">' + target.attr('rel') + '</option>';
39          refreshOverlay(wrap, srcfilter, newoption);
40          return false;
41          });
42
43      // the links for inserting referencens
44      jq('[id^=atrb_] input.insertreference', context).live('click', function(event) {
45          var target = jq(this);
46          var wrap = target.parents('.overlaycontent');
47          var fieldname = wrap.find('input[name=fieldName]').attr('value');
48          var fieldtitle = wrap.find('input[name=fieldTitleName]').attr('value');
49          var fieldlink = wrap.find('input[name=fieldLinkName]').attr('value');
50          var multi = wrap.find('input[name=multiValued]').attr('value');
51          var close_window = wrap.find('input[name=close_window]').attr('value');
52          var title = target.parents('tr').find('img').attr('alt');
53          var linkpath = target.parents('tr').find('img').attr('rel');
54          var active_tr = wrap.parents('tr[id=datagridwidget-row]');
55          var uid = target.attr('rel');
56          refbrowser_setReference(fieldname, uid, title, parseInt(multi),
57                                  active_tr, fieldtitle, title, fieldlink, linkpath);
58          if (close_window === '1') {
59              overlay = jq('div#content').data('overlay');
60              overlay.close();
61          } else {
62              showMessage(title);
63          };
64          jq(this).attr('disabled', 'disabled');
65          });
66
67
68      // the history menu
69      jq('[id^=atrb_] form#history select[name=path]', context).live('change', function(event) {
70          var target = jq(this);
71          var wrap = target.parents('.overlaycontent');
72          src = jq('[id^=atrb_] form#history select[name=path] :selected', this).attr('value');
73          var srcfilter = src + ' >*';
74          refreshOverlay(wrap, srcfilter, '');
75          return false;
76          });
77
78      // the search form
79      jq('[id^=atrb_] form#search input[name=submit]', context).live('click', function(event) {
80          var target = jq(this);
81          var src = target.parents('form').attr('action');
82          var wrap = target.parents('.overlaycontent');
83          var fieldname = wrap.find('input[name=fieldName]').attr('value');
84          var fieldtitle = wrap.find('input[name=fieldTitleName]').attr('value');
85          var fieldlink = wrap.find('input[name=fieldLinkName]').attr('value');
86          var fieldrealname = wrap.find('input[name=fieldRealName]').attr('value');
87          var at_url = wrap.find('input[name=at_url]').attr('value');
88          var searchvalue = wrap.find('input[name=searchValue]').attr('value');
89          var multi = wrap.find('input[name=multiValued]').attr('value');
90          var close_window = wrap.find('input[name=close_window]').attr('value');
91          qs = 'searchValue=' + searchvalue + '&fieldRealName=' + fieldrealname +
92            '&fieldName=' + fieldname + '&multiValued=' + multi +
93            '&close_window=' + close_window + '&at_url=' + at_url +
94            '&fieldTitleName=' + fieldtitle + '&fieldLinkName=' + fieldlink;
95          var srcfilter = src + '?' + qs + ' >*';
96          pushToHistory(wrap.data('srcfilter'));
97          wrap.data('srcfilter', srcfilter);
98          refreshOverlay(wrap, srcfilter, '');
99          return false;
100          });
101
102    });
103};
104
105jq(document).ready(function(){
106    prepareRefPopup(this);
107});
108jq.fn.prepRefPopup = function() {
109    prepareRefPopup(this);
110};
111
112function disablecurrentrelations (widget_id) {
113   jq('ul#' + widget_id + ' :input').each(
114       function (intIndex) {
115         uid = jq(this).attr('value');
116         cb = jq('input[rel=' + uid + ']');
117         cb.attr('disabled', 'disabled');
118         cb.attr('checked', 'checked');
119       });
120}
121
122// function to return a reference from the popup window back into the widget
123function refbrowser_setReference(widget_id, uid, label, multi,
124                                 active_tr, widget_title_id, link_title,
125                                 widget_link_id, link_path)
126{
127    var element = null,
128        label_element = null,
129        current_values = null,
130        i = null,
131        list = null,
132        li = null,
133        input = null,
134        up_element = null,
135        down_element = null,
136        container = null;
137
138    if (typeof(active_tr) != "undefined"
139       && typeof(link_title) != "undefined"
140       && typeof(link_path) != "undefined"
141       && typeof(widget_title_id) != "undefined"
142       && typeof(widget_link_id) != "undefined") {
143        // Update Uid field
144        jq('#' + widget_id, active_tr).attr("value", uid);
145        // Update title field
146        title = jq('#' + widget_title_id, active_tr);
147        title.attr("value", link_title);
148        title.addClass("not-changed-title-field");
149        title.attr("default_value", link_title);
150        title.blur(triggerTitleClass);
151        title.focus(triggerOnFocusStyles);
152        // Update link field
153        link = jq('#' + widget_link_id, active_tr);
154        link.attr('readonly', false);
155        link.attr('value', link_path);
156        link.attr('readonly', true);
157        link.addClass("hidden-field");
158
159    } else if (multi === 0) {
160        // differentiate between the single and mulitselect widget
161        // since the single widget has an extra label field.
162        jq('#' + widget_id).attr('value', uid);
163        jq('#' + widget_id + '_label').attr('value', label);
164    }  else {
165        // check if the item isn't already in the list
166        current_values = jq('#' + widget_id + ' input');
167        for (i = 0; i < current_values.length; i++) {
168            if (current_values[i].value === uid) {
169                return false;
170            }
171        }
172        // now add the new item
173        list = document.getElementById(widget_id);
174        // add ul-element to DOM, if it is not there
175        if (list === null) {
176            container = jq('#archetypes-fieldname-' + widget_id + ' input + div');
177            if (!container.length) {
178                // fix for Plone 3.3 collections, with a weird widget-id
179                container = jq('#archetypes-fieldname-value input + div');
180            }
181            container.after(
182               '<ul class="visualNoMarker" id="' + widget_id + '"></ul>');
183            list = document.getElementById(widget_id);
184        }
185        li = document.createElement('li');
186        label_element = document.createElement('label');
187        input = document.createElement('input');
188        input.type = 'checkbox';
189        input.value = uid;
190        input.checked = true;
191        input.name = widget_id + ':list';
192        label_element.appendChild(input);
193        label_element.appendChild(document.createTextNode(' ' + label));
194        li.appendChild(label_element);
195        li.id = 'ref-' + widget_id + '-' + current_values.length;
196
197        sortable = jq('input[name=' + widget_id + '-sortable]').attr('value');
198        if (sortable === '1') {
199          up_element = document.createElement('a');
200          up_element.title = 'Move Up';
201          up_element.innerHTML = '&#x25b2;';
202          up_element.onclick = function () {
203              refbrowser_moveReferenceUp(this);
204              return false;
205          };
206
207          li.appendChild(up_element);
208
209          down_element = document.createElement('a');
210          down_element.title = 'Move Down';
211          down_element.innerHTML = '&#x25bc;';
212          down_element.onclick = function () {
213              refbrowser_moveReferenceDown(this);
214              return false;
215          };
216
217          li.appendChild(down_element);
218        }
219        list.appendChild(li);
220
221        // fix on IE7 - check *after* adding to DOM
222        input.checked = true;
223    }
224}
225
226// function to clear the reference field or remove items
227// from the multivalued reference list.
228function refbrowser_removeReference(widget_id, multi)
229{
230    var x = null,
231        element = null,
232        label_element = null,
233        list = null;
234
235    if (multi) {
236        list = document.getElementById(widget_id);
237        for (x = list.length - 1; x >= 0; x--) {
238            if (list[x].selected) {
239                list[x] = null;
240            }
241        }
242        for (x = 0; x < list.length; x++) {
243            list[x].selected = 'selected';
244        }
245    } else {
246        jq('#' + widget_id).attr('value', "");
247        jq('#' + widget_id + '_label').attr('value', "");
248    }
249}
250
251function refbrowser_moveReferenceUp(self)
252{
253    var elem = self.parentNode,
254        eid = null,
255        pos = null,
256        widget_id = null,
257        newelem = null,
258        prevelem = null,
259        arrows = null,
260        cbs = null;
261    if (elem === null) {
262        return false;
263    }
264    eid = elem.id.split('-');
265    pos = eid.pop();
266    if (pos === 0) {
267        return false;
268    }
269    widget_id = eid.pop();
270    newelem = elem.cloneNode(true);
271
272    //Fix: (IE keep the standard value)
273    cbs = newelem.getElementsByTagName("input");
274    if (cbs.length > 0) {
275        cbs[0].checked = elem.getElementsByTagName("input")[0].checked;
276    }
277
278    prevelem = document.getElementById('ref-' + widget_id + '-' + (pos - 1));
279
280    // up arrow
281    arrows = newelem.getElementsByTagName("a");
282    arrows[0].onclick = function () {
283        refbrowser_moveReferenceUp(this);
284    };
285    // down arrow
286    arrows[1].onclick = function () {
287        refbrowser_moveReferenceDown(this);
288    };
289
290    elem.parentNode.insertBefore(newelem, prevelem);
291    elem.parentNode.removeChild(elem);
292    newelem.id = 'ref-' + widget_id + '-' + (pos - 1);
293    prevelem.id = 'ref-' + widget_id + '-' + pos;
294}
295
296function refbrowser_moveReferenceDown(self)
297{
298    var elem = self.parentNode,
299        eid = null,
300        pos = null,
301        widget_id = null,
302        current_values = null,
303        newelem = null,
304        nextelem = null,
305        cbs = null,
306        arrows = null;
307    if (elem === null) {
308        return false;
309    }
310    eid = elem.id.split('-');
311    pos = parseInt(eid.pop(), 10);
312    widget_id = eid.pop();
313    current_values = jq('#' + widget_id + ' input');
314    if ((pos + 1) === current_values.length) {
315        return false;
316    }
317
318    newelem = elem.cloneNode(true);
319    //Fix: (IE keep the standard value)
320    cbs = newelem.getElementsByTagName("input");
321    if (cbs.length > 0) {
322        cbs[0].checked = elem.getElementsByTagName("input")[0].checked;
323    }
324
325    // up img
326    arrows = newelem.getElementsByTagName("a");
327    arrows[0].onclick = function () {
328        refbrowser_moveReferenceUp(this);
329    };
330    // down img
331    arrows[1].onclick = function () {
332        refbrowser_moveReferenceDown(this);
333    };
334
335    nextelem = document.getElementById('ref-' + widget_id + '-' + (pos + 1));
336
337    elem.parentNode.insertBefore(newelem, nextelem.nextSibling);
338    elem.parentNode.removeChild(elem);
339    newelem.id = 'ref-' + widget_id + '-' + (pos + 1);
340    nextelem.id = 'ref-' + widget_id + '-' + pos;
341}
342
343function showMessage(message) {
344    jq('#messageTitle').text(message);
345    jq('#message').show();
346}
347
348function submitHistoryForm() {
349     var form = document.history;
350     var path = form.path.options[form.path.selectedIndex].value;
351     form.action = path;
352     form.submit();
353}
354
355function pushToHistory(url) {
356  var history = jq(document).data('atrb_history');
357  history.push(url);
358  jq(document).data('atrb_history', history);
359}
360
361function resetHistory() {
362  jq(document).data('atrb_history', []);
363}
364
365function popFromHistory() {
366  var history = jq(document).data('atrb_history');
367  value = history.pop();
368  jq(document).data('atrb_history', history);
369  return value;
370}
371
372function refreshOverlay(wrap, srcfilter, newoption) {
373    var oldhistory = jq('[id^=atrb_] form#history select');
374    wrap.load(srcfilter, function() { 
375        jq('[id^=atrb_] form#history select').append(newoption + oldhistory.html());
376        ov = jq('div#content').data('overlay');
377        widget_id = ov.getTrigger().attr('rel').substring(6);
378        disablecurrentrelations(widget_id);
379        });
380}
Note: See TracBrowser for help on using the repository browser.