1/** 2 * @file 3 * Select2 integration. 4 */ 5 6// Support for jQuery 4 #6298 7// @see https://github.com/select2/select2/issues/6298 8if (!jQuery.isArray) { 9 jQuery.isArray = Array.isArray || function (value) { 10 return Object.prototype.toString.call(value) === '[object Array]'; 11 }; 12} 13if (!jQuery.trim) { 14 jQuery.trim = function (text) { 15 return text == null ? "" : text.trim(); 16 }; 17} 18 19(function ($, drupalSettings, Sortable, once) { 20 'use strict'; 21 22 Drupal.behaviors.select2 = { 23 attach: function (context) { 24 $(once('select2-init', '.select2-widget', context)).each(function () { 25 var config = $(this).data('select2-config'); 26 config.createTag = function (params) { 27 var term = params.term.trim(); 28 if (term === '') { 29 return null; 30 } 31 return { 32 id: '$ID:' + term, 33 text: term 34 }; 35 }; 36 config.templateSelection = function (option, container) { 37 // The placeholder doesn't have value. 38 if ('element' in option && 'value' in option.element) { 39 // Add option value to selection container for sorting. 40 $(container).data('optionValue', option.element.value); 41 } 42 return option.text; 43 }; 44 if (Object.prototype.hasOwnProperty.call(config, 'ajax')) { 45 config.ajax.data = function (params) { 46 var selected = []; 47 if (Array.isArray($(this).val())) { 48 selected = $(this).val(); 49 } 50 else if ($(this).val() !== '') { 51 selected = [$(this).val()]; 52 } 53 return $.extend({}, params, { 54 q: params.term, 55 selected: selected.filter(function (selected) { 56 return !selected.startsWith('$ID:'); 57 }) 58 }); 59 }; 60 } 61 $(this).data('select2-config', config); 62 63 // Emit an event, that other modules have the chance to modify the 64 // select2 settings. Make sure that other JavaScript code that rely on 65 // this event will be loaded first. 66 // @see: modules/select2_publish/select2_publish.libraries.yml 67 $(this).trigger('select2-init'); 68 config = $(this).data('select2-config'); 69 70 // If config has a dropdownParent property, wrap it a jQuery object. 71 if (Object.prototype.hasOwnProperty.call(config, 'dropdownParent')) { 72 config.dropdownParent = $(config.dropdownParent); 73 } 74 $(this).select2(config); 75 76 // Copied from https://github.com/woocommerce/woocommerce/blob/master/assets/js/admin/wc-enhanced-select.js#L118 77 if (config.multiple) { 78 var $select = $(this); 79 var $list = $select.next('.select2-container').find('ul.select2-selection__rendered'); 80 Sortable.create($list[0], { 81 draggable: 'li:not(.select2-search)', 82 onEnd: function (event) { 83 if (event.newIndex != event.oldIndex) { 84 $($list.find('.select2-selection__choice').get().reverse()).each(function () { 85 $select.prepend($select.find('option[value="' + $(this).data('optionValue') + '"]').first()); 86 }); 87 } 88 } 89 }); 90 } 91 }); 92 } 93 }; 94 95})(jQuery, drupalSettings, Sortable, once);
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.