dashboard.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * @file
  3. * Attaches behaviors for the Dashboard module.
  4. */
  5. (function ($) {
  6. /**
  7. * Implements Drupal.behaviors for the Dashboard module.
  8. */
  9. Drupal.behaviors.dashboard = {
  10. attach: function (context, settings) {
  11. $('#dashboard', context).once(function () {
  12. $(this).prepend('<div class="customize clearfix"><ul class="action-links"><li><a href="#">' + Drupal.t('Customize dashboard') + '</a></li></ul><div class="canvas"></div></div>');
  13. $('.customize .action-links a', this).click(Drupal.behaviors.dashboard.enterCustomizeMode);
  14. });
  15. Drupal.behaviors.dashboard.addPlaceholders();
  16. if (Drupal.settings.dashboard.launchCustomize) {
  17. Drupal.behaviors.dashboard.enterCustomizeMode();
  18. }
  19. },
  20. addPlaceholders: function() {
  21. $('#dashboard .dashboard-region .region').each(function () {
  22. var empty_text = "";
  23. // If the region is empty
  24. if ($('.block', this).length == 0) {
  25. // Check if we are in customize mode and grab the correct empty text
  26. if ($('#dashboard').hasClass('customize-mode')) {
  27. empty_text = Drupal.settings.dashboard.emptyRegionTextActive;
  28. } else {
  29. empty_text = Drupal.settings.dashboard.emptyRegionTextInactive;
  30. }
  31. // We need a placeholder.
  32. if ($('.dashboard-placeholder', this).length == 0) {
  33. $(this).append('<div class="dashboard-placeholder"></div>');
  34. }
  35. $('.dashboard-placeholder', this).html(empty_text);
  36. }
  37. else {
  38. $('.dashboard-placeholder', this).remove();
  39. }
  40. });
  41. },
  42. /**
  43. * Enters "customize" mode by displaying disabled blocks.
  44. */
  45. enterCustomizeMode: function () {
  46. $('#dashboard').addClass('customize-mode customize-inactive');
  47. Drupal.behaviors.dashboard.addPlaceholders();
  48. // Hide the customize link
  49. $('#dashboard .customize .action-links').hide();
  50. // Load up the disabled blocks
  51. $('div.customize .canvas').load(Drupal.settings.dashboard.drawer, Drupal.behaviors.dashboard.setupDrawer);
  52. },
  53. /**
  54. * Exits "customize" mode by simply forcing a page refresh.
  55. */
  56. exitCustomizeMode: function () {
  57. $('#dashboard').removeClass('customize-mode customize-inactive');
  58. Drupal.behaviors.dashboard.addPlaceholders();
  59. location.href = Drupal.settings.dashboard.dashboard;
  60. },
  61. /**
  62. * Sets up the drag-and-drop behavior and the 'close' button.
  63. */
  64. setupDrawer: function () {
  65. $('div.customize .canvas-content input').click(Drupal.behaviors.dashboard.exitCustomizeMode);
  66. $('div.customize .canvas-content').append('<a class="button" href="' + Drupal.settings.dashboard.dashboard + '">' + Drupal.t('Done') + '</a>');
  67. // Initialize drag-and-drop.
  68. var regions = $('#dashboard div.region');
  69. regions.sortable({
  70. connectWith: regions,
  71. cursor: 'move',
  72. cursorAt: {top:0},
  73. dropOnEmpty: true,
  74. items: '> div.block, > div.disabled-block',
  75. placeholder: 'block-placeholder clearfix',
  76. tolerance: 'pointer',
  77. start: Drupal.behaviors.dashboard.start,
  78. over: Drupal.behaviors.dashboard.over,
  79. sort: Drupal.behaviors.dashboard.sort,
  80. update: Drupal.behaviors.dashboard.update
  81. });
  82. },
  83. /**
  84. * Makes the block appear as a disabled block while dragging.
  85. *
  86. * This function is called on the jQuery UI Sortable "start" event.
  87. *
  88. * @param event
  89. * The event that triggered this callback.
  90. * @param ui
  91. * An object containing information about the item that is being dragged.
  92. */
  93. start: function (event, ui) {
  94. $('#dashboard').removeClass('customize-inactive');
  95. var item = $(ui.item);
  96. // If the block is already in disabled state, don't do anything.
  97. if (!item.hasClass('disabled-block')) {
  98. item.css({height: 'auto'});
  99. }
  100. },
  101. /**
  102. * Adapts block's width to the region it is moved into while dragging.
  103. *
  104. * This function is called on the jQuery UI Sortable "over" event.
  105. *
  106. * @param event
  107. * The event that triggered this callback.
  108. * @param ui
  109. * An object containing information about the item that is being dragged.
  110. */
  111. over: function (event, ui) {
  112. var item = $(ui.item);
  113. // If the block is in disabled state, remove width.
  114. if ($(this).closest('#disabled-blocks').length) {
  115. item.css('width', '');
  116. }
  117. else {
  118. item.css('width', $(this).width());
  119. }
  120. },
  121. /**
  122. * Adapts a block's position to stay connected with the mouse pointer.
  123. *
  124. * This function is called on the jQuery UI Sortable "sort" event.
  125. *
  126. * @param event
  127. * The event that triggered this callback.
  128. * @param ui
  129. * An object containing information about the item that is being dragged.
  130. */
  131. sort: function (event, ui) {
  132. var item = $(ui.item);
  133. if (event.pageX > ui.offset.left + item.width()) {
  134. item.css('left', event.pageX);
  135. }
  136. },
  137. /**
  138. * Sends block order to the server, and expand previously disabled blocks.
  139. *
  140. * This function is called on the jQuery UI Sortable "update" event.
  141. *
  142. * @param event
  143. * The event that triggered this callback.
  144. * @param ui
  145. * An object containing information about the item that was just dropped.
  146. */
  147. update: function (event, ui) {
  148. $('#dashboard').addClass('customize-inactive');
  149. var item = $(ui.item);
  150. // If the user dragged a disabled block, load the block contents.
  151. if (item.hasClass('disabled-block')) {
  152. var module, delta, itemClass;
  153. itemClass = item.attr('class');
  154. // Determine the block module and delta.
  155. module = itemClass.match(/\bmodule-(\S+)\b/)[1];
  156. delta = itemClass.match(/\bdelta-(\S+)\b/)[1];
  157. // Load the newly enabled block's content.
  158. $.get(Drupal.settings.dashboard.blockContent + '/' + module + '/' + delta, {},
  159. function (block) {
  160. if (block) {
  161. item.html(block);
  162. }
  163. if (item.find('div.content').is(':empty')) {
  164. item.find('div.content').html(Drupal.settings.dashboard.emptyBlockText);
  165. }
  166. Drupal.attachBehaviors(item);
  167. },
  168. 'html'
  169. );
  170. // Remove the "disabled-block" class, so we don't reload its content the
  171. // next time it's dragged.
  172. item.removeClass("disabled-block");
  173. }
  174. Drupal.behaviors.dashboard.addPlaceholders();
  175. // Let the server know what the new block order is.
  176. $.post(Drupal.settings.dashboard.updatePath, {
  177. 'form_token': Drupal.settings.dashboard.formToken,
  178. 'regions': Drupal.behaviors.dashboard.getOrder
  179. }
  180. );
  181. },
  182. /**
  183. * Returns the current order of the blocks in each of the sortable regions.
  184. *
  185. * @return
  186. * The current order of the blocks, in query string format.
  187. */
  188. getOrder: function () {
  189. var order = [];
  190. $('#dashboard div.region').each(function () {
  191. var region = $(this).parent().attr('id').replace(/-/g, '_');
  192. var blocks = $(this).sortable('toArray');
  193. $.each(blocks, function() {
  194. order.push(region + '[]=' + this);
  195. });
  196. });
  197. order = order.join('&');
  198. return order;
  199. }
  200. };
  201. })(jQuery);