UNPKG

@syncfusion/ej2-buttons

Version:

A package of feature-rich Essential JS 2 components such as Button, CheckBox, RadioButton and Switch.

1,329 lines (1,325 loc) 231 kB
import { getValue, attributes, setValue, deleteObject, detach, getUniqueID, addClass, rippleEffect, Observer, isBlazor, SanitizeHtmlHelper, EventHandler, removeClass, isRippleEnabled, Property, Event, NotifyPropertyChanges, Component, closest, isNullOrUndefined, getInstance, Draggable, remove, compile, append, select, ChildProperty, KeyboardEvents, selectAll, formatUnit, animationMode, extend, Complex, Collection, Animation } from '@syncfusion/ej2-base'; /** * Initialize wrapper element for angular. * * @private * * @param {CreateElementArgs} createElement - Specifies created element args * @param {string} tag - Specifies tag name * @param {string} type - Specifies type name * @param {HTMLInputElement} element - Specifies input element * @param {string} WRAPPER - Specifies wrapper element * @param {string} role - Specifies role * @returns {HTMLInputElement} - Input Element */ function wrapperInitialize(createElement, tag, type, element, WRAPPER, role) { var input = element; if (element.tagName === tag) { var ejInstance = getValue('ej2_instances', element); input = createElement('input', { attrs: { 'type': type } }); var props = ['change', 'cssClass', 'label', 'labelPosition', 'id']; for (var index = 0, len = element.attributes.length; index < len; index++) { if (props.indexOf(element.attributes[index].nodeName) === -1) { input.setAttribute(element.attributes[index].nodeName, element.attributes[index].nodeValue); } } attributes(element, { 'class': WRAPPER }); element.appendChild(input); element.classList.add(role); element.classList.remove(role); setValue('ej2_instances', ejInstance, input); deleteObject(element, 'ej2_instances'); } return input; } /** * Get the text node. * * @param {HTMLElement} element - Specifies html element * @private * @returns {Node} - Text node. */ function getTextNode(element) { var node; var childnode = element.childNodes; for (var i = 0; i < childnode.length; i++) { node = childnode[i]; if (node.nodeType === 3) { return node; } } return null; } /** * Destroy the button components. * * @private * @param {Switch | CheckBox} ejInst - Specifies eJ2 Instance * @param {Element} wrapper - Specifies wrapper element * @param {string} tagName - Specifies tag name * @returns {void} */ function destroy(ejInst, wrapper, tagName) { if (tagName === 'INPUT') { wrapper.parentNode.insertBefore(ejInst.element, wrapper); detach(wrapper); ejInst.element.checked = false; ['name', 'value', 'disabled'].forEach(function (key) { ejInst.element.removeAttribute(key); }); } else { ['role', 'aria-checked', 'class'].forEach(function (key) { wrapper.removeAttribute(key); }); wrapper.innerHTML = ''; ejInst.element = wrapper; } } /** * Initialize control pre rendering. * * @private * @param {Switch | CheckBox} proxy - Specifies proxy * @param {string} control - Specifies control * @param {string} wrapper - Specifies wrapper element * @param {HTMLInputElement} element - Specifies input element * @param {string} moduleName - Specifies module name * @returns {void} */ function preRender(proxy, control, wrapper, element, moduleName) { element = wrapperInitialize(proxy.createElement, control, 'checkbox', element, wrapper, moduleName); proxy.element = element; if (proxy.element.getAttribute('type') !== 'checkbox') { proxy.element.setAttribute('type', 'checkbox'); } if (!proxy.element.id) { proxy.element.id = getUniqueID('e-' + moduleName); } } /** * Creates CheckBox component UI with theming and ripple support. * * @private * @param {CreateElementArgs} createElement - Specifies Created Element args * @param {boolean} enableRipple - Specifies ripple effect * @param {CheckBoxUtilModel} options - Specifies Checkbox util Model * @returns {Element} - Checkbox Element */ function createCheckBox(createElement, enableRipple, options) { if (enableRipple === void 0) { enableRipple = false; } if (options === void 0) { options = {}; } var wrapper = createElement('div', { className: 'e-checkbox-wrapper e-css' }); if (options.cssClass) { addClass([wrapper], options.cssClass.split(' ')); } if (options.enableRtl) { wrapper.classList.add('e-rtl'); } if (enableRipple) { var rippleSpan = createElement('span', { className: 'e-ripple-container' }); rippleEffect(rippleSpan, { isCenterRipple: true, duration: 400 }); wrapper.appendChild(rippleSpan); } var frameSpan = createElement('span', { className: 'e-frame e-icons' }); if (options.checked) { frameSpan.classList.add('e-check'); } wrapper.appendChild(frameSpan); if (options.label) { var labelSpan = createElement('span', { className: 'e-label' }); if (options.disableHtmlEncode) { labelSpan.textContent = options.label; } else { labelSpan.innerHTML = options.label; } wrapper.appendChild(labelSpan); } return wrapper; } /** * Handles ripple mouse. * * @private * @param {MouseEvent} e - Specifies mouse event * @param {Element} rippleSpan - Specifies Ripple span element * @returns {void} */ function rippleMouseHandler(e, rippleSpan) { if (rippleSpan) { var event_1 = document.createEvent('MouseEvents'); event_1.initEvent(e.type, false, true); rippleSpan.dispatchEvent(event_1); } } /** * Append hidden input to given element * * @private * @param {Switch | CheckBox} proxy - Specifies Proxy * @param {Element} wrap - Specifies Wrapper ELement * @returns {void} */ function setHiddenInput(proxy, wrap) { if (proxy.element.getAttribute('ejs-for')) { wrap.appendChild(proxy.createElement('input', { attrs: { 'name': proxy.name || proxy.element.name, 'value': 'false', 'type': 'hidden' } })); } } /** * Represents the event arguments for a "beforeChange" event. * * This object contains details about an action that is about to occur, allowing you to intercept and cancel the action before it's finalized. */ var BeforeChangeEventArgs = /** @__PURE__ @class */ (function () { function BeforeChangeEventArgs() { } return BeforeChangeEventArgs; }()); var __extends = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; /** * Defines the icon position of button. */ var IconPosition; (function (IconPosition) { /** * Positions the Icon at the left of the text content in the Button. */ IconPosition["Left"] = "Left"; /** * Positions the Icon at the right of the text content in the Button. */ IconPosition["Right"] = "Right"; /** * Positions the Icon at the top of the text content in the Button. */ IconPosition["Top"] = "Top"; /** * Positions the Icon at the bottom of the text content in the Button. */ IconPosition["Bottom"] = "Bottom"; })(IconPosition || (IconPosition = {})); var buttonObserver = new Observer(); var cssClassName = { RTL: 'e-rtl', BUTTON: 'e-btn', PRIMARY: 'e-primary', ICONBTN: 'e-icon-btn' }; /** * The Button is a graphical user interface element that triggers an event on its click action. It can contain a text, an image, or both. * ```html * <button id="button">Button</button> * ``` * ```typescript * <script> * var btnObj = new Button(); * btnObj.appendTo("#button"); * </script> * ``` */ var Button = /** @__PURE__ @class */ (function (_super) { __extends(Button, _super); /** * Constructor for creating the widget * * @param {ButtonModel} options - Specifies the button model * @param {string|HTMLButtonElement} element - Specifies the target element */ function Button(options, element) { return _super.call(this, options, element) || this; } Button.prototype.preRender = function () { // pre render code snippets }; /** * Initialize the control rendering * * @returns {void} * @private */ Button.prototype.render = function () { this.initialize(); this.removeRippleEffect = rippleEffect(this.element, { selector: '.' + cssClassName.BUTTON }); this.renderComplete(); }; Button.prototype.initialize = function () { if (this.cssClass) { addClass([this.element], this.cssClass.replace(/\s+/g, ' ').trim().split(' ')); } if (this.isPrimary) { this.element.classList.add(cssClassName.PRIMARY); } if (!isBlazor() || (isBlazor() && this.getModuleName() !== 'progress-btn')) { if (this.content) { var tempContent = (this.enableHtmlSanitizer) ? SanitizeHtmlHelper.sanitize(this.content) : this.content; this.element.innerHTML = tempContent; } this.setIconCss(); } if (this.enableRtl) { this.element.classList.add(cssClassName.RTL); } if (this.disabled) { this.controlStatus(this.disabled); } else { this.wireEvents(); } }; Button.prototype.controlStatus = function (disabled) { this.element.disabled = disabled; }; Button.prototype.setIconCss = function () { if (this.iconCss) { var span = this.createElement('span', { className: 'e-btn-icon ' + this.iconCss }); if (!this.element.textContent.trim()) { this.element.classList.add(cssClassName.ICONBTN); } else { span.classList.add('e-icon-' + this.iconPosition.toLowerCase()); if (this.iconPosition === 'Top' || this.iconPosition === 'Bottom') { this.element.classList.add('e-' + this.iconPosition.toLowerCase() + '-icon-btn'); } } var node = this.element.childNodes[0]; if (node && (this.iconPosition === 'Left' || this.iconPosition === 'Top')) { this.element.insertBefore(span, node); } else { this.element.appendChild(span); } } }; Button.prototype.wireEvents = function () { if (this.isToggle) { EventHandler.add(this.element, 'click', this.btnClickHandler, this); } }; Button.prototype.unWireEvents = function () { if (this.isToggle) { EventHandler.remove(this.element, 'click', this.btnClickHandler); } }; Button.prototype.btnClickHandler = function () { if (this.element.classList.contains('e-active')) { this.element.classList.remove('e-active'); } else { this.element.classList.add('e-active'); } }; /** * Destroys the widget. * * @returns {void} */ Button.prototype.destroy = function () { var classList = [cssClassName.PRIMARY, cssClassName.RTL, cssClassName.ICONBTN, 'e-success', 'e-info', 'e-danger', 'e-warning', 'e-flat', 'e-outline', 'e-small', 'e-bigger', 'e-active', 'e-round', 'e-top-icon-btn', 'e-bottom-icon-btn']; if (this.cssClass) { classList = classList.concat(this.cssClass.split(/\s+/).filter(function (c) { return c.length > 0; })); } _super.prototype.destroy.call(this); removeClass([this.element], classList); if (!this.element.getAttribute('class')) { this.element.removeAttribute('class'); } if (this.disabled) { this.element.removeAttribute('disabled'); } if (this.content) { this.element.innerHTML = this.element.innerHTML.replace(this.content, ''); } var span = this.element.querySelector('span.e-btn-icon'); if (span) { detach(span); } this.unWireEvents(); if (isRippleEnabled) { this.removeRippleEffect(); } }; /** * Get component name. * * @returns {string} - Module name * @private */ Button.prototype.getModuleName = function () { return 'btn'; }; /** * Get the properties to be maintained in the persisted state. * * @returns {string} - Persist Data * @private */ Button.prototype.getPersistData = function () { return this.addOnPersist([]); }; /** * Dynamically injects the required modules to the component. * * @private * @returns {void} */ Button.Inject = function () { // Inject code snippets }; /** * Called internally if any of the property value changed. * * @param {ButtonModel} newProp - Specifies new properties * @param {ButtonModel} oldProp - Specifies old properties * @returns {void} * @private */ Button.prototype.onPropertyChanged = function (newProp, oldProp) { var span = this.element.querySelector('span.e-btn-icon'); for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) { var prop = _a[_i]; switch (prop) { case 'isPrimary': if (newProp.isPrimary) { this.element.classList.add(cssClassName.PRIMARY); } else { this.element.classList.remove(cssClassName.PRIMARY); } break; case 'disabled': this.controlStatus(newProp.disabled); break; case 'iconCss': { span = this.element.querySelector('span.e-btn-icon'); if (span) { if (newProp.iconCss) { span.className = 'e-btn-icon ' + newProp.iconCss; if (this.element.textContent.trim()) { if (this.iconPosition === 'Left') { span.classList.add('e-icon-left'); } else { span.classList.add('e-icon-right'); } } } else { detach(span); } } else { this.setIconCss(); } break; } case 'iconPosition': removeClass([this.element], ['e-top-icon-btn', 'e-bottom-icon-btn']); span = this.element.querySelector('span.e-btn-icon'); if (span) { detach(span); } this.setIconCss(); break; case 'cssClass': if (oldProp.cssClass) { removeClass([this.element], oldProp.cssClass.split(/\s+/).filter(function (c) { return c.length > 0; })); } if (newProp.cssClass) { addClass([this.element], newProp.cssClass.replace(/\s+/g, ' ').trim().split(' ')); } break; case 'enableRtl': if (newProp.enableRtl) { this.element.classList.add(cssClassName.RTL); } else { this.element.classList.remove(cssClassName.RTL); } break; case 'content': { var node = getTextNode(this.element); if (!node) { this.element.classList.remove(cssClassName.ICONBTN); } if (!isBlazor() || (isBlazor() && !this.isServerRendered && this.getModuleName() !== 'progress-btn')) { if (this.enableHtmlSanitizer) { newProp.content = SanitizeHtmlHelper.sanitize(newProp.content); } this.element.innerHTML = newProp.content; this.setIconCss(); } break; } case 'isToggle': if (newProp.isToggle) { EventHandler.add(this.element, 'click', this.btnClickHandler, this); } else { EventHandler.remove(this.element, 'click', this.btnClickHandler); removeClass([this.element], ['e-active']); } break; } } }; /** * Click the button element * its native method * * @public * @returns {void} */ Button.prototype.click = function () { this.element.click(); }; /** * Sets the focus to Button * its native method * * @public * @returns {void} */ Button.prototype.focusIn = function () { this.element.focus(); }; __decorate([ Property('Left') ], Button.prototype, "iconPosition", void 0); __decorate([ Property('') ], Button.prototype, "iconCss", void 0); __decorate([ Property(false) ], Button.prototype, "disabled", void 0); __decorate([ Property(false) ], Button.prototype, "isPrimary", void 0); __decorate([ Property('') ], Button.prototype, "cssClass", void 0); __decorate([ Property('') ], Button.prototype, "content", void 0); __decorate([ Property(false) ], Button.prototype, "isToggle", void 0); __decorate([ Property() ], Button.prototype, "locale", void 0); __decorate([ Property(true) ], Button.prototype, "enableHtmlSanitizer", void 0); __decorate([ Event() ], Button.prototype, "created", void 0); Button = __decorate([ NotifyPropertyChanges ], Button); return Button; }(Component)); var __extends$1 = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate$1 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var CHECK = 'e-check'; var DISABLED = 'e-checkbox-disabled'; var FRAME = 'e-frame'; var INDETERMINATE = 'e-stop'; var LABEL = 'e-label'; var RIPPLE = 'e-ripple-container'; var RIPPLECHECK = 'e-ripple-check'; var RIPPLEINDETERMINATE = 'e-ripple-stop'; var RTL = 'e-rtl'; var WRAPPER = 'e-checkbox-wrapper'; var containerAttr = ['title', 'class', 'style', 'disabled', 'readonly', 'name', 'value', 'id', 'tabindex', 'aria-label', 'required']; /** * The CheckBox is a graphical user interface element that allows you to select one or more options from the choices. * It contains checked, unchecked, and indeterminate states. * ```html * <input type="checkbox" id="checkbox"/> * <script> * var checkboxObj = new CheckBox({ label: "Default" }); * checkboxObj.appendTo("#checkbox"); * </script> * ``` */ var CheckBox = /** @__PURE__ @class */ (function (_super) { __extends$1(CheckBox, _super); /** * Constructor for creating the widget * * @private * @param {CheckBoxModel} options - Specifies checkbox model * @param {string | HTMLInputElement} element - Specifies target element */ function CheckBox(options, element) { var _this = _super.call(this, options, element) || this; _this.isFocused = false; _this.isMouseClick = false; _this.clickTriggered = false; _this.validCheck = true; _this.type = 'checkbox'; return _this; } CheckBox.prototype.changeState = function (state, isInitialize, isInterAction) { var wrapper = this.getWrapper(); var rippleSpan = null; var frameSpan = null; if (wrapper) { frameSpan = wrapper.getElementsByClassName(FRAME)[0]; if (isRippleEnabled) { rippleSpan = wrapper.getElementsByClassName(RIPPLE)[0]; } } if (state === 'check') { if (frameSpan) { frameSpan.classList.remove(INDETERMINATE); frameSpan.classList.add(CHECK); } if (rippleSpan) { rippleSpan.classList.remove(RIPPLEINDETERMINATE); rippleSpan.classList.add(RIPPLECHECK); } this.element.checked = true; if ((this.element.required || closest(this.element, 'form') && closest(this.element, 'form').classList.contains('e-formvalidator')) && this.validCheck && !isInitialize && isInterAction) { this.element.checked = false; this.validCheck = false; } else if (this.element.required || closest(this.element, 'form') && closest(this.element, 'form').classList.contains('e-formvalidator')) { this.validCheck = true; } } else if (state === 'uncheck') { if (frameSpan) { removeClass([frameSpan], [CHECK, INDETERMINATE]); } if (rippleSpan) { removeClass([rippleSpan], [RIPPLECHECK, RIPPLEINDETERMINATE]); } this.element.checked = false; if ((this.element.required || closest(this.element, 'form') && closest(this.element, 'form').classList.contains('e-formvalidator')) && this.validCheck && !isInitialize && isInterAction) { this.element.checked = true; this.validCheck = false; } else if (this.element.required || closest(this.element, 'form') && closest(this.element, 'form').classList.contains('e-formvalidator')) { this.validCheck = true; } } else { if (frameSpan) { frameSpan.classList.remove(CHECK); frameSpan.classList.add(INDETERMINATE); } if (rippleSpan) { rippleSpan.classList.remove(RIPPLECHECK); rippleSpan.classList.add(RIPPLEINDETERMINATE); } this.element.indeterminate = true; this.indeterminate = true; } }; CheckBox.prototype.clickHandler = function (event) { if (event.target.tagName === 'INPUT' && this.clickTriggered) { this.changeState(this.checked ? 'check' : 'uncheck'); this.clickTriggered = false; return; } if (event.target.tagName === 'SPAN' || event.target.tagName === 'LABEL' || closest(event.target, '.e-label')) { this.clickTriggered = true; } if (this.isMouseClick) { this.focusOutHandler(); this.isMouseClick = false; } if (this.indeterminate) { this.changeState(this.checked ? 'check' : 'uncheck', false, true); this.indeterminate = false; this.element.indeterminate = false; } else if (this.checked) { this.changeState('uncheck', false, true); this.checked = false; } else { this.changeState('check', false, true); this.checked = true; } var changeEventArgs = { checked: this.updateVueArrayModel(false), event: event }; this.trigger('change', changeEventArgs); event.stopPropagation(); }; /** * Destroys the widget. * * @returns {void} */ CheckBox.prototype.destroy = function () { var _this = this; var wrapper = this.getWrapper(); _super.prototype.destroy.call(this); if (this.wrapper) { wrapper = this.wrapper; if (!this.disabled) { this.unWireEvents(); } if (this.tagName === 'INPUT') { if (this.getWrapper() && wrapper.parentNode) { wrapper.parentNode.insertBefore(this.element, wrapper); } detach(wrapper); this.element.checked = false; if (this.indeterminate) { this.element.indeterminate = false; } ['name', 'value', 'disabled'].forEach(function (key) { _this.element.removeAttribute(key); }); } else { ['class'].forEach(function (key) { wrapper.removeAttribute(key); }); wrapper.innerHTML = ''; this.element = wrapper; if (this.refreshing) { ['e-control', 'e-checkbox', 'e-lib'].forEach(function (key) { _this.element.classList.add(key); }); setValue('ej2_instances', [this], this.element); } } } }; CheckBox.prototype.focusHandler = function () { this.isFocused = true; }; CheckBox.prototype.focusOutHandler = function () { var wrapper = this.getWrapper(); if (wrapper) { wrapper.classList.remove('e-focus'); } this.isFocused = false; }; /** * Gets the module name. * * @private * @returns {string} - Module Name */ CheckBox.prototype.getModuleName = function () { return 'checkbox'; }; /** * Gets the properties to be maintained in the persistence state. * * @private * @returns {string} - Persist Data */ CheckBox.prototype.getPersistData = function () { return this.addOnPersist(['checked', 'indeterminate']); }; CheckBox.prototype.getWrapper = function () { if (this.element && this.element.parentElement) { return this.element.parentElement.parentElement; } else { return null; } }; CheckBox.prototype.getLabel = function () { if (this.element) { return this.element.parentElement; } else { return null; } }; CheckBox.prototype.initialize = function () { if (isNullOrUndefined(this.initialCheckedValue)) { this.initialCheckedValue = this.checked; } if (this.name) { this.element.setAttribute('name', this.name); } this.element.setAttribute('tabindex', '0'); if (this.value) { this.element.setAttribute('value', this.value); if (this.isVue && typeof this.value === 'boolean' && this.value === true) { this.setProperties({ 'checked': true }, true); } } if (this.checked) { this.changeState('check', true); } if (this.indeterminate) { this.changeState(); } if (this.disabled) { this.setDisabled(); } }; CheckBox.prototype.initWrapper = function () { var wrapper = this.element.parentElement; if (!wrapper.classList.contains(WRAPPER)) { wrapper = this.createElement('div', { className: WRAPPER }); if (this.element.parentNode) { this.element.parentNode.insertBefore(wrapper, this.element); } } var label = this.createElement('label', { attrs: { for: this.htmlAttributes.id ? this.htmlAttributes.id : this.element.id } }); var frameSpan = this.createElement('span', { className: 'e-icons ' + FRAME }); wrapper.classList.add('e-wrapper'); if (this.enableRtl) { wrapper.classList.add(RTL); } if (this.cssClass) { addClass([wrapper], this.cssClass.replace(/\s+/g, ' ').trim().split(' ')); } wrapper.appendChild(label); label.appendChild(this.element); setHiddenInput(this, label); label.appendChild(frameSpan); if (isRippleEnabled) { var rippleSpan = this.createElement('span', { className: RIPPLE }); if (this.labelPosition === 'Before') { label.appendChild(rippleSpan); } else { label.insertBefore(rippleSpan, frameSpan); } rippleEffect(rippleSpan, { duration: 400, isCenterRipple: true }); } if (this.label) { this.setText(this.label); } }; CheckBox.prototype.keyUpHandler = function () { if (this.isFocused) { this.getWrapper().classList.add('e-focus'); } }; CheckBox.prototype.labelMouseDownHandler = function (e) { this.isMouseClick = true; var rippleSpan = this.getWrapper().getElementsByClassName(RIPPLE)[0]; rippleMouseHandler(e, rippleSpan); }; CheckBox.prototype.labelMouseLeaveHandler = function (e) { var rippleSpan = this.getLabel().getElementsByClassName(RIPPLE)[0]; if (rippleSpan) { var rippleElem = rippleSpan.querySelectorAll('.e-ripple-element'); for (var i = rippleElem.length - 1; i > 0; i--) { rippleSpan.removeChild(rippleSpan.childNodes[i]); } rippleMouseHandler(e, rippleSpan); } }; CheckBox.prototype.labelMouseUpHandler = function (e) { this.isMouseClick = true; var rippleSpan = this.getWrapper().getElementsByClassName(RIPPLE)[0]; if (rippleSpan) { var rippleElem = rippleSpan.querySelectorAll('.e-ripple-element'); for (var i = 0; i < rippleElem.length - 1; i++) { rippleSpan.removeChild(rippleSpan.childNodes[i]); } rippleMouseHandler(e, rippleSpan); } }; /** * Called internally if any of the property value changes. * * @private * @param {CheckBoxModel} newProp - Specifies new Properties * @param {CheckBoxModel} oldProp - Specifies old Properties * * @returns {void} */ CheckBox.prototype.onPropertyChanged = function (newProp, oldProp) { var wrapper = this.getWrapper(); for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) { var prop = _a[_i]; switch (prop) { case 'checked': this.indeterminate = false; this.element.indeterminate = false; this.changeState(newProp.checked ? 'check' : 'uncheck'); break; case 'indeterminate': if (newProp.indeterminate) { this.changeState(); } else { this.element.indeterminate = false; this.changeState(this.checked ? 'check' : 'uncheck'); } break; case 'disabled': if (newProp.disabled) { this.setDisabled(); this.wrapper = this.getWrapper(); this.unWireEvents(); } else { this.element.disabled = false; wrapper.classList.remove(DISABLED); wrapper.setAttribute('aria-disabled', 'false'); this.wireEvents(); } break; case 'cssClass': if (oldProp.cssClass) { removeClass([wrapper], oldProp.cssClass.split(/\s+/).filter(function (c) { return c.length > 0; })); } if (newProp.cssClass) { addClass([wrapper], newProp.cssClass.replace(/\s+/g, ' ').trim().split(' ')); } break; case 'enableRtl': if (newProp.enableRtl) { wrapper.classList.add(RTL); } else { wrapper.classList.remove(RTL); } break; case 'label': this.setText(newProp.label); break; case 'labelPosition': { var label = wrapper.getElementsByClassName(LABEL)[0]; var labelWrap = wrapper.getElementsByTagName('label')[0]; detach(label); if (newProp.labelPosition === 'After') { labelWrap.appendChild(label); } else { labelWrap.insertBefore(label, wrapper.getElementsByClassName(FRAME)[0]); } break; } case 'name': this.element.setAttribute('name', newProp.name); break; case 'value': if (this.isVue && typeof newProp.value === 'object') { break; } this.element.setAttribute('value', newProp.value); break; case 'htmlAttributes': this.updateHtmlAttributeToWrapper(); break; } } }; /** * Initialize Angular, React and Unique ID support. * * @private * @returns {void} */ CheckBox.prototype.preRender = function () { var element = this.element; this.tagName = this.element.tagName; element = wrapperInitialize(this.createElement, 'EJS-CHECKBOX', 'checkbox', element, WRAPPER, 'checkbox'); this.element = element; if (this.element.getAttribute('type') !== 'checkbox') { this.element.setAttribute('type', 'checkbox'); } if (!this.element.id) { this.element.id = getUniqueID('e-' + this.getModuleName()); } }; /** * Initialize the control rendering. * * @private * @returns {void} */ CheckBox.prototype.render = function () { this.initWrapper(); this.initialize(); if (!this.disabled) { this.wireEvents(); } this.updateHtmlAttributeToWrapper(); this.updateVueArrayModel(true); this.renderComplete(); this.wrapper = this.getWrapper(); }; CheckBox.prototype.setDisabled = function () { var wrapper = this.getWrapper(); this.element.disabled = true; wrapper.classList.add(DISABLED); wrapper.setAttribute('aria-disabled', 'true'); }; CheckBox.prototype.setText = function (text) { var wrapper = this.getWrapper(); if (!wrapper) { return; } var label = wrapper.getElementsByClassName(LABEL)[0]; if (label) { label.innerHTML = (this.enableHtmlSanitizer) ? SanitizeHtmlHelper.sanitize(text) : text; } else { text = (this.enableHtmlSanitizer) ? SanitizeHtmlHelper.sanitize(text) : text; label = this.createElement('span', { className: LABEL, innerHTML: text }); var labelWrap = wrapper.getElementsByTagName('label')[0]; if (this.labelPosition === 'Before') { labelWrap.insertBefore(label, wrapper.getElementsByClassName(FRAME)[0]); } else { labelWrap.appendChild(label); } } }; CheckBox.prototype.changeHandler = function (e) { e.stopPropagation(); }; CheckBox.prototype.formResetHandler = function () { this.checked = this.initialCheckedValue; this.element.checked = this.initialCheckedValue; }; CheckBox.prototype.unWireEvents = function () { var wrapper = this.wrapper; EventHandler.remove(this.element, 'keyup', this.keyUpHandler); EventHandler.remove(this.element, 'focus', this.focusHandler); EventHandler.remove(this.element, 'focusout', this.focusOutHandler); if (wrapper) { EventHandler.remove(wrapper, 'click', this.clickHandler); var label = wrapper.getElementsByTagName('label')[0]; if (label) { EventHandler.remove(label, 'mousedown', this.labelMouseDownHandler); EventHandler.remove(label, 'mouseup', this.labelMouseUpHandler); EventHandler.remove(label, 'mouseleave', this.labelMouseLeaveHandler); } } var formElem = closest(this.element, 'form'); if (formElem) { EventHandler.remove(formElem, 'reset', this.formResetHandler); } if (this.tagName === 'EJS-CHECKBOX') { EventHandler.remove(this.element, 'change', this.changeHandler); } }; CheckBox.prototype.wireEvents = function () { var wrapper = this.getWrapper(); EventHandler.add(wrapper, 'click', this.clickHandler, this); EventHandler.add(this.element, 'keyup', this.keyUpHandler, this); EventHandler.add(this.element, 'focus', this.focusHandler, this); EventHandler.add(this.element, 'focusout', this.focusOutHandler, this); var label = wrapper.getElementsByTagName('label')[0]; EventHandler.add(label, 'mousedown', this.labelMouseDownHandler, this); EventHandler.add(label, 'mouseup', this.labelMouseUpHandler, this); EventHandler.add(label, 'mouseleave', this.labelMouseLeaveHandler, this); var formElem = closest(this.element, 'form'); if (formElem) { EventHandler.add(formElem, 'reset', this.formResetHandler, this); } if (this.tagName === 'EJS-CHECKBOX') { EventHandler.add(this.element, 'change', this.changeHandler, this); } }; CheckBox.prototype.updateVueArrayModel = function (init) { if (this.isVue && typeof this.value === 'object') { var value = this.element.value; if (value && this.value) { if (init) { for (var i = 0; i < this.value.length; i++) { if (value === this.value[i]) { this.changeState('check'); this.setProperties({ 'checked': true }, true); } } } else { var index = this.value.indexOf(value); if (this.checked) { if (index < 0) { this.value.push(value); } } else { if (index > -1) { this.value.splice(index, 1); } } // eslint-disable-next-line @typescript-eslint/no-explicit-any return this.value; } } } return this.validCheck ? this.element.checked : !this.element.checked; }; CheckBox.prototype.updateHtmlAttributeToWrapper = function () { if (!isNullOrUndefined(this.htmlAttributes)) { for (var _i = 0, _a = Object.keys(this.htmlAttributes); _i < _a.length; _i++) { var key = _a[_i]; var wrapper = this.getWrapper(); if (containerAttr.indexOf(key) > -1) { if (key === 'class') { addClass([wrapper], this.htmlAttributes["" + key].split(' ')); } else if (key === 'title') { wrapper.setAttribute(key, this.htmlAttributes["" + key]); } else if (key === 'style') { var frameSpan = this.getWrapper().getElementsByClassName(FRAME)[0]; frameSpan.setAttribute(key, this.htmlAttributes["" + key]); } else if (key === 'disabled') { if (this.htmlAttributes["" + key] === 'true') { this.setDisabled(); } this.element.setAttribute(key, this.htmlAttributes["" + key]); } else { this.element.setAttribute(key, this.htmlAttributes["" + key]); } } else { wrapper.setAttribute(key, this.htmlAttributes["" + key]); } } } }; /** * Click the CheckBox element * its native method * * @public * @returns {void} */ CheckBox.prototype.click = function () { this.element.click(); }; /** * Sets the focus to CheckBox * its native method * * @public * @returns {void} */ CheckBox.prototype.focusIn = function () { this.element.focus(); }; __decorate$1([ Event() ], CheckBox.prototype, "change", void 0); __decorate$1([ Event() ], CheckBox.prototype, "created", void 0); __decorate$1([ Property(false) ], CheckBox.prototype, "checked", void 0); __decorate$1([ Property('') ], CheckBox.prototype, "cssClass", void 0); __decorate$1([ Property(false) ], CheckBox.prototype, "disabled", void 0); __decorate$1([ Property(false) ], CheckBox.prototype, "indeterminate", void 0); __decorate$1([ Property('') ], CheckBox.prototype, "label", void 0); __decorate$1([ Property('After') ], CheckBox.prototype, "labelPosition", void 0); __decorate$1([ Property('') ], CheckBox.prototype, "name", void 0); __decorate$1([ Property('') ], CheckBox.prototype, "value", void 0); __decorate$1([ Property(true) ], CheckBox.prototype, "enableHtmlSanitizer", void 0); __decorate$1([ Property({}) ], CheckBox.prototype, "htmlAttributes", void 0); CheckBox = __decorate$1([ NotifyPropertyChanges ], CheckBox); return CheckBox; }(Component)); var __extends$2 = (undefined && undefined.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); var __decorate$2 = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var LABEL$1 = 'e-label'; var RIPPLE$1 = 'e-ripple-container'; var RTL$1 = 'e-rtl'; var WRAPPER$1 = 'e-radio-wrapper'; var ATTRIBUTES = ['title', 'class', 'style', 'disabled', 'readonly', 'name', 'value', 'id']; /** * The RadioButton is a graphical user interface element that allows you to select one option from the choices. * It contains checked and unchecked states. * ```html * <input type="radio" id="radio"/> * <script> * var radioObj = new RadioButton({ label: "Default" }); * radioObj.appendTo("#radio"); * </script> * ``` */ var RadioButton = /** @__PURE__ @class */ (function (_super) { __extends$2(RadioButton, _super); /** * Constructor for creating the widget * * @private * @param {RadioButtonModel} options - Specifies Radio button model * @param {string | HTMLInputElement} element - Specifies target element */ function RadioButton(options, element) { var _this = _super.call(this, options, element) || this; _this.isFocused = false; _this.type = 'radio'; return _this; } RadioButton_1 = RadioButton; RadioButton.prototype.changeHandler = function (event) { this.checked = true; this.dataBind(); var value = this.element.getAttribute('value'); value = this.isVue && value ? this.element.value : this.value; var type = typeof this.value; if (this.isVue && type === 'boolean') { value = value === 'true' ? true : false; } this.trigger('change', { value: value, event: event }); // eslint-disable-next-line @typescript-eslint/no-explicit-any if (this.isAngular) { event.stopPropagation(); } }; RadioButton.prototype.updateChange = function () { var input; var instance; var radioGrp = this.getRadioGroup(); for (var i = 0; i < radioGrp.length; i++) { input = radioGrp[i]; if (input !== this.element) { instance = getInstance(input, RadioButton_1); instance.checked = false; if (this.tagName === 'EJS-RADIOBUTTON') { instance.angularValue = this.value; } } } }; /** * Destroys the widget. * * @returns {void} */ RadioButton.prototype.destroy = function () { var _this = this; var radioWrap = this.wrapper; _super.prototype.destroy.call(this); if (radioWrap) { if (!this.disabled) { this.unWireEvents(); } if (this.tagName === 'INPUT') { if (radioWrap.parentNode) { radioWrap.parentNode.insertBefore(this.element, radioWrap); } detach(radioWrap); this.element.checked = false; ['name', 'value', 'disabled'].forEach(function (key) { _this.element.removeAttribute(key); }); } else { ['role', 'aria-checked', 'class'].forEach(function (key) { radioWrap.removeAttribute(key); }); radioWrap.innerHTML = ''; this.element = this.wrapper; if (this.refreshing) { ['e-control', 'e-radio', 'e-lib'].forEach(function (key) { _this.element.classList.add(key); }); setValue('ej2_instances', [this], this.element); } } } }; RadioButton.prototype.focusHandler = function () { this.isFocused = true; }; RadioButton.prototype.focusOutHandler = function () { var label = this.getLabel(); if (label) { label.classList.remove('e-focus'); } }; RadioButton.prototype.getModuleName = function () { return 'radio'; }; /** * To get the value of selected radio button in a group. * * @method getSelectedValue * @returns {string} - Selected Value */ RadioButton.prototype.getSelectedValue = fun