source: products/quintagroup.referencedatagridfield/branches/plone4/quintagroup/referencedatagridfield/skins/referencedatagridfield/referencebrowser.js @ 2312

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

Update default referencebrowser javascript / datagrid field to popup work correctly for new added row

File size: 11.2 KB
Line 
1prepareRefPopup = function(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', this).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', this).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 multi = wrap.find('input[name=multiValued]').attr('value');
49          var close_window = wrap.find('input[name=close_window]').attr('value');
50          var title = target.parents('tr').find('img').attr('alt');
51          var uid = target.attr('rel');
52          refbrowser_setReference(fieldname, uid, title, parseInt(multi));
53          if (close_window === '1') {
54              overlay = jq('div#content', this).data('overlay');
55              overlay.close();
56          } else {
57              showMessage(title);
58          };
59          jq(this).attr('disabled', 'disabled');
60          });
61
62
63      // the history menu
64      jq('[id^=atrb_] form#history select[name=path]', context).live('change', function(event) {
65          var target = jq(this);
66          var wrap = target.parents('.overlaycontent');
67          src = jq('[id^=atrb_] form#history select[name=path] :selected', this).attr('value');
68          var srcfilter = src + ' >*';
69          refreshOverlay(wrap, srcfilter, '');
70          return false;
71          });
72
73      // the search form
74      jq('[id^=atrb_] form#search input[name=submit]', context).live('click', function(event) {
75          var target = jq(this);
76          var src = target.parents('form').attr('action');
77          var wrap = target.parents('.overlaycontent');
78          var fieldname = wrap.find('input[name=fieldName]').attr('value');
79          var fieldrealname = wrap.find('input[name=fieldRealName]').attr('value');
80          var at_url = wrap.find('input[name=at_url]').attr('value');
81          var searchvalue = wrap.find('input[name=searchValue]').attr('value');
82          var multi = wrap.find('input[name=multiValued]').attr('value');
83          var close_window = wrap.find('input[name=close_window]').attr('value');
84          qs = 'searchValue=' + searchvalue + '&fieldRealName=' + fieldrealname +
85            '&fieldName=' + fieldname + '&multiValued=' + multi +
86            '&close_window' + close_window + '&at_url=' + at_url;
87          var srcfilter = src + '?' + qs + ' >*';
88          pushToHistory(wrap.data('srcfilter'));
89          wrap.data('srcfilter', srcfilter);
90          refreshOverlay(wrap, srcfilter, '');
91          return false;
92          });
93
94    });
95};
96
97jq(document).ready(function(){
98    prepareRefPopup(this);
99});
100jq.fn.prepRefPopup = function() {
101    prepareRefPopup(this);
102};
103
104function disablecurrentrelations (widget_id) {
105   jq('ul#' + widget_id + ' :input').each(
106       function (intIndex) {
107         uid = jq(this).attr('value');
108         cb = jq('input[rel=' + uid + ']');
109         cb.attr('disabled', 'disabled');
110         cb.attr('checked', 'checked');
111       });
112}
113
114// function to return a reference from the popup window back into the widget
115function refbrowser_setReference(widget_id, uid, label, multi)
116{
117    var element = null,
118        label_element = null,
119        current_values = null,
120        i = null,
121        list = null,
122        li = null,
123        input = null,
124        up_element = null,
125        down_element = null,
126        container = null;
127    // differentiate between the single and mulitselect widget
128    // since the single widget has an extra label field.
129    if (multi === 0) {
130        jq('#' + widget_id).attr('value', uid);
131        jq('#' + widget_id + '_label').attr('value', label);
132    }  else {
133        // check if the item isn't already in the list
134        current_values = jq('#' + widget_id + ' input');
135        for (i = 0; i < current_values.length; i++) {
136            if (current_values[i].value === uid) {
137                return false;
138            }
139        }
140        // now add the new item
141        list = document.getElementById(widget_id);
142        // add ul-element to DOM, if it is not there
143        if (list === null) {
144            container = jq('#archetypes-fieldname-' + widget_id + ' input + div');
145            if (!container.length) {
146                // fix for Plone 3.3 collections, with a weird widget-id
147                container = jq('#archetypes-fieldname-value input + div');
148            }
149            container.after(
150               '<ul class="visualNoMarker" id="' + widget_id + '"></ul>');
151            list = document.getElementById(widget_id);
152        }
153        li = document.createElement('li');
154        label_element = document.createElement('label');
155        input = document.createElement('input');
156        input.type = 'checkbox';
157        input.value = uid;
158        input.checked = true;
159        input.name = widget_id + ':list';
160        label_element.appendChild(input);
161        label_element.appendChild(document.createTextNode(' ' + label));
162        li.appendChild(label_element);
163        li.id = 'ref-' + widget_id + '-' + current_values.length;
164
165        sortable = jq('input[name=' + widget_id + '-sortable]').attr('value');
166        if (sortable === '1') {
167          up_element = document.createElement('a');
168          up_element.title = 'Move Up';
169          up_element.innerHTML = '&#x25b2;';
170          up_element.onclick = function () {
171              refbrowser_moveReferenceUp(this);
172              return false;
173          };
174
175          li.appendChild(up_element);
176
177          down_element = document.createElement('a');
178          down_element.title = 'Move Down';
179          down_element.innerHTML = '&#x25bc;';
180          down_element.onclick = function () {
181              refbrowser_moveReferenceDown(this);
182              return false;
183          };
184
185          li.appendChild(down_element);
186        }
187        list.appendChild(li);
188
189        // fix on IE7 - check *after* adding to DOM
190        input.checked = true;
191    }
192}
193
194// function to clear the reference field or remove items
195// from the multivalued reference list.
196function refbrowser_removeReference(widget_id, multi)
197{
198    var x = null,
199        element = null,
200        label_element = null,
201        list = null;
202
203    if (multi) {
204        list = document.getElementById(widget_id);
205        for (x = list.length - 1; x >= 0; x--) {
206            if (list[x].selected) {
207                list[x] = null;
208            }
209        }
210        for (x = 0; x < list.length; x++) {
211            list[x].selected = 'selected';
212        }
213    } else {
214        jq('#' + widget_id).attr('value', "");
215        jq('#' + widget_id + '_label').attr('value', "");
216    }
217}
218
219function refbrowser_moveReferenceUp(self)
220{
221    var elem = self.parentNode,
222        eid = null,
223        pos = null,
224        widget_id = null,
225        newelem = null,
226        prevelem = null,
227        arrows = null,
228        cbs = null;
229    if (elem === null) {
230        return false;
231    }
232    eid = elem.id.split('-');
233    pos = eid.pop();
234    if (pos === 0) {
235        return false;
236    }
237    widget_id = eid.pop();
238    newelem = elem.cloneNode(true);
239
240    //Fix: (IE keep the standard value)
241    cbs = newelem.getElementsByTagName("input");
242    if (cbs.length > 0) {
243        cbs[0].checked = elem.getElementsByTagName("input")[0].checked;
244    }
245
246    prevelem = document.getElementById('ref-' + widget_id + '-' + (pos - 1));
247
248    // up arrow
249    arrows = newelem.getElementsByTagName("a");
250    arrows[0].onclick = function () {
251        refbrowser_moveReferenceUp(this);
252    };
253    // down arrow
254    arrows[1].onclick = function () {
255        refbrowser_moveReferenceDown(this);
256    };
257
258    elem.parentNode.insertBefore(newelem, prevelem);
259    elem.parentNode.removeChild(elem);
260    newelem.id = 'ref-' + widget_id + '-' + (pos - 1);
261    prevelem.id = 'ref-' + widget_id + '-' + pos;
262}
263
264function refbrowser_moveReferenceDown(self)
265{
266    var elem = self.parentNode,
267        eid = null,
268        pos = null,
269        widget_id = null,
270        current_values = null,
271        newelem = null,
272        nextelem = null,
273        cbs = null,
274        arrows = null;
275    if (elem === null) {
276        return false;
277    }
278    eid = elem.id.split('-');
279    pos = parseInt(eid.pop(), 10);
280    widget_id = eid.pop();
281    current_values = jq('#' + widget_id + ' input');
282    if ((pos + 1) === current_values.length) {
283        return false;
284    }
285
286    newelem = elem.cloneNode(true);
287    //Fix: (IE keep the standard value)
288    cbs = newelem.getElementsByTagName("input");
289    if (cbs.length > 0) {
290        cbs[0].checked = elem.getElementsByTagName("input")[0].checked;
291    }
292
293    // up img
294    arrows = newelem.getElementsByTagName("a");
295    arrows[0].onclick = function () {
296        refbrowser_moveReferenceUp(this);
297    };
298    // down img
299    arrows[1].onclick = function () {
300        refbrowser_moveReferenceDown(this);
301    };
302
303    nextelem = document.getElementById('ref-' + widget_id + '-' + (pos + 1));
304
305    elem.parentNode.insertBefore(newelem, nextelem.nextSibling);
306    elem.parentNode.removeChild(elem);
307    newelem.id = 'ref-' + widget_id + '-' + (pos + 1);
308    nextelem.id = 'ref-' + widget_id + '-' + pos;
309}
310
311function showMessage(message) {
312    jq('#messageTitle').text(message);
313    jq('#message').show();
314}
315
316function submitHistoryForm() {
317     var form = document.history;
318     var path = form.path.options[form.path.selectedIndex].value;
319     form.action = path;
320     form.submit();
321}
322
323function pushToHistory(url) {
324  var history = jq(document).data('atrb_history');
325  history.push(url);
326  jq(document).data('atrb_history', history);
327}
328
329function resetHistory() {
330  jq(document).data('atrb_history', []);
331}
332
333function popFromHistory() {
334  var history = jq(document).data('atrb_history');
335  value = history.pop();
336  jq(document).data('atrb_history', history);
337  return value;
338}
339
340function refreshOverlay(wrap, srcfilter, newoption) {
341    var oldhistory = jq('[id^=atrb_] form#history select');
342    wrap.load(srcfilter, function() { 
343        jq('[id^=atrb_] form#history select').append(newoption + oldhistory.html());
344        ov = jq('div#content').data('overlay');
345        widget_id = ov.getTrigger().attr('rel').substring(6);
346        disablecurrentrelations(widget_id);
347        });
348}
Note: See TracBrowser for help on using the repository browser.