ajax.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /**
  2. * @file
  3. * Handles AJAX submission and response in Views UI.
  4. */
  5. (function ($) {
  6. Drupal.ajax.prototype.commands.viewsSetForm = function (ajax, response, status) {
  7. var ajax_title = Drupal.settings.views.ajax.title;
  8. var ajax_body = Drupal.settings.views.ajax.id;
  9. var ajax_popup = Drupal.settings.views.ajax.popup;
  10. $(ajax_title).html(response.title);
  11. $(ajax_body).html(response.output);
  12. $(ajax_popup).dialog('open');
  13. Drupal.attachBehaviors($(ajax_popup), ajax.settings);
  14. if (response.url) {
  15. // Identify the button that was clicked so that .ajaxSubmit() can use it.
  16. // We need to do this for both .click() and .mousedown() since JavaScript
  17. // code might trigger either behavior.
  18. var $submit_buttons = $('input[type=submit], button', ajax_body);
  19. $submit_buttons.click(function(event) {
  20. this.form.clk = this;
  21. });
  22. $submit_buttons.mousedown(function(event) {
  23. this.form.clk = this;
  24. });
  25. $('form', ajax_body).once('views-ajax-submit-processed').each(function() {
  26. var element_settings = { 'url': response.url, 'event': 'submit', 'progress': { 'type': 'throbber' } };
  27. var $form = $(this);
  28. var id = $form.attr('id');
  29. Drupal.ajax[id] = new Drupal.ajax(id, this, element_settings);
  30. Drupal.ajax[id].form = $form;
  31. });
  32. }
  33. Drupal.viewsUi.resizeModal();
  34. };
  35. Drupal.ajax.prototype.commands.viewsDismissForm = function (ajax, response, status) {
  36. Drupal.ajax.prototype.commands.viewsSetForm({}, {'title': '', 'output': Drupal.settings.views.ajax.defaultForm});
  37. $(Drupal.settings.views.ajax.popup).dialog('close');
  38. }
  39. Drupal.ajax.prototype.commands.viewsHilite = function (ajax, response, status) {
  40. $('.hilited').removeClass('hilited');
  41. $(response.selector).addClass('hilited');
  42. };
  43. Drupal.ajax.prototype.commands.viewsAddTab = function (ajax, response, status) {
  44. var id = '#views-tab-' + response.id;
  45. $('#views-tabset').viewsAddTab(id, response.title, 0);
  46. $(id).html(response.body).addClass('views-tab');
  47. // Update the preview widget to preview the new tab.
  48. var display_id = id.replace('#views-tab-', '');
  49. $("#preview-display-id").append('<option selected="selected" value="' + display_id + '">' + response.title + '</option>');
  50. Drupal.attachBehaviors(id);
  51. var instance = $.viewsUi.tabs.instances[$('#views-tabset').get(0).UI_TABS_UUID];
  52. $('#views-tabset').viewsClickTab(instance.$tabs.length);
  53. };
  54. Drupal.ajax.prototype.commands.viewsShowButtons = function (ajax, response, status) {
  55. $('div.views-edit-view div.form-actions').removeClass('js-hide');
  56. $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
  57. };
  58. Drupal.ajax.prototype.commands.viewsTriggerPreview = function (ajax, response, status) {
  59. if ($('input#edit-displays-live-preview').is(':checked')) {
  60. $('#preview-submit').trigger('click');
  61. }
  62. };
  63. Drupal.ajax.prototype.commands.viewsReplaceTitle = function (ajax, response, status) {
  64. // In case we're in the overlay, get a reference to the underlying window.
  65. var doc = parent.document;
  66. // For the <title> element, make a best-effort attempt to replace the page
  67. // title and leave the site name alone. If the theme doesn't use the site
  68. // name in the <title> element, this will fail.
  69. var oldTitle = doc.title;
  70. // Escape the site name, in case it has special characters in it, so we can
  71. // use it in our regex.
  72. var escapedSiteName = response.siteName.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  73. var re = new RegExp('.+ (.) ' + escapedSiteName);
  74. doc.title = oldTitle.replace(re, response.title + ' $1 ' + response.siteName);
  75. $('h1.page-title').text(response.title);
  76. $('h1#overlay-title').text(response.title);
  77. };
  78. /**
  79. * Get rid of irritating tabledrag messages
  80. */
  81. Drupal.theme.tableDragChangedWarning = function () {
  82. return [];
  83. }
  84. /**
  85. * Trigger preview when the "live preview" checkbox is checked.
  86. */
  87. Drupal.behaviors.livePreview = {
  88. attach: function (context) {
  89. $('input#edit-displays-live-preview', context).once('views-ajax-processed').click(function() {
  90. if ($(this).is(':checked')) {
  91. $('#preview-submit').click();
  92. }
  93. });
  94. }
  95. }
  96. /**
  97. * Sync preview display.
  98. */
  99. Drupal.behaviors.syncPreviewDisplay = {
  100. attach: function (context) {
  101. $("#views-tabset a").once('views-ajax-processed').click(function() {
  102. var href = $(this).attr('href');
  103. // Cut of #views-tabset.
  104. var display_id = href.substr(11);
  105. // Set the form element.
  106. $("#views-live-preview #preview-display-id").val(display_id);
  107. }).addClass('views-ajax-processed');
  108. }
  109. }
  110. Drupal.behaviors.viewsAjax = {
  111. collapseReplaced: false,
  112. attach: function (context, settings) {
  113. if (!settings.views) {
  114. return;
  115. }
  116. // Create a jQuery UI dialog, but leave it closed.
  117. var dialog_area = $(settings.views.ajax.popup, context);
  118. dialog_area.dialog({
  119. 'autoOpen': false,
  120. 'dialogClass': 'views-ui-dialog',
  121. 'modal': true,
  122. 'position': 'center',
  123. 'resizable': false,
  124. 'width': 750
  125. });
  126. var base_element_settings = {
  127. 'event': 'click',
  128. 'progress': { 'type': 'throbber' }
  129. };
  130. // Bind AJAX behaviors to all items showing the class.
  131. $('a.views-ajax-link', context).once('views-ajax-processed').each(function () {
  132. var element_settings = base_element_settings;
  133. // Set the URL to go to the anchor.
  134. if ($(this).attr('href')) {
  135. element_settings.url = $(this).attr('href');
  136. }
  137. var base = $(this).attr('id');
  138. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  139. });
  140. $('div#views-live-preview a')
  141. .once('views-ajax-processed').each(function () {
  142. // We don't bind to links without a URL.
  143. if (!$(this).attr('href')) {
  144. return true;
  145. }
  146. var element_settings = base_element_settings;
  147. // Set the URL to go to the anchor.
  148. element_settings.url = $(this).attr('href');
  149. if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
  150. return true;
  151. }
  152. element_settings.wrapper = 'views-live-preview';
  153. element_settings.method = 'html';
  154. var base = $(this).attr('id');
  155. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  156. });
  157. // Within a live preview, make exposed widget form buttons re-trigger the
  158. // Preview button.
  159. // @todo Revisit this after fixing Views UI to display a Preview outside
  160. // of the main Edit form.
  161. $('div#views-live-preview input[type=submit]')
  162. .once('views-ajax-processed').each(function(event) {
  163. $(this).click(function () {
  164. this.form.clk = this;
  165. return true;
  166. });
  167. var element_settings = base_element_settings;
  168. // Set the URL to go to the anchor.
  169. element_settings.url = $(this.form).attr('action');
  170. if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
  171. return true;
  172. }
  173. element_settings.wrapper = 'views-live-preview';
  174. element_settings.method = 'html';
  175. element_settings.event = 'click';
  176. var base = $(this).attr('id');
  177. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  178. });
  179. if (!this.collapseReplaced && Drupal.collapseScrollIntoView) {
  180. this.collapseReplaced = true;
  181. Drupal.collapseScrollIntoView = function (node) {
  182. for (var $parent = $(node); $parent.get(0) != document && $parent.size() != 0; $parent = $parent.parent()) {
  183. if ($parent.css('overflow') == 'scroll' || $parent.css('overflow') == 'auto') {
  184. if (Drupal.viewsUi.resizeModal) {
  185. // If the modal is already at the max height, don't bother with
  186. // this since the only reason to do it is to grow the modal.
  187. if ($('.views-ui-dialog').height() < parseInt($(window).height() * .8)) {
  188. Drupal.viewsUi.resizeModal('', true);
  189. }
  190. }
  191. return;
  192. }
  193. }
  194. var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
  195. var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
  196. var posY = $(node).offset().top;
  197. var fudge = 55;
  198. if (posY + node.offsetHeight + fudge > h + offset) {
  199. if (node.offsetHeight > h) {
  200. window.scrollTo(0, posY);
  201. }
  202. else {
  203. window.scrollTo(0, posY + node.offsetHeight - h + fudge);
  204. }
  205. }
  206. };
  207. }
  208. }
  209. };
  210. })(jQuery);