ajax.js 8.9 KB

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