devel_node_access.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /**
  2. * @file
  3. * Behavior for Devel node access.
  4. */
  5. (function ($) {
  6. /**
  7. * Performs the access by user ajax request.
  8. */
  9. function devel_node_access_user_ajax(context, settings) {
  10. // Get the cell ID for the first .dna-permission that isn't processed.
  11. var cell = $('td.dna-permission', context)
  12. .not('.ajax-processed', context)
  13. .attr('id');
  14. if (cell !== undefined) {
  15. // Generate the URI from the basePath, path, data type, cell ID, and a
  16. // random token to bypass caching.
  17. var url = settings.basePath
  18. + "?q="
  19. + 'devel/node_access/by_user/json/'
  20. + cell
  21. + '/'
  22. + Math.floor((1000000000 * Math.random())).toString(16);
  23. // Execute Ajax callback and handle the response.
  24. $.getJSON(url, function(data) {
  25. $('#' + cell, context).html(data).addClass('ajax-processed');
  26. // Call this function again.
  27. devel_node_access_user_ajax(context, settings);
  28. });
  29. // Ajax fails silently on error, mark bad requests with an error message.
  30. // If the request is just slow this will update when the request succeeds.
  31. setTimeout(
  32. function() {
  33. if ($('#' + cell, context).hasClass('ajax-processed') == false) {
  34. $('#' + cell, context)
  35. .html(
  36. '<span class="error">'
  37. + '<a href="' + url.replace('/json/', '/html/') + '">'
  38. + Drupal.t('Error: could not explain access')
  39. + '</a>'
  40. + '</span>'
  41. )
  42. .addClass('ajax-processed');
  43. // Call this function again.
  44. devel_node_access_user_ajax(context, settings);
  45. }
  46. },
  47. 3000
  48. );
  49. }
  50. }
  51. /**
  52. * Attach the access by user behavior which initiates ajax.
  53. *
  54. * @type {Drupal~behavior}
  55. */
  56. Drupal.behaviors.develNodeAccessUserAjax = {
  57. attach: function(context, settings) {
  58. // Start the ajax.
  59. devel_node_access_user_ajax(context, settings);
  60. }
  61. };
  62. })(jQuery);