diff --git a/js/ajax_view.js b/js/ajax_view.js index ce6cc52..21f1ea7 100644 --- a/js/ajax_view.js +++ b/js/ajax_view.js @@ -13,6 +13,40 @@ Drupal.behaviors.ViewsAjaxView.attach = function() { $.each(Drupal.settings.views.ajaxViews, function(i, settings) { Drupal.views.instances[i] = new Drupal.views.ajaxView(settings); }); + + if (window.location.hash) { + hash = Drupal.Views.getLocationHash(); + + // Do we have a hash that corresponds to an auto-submit form? + $exposed_form_submitted = $('form#views-exposed-form-'+ hash.f); + + $lastLinkEvent = location.hash; + + $('body').once('views-hash', function() { + if (Drupal.Views.isNumeric(hash.p)) { + var pageValue = $('').val(hash.p); + $exposed_form_submitted.prepend(pageValue); + } + for (var v in hash) { + if (hash.hasOwnProperty(v)) { + // Set the form element to the value from the hash. + if (hash[v] == 'true') { + $exposed_form_submitted.find(('[id='+ v +']')).prop('checked', true); + } + else { + $exposed_form_submitted.find(('[id='+ v +']')).prop('value', hash[v]); + } + } + } + + if ($exposed_form_submitted.hasClass('ctools-auto-submit-full-form')) { + $exposed_form_submitted.find('input, select').first().change(); + } + else { + $exposed_form_submitted.find(':submit').click(); + } + }); + } // End if hash. } }; @@ -59,6 +93,7 @@ Drupal.views.ajaxView = function(settings) { // Add the ajax to exposed forms. this.$exposed_form = $('#views-exposed-form-'+ settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-')); this.$exposed_form.once(jQuery.proxy(this.attachExposedFormAjax, this)); + $(document).ajaxComplete(jQuery.proxy(this.ajaxCompleteExposedCallback, this)); // Add the ajax to pagers. this.$view @@ -79,6 +114,49 @@ Drupal.views.ajaxView = function(settings) { this.refreshViewAjax = new Drupal.ajax(this.selector, this.$view, self_settings); }; +/** + * Exposed forms use this to append their id as a URL hash so we can esubmit the + * form when the browser's "Back" button is used. + */ +Drupal.views.ajaxView.prototype.ajaxCompleteExposedCallback = function(event, request, options) { + if (options.url === this.element_settings.url) { + var data = Drupal.Views.parseQueryString(options.data); + hash = Drupal.Views.getLocationHash(); + + var val = {}; + var changed_exposed_filter = false; + this.$exposed_form.find('select, input').each(function(i, element) { + if ((this.name !== "" && this.type == 'select-one') || (this.type == 'checkbox')) { + changed_exposed_filter = true; + if (this.type == 'checkbox') { + if (this.checked) { + val[this.id] = 'true'; + } + else { + if (hash[this.id] == 'true') { + val[this.id] = 'false'; + } + } + } + else { + val[this.id] = this.value; + } + } + else { + val[this.id] = this.value; + } + }); + + if (changed_exposed_filter) { + val['p'] = 0; + } + Drupal.Views.updateLocationHash(val); + + Drupal.Views.updateLocationHash({ f: this.settings.view_name.replace(/_/g, '-') + '-' + this.settings.view_display_id.replace(/_/g, '-'), p: data.page}); + this.$exposed_form.find('input[name=page]').remove(); + } +}; + Drupal.views.ajaxView.prototype.attachExposedFormAjax = function() { var button = $('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form); button = button[0]; @@ -123,6 +201,20 @@ Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function(id, link) { this.element_settings.submit = viewData; this.pagerAjax = new Drupal.ajax(false, $link, this.element_settings); + + // Attach click handler to update hash. + $link.click(this.pagerHandler); +}; + +/** + * Click handler updates the location hash based on pager. + */ +Drupal.views.ajaxView.prototype.pagerHandler = function(event) { + var args = Drupal.Views.parseQueryString(event.target.href); + if (typeof args.page === "undefined") { + args.page = 0; + } + Drupal.Views.updateLocationHash({ p: args.page }); }; Drupal.ajax.prototype.commands.viewsScrollTop = function (ajax, response, status) { diff --git a/js/base.js b/js/base.js index 5855dce..6439473 100644 --- a/js/base.js +++ b/js/base.js @@ -107,4 +107,32 @@ Drupal.Views.getPath = function (href) { return href; }; +/** + * Helper function updates window.location.hash data from a javascript object. + * + * We provide for multiple arbitrary values to be stored in the hash by using a + * urlencoded string. + */ +Drupal.Views.updateLocationHash = function (data) { + hash = Drupal.Views.getLocationHash(); + $.extend(hash, data); + window.location.hash = decodeURIComponent($.param(hash)); +}; + +/** + * Helper function to get the window.location.hash data. + */ +Drupal.Views.getLocationHash = function () { + hashStr = window.location.hash.substr(1); // Strips the # itself. + return Drupal.Views.parseQueryString(hashStr); +}; + +/** + * Determines whether its argument represents a Javascript number (This is + * provided in jQuery 1.7, but we're not there yet). + */ +Drupal.Views.isNumeric = function (n) { + return !isNaN(parseFloat(n)) && isFinite(n); +}; + })(jQuery);