1
0
قرینه از https://github.com/matomo-org/matomo.git synced 2025-08-22 15:07:44 +00:00
Files
matomo/node_modules/@materializecss/materialize/js/characterCounter.js
Stefan Giehl aea8f07de8 Use materializecss/materialize instead of Dogfalo/materialize (#19622)
* remove old materialize css

* add new materlialize css

* update paths of materialize css

* fix visibility of normal selects

* fix selected in segmentation layer

* fix style for disabled selects

* update materializecss to 1.2.0

* update/fix some ui tests

* update materializecss to 1.2.1

* Apply patch from https://github.com/materializecss/materialize/pull/331

* update expected test files

* ignore list elements for modal buttons

* update expected test files

* update materializecss to 1.2.2

* fix ui test

* Update UI test screenshots

---------

Co-authored-by: Ben <ben.burgess@innocraft.com>
2023-03-28 11:51:14 +02:00

137 خطوط
3.0 KiB
JavaScript
Vendored

(function($) {
'use strict';
let _defaults = {};
/**
* @class
*
*/
class CharacterCounter extends Component {
/**
* Construct CharacterCounter instance
* @constructor
* @param {Element} el
* @param {Object} options
*/
constructor(el, options) {
super(CharacterCounter, el, options);
this.el.M_CharacterCounter = this;
/**
* Options for the character counter
*/
this.options = $.extend({}, CharacterCounter.defaults, options);
this.isInvalid = false;
this.isValidLength = false;
this._setupCounter();
this._setupEventHandlers();
}
static get defaults() {
return _defaults;
}
static init(els, options) {
return super.init(this, els, options);
}
/**
* Get Instance
*/
static getInstance(el) {
let domElem = !!el.jquery ? el[0] : el;
return domElem.M_CharacterCounter;
}
/**
* Teardown component
*/
destroy() {
this._removeEventHandlers();
this.el.CharacterCounter = undefined;
this._removeCounter();
}
/**
* Setup Event Handlers
*/
_setupEventHandlers() {
this._handleUpdateCounterBound = this.updateCounter.bind(this);
this.el.addEventListener('focus', this._handleUpdateCounterBound, true);
this.el.addEventListener('input', this._handleUpdateCounterBound, true);
}
/**
* Remove Event Handlers
*/
_removeEventHandlers() {
this.el.removeEventListener('focus', this._handleUpdateCounterBound, true);
this.el.removeEventListener('input', this._handleUpdateCounterBound, true);
}
/**
* Setup counter element
*/
_setupCounter() {
this.counterEl = document.createElement('span');
$(this.counterEl)
.addClass('character-counter')
.css({
float: 'right',
'font-size': '12px',
height: 1
});
this.$el.parent().append(this.counterEl);
}
/**
* Remove counter element
*/
_removeCounter() {
$(this.counterEl).remove();
}
/**
* Update counter
*/
updateCounter() {
let maxLength = +this.$el.attr('data-length'),
actualLength = this.el.value.length;
this.isValidLength = actualLength <= maxLength;
let counterString = actualLength;
if (maxLength) {
counterString += '/' + maxLength;
this._validateInput();
}
$(this.counterEl).html(counterString);
}
/**
* Add validation classes
*/
_validateInput() {
if (this.isValidLength && this.isInvalid) {
this.isInvalid = false;
this.$el.removeClass('invalid');
} else if (!this.isValidLength && !this.isInvalid) {
this.isInvalid = true;
this.$el.removeClass('valid');
this.$el.addClass('invalid');
}
}
}
M.CharacterCounter = CharacterCounter;
if (M.jQueryLoaded) {
M.initializeJqueryWrapper(CharacterCounter, 'characterCounter', 'M_CharacterCounter');
}
})(cash);