drupal/core/modules/views/js/ajax_view.js

135 lines
4.3 KiB
JavaScript

/**
* DO NOT EDIT THIS FILE.
* See the following change record for more information,
* https://www.drupal.org/node/2815083
* @preserve
**/
(function ($, Drupal, drupalSettings) {
Drupal.behaviors.ViewsAjaxView = {};
Drupal.behaviors.ViewsAjaxView.attach = function (context, settings) {
if (settings && settings.views && settings.views.ajaxViews) {
const {
views: {
ajaxViews
}
} = settings;
Object.keys(ajaxViews || {}).forEach(i => {
Drupal.views.instances[i] = new Drupal.views.ajaxView(ajaxViews[i]);
});
}
};
Drupal.behaviors.ViewsAjaxView.detach = (context, settings, trigger) => {
if (trigger === 'unload') {
if (settings && settings.views && settings.views.ajaxViews) {
const {
views: {
ajaxViews
}
} = settings;
Object.keys(ajaxViews || {}).forEach(i => {
const selector = `.js-view-dom-id-${ajaxViews[i].view_dom_id}`;
if ($(selector, context).length) {
delete Drupal.views.instances[i];
delete settings.views.ajaxViews[i];
}
});
}
}
};
Drupal.views = {};
Drupal.views.instances = {};
Drupal.views.ajaxView = function (settings) {
const selector = `.js-view-dom-id-${settings.view_dom_id}`;
this.$view = $(selector);
let ajaxPath = drupalSettings.views.ajax_path;
if (ajaxPath.constructor.toString().indexOf('Array') !== -1) {
ajaxPath = ajaxPath[0];
}
let queryString = window.location.search || '';
if (queryString !== '') {
queryString = queryString.slice(1).replace(/q=[^&]+&?|&?render=[^&]+/, '');
if (queryString !== '') {
queryString = (/\?/.test(ajaxPath) ? '&' : '?') + queryString;
}
}
this.element_settings = {
url: ajaxPath + queryString,
submit: settings,
setClick: true,
event: 'click',
selector,
progress: {
type: 'fullscreen'
}
};
this.settings = settings;
this.$exposed_form = $(`form#views-exposed-form-${settings.view_name.replace(/_/g, '-')}-${settings.view_display_id.replace(/_/g, '-')}`);
once('exposed-form', this.$exposed_form).forEach($.proxy(this.attachExposedFormAjax, this));
once('ajax-pager', this.$view.filter($.proxy(this.filterNestedViews, this))).forEach($.proxy(this.attachPagerAjax, this));
const selfSettings = $.extend({}, this.element_settings, {
event: 'RefreshView',
base: this.selector,
element: this.$view.get(0)
});
this.refreshViewAjax = Drupal.ajax(selfSettings);
};
Drupal.views.ajaxView.prototype.attachExposedFormAjax = function () {
const that = this;
this.exposedFormAjax = [];
$('input[type=submit], button[type=submit], input[type=image]', this.$exposed_form).not('[data-drupal-selector=edit-reset]').each(function (index) {
const selfSettings = $.extend({}, that.element_settings, {
base: $(this).attr('id'),
element: this
});
that.exposedFormAjax[index] = Drupal.ajax(selfSettings);
});
};
Drupal.views.ajaxView.prototype.filterNestedViews = function () {
return !this.$view.parents('.view').length;
};
Drupal.views.ajaxView.prototype.attachPagerAjax = function () {
this.$view.find('ul.js-pager__items > li > a, th.views-field a, .attachment .views-summary a').each($.proxy(this.attachPagerLinkAjax, this));
};
Drupal.views.ajaxView.prototype.attachPagerLinkAjax = function (id, link) {
const $link = $(link);
const viewData = {};
const href = $link.attr('href');
$.extend(viewData, this.settings, Drupal.Views.parseQueryString(href), Drupal.Views.parseViewArgs(href, this.settings.view_base_path));
const selfSettings = $.extend({}, this.element_settings, {
submit: viewData,
base: false,
element: link
});
this.pagerAjax = Drupal.ajax(selfSettings);
};
Drupal.AjaxCommands.prototype.viewsScrollTop = function (ajax, response) {
const offset = $(response.selector).offset();
let scrollTarget = response.selector;
while ($(scrollTarget).scrollTop() === 0 && $(scrollTarget).parent()) {
scrollTarget = $(scrollTarget).parent();
}
if (offset.top - 10 < $(scrollTarget).scrollTop()) {
$(scrollTarget).animate({
scrollTop: offset.top - 10
}, 500);
}
};
})(jQuery, Drupal, drupalSettings);