|
1 | 1 | /* global define, jQuery */ |
2 | | -(function (factory) { |
| 2 | +((factory => { |
3 | 3 | if (typeof define === 'function' && define.amd) { |
4 | 4 | define(['jquery'], factory) |
5 | 5 | } else if (typeof module === 'object' && module.exports) { |
6 | 6 | module.exports = factory(require('jquery')) |
7 | 7 | } else { |
8 | 8 | // Browser globals - prefer Django's jQuery to avoid conflicts |
9 | | - factory(window.django?.jQuery || jQuery) |
| 9 | + factory(globalThis.django?.jQuery || jQuery) |
10 | 10 | } |
11 | | -}(function ($) { |
| 11 | +})($ => { |
12 | 12 | 'use strict' |
13 | | - const init = function ($element, options) { |
| 13 | + const init = ($element, options) => { |
14 | 14 | $element.select2(options) |
15 | 15 | } |
16 | 16 |
|
17 | | - const initHeavy = function ($element, options) { |
| 17 | + function initHeavy ($element, options) { |
18 | 18 | const settings = $.extend({ |
19 | 19 | ajax: { |
20 | | - data: function (params) { |
| 20 | + data: params => { |
21 | 21 | const result = { |
22 | 22 | term: params.term, |
23 | 23 | page: params.page, |
|
26 | 26 |
|
27 | 27 | let dependentFields = $element.data('select2-dependent-fields') |
28 | 28 | if (dependentFields) { |
29 | | - const findElement = function (selector) { |
| 29 | + function findElement (selector) { |
30 | 30 | const result = $(selector, $element.closest(`:has(${selector})`)) |
31 | 31 | if (result.length > 0) return result |
32 | 32 | else return null |
33 | 33 | } |
34 | 34 | dependentFields = dependentFields.trim().split(/\s+/) |
35 | | - $.each(dependentFields, function (i, dependentField) { |
| 35 | + $.each(dependentFields, (i, dependentField) => { |
36 | 36 | const nameIs = `[name=${dependentField}]` |
37 | 37 | const nameEndsWith = `[name$=-${dependentField}]` |
38 | 38 | result[dependentField] = (findElement(nameIs) || findElement(nameEndsWith)).val() |
|
41 | 41 |
|
42 | 42 | return result |
43 | 43 | }, |
44 | | - processResults: function (data, page) { |
| 44 | + processResults: (data, page) => { |
45 | 45 | return { |
46 | 46 | results: data.results, |
47 | 47 | pagination: { |
|
57 | 57 |
|
58 | 58 | $.fn.djangoSelect2 = function (options) { |
59 | 59 | const settings = $.extend({}, options) |
60 | | - $.each(this, function (i, element) { |
| 60 | + $.each(this, (i, element) => { |
61 | 61 | const $element = $(element) |
62 | 62 | if ($element.hasClass('django-select2-heavy')) { |
63 | 63 | initHeavy($element, settings) |
64 | 64 | } else { |
65 | 65 | init($element, settings) |
66 | 66 | } |
67 | | - $element.on('select2:select', function (e) { |
| 67 | + $element.on('select2:select', e => { |
68 | 68 | const name = $(e.currentTarget).attr('name') |
69 | | - $('[data-select2-dependent-fields~=' + name + ']').each(function () { |
| 69 | + $(`[data-select2-dependent-fields~=${name}]`).each(function () { |
70 | 70 | $(this).val('').trigger('change') |
71 | 71 | }) |
72 | 72 | }) |
73 | 73 | }) |
74 | 74 | return this |
75 | 75 | } |
76 | 76 |
|
77 | | - $(function () { |
| 77 | + $(() => { |
78 | 78 | $('.django-select2').not('[name*=__prefix__]').djangoSelect2() |
79 | 79 |
|
80 | 80 | document.addEventListener('formset:added', (event) => { |
|
0 commit comments