ui.widgets.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. elgg.provide('elgg.ui.widgets');
  2. /**
  3. * Widgets initialization
  4. *
  5. * @return void
  6. * @requires jqueryui.sortable
  7. */
  8. elgg.ui.widgets.init = function() {
  9. // widget layout?
  10. if ($(".elgg-widgets").length === 0) {
  11. return;
  12. }
  13. $(".elgg-widgets").sortable({
  14. items: 'div.elgg-module-widget.elgg-state-draggable',
  15. connectWith: '.elgg-widgets',
  16. handle: '.elgg-widget-handle',
  17. forcePlaceholderSize: true,
  18. placeholder: 'elgg-widget-placeholder',
  19. opacity: 0.8,
  20. revert: 500,
  21. stop: elgg.ui.widgets.move
  22. });
  23. $('.elgg-widgets-add-panel li.elgg-state-available').click(elgg.ui.widgets.add);
  24. $('a.elgg-widget-delete-button').live('click', elgg.ui.widgets.remove);
  25. $('.elgg-widget-edit > form ').live('submit', elgg.ui.widgets.saveSettings);
  26. $('a.elgg-widget-collapse-button').live('click', elgg.ui.widgets.collapseToggle);
  27. elgg.ui.widgets.setMinHeight(".elgg-widgets");
  28. };
  29. /**
  30. * Adds a new widget
  31. *
  32. * Makes Ajax call to persist new widget and inserts the widget html
  33. *
  34. * @param {Object} event
  35. * @return void
  36. */
  37. elgg.ui.widgets.add = function(event) {
  38. var type = $(this).data('elgg-widget-type');
  39. // if multiple instances not allow, disable this widget type add button
  40. var multiple = $(this).attr('class').indexOf('elgg-widget-multiple') != -1;
  41. if (multiple === false) {
  42. $(this).addClass('elgg-state-unavailable');
  43. $(this).removeClass('elgg-state-available');
  44. $(this).unbind('click', elgg.ui.widgets.add);
  45. }
  46. elgg.action('widgets/add', {
  47. data: {
  48. handler: type,
  49. page_owner_guid: elgg.get_page_owner_guid(),
  50. context: $("input[name='widget_context']").val(),
  51. show_access: $("input[name='show_access']").val(),
  52. default_widgets: $("input[name='default_widgets']").val() || 0
  53. },
  54. success: function(json) {
  55. $('#elgg-widget-col-1').prepend(json.output);
  56. }
  57. });
  58. event.preventDefault();
  59. };
  60. /**
  61. * Persist the widget's new position
  62. *
  63. * @param {Object} event
  64. * @param {Object} ui
  65. *
  66. * @return void
  67. */
  68. elgg.ui.widgets.move = function(event, ui) {
  69. // elgg-widget-<guid>
  70. var guidString = ui.item.attr('id');
  71. guidString = guidString.substr(guidString.indexOf('elgg-widget-') + "elgg-widget-".length);
  72. // elgg-widget-col-<column>
  73. var col = ui.item.parent().attr('id');
  74. col = col.substr(col.indexOf('elgg-widget-col-') + "elgg-widget-col-".length);
  75. elgg.action('widgets/move', {
  76. data: {
  77. widget_guid: guidString,
  78. column: col,
  79. position: ui.item.index()
  80. }
  81. });
  82. // @hack fixes jquery-ui/opera bug where draggable elements jump
  83. ui.item.css('top', 0);
  84. ui.item.css('left', 0);
  85. };
  86. /**
  87. * Removes a widget from the layout
  88. *
  89. * Event callback the uses Ajax to delete the widget and removes its HTML
  90. *
  91. * @param {Object} event
  92. * @return void
  93. */
  94. elgg.ui.widgets.remove = function(event) {
  95. if (confirm(elgg.echo('deleteconfirm')) === false) {
  96. event.preventDefault();
  97. return;
  98. }
  99. var $widget = $(this).closest('.elgg-module-widget');
  100. // if widget type is single instance type, enable the add buton
  101. var type = $(this).data('elgg-widget-type');
  102. $container = $(this).parents('.elgg-layout-widgets').first();
  103. $button = $('[data-elgg-widget-type="' + type + '"]', $container);
  104. var multiple = $button.attr('class').indexOf('elgg-widget-multiple') != -1;
  105. if (multiple === false) {
  106. $button.addClass('elgg-state-available');
  107. $button.removeClass('elgg-state-unavailable');
  108. $button.unbind('click', elgg.ui.widgets.add); // make sure we don't bind twice
  109. $button.click(elgg.ui.widgets.add);
  110. }
  111. $widget.remove();
  112. // delete the widget through ajax
  113. elgg.action($(this).attr('href'));
  114. event.preventDefault();
  115. };
  116. /**
  117. * Toggle the collapse state of the widget
  118. *
  119. * @param {Object} event
  120. * @return void
  121. */
  122. elgg.ui.widgets.collapseToggle = function(event) {
  123. $(this).toggleClass('elgg-widget-collapsed');
  124. $(this).parent().parent().find('.elgg-body').slideToggle('medium');
  125. event.preventDefault();
  126. };
  127. /**
  128. * Save a widget's settings
  129. *
  130. * Uses Ajax to save the settings and updates the HTML.
  131. *
  132. * @param {Object} event
  133. * @return void
  134. */
  135. elgg.ui.widgets.saveSettings = function(event) {
  136. $(this).parent().slideToggle('medium');
  137. var $widgetContent = $(this).parent().parent().children('.elgg-widget-content');
  138. // stick the ajax loader in there
  139. var $loader = $('#elgg-widget-loader').clone();
  140. $loader.attr('id', '#elgg-widget-active-loader');
  141. $loader.removeClass('hidden');
  142. $widgetContent.html($loader);
  143. var default_widgets = $("input[name='default_widgets']").val() || 0;
  144. if (default_widgets) {
  145. $(this).append('<input type="hidden" name="default_widgets" value="1">');
  146. }
  147. elgg.action('widgets/save', {
  148. data: $(this).serialize(),
  149. success: function(json) {
  150. $widgetContent.html(json.output);
  151. if (typeof(json.title) != "undefined") {
  152. var $widgetTitle = $widgetContent.parent().parent().find('.elgg-widget-title');
  153. $widgetTitle.html(json.title);
  154. }
  155. }
  156. });
  157. event.preventDefault();
  158. };
  159. /**
  160. * Set the min-height so that all widget column bottoms are the same
  161. *
  162. * This addresses the issue of trying to drag a widget into a column that does
  163. * not have any widgets or many fewer widgets than other columns.
  164. *
  165. * @param {String} selector
  166. * @return void
  167. */
  168. elgg.ui.widgets.setMinHeight = function(selector) {
  169. var maxBottom = 0;
  170. $(selector).each(function() {
  171. var bottom = parseInt($(this).offset().top + $(this).height());
  172. if (bottom > maxBottom) {
  173. maxBottom = bottom;
  174. }
  175. });
  176. $(selector).each(function() {
  177. var bottom = parseInt($(this).offset().top + $(this).height());
  178. if (bottom < maxBottom) {
  179. var newMinHeight = parseInt($(this).height() + (maxBottom - bottom));
  180. $(this).css('min-height', newMinHeight + 'px');
  181. }
  182. });
  183. };
  184. elgg.register_hook_handler('init', 'system', elgg.ui.widgets.init);