PageSourceSearch

https://www.hemmingen.de/fileadmin/templates/js/libs/parsley-2.8.1/src/parsley/remote.js

js hemmingen.de collected 2026-10-02 16:47:52 UTC 3,289 bytes, 108 lines download raw bytes

vendor: 3,289 bytes, lines 1-108
1import '../../../jquery/jquery-3.3.1.js'
2import Utils from './utils.js';
3import Base from './base.js';
4
5import Parsley from './main.js';
6
7$.extend(true, Parsley, {
8  asyncValidators: {
9    'default': {
10      fn: function (xhr) {
11        // By default, only status 2xx are deemed successful.
12        // Note: we use status instead of state() because responses with status 200
13        // but invalid messages (e.g. an empty body for content type set to JSON) will
14        // result in state() === 'rejected'.
15        return xhr.status >= 200 && xhr.status < 300;
16      },
17      url: false
18    },
19    reverse: {
20      fn: function (xhr) {
21        // If reverse option is set, a failing ajax request is considered successful
22        return xhr.status < 200 || xhr.status >= 300;
23      },
24      url: false
25    }
26  },
27
28  addAsyncValidator: function (name, fn, url, options) {
29    Parsley.asyncValidators[name] = {
30      fn: fn,
31      url: url || false,
32      options: options || {}
33    };
34
35    return this;
36  }
37
38});
39
40Parsley.addValidator('remote', {
41  requirementType: {
42    '': 'string',
43    'validator': 'string',
44    'reverse': 'boolean',
45    'options': 'object'
46  },
47
48  validateString: function (value, url, options, instance) {
49    var data = {};
50    var ajaxOptions;
51    var csr;
52    var validator = options.validator || (true === options.reverse ? 'reverse' : 'default');
53
54    if ('undefined' === typeof Parsley.asyncValidators[validator])
55      throw new Error('Calling an undefined async validator: `' + validator + '`');
56
57    url = Parsley.asyncValidators[validator].url || url;
58
59    // Fill current value
60    if (url.indexOf('{value}') > -1) {
61      url = url.replace('{value}', encodeURIComponent(value));
62    } else {
63      data[instance.element.getAttribute('name') || instance.element.getAttribute('id')] = value;
64    }
65
66    // Merge options passed in from the function with the ones in the attribute
67    var remoteOptions = $.extend(true, options.options || {} , Parsley.asyncValidators[validator].options);
68
69    // All `$.ajax(options)` could be overridden or extended directly from DOM in `data-parsley-remote-options`
70    ajaxOptions = $.extend(true, {}, {
71      url: url,
72      data: data,
73      type: 'GET'
74    }, remoteOptions);
75
76    // Generate store key based on ajax options
77    instance.trigger('field:ajaxoptions', instance, ajaxOptions);
78
79    csr = $.param(ajaxOptions);
80
81    // Initialise querry cache
82    if ('undefined' === typeof Parsley._remoteCache)
83      Parsley._remoteCache = {};
84
85    // Try to retrieve stored xhr
86    var xhr = Parsley._remoteCache[csr] = Parsley._remoteCache[csr] || $.ajax(ajaxOptions);
87
88    var handleXhr = function () {
89      var result = Parsley.asyncValidators[validator].fn.call(instance, xhr, url, options);
90      if (!result) // Map falsy results to rejected promise
91        result = $.Deferred().reject();
92      return $.when(result);
93    };
94
95    return xhr.then(handleXhr, handleXhr);
96  },
97
98  priority: -1
99});
100
101Parsley.on('form:submit', function () {
102  Parsley._remoteCache = {};
103});
104
105Base.prototype.addAsyncValidator = function () {
106  Utils.warnOnce('Accessing the method `addAsyncValidator` through an instance is deprecated. Simply call `Parsley.addAsyncValidator(...)`');
107  return Parsley.addAsyncValidator(...arguments);
108};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.