1 // $Id: ajax.js,v 1.25 2009/03/24 23:03:32 merlinofchaos Exp $
5 * Handles AJAX submission and response in Views UI.
8 Drupal.Views.Ajax = Drupal.Views.Ajax || {};
11 * Handles the simple process of setting the ajax form area with new data.
13 Drupal.Views.Ajax.setForm = function(title, output) {
14 $(Drupal.settings.views.ajax.title).html(title);
15 $(Drupal.settings.views.ajax.id).html(output);
19 * An ajax responder that accepts a packet of JSON data and acts appropriately.
21 * The following fields control behavior.
22 * - 'display': Display the associated data in the form area; bind the new
23 * form to 'url' if appropriate. The 'title' field may also be used.
24 * - 'add': This is a keyed array of HTML output to add via append. The key is
25 * the id to append via $(key).append(value)
26 * - 'replace': This is a keyed array of HTML output to add via replace. The key is
27 * the id to append via $(key).html(value)
30 Drupal.Views.Ajax.ajaxResponse = function(data) {
31 $('a.views-throbbing').removeClass('views-throbbing');
32 $('span.views-throbbing').remove();
38 // See if we have any settings to extend. Do this first so that behaviors
39 // can access the new settings easily.
41 if (Drupal.settings.viewsAjax) {
42 Drupal.settings.viewsAjax = {};
45 $.extend(Drupal.settings, data.js);
48 // Check the 'display' for data.
50 Drupal.Views.Ajax.setForm(data.title, data.display);
52 // if a URL was supplied, bind the form to it.
54 var ajax_area = Drupal.settings.views.ajax.id;
55 var ajax_title = Drupal.settings.views.ajax.title;
57 // Bind a click to the button to set the value for the button.
58 $('input[type=submit]', ajax_area).unbind('click');
59 $('input[type=submit]', ajax_area).click(function() {
60 $('form', ajax_area).append('<input type="hidden" name="'
61 + $(this).attr('name') + '" value="' + $(this).val() + '">');
62 $(this).after('<span class="views-throbbing"> </span>');
65 // Bind forms to ajax submit.
66 $('form', ajax_area).unbind('submit'); // be safe here.
67 $('form', ajax_area).submit(function(arg) {
72 success: Drupal.Views.Ajax.ajaxResponse,
73 error: function() { $('span.views-throbbing').remove(); alert(Drupal.t("An error occurred at @path.", {'@path': data.url})); },
80 Drupal.attachBehaviors(ajax_area);
83 // If no display, reset the form.
84 Drupal.Views.Ajax.setForm('', Drupal.settings.views.ajax.defaultForm);
85 //Enable the save button.
86 $('#edit-save').removeAttr('disabled');
87 // Trigger an update for the live preview when we reach this state:
88 $('#views-ui-preview-form').trigger('submit');
91 // Go through the 'add' array and add any new content we're instructed to add.
93 for (id in data.add) {
94 var newContent = $(id).append(data.add[id]);
95 Drupal.attachBehaviors(newContent);
99 // Go through the 'replace' array and replace any content we're instructed to.
101 for (id in data.replace) {
102 $(id).html(data.replace[id]);
103 Drupal.attachBehaviors(id);
107 // Go through and add any requested tabs
109 for (id in data.tab) {
110 $('#views-tabset').addTab(id, data.tab[id]['title'], 0);
111 $(id).html(data.tab[id]['body']);
112 $(id).addClass('views-tab');
113 Drupal.attachBehaviors(id);
115 // This is kind of annoying, but we have to actually to find where the new
117 var instance = $.ui.tabs.instances[$('#views-tabset').get(0).UI_TABS_UUID];
118 $('#views-tabset').clickTab(instance.$tabs.length);
123 $('.hilited').removeClass('hilited');
124 $(data.hilite).addClass('hilited');
128 $('div.views-basic-info').addClass('changed');
133 * An ajax responder that accepts a packet of JSON data and acts appropriately.
134 * This one specifically responds to the Views live preview area, so it's
135 * hardcoded and specialized.
137 Drupal.Views.Ajax.previewResponse = function(data) {
138 $('a.views-throbbing').removeClass('views-throbbing');
139 $('span.views-throbbing').remove();
145 // See if we have any settings to extend. Do this first so that behaviors
146 // can access the new settings easily.
148 // Clear any previous viewsAjax settings.
149 if (Drupal.settings.viewsAjax) {
150 Drupal.settings.viewsAjax = {};
153 $.extend(Drupal.settings, data.js);
156 // Check the 'display' for data.
158 var ajax_area = 'div#views-live-preview';
159 $(ajax_area).html(data.display);
161 var url = $(ajax_area, 'form').attr('action');
163 // if a URL was supplied, bind the form to it.
165 // Bind a click to the button to set the value for the button.
166 $('input[type=submit]', ajax_area).unbind('click');
167 $('input[type=submit]', ajax_area).click(function() {
168 $('form', ajax_area).append('<input type="hidden" name="'
169 + $(this).attr('name') + '" value="' + $(this).val() + '">');
170 $(this).after('<span class="views-throbbing"> </span>');
173 // Bind forms to ajax submit.
174 $('form', ajax_area).unbind('submit'); // be safe here.
175 $('form', ajax_area).submit(function() {
180 success: Drupal.Views.Ajax.previewResponse,
181 error: function() { $('span.views-throbbing').remove(); alert(Drupal.t("An error occurred at @path.", {'@path': url})); },
188 Drupal.attachBehaviors(ajax_area);
192 Drupal.Views.updatePreviewForm = function() {
193 var url = $(this).attr('action');
194 url = url.replace('nojs', 'ajax');
196 $('input[type=submit]', this).after('<span class="views-throbbing"> </span>');
201 success: Drupal.Views.Ajax.previewResponse,
202 error: function() { $('span.views-throbbing').remove(); alert(Drupal.t("An error occurred at @path.", {'@path': url})); },
209 Drupal.Views.updatePreviewFilterForm = function() {
210 var url = $(this).attr('action');
211 url = url.replace('nojs', 'ajax');
213 $('input[type=submit]', this).after('<span class="views-throbbing"> </span>');
214 $('input[name=q]', this).remove(); // remove 'q' for live preview.
219 success: Drupal.Views.Ajax.previewResponse,
220 error: function() { $('span.views-throbbing').remove(); alert(Drupal.t("An error occurred at @path.", {'@path': url})); },
227 Drupal.Views.updatePreviewLink = function() {
228 var url = $(this).attr('href');
229 url = url.replace('nojs', 'ajax');
230 if (url.substring(0, 18) != '/admin/build/views') {
234 $(this).addClass('views-throbbing');
239 success: Drupal.Views.Ajax.previewResponse,
240 error: function() { $(this).removeClass('views-throbbing'); alert(Drupal.t("An error occurred at @path.", {'@path': url})); },
247 Drupal.behaviors.ViewsAjaxLinks = function() {
248 // Make specified links ajaxy.
249 $('a.views-ajax-link:not(.views-processed)').addClass('views-processed').click(function() {
250 // Translate the href on the link to the ajax href. That way this degrades
251 // into a nice, normal link.
252 var url = $(this).attr('href');
253 url = url.replace('nojs', 'ajax');
255 // Turn on the hilite to indicate this is in use.
256 $(this).addClass('hilite');
258 // Disable the save button.
259 $('#edit-save').attr('disabled', 'true');
261 $(this).addClass('views-throbbing');
266 success: Drupal.Views.Ajax.ajaxResponse,
267 error: function() { $(this).removeClass('views-throbbing'); alert(Drupal.t("An error occurred at @path.", {'@path': url})); },
274 $('form.views-ajax-form:not(.views-processed)').addClass('views-processed').submit(function(arg) {
275 // Translate the href on the link to the ajax href. That way this degrades
276 // into a nice, normal link.
277 var url = $(this).attr('action');
278 url = url.replace('nojs', 'ajax');
280 // $('input[@type=submit]', this).after('<span class="views-throbbing"> </span>');
285 success: Drupal.Views.Ajax.ajaxResponse,
286 error: function() { $('span.views-throbbing').remove(); alert(Drupal.t("An error occurred at @path.", {'@path': url})); },
293 // Bind the live preview to where it's supposed to go.
295 $('form#views-ui-preview-form:not(.views-processed)')
296 .addClass('views-processed')
297 .submit(Drupal.Views.updatePreviewForm);
299 $('div#views-live-preview form:not(.views-processed)')
300 .addClass('views-processed')
301 .submit(Drupal.Views.updatePreviewFilterForm);
303 $('div#views-live-preview a:not(.views-processed)')
304 .addClass('views-processed')
305 .click(Drupal.Views.updatePreviewLink);
309 * Get rid of irritating tabledrag messages
311 Drupal.theme.tableDragChangedWarning = function () {