[2323] | 1 | function prepareRefPopup(context) { |
---|
[2312] | 2 | jq(function() { |
---|
[2298] | 3 | |
---|
[2312] | 4 | // the overlay itself |
---|
| 5 | jq('.addreference', context).overlay({ |
---|
| 6 | closeOnClick: false, |
---|
| 7 | onBeforeLoad: function() { |
---|
[2323] | 8 | ov = jq('div#content').data('overlay'); |
---|
[2312] | 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); |
---|
[2323] | 16 | jq('div#content').data('overlay', this); |
---|
[2312] | 17 | resetHistory(); |
---|
| 18 | wrap.load(srcfilter); |
---|
| 19 | }, |
---|
[2323] | 20 | onLoad: function() { |
---|
[2312] | 21 | widget_id = this.getTrigger().attr('rel').substring(6); |
---|
| 22 | disablecurrentrelations(widget_id); |
---|
| 23 | }}); |
---|
[2298] | 24 | |
---|
[2312] | 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 | }); |
---|
[2298] | 42 | |
---|
[2312] | 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'); |
---|
[2319] | 48 | var fieldtitle = wrap.find('input[name=fieldTitleName]').attr('value'); |
---|
| 49 | var fieldlink = wrap.find('input[name=fieldLinkName]').attr('value'); |
---|
[2312] | 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'); |
---|
[2319] | 53 | var linkpath = target.parents('tr').find('img').attr('rel'); |
---|
| 54 | var active_tr = wrap.parents('tr[id=datagridwidget-row]'); |
---|
[2312] | 55 | var uid = target.attr('rel'); |
---|
[2319] | 56 | refbrowser_setReference(fieldname, uid, title, parseInt(multi), |
---|
| 57 | active_tr, fieldtitle, title, fieldlink, linkpath); |
---|
[2312] | 58 | if (close_window === '1') { |
---|
[2326] | 59 | overlay = jq('div#content').data('overlay'); |
---|
[2312] | 60 | overlay.close(); |
---|
| 61 | } else { |
---|
| 62 | showMessage(title); |
---|
| 63 | }; |
---|
| 64 | jq(this).attr('disabled', 'disabled'); |
---|
| 65 | }); |
---|
[2291] | 66 | |
---|
| 67 | |
---|
[2312] | 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 | }); |
---|
[2291] | 77 | |
---|
[2312] | 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'); |
---|
[2319] | 84 | var fieldtitle = wrap.find('input[name=fieldTitleName]').attr('value'); |
---|
| 85 | var fieldlink = wrap.find('input[name=fieldLinkName]').attr('value'); |
---|
[2312] | 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 + |
---|
[2319] | 93 | '&close_window=' + close_window + '&at_url=' + at_url + |
---|
| 94 | '&fieldTitleName=' + fieldtitle + '&fieldLinkName=' + fieldlink; |
---|
[2312] | 95 | var srcfilter = src + '?' + qs + ' >*'; |
---|
| 96 | pushToHistory(wrap.data('srcfilter')); |
---|
| 97 | wrap.data('srcfilter', srcfilter); |
---|
| 98 | refreshOverlay(wrap, srcfilter, ''); |
---|
| 99 | return false; |
---|
| 100 | }); |
---|
[2291] | 101 | |
---|
[2312] | 102 | }); |
---|
| 103 | }; |
---|
| 104 | |
---|
| 105 | jq(document).ready(function(){ |
---|
| 106 | prepareRefPopup(this); |
---|
[2311] | 107 | }); |
---|
[2312] | 108 | jq.fn.prepRefPopup = function() { |
---|
| 109 | prepareRefPopup(this); |
---|
| 110 | }; |
---|
[2291] | 111 | |
---|
[2311] | 112 | function 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 | }); |
---|
[2291] | 120 | } |
---|
| 121 | |
---|
[2311] | 122 | // function to return a reference from the popup window back into the widget |
---|
[2319] | 123 | function refbrowser_setReference(widget_id, uid, label, multi, |
---|
| 124 | active_tr, widget_title_id, link_title, |
---|
| 125 | widget_link_id, link_path) |
---|
[2311] | 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; |
---|
[2319] | 137 | |
---|
[2363] | 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") { |
---|
[2325] | 143 | // Update Uid field |
---|
| 144 | jq('#' + widget_id, active_tr).attr("value", uid); |
---|
| 145 | // Update title field |
---|
[2319] | 146 | title = jq('#' + widget_title_id, active_tr); |
---|
[2325] | 147 | title.attr("value", link_title); |
---|
[2319] | 148 | title.addClass("not-changed-title-field"); |
---|
| 149 | title.attr("default_value", link_title); |
---|
| 150 | title.blur(triggerTitleClass); |
---|
| 151 | title.focus(triggerOnFocusStyles); |
---|
[2325] | 152 | // Update link field |
---|
[2319] | 153 | link = jq('#' + widget_link_id, active_tr); |
---|
| 154 | link.attr('readonly', false); |
---|
[2325] | 155 | link.attr('value', link_path); |
---|
[2319] | 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. |
---|
[2311] | 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; |
---|
[2291] | 196 | |
---|
[2311] | 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 = '▲'; |
---|
| 202 | up_element.onclick = function () { |
---|
| 203 | refbrowser_moveReferenceUp(this); |
---|
| 204 | return false; |
---|
| 205 | }; |
---|
[2269] | 206 | |
---|
[2311] | 207 | li.appendChild(up_element); |
---|
[2269] | 208 | |
---|
[2311] | 209 | down_element = document.createElement('a'); |
---|
| 210 | down_element.title = 'Move Down'; |
---|
| 211 | down_element.innerHTML = '▼'; |
---|
| 212 | down_element.onclick = function () { |
---|
| 213 | refbrowser_moveReferenceDown(this); |
---|
| 214 | return false; |
---|
| 215 | }; |
---|
[2269] | 216 | |
---|
[2311] | 217 | li.appendChild(down_element); |
---|
| 218 | } |
---|
| 219 | list.appendChild(li); |
---|
| 220 | |
---|
| 221 | // fix on IE7 - check *after* adding to DOM |
---|
| 222 | input.checked = true; |
---|
[2269] | 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
[2311] | 226 | // function to clear the reference field or remove items |
---|
| 227 | // from the multivalued reference list. |
---|
| 228 | function refbrowser_removeReference(widget_id, multi) |
---|
| 229 | { |
---|
| 230 | var x = null, |
---|
| 231 | element = null, |
---|
| 232 | label_element = null, |
---|
| 233 | list = null; |
---|
[2269] | 234 | |
---|
[2311] | 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; |
---|
[2269] | 240 | } |
---|
| 241 | } |
---|
[2311] | 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', ""); |
---|
[2269] | 248 | } |
---|
| 249 | } |
---|
| 250 | |
---|
[2311] | 251 | function 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); |
---|
[2269] | 271 | |
---|
[2311] | 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 | } |
---|
[2269] | 277 | |
---|
[2311] | 278 | prevelem = document.getElementById('ref-' + widget_id + '-' + (pos - 1)); |
---|
[2269] | 279 | |
---|
[2311] | 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 | }; |
---|
[2269] | 289 | |
---|
[2311] | 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; |
---|
[2269] | 294 | } |
---|
| 295 | |
---|
[2311] | 296 | function refbrowser_moveReferenceDown(self) |
---|
[2269] | 297 | { |
---|
[2311] | 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; |
---|
[2273] | 309 | } |
---|
[2311] | 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 | } |
---|
[2269] | 317 | |
---|
[2311] | 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; |
---|
[2269] | 323 | } |
---|
[2311] | 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; |
---|
[2269] | 341 | } |
---|
| 342 | |
---|
[2311] | 343 | function showMessage(message) { |
---|
| 344 | jq('#messageTitle').text(message); |
---|
| 345 | jq('#message').show(); |
---|
| 346 | } |
---|
[2269] | 347 | |
---|
[2311] | 348 | function 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 | |
---|
| 355 | function pushToHistory(url) { |
---|
| 356 | var history = jq(document).data('atrb_history'); |
---|
| 357 | history.push(url); |
---|
| 358 | jq(document).data('atrb_history', history); |
---|
| 359 | } |
---|
| 360 | |
---|
| 361 | function resetHistory() { |
---|
| 362 | jq(document).data('atrb_history', []); |
---|
| 363 | } |
---|
| 364 | |
---|
| 365 | function 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 | |
---|
| 372 | function 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 | } |
---|