boxes.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. (function ($) {
  2. //create ajax commands to be used from the drupal ajax api
  3. Drupal.ajax.prototype.commands['getBlock'] = function(ajax, response, status) {
  4. var box = $(this).parents('.boxes-box');
  5. data = response;
  6. $.ajax({
  7. type: "GET",
  8. url: data.url,
  9. data: { 'boxes_delta': data.delta },
  10. global: true,
  11. success: function(response, status) {
  12. if($('#boxes-box-form').dialog('isOpen')){
  13. $('#boxes-box-form').dialog('close');
  14. }
  15. $('.hasPopup').removeClass('hasPopup');
  16. box.removeClass('boxes-box-editing').find('.box-editor').remove().end().find('.boxes-box-content').show();
  17. ajax.success(response, status);
  18. },
  19. error: Drupal.ajax.error,
  20. dataType: 'json'
  21. });
  22. };
  23. Drupal.ajax.prototype.commands['showBoxForm'] = function(ajax, response, status) {
  24. if(!$('#boxes-box-form').size() || !$('#boxes-box-form').dialog('isOpen')){
  25. Drupal.ajax.prototype.commands.insert(ajax, response, status);
  26. $(response.selector).addClass('hasPopup');
  27. $('#boxes-box-form')
  28. .dialog({
  29. modal : true,
  30. close: function(e){
  31. //handle someone closing the box without clicking any buttons
  32. if (Drupal.wysiwygDetach && $('.wysiwyg', this).val()) {
  33. var item = $('.wysiwyg', this)[0];
  34. var params = Drupal.settings.wysiwyg.triggers[item.id];
  35. Drupal.wysiwygDetach(this, params['format'+$(item).val()])
  36. }
  37. $(response.selector).removeClass('hasPopup').html(Drupal.t('Loading...'));
  38. $('.boxes-ajax.use-ajax-submit.form-submit[value="Cancel"]').click();
  39. $(this).dialog('destroy').remove();
  40. },
  41. open: function(ui, event){
  42. $(this).siblings('.ui-dialog-titlebar').children('.ui-dialog-titlebar-close').click(function(e){
  43. $('.boxes-ajax.use-ajax-submit.form-submit[value="Cancel"]').click();
  44. });
  45. },
  46. width: Math.min($(window).width() * .75, 750),
  47. height: Math.min($(window).height() * .75, 750),
  48. title : Drupal.t('Edit Box')
  49. });
  50. } else {
  51. //change the selector to just update the current form - in place (in the popup)
  52. response.selector = '#boxes-box-form';
  53. Drupal.ajax.prototype.commands.insert(ajax, response, status);
  54. }
  55. };
  56. Drupal.behaviors.boxes = {
  57. attach: function(context, settings) {
  58. $('div.boxes-box-controls a:not(.boxes-processed)')
  59. .addClass('boxes-processed')
  60. .click(function() {
  61. // If we are not using edit-in-place, bail.
  62. if (this.href.indexOf('/admin/structure/block/manage/boxes/') != -1) {
  63. return;
  64. }
  65. var box = $(this).parents('.boxes-box');
  66. if (box.is('.boxes-box-editing')) {
  67. box.removeClass('boxes-box-editing').find('.box-editor').remove().end().find('.boxes-box-content').show();
  68. }
  69. else {
  70. // Show editing form - the form itself gets loaded via ajax..
  71. box.find('.boxes-box-content').hide().end().addClass('boxes-box-editing').prepend('<div class="box-editor"><div class="swirly"></div></div>');
  72. }
  73. return false;
  74. });
  75. Drupal.ajax.prototype.commands['preReplaceContextBlock'] = function(ajax, response, status) {
  76. data = response
  77. Drupal.settings.boxes = Drupal.settings.boxes || {};
  78. var e = $('#' + data.id + ' a.context-block:first').clone();
  79. Drupal.settings.boxes[data.id] = e;
  80. };
  81. Drupal.ajax.prototype.commands['postReplaceContextBlock'] = function(ajax, response, status) {
  82. data = response
  83. $('#' + data.id).append(Drupal.settings.boxes[data.id]);
  84. $('form.context-editor.context-editing').each(function() {
  85. var id = $(this).attr('id');
  86. if (Drupal.contextBlockEditor[id]) {
  87. Drupal.contextBlockEditor[id].initBlocks($('#' + data.id));
  88. }
  89. });
  90. };
  91. //If we have a contextual link to configure the block lets get rid of that and move our edit link
  92. //to the contextual dropdown
  93. $('.boxes-box-controls', context).each(function () {
  94. // See if we are within a panel.
  95. if ($(this).parent().parent().hasClass("pane-content")) {
  96. $(this).hide();
  97. }
  98. if($(this).parents(".block").find(".block-configure").length > 0) {
  99. $(this).parents(".block").find(".block-configure").after($(this).find("li.edit"));
  100. $(this).parents(".block").find(".block-configure").detach();
  101. }
  102. });
  103. // Submit box form if Enter is pressed
  104. $('#boxes-box-form input').keydown(function (e) {
  105. if (!e) {
  106. e = window.event;
  107. }
  108. // Enter
  109. if (e.keyCode == 13) {
  110. e.preventDefault();
  111. // Save is always the first button (see boxes.module)
  112. $('.boxes-ajax.use-ajax-submit.form-submit:first').click();
  113. }
  114. });
  115. //apply the popup form to 'add boxes' also
  116. $('.boxes-box-editing .box-editor #boxes-box-form').not('.processed').addClass('processed').dialog({
  117. modal : true,
  118. close: function(e){
  119. //handle someone closing the box without clicking any buttons
  120. $(this).remove();
  121. },
  122. open: function(event, ui) {
  123. //hide the close button on add on the popup to prevent various annoying errors
  124. $(this).siblings('.ui-dialog-titlebar').children('.ui-dialog-titlebar-close').hide();
  125. },
  126. width: Math.min($(window).width() * .75, 750),
  127. height: Math.min($(window).height() * .75, 750),
  128. title : Drupal.t('Configure Box')
  129. });
  130. }
  131. };
  132. })(jQuery);