diff --git a/js/ajax_view.js b/js/ajax_view.js index 780691c..1702f36 100644 --- a/js/ajax_view.js +++ b/js/ajax_view.js @@ -13,6 +13,43 @@ Drupal.behaviors.ViewsAjaxView.attach = function() { $.each(Drupal.settings.views.ajaxViews, function(i, settings) { Drupal.views.instances[i] = new Drupal.views.ajaxView(settings); }); + + + // Trigger the previous filtered search if the filter form id is in the URL hash. + 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); + + if ($exposed_form_submitted.hasClass('ctools-auto-submit-full-form')) { + $('body').once(function() { + if (Drupal.Views.isNumeric(hash.p)) { + var pageValue = $('').val(hash.p) + $exposed_form_submitted.prepend(pageValue); + } + for (v in hash) { + if (hash.hasOwnProperty(v)) { + //Set the form element to the value form the hash. + $exposed_form_submitted.find(('[name='+ v +']')).val(hash[v]); + } + } + // @see Drupal.views.ajaxView.prototype.attachExposedFormAjax + $exposed_form_submitted.find('input, select').first().change(); + }); + } else if (Drupal.Views.isNumeric(hash.p)) { + // No filters, but check for page specified in location hash. + $('body').once(function () { + $('ul.pager .pager-item a').each( function() { + var args = Drupal.Views.parseQueryString(this.href); + if (hash.p == args.page) { + $(this).click(); + return false; + } + return true; + }); + }); + } + }// end if hash } }; @@ -59,6 +96,7 @@ Drupal.views.ajaxView = function(settings) { // Add the ajax to exposed forms. this.$exposed_form = $('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 @@ -73,6 +111,28 @@ 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 + * resubmit 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); + + this.$exposed_form.find('select, input').each(function(i, element) { + if (this.name != "") { + var val = {}; + val[this.name] = this.value; + 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]; @@ -117,8 +177,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) { // Scroll to the top of the view. This will allow users // to browse newly loaded content after e.g. clicking a pager diff --git a/js/base.js b/js/base.js index 5855dce..33a34a2 100644 --- a/js/base.js +++ b/js/base.js @@ -107,4 +107,26 @@ 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)); +} + +Drupal.Views.getLocationHash = function () { + hashStr = window.location.hash.substr(1); // Strips the # itself + return Drupal.Views.parseQueryString(hashStr); +} + +//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);