annotate noderef_view.js @ 0:a3c1e224e807

Import initial du module noderef_view
author Franck Deroche <franck@defr.org>
date Fri, 23 Jan 2009 14:52:03 +0100
parents
children 25a0c2fcbcfb
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@0 9 var id = $(this).attr('rel');
franck@0 10 var wrapper = $('#' + id);
franck@0 11
franck@0 12 // Suppress behavior if it was previsously attached
franck@0 13 $(document).unbind('popups_form_success.noderef_view');
franck@0 14
franck@0 15 // And now bind it. This code will be executed after successful
franck@0 16 // completion of the popup select form
franck@0 17 $(document).bind('popups_form_success.noderef_view', function() {
franck@0 18 selection = noderef_view_get_selection();
franck@0 19 for(nid in selection) {
franck@0 20 // Autocomplete field
franck@0 21 $('input.form-autocomplete:not([value])', wrapper)
franck@0 22 .filter(':first')
franck@0 23 .val(selection[nid]);
franck@0 24 // Select field
franck@0 25 $('select', wrapper).val(nid);
franck@0 26 // Radio field
franck@0 27 $('input:radio[value='+ nid + '], input:checkbox[value='+ nid +']', wrapper).attr('checked', 'checked');
franck@0 28 }
franck@0 29 });
franck@0 30 });
franck@0 31 });
franck@0 32 }
franck@0 33
franck@0 34 /**
franck@0 35 * Helper function to extract the references values
franck@0 36 */
franck@0 37 function noderef_view_get_selection() {
franck@0 38 var cookies = document.cookie.split(';'), selection = {};
franck@0 39 for(var i = 0; i < cookies.length; i++) {
franck@0 40 var cookie = $.trim(cookies[i]).split('=');
franck@0 41 if (cookie[0].indexOf('noderef[') == 0) {
franck@0 42 var nid = cookie[0].replace(/noderef\[(\d+)\]/, "$1");
franck@0 43 var title = decodeURIComponent(cookie[1]);
franck@0 44 selection[nid] = title;
franck@0 45 }
franck@0 46 }
franck@0 47 return selection;
franck@0 48 }