/* ======================================================================== * Winter CMS: front-end JavaScript framework * https://wintercms.com * ======================================================================== * Copyright 2016-2020 Alexey Bobkov, Samuel Georges * ======================================================================== */ if (window.jQuery === undefined) { throw new Error('The jQuery library is not loaded. The Winter CMS framework cannot be initialized.'); } if (window.jQuery.request !== undefined) { throw new Error('The Winter CMS framework is already loaded.'); } +function ($) { "use strict"; var Request = function (element, handler, options) { var $el = this.$el = $(element); this.options = options || {}; /* * Validate handler name */ if (handler === undefined) { throw new Error('The request handler name is not specified.') } if (!handler.match(/^(?:\w+\:{2})?on*/)) { throw new Error('Invalid handler name. The correct handler name format is: "onEvent".') } /* * Prepare the options */ var $form = options.form ? $(options.form) : $el.closest('form'), $triggerEl = !!$form.length ? $form : $el, context = { handler: handler, options: options } /* * Validate the form client-side */ if ((options.browserValidate !== undefined) && typeof document.createElement('input').reportValidity == 'function' && $form && $form[0] && !$form[0].checkValidity()) { $form[0].reportValidity(); return false; } /* * Execute the request */ $el.trigger('ajaxSetup', [context]) var _event = jQuery.Event('oc.beforeRequest') $triggerEl.trigger(_event, context) if (_event.isDefaultPrevented()) return var loading = options.loading !== undefined ? options.loading : null, url = options.url !== undefined ? options.url : window.location.href, isRedirect = options.redirect !== undefined && options.redirect.length, useFlash = options.flash !== undefined, useFiles = options.files !== undefined if (useFiles && typeof FormData === 'undefined') { console.warn('This browser does not support file uploads via FormData') useFiles = false } if ($.type(loading) == 'string') { loading = $(loading) } /* * Request headers */ var requestHeaders = { 'X-WINTER-REQUEST-HANDLER': handler, 'X-WINTER-REQUEST-PARTIALS': this.extractPartials(options.update) } if (useFlash) { requestHeaders['X-WINTER-REQUEST-FLASH'] = 1 } var csrfToken = getXSRFToken() if (csrfToken) { requestHeaders['X-XSRF-TOKEN'] = csrfToken } /** * Request Data Merging / Prioritization Process: * * 1. START: * options.data contains $el's data-request-data * data is an empty object * * 2. Populate data with data-request-data attributes * The data var is populated with the values from data-request-data attributes present on any elements * that are parents of the $el element (all the way up to the document root) with closer elements taking priority * * 3. Add $el's data to options.data if $el is a form input AND is not present in a containing