| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- /**
- * DevExtreme (ui/editor/editor.js)
- * Version: 19.1.16
- * Build date: Tue Oct 18 2022
- *
- * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
- * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
- */
- "use strict";
- var _string = require("../../core/utils/string");
- var $ = require("../../core/renderer");
- var dataUtils = require("../../core/element_data");
- var Callbacks = require("../../core/utils/callbacks");
- var commonUtils = require("../../core/utils/common");
- var windowUtils = require("../../core/utils/window");
- var Guid = require("../../core/guid");
- var getDefaultAlignment = require("../../core/utils/position").getDefaultAlignment;
- var extend = require("../../core/utils/extend").extend;
- var Widget = require("../widget/ui.widget");
- var ValidationMixin = require("../validation/validation_mixin");
- var Overlay = require("../overlay");
- var EventsEngine = require("../../events/core/events_engine");
- var eventUtils = require("../../events/utils");
- var READONLY_STATE_CLASS = "dx-state-readonly";
- var INVALID_CLASS = "dx-invalid";
- var INVALID_MESSAGE = "dx-invalid-message";
- var INVALID_MESSAGE_CONTENT = "dx-invalid-message-content";
- var INVALID_MESSAGE_AUTO = "dx-invalid-message-auto";
- var INVALID_MESSAGE_ALWAYS = "dx-invalid-message-always";
- var VALIDATION_TARGET = "dx-validation-target";
- var VALIDATION_MESSAGE_MIN_WIDTH = 100;
- var READONLY_NAMESPACE = "editorReadOnly";
- var Editor = Widget.inherit({
- ctor: function() {
- this.showValidationMessageTimeout = null;
- this.validationRequest = Callbacks();
- this.callBase.apply(this, arguments);
- var $element = this.$element();
- if ($element) {
- dataUtils.data($element[0], VALIDATION_TARGET, this)
- }
- },
- _init: function() {
- this.callBase();
- this._initInnerOptionCache("validationTooltipOptions")
- },
- _getDefaultOptions: function() {
- return extend(this.callBase(), {
- value: null,
- name: "",
- onValueChanged: null,
- readOnly: false,
- isValid: true,
- validationError: null,
- validationMessageMode: "auto",
- validationBoundary: void 0,
- validationMessageOffset: {
- h: 0,
- v: 0
- },
- validationTooltipOptions: {}
- })
- },
- _attachKeyboardEvents: function() {
- if (this.option("readOnly")) {
- return
- }
- this.callBase();
- if (this._keyboardProcessor) {
- this._attachChildKeyboardEvents()
- }
- },
- _attachChildKeyboardEvents: commonUtils.noop,
- _setOptionsByReference: function() {
- this.callBase();
- extend(this._optionsByReference, {
- validationError: true
- })
- },
- _createValueChangeAction: function() {
- this._valueChangeAction = this._createActionByOption("onValueChanged", {
- excludeValidators: ["disabled", "readOnly"]
- })
- },
- _suppressValueChangeAction: function() {
- this._valueChangeActionSuppressed = true
- },
- _resumeValueChangeAction: function() {
- this._valueChangeActionSuppressed = false
- },
- _initMarkup: function() {
- this._toggleReadOnlyState();
- this._setSubmitElementName(this.option("name"));
- this.callBase();
- this._renderValidationState()
- },
- _raiseValueChangeAction: function(value, previousValue) {
- if (!this._valueChangeAction) {
- this._createValueChangeAction()
- }
- this._valueChangeAction(this._valueChangeArgs(value, previousValue))
- },
- _valueChangeArgs: function(value, previousValue) {
- return {
- value: value,
- previousValue: previousValue,
- event: this._valueChangeEventInstance
- }
- },
- _saveValueChangeEvent: function(e) {
- this._valueChangeEventInstance = e
- },
- _focusInHandler: function(e) {
- var _this = this;
- var isValidationMessageShownOnFocus = "auto" === this.option("validationMessageMode");
- if (this._canValueBeChangedByClick() && isValidationMessageShownOnFocus) {
- this._$validationMessage && this._$validationMessage.removeClass(INVALID_MESSAGE_AUTO);
- clearTimeout(this.showValidationMessageTimeout);
- this.showValidationMessageTimeout = setTimeout(function() {
- return _this._$validationMessage && _this._$validationMessage.addClass(INVALID_MESSAGE_AUTO)
- }, 150)
- }
- return this.callBase(e)
- },
- _canValueBeChangedByClick: function() {
- return false
- },
- _renderValidationState: function() {
- var isValid = this.option("isValid");
- var validationError = this.option("validationError");
- var validationMessageMode = this.option("validationMessageMode");
- var $element = this.$element();
- $element.toggleClass(INVALID_CLASS, !isValid);
- this.setAria("invalid", !isValid || void 0);
- if (!windowUtils.hasWindow()) {
- return
- }
- if (this._$validationMessage) {
- this._$validationMessage.remove();
- this.setAria("describedby", null);
- this._$validationMessage = null
- }
- if (!isValid && validationError && validationError.message) {
- this._$validationMessage = $("<div>").addClass(INVALID_MESSAGE).html((0, _string.encodeHtml)(validationError.message)).appendTo($element);
- var validationTarget = this._getValidationMessageTarget();
- this._validationMessage = this._createComponent(this._$validationMessage, Overlay, extend({
- integrationOptions: {},
- templatesRenderAsynchronously: false,
- target: validationTarget,
- shading: false,
- width: "auto",
- height: "auto",
- container: $element,
- position: this._getValidationMessagePosition("below"),
- closeOnOutsideClick: false,
- closeOnTargetScroll: false,
- animation: null,
- visible: true,
- propagateOutsideClick: true,
- _checkParentVisibility: false
- }, this._getInnerOptionsCache("validationTooltipOptions")));
- this._$validationMessage.toggleClass(INVALID_MESSAGE_AUTO, "auto" === validationMessageMode).toggleClass(INVALID_MESSAGE_ALWAYS, "always" === validationMessageMode);
- var messageId = "dx-" + new Guid;
- this._validationMessage.$content().addClass(INVALID_MESSAGE_CONTENT).attr("id", messageId);
- this.setAria("describedby", messageId);
- this._setValidationMessageMaxWidth();
- this._bindInnerWidgetOptions(this._validationMessage, "validationTooltipOptions")
- }
- },
- _setValidationMessageMaxWidth: function() {
- if (!this._validationMessage) {
- return
- }
- if (0 === this._getValidationMessageTarget().outerWidth()) {
- this._validationMessage.option("maxWidth", "100%");
- return
- }
- var validationMessageMaxWidth = Math.max(VALIDATION_MESSAGE_MIN_WIDTH, this._getValidationMessageTarget().outerWidth());
- this._validationMessage.option("maxWidth", validationMessageMaxWidth)
- },
- _getValidationMessageTarget: function() {
- return this.$element()
- },
- _getValidationMessagePosition: function(positionRequest) {
- var rtlEnabled = this.option("rtlEnabled");
- var messagePositionSide = getDefaultAlignment(rtlEnabled);
- var messageOriginalOffset = this.option("validationMessageOffset");
- var messageOffset = {
- h: messageOriginalOffset.h,
- v: messageOriginalOffset.v
- };
- var verticalPositions = "below" === positionRequest ? [" top", " bottom"] : [" bottom", " top"];
- if (rtlEnabled) {
- messageOffset.h = -messageOffset.h
- }
- if ("below" !== positionRequest) {
- messageOffset.v = -messageOffset.v
- }
- return {
- offset: messageOffset,
- boundary: this.option("validationBoundary"),
- my: messagePositionSide + verticalPositions[0],
- at: messagePositionSide + verticalPositions[1],
- collision: "none flip"
- }
- },
- _toggleReadOnlyState: function() {
- var readOnly = this.option("readOnly");
- this._toggleBackspaceHandler(readOnly);
- this.$element().toggleClass(READONLY_STATE_CLASS, !!readOnly);
- this.setAria("readonly", readOnly || void 0)
- },
- _toggleBackspaceHandler: function(isReadOnly) {
- var $eventTarget = this._keyboardEventBindingTarget();
- var eventName = eventUtils.addNamespace("keydown", READONLY_NAMESPACE);
- EventsEngine.off($eventTarget, eventName);
- if (isReadOnly) {
- EventsEngine.on($eventTarget, eventName, function(e) {
- if ("backspace" === eventUtils.normalizeKeyName(e)) {
- e.preventDefault()
- }
- })
- }
- },
- _dispose: function() {
- var element = this.$element()[0];
- dataUtils.data(element, VALIDATION_TARGET, null);
- clearTimeout(this.showValidationMessageTimeout);
- this.callBase()
- },
- _setSubmitElementName: function(name) {
- var $submitElement = this._getSubmitElement();
- if (!$submitElement) {
- return
- }
- if (name.length > 0) {
- $submitElement.attr("name", name)
- } else {
- $submitElement.removeAttr("name")
- }
- },
- _getSubmitElement: function() {
- return null
- },
- _optionChanged: function(args) {
- switch (args.name) {
- case "onValueChanged":
- this._createValueChangeAction();
- break;
- case "isValid":
- case "validationError":
- case "validationBoundary":
- case "validationMessageMode":
- this._renderValidationState();
- break;
- case "validationTooltipOptions":
- this._innerOptionChanged(this._validationMessage, args);
- break;
- case "readOnly":
- this._toggleReadOnlyState();
- this._refreshFocusState();
- break;
- case "value":
- if (args.value != args.previousValue) {
- this.validationRequest.fire({
- value: args.value,
- editor: this
- })
- }
- if (!this._valueChangeActionSuppressed) {
- this._raiseValueChangeAction(args.value, args.previousValue);
- this._saveValueChangeEvent(void 0)
- }
- break;
- case "width":
- this.callBase(args);
- this._setValidationMessageMaxWidth();
- break;
- case "name":
- this._setSubmitElementName(args.value);
- break;
- default:
- this.callBase(args)
- }
- },
- reset: function() {
- var defaultOptions = this._getDefaultOptions();
- this.option("value", defaultOptions.value)
- }
- }).include(ValidationMixin);
- module.exports = Editor;
|