All Downloads are FREE. Search and download functionalities are using the official Maven repository.

package.src.js.select2.selection.multiple.js Maven / Gradle / Ivy

define([
  'jquery',
  './base',
  '../utils'
], function ($, BaseSelection, Utils) {
  function MultipleSelection ($element, options) {
    MultipleSelection.__super__.constructor.apply(this, arguments);
  }

  Utils.Extend(MultipleSelection, BaseSelection);

  MultipleSelection.prototype.render = function () {
    var $selection = MultipleSelection.__super__.render.call(this);

    $selection[0].classList.add('select2-selection--multiple');

    $selection.html(
      '
    ' ); return $selection; }; MultipleSelection.prototype.bind = function (container, $container) { var self = this; MultipleSelection.__super__.bind.apply(this, arguments); var id = container.id + '-container'; this.$selection.find('.select2-selection__rendered').attr('id', id); this.$selection.on('click', function (evt) { self.trigger('toggle', { originalEvent: evt }); }); this.$selection.on( 'click', '.select2-selection__choice__remove', function (evt) { // Ignore the event if it is disabled if (self.isDisabled()) { return; } var $remove = $(this); var $selection = $remove.parent(); var data = Utils.GetData($selection[0], 'data'); self.trigger('unselect', { originalEvent: evt, data: data }); } ); this.$selection.on( 'keydown', '.select2-selection__choice__remove', function (evt) { // Ignore the event if it is disabled if (self.isDisabled()) { return; } evt.stopPropagation(); } ); }; MultipleSelection.prototype.clear = function () { var $rendered = this.$selection.find('.select2-selection__rendered'); $rendered.empty(); $rendered.removeAttr('title'); }; MultipleSelection.prototype.display = function (data, container) { var template = this.options.get('templateSelection'); var escapeMarkup = this.options.get('escapeMarkup'); return escapeMarkup(template(data, container)); }; MultipleSelection.prototype.selectionContainer = function () { var $container = $( '
  • ' + '' + '' + '
  • ' ); return $container; }; MultipleSelection.prototype.update = function (data) { this.clear(); if (data.length === 0) { return; } var $selections = []; var selectionIdPrefix = this.$selection.find('.select2-selection__rendered') .attr('id') + '-choice-'; for (var d = 0; d < data.length; d++) { var selection = data[d]; var $selection = this.selectionContainer(); var formatted = this.display(selection, $selection); var selectionId = selectionIdPrefix + Utils.generateChars(4) + '-'; if (selection.id) { selectionId += selection.id; } else { selectionId += Utils.generateChars(4); } $selection.find('.select2-selection__choice__display') .append(formatted) .attr('id', selectionId); var title = selection.title || selection.text; if (title) { $selection.attr('title', title); } var removeItem = this.options.get('translations').get('removeItem'); var $remove = $selection.find('.select2-selection__choice__remove'); $remove.attr('title', removeItem()); $remove.attr('aria-label', removeItem()); $remove.attr('aria-describedby', selectionId); Utils.StoreData($selection[0], 'data', selection); $selections.push($selection); } var $rendered = this.$selection.find('.select2-selection__rendered'); $rendered.append($selections); }; return MultipleSelection; });




    © 2015 - 2024 Weber Informatics LLC | Privacy Policy