ajax.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. if (response.changed) {
  57. $('div.views-edit-view div.view-changed.messages').removeClass('js-hide');
  58. }
  59. };
  60. Drupal.ajax.prototype.commands.viewsTriggerPreview = function (ajax, response, status) {
  61. if ($('input#edit-displays-live-preview').is(':checked')) {
  62. $('#preview-submit').trigger('click');
  63. }
  64. };
  65. Drupal.ajax.prototype.commands.viewsReplaceTitle = function (ajax, response, status) {
  66. // In case we're in the overlay, get a reference to the underlying window.
  67. var doc = parent.document;
  68. // For the <title> element, make a best-effort attempt to replace the page
  69. // title and leave the site name alone. If the theme doesn't use the site
  70. // name in the <title> element, this will fail.
  71. var oldTitle = doc.title;
  72. // Escape the site name, in case it has special characters in it, so we can
  73. // use it in our regex.
  74. var escapedSiteName = response.siteName.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
  75. var re = new RegExp('.+ (.) ' + escapedSiteName);
  76. doc.title = oldTitle.replace(re, response.title + ' $1 ' + response.siteName);
  77. $('h1.page-title').text(response.title);
  78. $('h1#overlay-title').text(response.title);
  79. };
  80. /**
  81. * Get rid of irritating tabledrag messages
  82. */
  83. Drupal.theme.tableDragChangedWarning = function () {
  84. return [];
  85. }
  86. /**
  87. * Trigger preview when the "live preview" checkbox is checked.
  88. */
  89. Drupal.behaviors.livePreview = {
  90. attach: function (context) {
  91. $('input#edit-displays-live-preview', context).once('views-ajax-processed').click(function() {
  92. if ($(this).is(':checked')) {
  93. $('#preview-submit').click();
  94. }
  95. });
  96. }
  97. }
  98. /**
  99. * Sync preview display.
  100. */
  101. Drupal.behaviors.syncPreviewDisplay = {
  102. attach: function (context) {
  103. $("#views-tabset a").once('views-ajax-processed').click(function() {
  104. var href = $(this).attr('href');
  105. // Cut of #views-tabset.
  106. var display_id = href.substr(11);
  107. // Set the form element.
  108. $("#views-live-preview #preview-display-id").val(display_id);
  109. }).addClass('views-ajax-processed');
  110. }
  111. }
  112. Drupal.behaviors.viewsAjax = {
  113. collapseReplaced: false,
  114. attach: function (context, settings) {
  115. if (!settings.views) {
  116. return;
  117. }
  118. // Create a jQuery UI dialog, but leave it closed.
  119. var dialog_area = $(settings.views.ajax.popup, context);
  120. dialog_area.dialog({
  121. 'autoOpen': false,
  122. 'dialogClass': 'views-ui-dialog',
  123. 'modal': true,
  124. 'position': 'center',
  125. 'resizable': false,
  126. 'width': 750
  127. });
  128. var base_element_settings = {
  129. 'event': 'click',
  130. 'progress': { 'type': 'throbber' }
  131. };
  132. // Bind AJAX behaviors to all items showing the class.
  133. $('a.views-ajax-link', context).once('views-ajax-processed').each(function () {
  134. var element_settings = base_element_settings;
  135. // Set the URL to go to the anchor.
  136. if ($(this).attr('href')) {
  137. element_settings.url = $(this).attr('href');
  138. }
  139. var base = $(this).attr('id');
  140. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  141. });
  142. $('div#views-live-preview a')
  143. .once('views-ajax-processed').each(function () {
  144. // We don't bind to links without a URL.
  145. if (!$(this).attr('href')) {
  146. return true;
  147. }
  148. var element_settings = base_element_settings;
  149. // Set the URL to go to the anchor.
  150. element_settings.url = $(this).attr('href');
  151. if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
  152. return true;
  153. }
  154. element_settings.wrapper = 'views-live-preview';
  155. element_settings.method = 'html';
  156. var base = $(this).attr('id');
  157. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  158. });
  159. // Within a live preview, make exposed widget form buttons re-trigger the
  160. // Preview button.
  161. // @todo Revisit this after fixing Views UI to display a Preview outside
  162. // of the main Edit form.
  163. $('div#views-live-preview input[type=submit]')
  164. .once('views-ajax-processed').each(function(event) {
  165. $(this).click(function () {
  166. this.form.clk = this;
  167. return true;
  168. });
  169. var element_settings = base_element_settings;
  170. // Set the URL to go to the anchor.
  171. element_settings.url = $(this.form).attr('action');
  172. if (Drupal.Views.getPath(element_settings.url).substring(0, 21) != 'admin/structure/views') {
  173. return true;
  174. }
  175. element_settings.wrapper = 'views-live-preview';
  176. element_settings.method = 'html';
  177. element_settings.event = 'click';
  178. var base = $(this).attr('id');
  179. Drupal.ajax[base] = new Drupal.ajax(base, this, element_settings);
  180. });
  181. if (!this.collapseReplaced && Drupal.collapseScrollIntoView) {
  182. this.collapseReplaced = true;
  183. Drupal.collapseScrollIntoView = function (node) {
  184. for (var $parent = $(node); $parent.get(0) != document && $parent.size() != 0; $parent = $parent.parent()) {
  185. if ($parent.css('overflow') == 'scroll' || $parent.css('overflow') == 'auto') {
  186. if (Drupal.viewsUi.resizeModal) {
  187. // If the modal is already at the max height, don't bother with
  188. // this since the only reason to do it is to grow the modal.
  189. if ($('.views-ui-dialog').height() < parseInt($(window).height() * .8)) {
  190. Drupal.viewsUi.resizeModal('', true);
  191. }
  192. }
  193. return;
  194. }
  195. }
  196. var h = document.documentElement.clientHeight || document.body.clientHeight || 0;
  197. var offset = document.documentElement.scrollTop || document.body.scrollTop || 0;
  198. var posY = $(node).offset().top;
  199. var fudge = 55;
  200. if (posY + node.offsetHeight + fudge > h + offset) {
  201. if (node.offsetHeight > h) {
  202. window.scrollTo(0, posY);
  203. }
  204. else {
  205. window.scrollTo(0, posY + node.offsetHeight - h + fudge);
  206. }
  207. }
  208. };
  209. }
  210. }
  211. };
  212. })(jQuery);