diff --git a/js/ajax_view.js b/js/ajax_view.js index 780691c..9122cf6 100644 --- a/js/ajax_view.js +++ b/js/ajax_view.js @@ -13,6 +13,16 @@ Drupal.behaviors.ViewsAjaxView.attach = function() { $.each(Drupal.settings.views.ajaxViews, function(i, settings) { Drupal.views.instances[i] = new Drupal.views.ajaxView(settings); }); + + var originalHash = window.location.hash; + $(window).bind('hashchange', function(e) { + $('body').removeOnce('views-hash'); + Drupal.views.ajaxView.prototype.ajaxUpdatePage(originalHash); + originalHash = location.hash; + }); + + Drupal.views.ajaxView.prototype.ajaxUpdatePage(originalHash); + } }; @@ -59,6 +69,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 +84,79 @@ Drupal.views.ajaxView = function(settings) { this.refreshViewAjax = new Drupal.ajax(this.selector, this.$view, self_settings); }; +Drupal.views.ajaxView.prototype.ajaxUpdatePage = function(originalHash) { + + // 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('views-hash', 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 from 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('views-hash', 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 + else { + + // Handle the case of being on a different page of the view and + // pressing the back button until there is no hash. + var original = Drupal.Views.parseQueryString(originalHash.replace(/^#/g, '')); + if (Drupal.Views.isNumeric(original.p)) { + $('body').once('views-hash', function () { + $('ul.pager .pager__item--first a').click(); + return false; + }); + } + } +} + +/** + * 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]; @@ -95,7 +179,7 @@ Drupal.views.ajaxView.prototype.attachPagerAjax = function() { }; /** - * Attach the ajax behavior to a singe link. + * Attach the ajax behavior to a single link. */ Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function(id, link) { var $link = $(link); @@ -117,8 +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) { // 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..e96a059 100644 --- a/js/base.js +++ b/js/base.js @@ -107,4 +107,39 @@ 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); + + // Iterate over URL parameters and unset any empty parameters, in order + // to prevent redundant history entries. + var hashObj = $.deparam($.param(hash)); + $.each(hashObj, function(index, value) { + if (value == '') { + delete hashObj[index]; + } + }); + + // Set new location hash, but only if it differs from the existing hash. + if (window.location.hash.replace(/^#/g, '') != $.param(hashObj)) { + window.location.hash = $.param(hashObj); + } +} + +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); diff --git a/views.module b/views.module index fc427f4..7ea5798 100644 --- a/views.module +++ b/views.module @@ -1119,6 +1119,7 @@ function views_add_js($file) { static $base = TRUE, $ajax = TRUE; if ($base) { drupal_add_js(drupal_get_path('module', 'views') . "/js/base.js"); + drupal_add_js('/misc/jquery.ba-bbq.js'); $base = FALSE; } if ($ajax && in_array($file, array('ajax', 'ajax_view'))) {