PageRenderTime 24ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/plugins/sfDrupalPlugin/vendor/drupal/modules/dashboard/dashboard.js

https://github.com/mikesname/ehri-ica-atom
JavaScript | 176 lines | 109 code | 17 blank | 50 comment | 14 complexity | 80a34bced4a27a84215abf394da17d33 MD5 | raw file
  1. // $Id: dashboard.js,v 1.6 2010/01/14 04:04:30 webchick Exp $
  2. (function ($) {
  3. /**
  4. * Implementation of Drupal.behaviors for dashboard.
  5. */
  6. Drupal.behaviors.dashboard = {
  7. attach: function () {
  8. $('#dashboard').prepend('<div class="customize"><ul class="action-links"><a href="#">' + Drupal.t('Customize dashboard') + '</a></ul><div class="canvas"></div></div>');
  9. $('#dashboard .customize .action-links a').click(Drupal.behaviors.dashboard.enterCustomizeMode);
  10. if ($('#dashboard .region .block').length == 0) {
  11. Drupal.settings.dashboard.launchCustomize = true;
  12. }
  13. Drupal.behaviors.dashboard.addPlaceholders();
  14. if (Drupal.settings.dashboard.launchCustomize) {
  15. Drupal.behaviors.dashboard.enterCustomizeMode();
  16. }
  17. },
  18. addPlaceholders: function() {
  19. $('#dashboard .dashboard-region .region').each(function () {
  20. var empty_text = "";
  21. // If the region is empty
  22. if ($('.block', this).length == 0) {
  23. // Check if we are in customize mode and grab the correct empty text
  24. if ($('#dashboard').hasClass('customize-mode')) {
  25. empty_text = Drupal.settings.dashboard.emptyRegionTextActive;
  26. } else {
  27. empty_text = Drupal.settings.dashboard.emptyRegionTextInactive;
  28. }
  29. // We need a placeholder.
  30. if ($('.placeholder', this).length == 0) {
  31. $(this).append('<div class="placeholder"></div>');
  32. }
  33. $('.placeholder', this).html(empty_text);
  34. }
  35. else {
  36. $('.placeholder', this).remove();
  37. }
  38. });
  39. },
  40. /**
  41. * Enter "customize" mode by displaying disabled blocks.
  42. */
  43. enterCustomizeMode: function () {
  44. $('#dashboard').addClass('customize-mode');
  45. Drupal.behaviors.dashboard.addPlaceholders();
  46. // Hide the customize link
  47. $('#dashboard .customize .action-links').hide();
  48. // Load up the disabled blocks
  49. $('div.customize .canvas').load(Drupal.settings.dashboard.drawer, Drupal.behaviors.dashboard.setupDrawer);
  50. },
  51. /**
  52. * Exit "customize" mode by simply forcing a page refresh.
  53. */
  54. exitCustomizeMode: function () {
  55. $('#dashboard').removeClass('customize-mode');
  56. Drupal.behaviors.dashboard.addPlaceholders();
  57. location.href = Drupal.settings.dashboard.dashboard;
  58. },
  59. /**
  60. * Helper for enterCustomizeMode; sets up drag-and-drop and close button.
  61. */
  62. setupDrawer: function () {
  63. $('div.customize .canvas-content input').click(Drupal.behaviors.dashboard.exitCustomizeMode);
  64. $('div.customize .canvas-content').append('<a class="button" href="">' + Drupal.t('Done') + '</a>');
  65. // Initialize drag-and-drop.
  66. var regions = $('#dashboard div.region');
  67. regions.sortable({
  68. connectWith: regions,
  69. cursor: 'move',
  70. cursorAt: {top:0},
  71. dropOnEmpty: true,
  72. items: '>div.block, div.disabled-block',
  73. opacity: 1,
  74. helper: 'block-dragging',
  75. placeholder: 'block-placeholder clearfix',
  76. start: Drupal.behaviors.dashboard.start,
  77. update: Drupal.behaviors.dashboard.update
  78. });
  79. },
  80. /**
  81. * While dragging, make the block appear as a disabled block
  82. *
  83. * This function is called on the jQuery UI Sortable "start" event.
  84. *
  85. * @param event
  86. * The event that triggered this callback.
  87. * @param ui
  88. * An object containing information about the item that is being dragged.
  89. */
  90. start: function (event, ui) {
  91. var item = $(ui.item);
  92. // If the block is already in disabled state, don't do anything.
  93. if (!item.hasClass('disabled-block')) {
  94. item.css({height: 'auto'});
  95. }
  96. },
  97. /**
  98. * Send block order to the server, and expand previously disabled blocks.
  99. *
  100. * This function is called on the jQuery UI Sortable "update" event.
  101. *
  102. * @param event
  103. * The event that triggered this callback.
  104. * @param ui
  105. * An object containing information about the item that was just dropped.
  106. */
  107. update: function (event, ui) {
  108. var item = $(ui.item);
  109. // If the user dragged a disabled block, load the block contents.
  110. if (item.hasClass('disabled-block')) {
  111. var module, delta, itemClass;
  112. itemClass = item.attr('class');
  113. // Determine the block module and delta.
  114. module = itemClass.match(/\bmodule-(\S+)\b/)[1];
  115. delta = itemClass.match(/\bdelta-(\S+)\b/)[1];
  116. // Load the newly enabled block's content.
  117. $.get(Drupal.settings.dashboard.blockContent + '/' + module + '/' + delta, {},
  118. function (block) {
  119. var blockContent = "";
  120. if (block) {
  121. blockContent = $("div.content", $(block));
  122. }
  123. if (!blockContent) {
  124. blockContent = $('<div class="content">' + Drupal.settings.dashboard.emptyBlockText + '</div>');
  125. }
  126. $("div.content", item).after(blockContent).remove();
  127. },
  128. 'html'
  129. );
  130. // Remove the "disabled-block" class, so we don't reload its content the
  131. // next time it's dragged.
  132. item.removeClass("disabled-block");
  133. }
  134. Drupal.behaviors.dashboard.addPlaceholders();
  135. // Let the server know what the new block order is.
  136. $.post(Drupal.settings.dashboard.updatePath, {
  137. 'form_token': Drupal.settings.dashboard.formToken,
  138. 'regions': Drupal.behaviors.dashboard.getOrder
  139. }
  140. );
  141. },
  142. /**
  143. * Return the current order of the blocks in each of the sortable regions,
  144. * in query string format.
  145. */
  146. getOrder: function () {
  147. var order = [];
  148. $('#dashboard div.region').each(function () {
  149. var region = $(this).parent().attr('id').replace(/-/g, '_');
  150. var blocks = $(this).sortable('toArray');
  151. $.each(blocks, function() {
  152. order.push(region + '[]=' + this);
  153. });
  154. });
  155. order = order.join('&');
  156. return order;
  157. }
  158. };
  159. })(jQuery);