annotate noderef_view.js @ 3:9372c6448311

Add the link in an after_build callback to be a lot more robust. This way, we act after the noderefence fields got transformed into normal widgets by the form processing code. We can thus be pretty sure that the code we put in there will end up where we expect it too.
author Franck Deroche <franck@defr.org>
date Mon, 26 Jan 2009 12:28:09 +0100
parents 25a0c2fcbcfb
children b4c1e3d5d5ce
rev   line source
franck@0 1 // vim: set ts=2 sw=2 expandtab syntax=php:
franck@0 2 /**
franck@0 3 * Attach the noderef_view behavior.
franck@0 4 */
franck@0 5 Drupal.behaviors.noderef_view = function(context) {
franck@0 6 $('.noderef_view_link', context).each(function(i, obj) {
franck@0 7 $(obj).addClass('noderef_view_link_processed').click(function() {
franck@0 8 // Get the wrapper
franck@2 9 var wrapper = $(this).prev();
franck@2 10 while (!wrapper.is('div')) {
franck@2 11 wrapper = wrapper.prev();
franck@2 12 }
franck@0 13
franck@0 14 // Suppress behavior if it was previsously attached
franck@0 15 $(document).unbind('popups_form_success.noderef_view');
franck@0 16
franck@0 17 // And now bind it. This code will be executed after successful
franck@0 18 // completion of the popup select form
franck@0 19 $(document).bind('popups_form_success.noderef_view', function() {
franck@0 20 selection = noderef_view_get_selection();
franck@0 21 for(nid in selection) {
franck@0 22 // Autocomplete field
franck@0 23 $('input.form-autocomplete:not([value])', wrapper)
franck@0 24 .filter(':first')
franck@0 25 .val(selection[nid]);
franck@0 26 // Select field
franck@0 27 $('select', wrapper).val(nid);
franck@0 28 // Radio field
franck@0 29 $('input:radio[value='+ nid + '], input:checkbox[value='+ nid +']', wrapper).attr('checked', 'checked');
franck@0 30 }
franck@0 31 });
franck@0 32 });
franck@0 33 });
franck@0 34 }
franck@0 35
franck@0 36 /**
franck@0 37 * Helper function to extract the references values
franck@0 38 */
franck@0 39 function noderef_view_get_selection() {
franck@0 40 var cookies = document.cookie.split(';'), selection = {};
franck@0 41 for(var i = 0; i < cookies.length; i++) {
franck@0 42 var cookie = $.trim(cookies[i]).split('=');
franck@0 43 if (cookie[0].indexOf('noderef[') == 0) {
franck@0 44 var nid = cookie[0].replace(/noderef\[(\d+)\]/, "$1");
franck@0 45 var title = decodeURIComponent(cookie[1]);
franck@0 46 selection[nid] = title;
franck@0 47 }
franck@0 48 }
franck@0 49 return selection;
franck@0 50 }