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