From ab3c120858357acd91e51412fdc57162a70f289b Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Fri, 7 Feb 2020 12:56:00 +0100 Subject: [PATCH 1/2] feat: add select all button --- js/select2.js | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/js/select2.js b/js/select2.js index ff23e5ad..f2247bd3 100644 --- a/js/select2.js +++ b/js/select2.js @@ -5,9 +5,49 @@ (function ($, drupalSettings) { 'use strict'; + var passIt = function () { + function SelectAll() {} + + SelectAll.prototype.render = function (decorated) { + var $rendered = decorated.call(this); + var self = this; + + var $selectAll = $( + '' + ); + + $rendered.find('.select2-dropdown').prepend($selectAll); + + $selectAll.on('click', function (e) { + var $results = $rendered.find('.select2-results__option[aria-selected=false]'); + + // Get all results that aren't selected + $results.each(function () { + var $result = $(this); + + // Get the data object for it + var data = $result.data('data'); + + // Trigger the select event + self.trigger('select', { + data: data + }); + }); + + self.trigger('close'); + }); + + return $rendered; + } + + return SelectAll; + } + + 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); @@ -27,6 +67,22 @@ } return option.text; }; + + $.fn.select2.amd.require([ + 'select2/utils', + 'select2/dropdown', + 'select2/dropdown/attachBody' + ], function (Utils, Dropdown, AttachBody) { + config.dropdownAdapter = Utils.Decorate( + Utils.Decorate( + Dropdown, + AttachBody + ), + passIt() + ); + + }); + $(this).data('select2-config', config); // Emit an event, that other modules have the chance to modify the From 2a4b073ab67b262fe5e1f45d3e592ab098b45876 Mon Sep 17 00:00:00 2001 From: Christian Fritsch Date: Fri, 7 Feb 2020 14:35:15 +0100 Subject: [PATCH 2/2] fix: make it working --- js/select2.js | 74 ++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 42 deletions(-) diff --git a/js/select2.js b/js/select2.js index f2247bd3..690edb29 100644 --- a/js/select2.js +++ b/js/select2.js @@ -2,47 +2,48 @@ * @file * Select2 integration. */ + (function ($, drupalSettings) { 'use strict'; - var passIt = function () { - function SelectAll() {} - - SelectAll.prototype.render = function (decorated) { - var $rendered = decorated.call(this); - var self = this; + $.fn.select2.amd.define("CustomDropdownAdapter", [ + 'select2/utils', + 'select2/dropdown', + 'select2/dropdown/attachBody' + ], + function (Utils, Dropdown, AttachBody) { - var $selectAll = $( - '' - ); + let dropdownWithButton = Utils.Decorate(Dropdown, AttachBody); - $rendered.find('.select2-dropdown').prepend($selectAll); + dropdownWithButton.prototype.render = function () { + var $rendered = Dropdown.prototype.render.call(this); - $selectAll.on('click', function (e) { - var $results = $rendered.find('.select2-results__option[aria-selected=false]'); + var $selectAll = $( + '' + ); - // Get all results that aren't selected - $results.each(function () { - var $result = $(this); + var self = this; - // Get the data object for it - var data = $result.data('data'); + $selectAll.on('click', function (e) { + // Get all results that aren't selected. + let $results = $rendered.find('.select2-results__option[aria-selected=false]'); - // Trigger the select event - self.trigger('select', { - data: data + $results.each(function (foo, bar) { + // Trigger the select event. + self.trigger('select', { + data: Utils.GetData(bar, 'data') + }); }); + self.trigger('close'); }); - self.trigger('close'); - }); - - return $rendered; - } + $rendered.prepend($selectAll); - return SelectAll; - } + return $rendered; + }; + return Utils.Decorate(dropdownWithButton, AttachBody); + }); Drupal.behaviors.select2 = { attach: function (context) { @@ -59,6 +60,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) { @@ -68,20 +71,6 @@ return option.text; }; - $.fn.select2.amd.require([ - 'select2/utils', - 'select2/dropdown', - 'select2/dropdown/attachBody' - ], function (Utils, Dropdown, AttachBody) { - config.dropdownAdapter = Utils.Decorate( - Utils.Decorate( - Dropdown, - AttachBody - ), - passIt() - ); - - }); $(this).data('select2-config', config); @@ -98,7 +87,8 @@ } $(this).select2(config); - // Copied from https://github.com/woocommerce/woocommerce/blob/master/assets/js/admin/wc-enhanced-select.js#L118 + // Copied from + // https://github.com/woocommerce/woocommerce/blob/master/assets/js/admin/wc-enhanced-select.js#L118 if (Object.prototype.hasOwnProperty.call(config, 'ajax')) { var $select = $(this); var $list = $(this).next('.select2-container').find('ul.select2-selection__rendered');