annotate core/modules/views/js/ajax_view.js @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 /**
Chris@0 2 * DO NOT EDIT THIS FILE.
Chris@0 3 * See the following change record for more information,
Chris@0 4 * https://www.drupal.org/node/2815083
Chris@0 5 * @preserve
Chris@0 6 **/
Chris@0 7
Chris@0 8 (function ($, Drupal, drupalSettings) {
Chris@0 9 Drupal.behaviors.ViewsAjaxView = {};
Chris@17 10 Drupal.behaviors.ViewsAjaxView.attach = function (context, settings) {
Chris@17 11 if (settings && settings.views && settings.views.ajaxViews) {
Chris@17 12 var ajaxViews = settings.views.ajaxViews;
Chris@17 13
Chris@14 14 Object.keys(ajaxViews || {}).forEach(function (i) {
Chris@14 15 Drupal.views.instances[i] = new Drupal.views.ajaxView(ajaxViews[i]);
Chris@14 16 });
Chris@0 17 }
Chris@0 18 };
Chris@17 19 Drupal.behaviors.ViewsAjaxView.detach = function (context, settings, trigger) {
Chris@17 20 if (trigger === 'unload') {
Chris@17 21 if (settings && settings.views && settings.views.ajaxViews) {
Chris@17 22 var ajaxViews = settings.views.ajaxViews;
Chris@17 23
Chris@17 24 Object.keys(ajaxViews || {}).forEach(function (i) {
Chris@17 25 var selector = '.js-view-dom-id-' + ajaxViews[i].view_dom_id;
Chris@17 26 if ($(selector, context).length) {
Chris@17 27 delete Drupal.views.instances[i];
Chris@17 28 delete settings.views.ajaxViews[i];
Chris@17 29 }
Chris@17 30 });
Chris@17 31 }
Chris@17 32 }
Chris@17 33 };
Chris@0 34
Chris@0 35 Drupal.views = {};
Chris@0 36
Chris@0 37 Drupal.views.instances = {};
Chris@0 38
Chris@0 39 Drupal.views.ajaxView = function (settings) {
Chris@0 40 var selector = '.js-view-dom-id-' + settings.view_dom_id;
Chris@0 41 this.$view = $(selector);
Chris@0 42
Chris@14 43 var ajaxPath = drupalSettings.views.ajax_path;
Chris@0 44
Chris@14 45 if (ajaxPath.constructor.toString().indexOf('Array') !== -1) {
Chris@14 46 ajaxPath = ajaxPath[0];
Chris@0 47 }
Chris@0 48
Chris@0 49 var queryString = window.location.search || '';
Chris@0 50 if (queryString !== '') {
Chris@0 51 queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
Chris@0 52 if (queryString !== '') {
Chris@14 53 queryString = (/\?/.test(ajaxPath) ? '&' : '?') + queryString;
Chris@0 54 }
Chris@0 55 }
Chris@0 56
Chris@0 57 this.element_settings = {
Chris@14 58 url: ajaxPath + queryString,
Chris@0 59 submit: settings,
Chris@0 60 setClick: true,
Chris@0 61 event: 'click',
Chris@0 62 selector: selector,
Chris@0 63 progress: { type: 'fullscreen' }
Chris@0 64 };
Chris@0 65
Chris@0 66 this.settings = settings;
Chris@0 67
Chris@0 68 this.$exposed_form = $('form#views-exposed-form-' + settings.view_name.replace(/_/g, '-') + '-' + settings.view_display_id.replace(/_/g, '-'));
Chris@0 69 this.$exposed_form.once('exposed-form').each($.proxy(this.attachExposedFormAjax, this));
Chris@0 70
Chris@0 71 this.$view.filter($.proxy(this.filterNestedViews, this)).once('ajax-pager').each($.proxy(this.attachPagerAjax, this));
Chris@0 72
Chris@14 73 var selfSettings = $.extend({}, this.element_settings, {
Chris@0 74 event: 'RefreshView',
Chris@0 75 base: this.selector,
Chris@0 76 element: this.$view.get(0)
Chris@0 77 });
Chris@14 78 this.refreshViewAjax = Drupal.ajax(selfSettings);
Chris@0 79 };
Chris@0 80
Chris@0 81 Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () {
Chris@0 82 var that = this;
Chris@0 83 this.exposedFormAjax = [];
Chris@0 84
Chris@0 85 $('input[type=submit], input[type=image]', this.$exposed_form).not('[data-drupal-selector=edit-reset]').each(function (index) {
Chris@14 86 var selfSettings = $.extend({}, that.element_settings, {
Chris@0 87 base: $(this).attr('id'),
Chris@0 88 element: this
Chris@0 89 });
Chris@14 90 that.exposedFormAjax[index] = Drupal.ajax(selfSettings);
Chris@0 91 });
Chris@0 92 };
Chris@0 93
Chris@0 94 Drupal.views.ajaxView.prototype.filterNestedViews = function () {
Chris@0 95 return !this.$view.parents('.view').length;
Chris@0 96 };
Chris@0 97
Chris@0 98 Drupal.views.ajaxView.prototype.attachPagerAjax = function () {
Chris@0 99 this.$view.find('ul.js-pager__items > li > a, th.views-field a, .attachment .views-summary a').each($.proxy(this.attachPagerLinkAjax, this));
Chris@0 100 };
Chris@0 101
Chris@0 102 Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function (id, link) {
Chris@0 103 var $link = $(link);
Chris@0 104 var viewData = {};
Chris@0 105 var href = $link.attr('href');
Chris@0 106
Chris@0 107 $.extend(viewData, this.settings, Drupal.Views.parseQueryString(href), Drupal.Views.parseViewArgs(href, this.settings.view_base_path));
Chris@0 108
Chris@14 109 var selfSettings = $.extend({}, this.element_settings, {
Chris@0 110 submit: viewData,
Chris@0 111 base: false,
Chris@0 112 element: link
Chris@0 113 });
Chris@14 114 this.pagerAjax = Drupal.ajax(selfSettings);
Chris@0 115 };
Chris@0 116
Chris@0 117 Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) {
Chris@0 118 var offset = $(response.selector).offset();
Chris@0 119
Chris@0 120 var scrollTarget = response.selector;
Chris@0 121 while ($(scrollTarget).scrollTop() === 0 && $(scrollTarget).parent()) {
Chris@0 122 scrollTarget = $(scrollTarget).parent();
Chris@0 123 }
Chris@0 124
Chris@0 125 if (offset.top - 10 < $(scrollTarget).scrollTop()) {
Chris@0 126 $(scrollTarget).animate({ scrollTop: offset.top - 10 }, 500);
Chris@0 127 }
Chris@0 128 };
Chris@0 129 })(jQuery, Drupal, drupalSettings);