Skip to content
This repository has been archived by the owner on Nov 18, 2022. It is now read-only.

Issue #3063281: 'Select All' option #88

Open
wants to merge 4 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions js/select2.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,49 @@
(function ($, drupalSettings, Sortable) {
'use strict';

$.fn.select2.amd.define("CustomDropdownAdapter", [
'select2/utils',
'select2/dropdown',
'select2/dropdown/attachBody'
],
function (Utils, Dropdown, AttachBody) {

let dropdownWithButton = Utils.Decorate(Dropdown, AttachBody);

dropdownWithButton.prototype.render = function () {
var $rendered = Dropdown.prototype.render.call(this);

var $selectAll = $(
'<button type="button">Select All</button>'
);

var self = this;

$selectAll.on('click', function (e) {
// Get all results that aren't selected.
let $results = $rendered.find('.select2-results__option[aria-selected=false]');

$results.each(function (foo, bar) {
// Trigger the select event.
self.trigger('select', {
data: Utils.GetData(bar, 'data')
});
});
self.trigger('close');
});

$rendered.prepend($selectAll);

return $rendered;
};

return Utils.Decorate(dropdownWithButton, AttachBody);
});

Drupal.behaviors.select2 = {
attach: function (context) {
$('.select2-widget', context).once('select2-init').each(function () {

var config = $(this).data('select2-config');
config.createTag = function (params) {
var term = $.trim(params.term);
Expand All @@ -19,6 +59,8 @@
text: term
};
};
config.dropdownAdapter = $.fn.select2.amd.require("CustomDropdownAdapter")

config.templateSelection = function (option, container) {
// The placeholder doesn't have value.
if ('element' in option && 'value' in option.element) {
Expand All @@ -27,6 +69,8 @@
}
return option.text;
};


$(this).data('select2-config', config);

// Emit an event, that other modules have the chance to modify the
Expand Down