123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
'use strict';
modulum('EditWidget', ['FieldWidgetBase', 'WidgetFactory'],
function(context, cls) {
cls.EditWidget = context.oo.Class(cls.FieldWidgetBase, function($super) {
return {
__name: 'EditWidget',
_completerCurrentChildrenChangeHandler: null,
__dataContentPlaceholderSelector: cls.WidgetBase.selfDataContent,
_completerWidget: null,
_inputType: 'text',
_maxLength: -1,
_displayFormat: null,
_title: null,
_canShowVirtualKeyboard: null,
_showVirtualKeyboardOnFocus: null,
_virtualKeyboardDisplayedHandler: null,
_realReadOnly: false,
_initLayout: function() {
if (!this._ignoreLayout) {
this._layoutInformation = new cls.LayoutInformation(this);
this._layoutEngine = new cls.LeafLayoutEngine(this);
this._layoutInformation._fixedSizePolicyForceMeasure = true;
this._layoutInformation.setSingleLineContentOnly(true);
}
},
_initElement: function() {
$super._initElement.call(this);
this._inputElement = this._element.getElementsByTagName('input')[0];
this._inputElement.on('blur.EditWidget', this._onBlur.bind(this));
this._inputElement.on('input.EditWidget', this._onInput.bind(this));
this._inputElement.on('mousedown.EditWidget', cls.WidgetBase._onSelect.bind(this));
this._canShowVirtualKeyboard = true;
this._realReadOnly = false;
this._notEditable = false;
if (context.__wrapper.isGMA()) {
this._virtualKeyboardDisplayedHandler = this._appWidget.when(context.constants.widgetEvents.virtualKeyboardDisplayed,
function() {
this._canShowVirtualKeyboard = true;
}.bind(this));
}
this._showVirtualKeyboardOnFocus = false;
},
destroy: function() {
if (this._inputElement) {
this._inputElement.off('input.EditWidget');
this._inputElement.off('mousedown.EditWidget');
this._inputElement.off('blur.EditWidget');
this._inputElement.remove();
this._inputElement = null;
}
if (this._completerWidget) {
this._completerWidget.destroy();
this._completerWidget = null;
if (this._completerCurrentChildrenChangeHandler) {
this._completerCurrentChildrenChangeHandler();
this._completerCurrentChildrenChangeHandler = null;
}
}
if (this._virtualKeyboardDisplayedHandler) {
this._virtualKeyboardDisplayedHandler();
this._virtualKeyboardDisplayedHandler = null;
}
$super.destroy.call(this);
},
manageMouseClick: function(domEvent) {
if (this.getApplicationWidget() && context.__wrapper.isGMA()) {
this._setInputReadOnly(this._realReadOnly || this._notEditable || !this._enabled);
this.getApplicationWidget().virtualKeyboardIsDisplayed();
}
this._elementState.backupCursorPos(this._inputElement);
this._onRequestFocus(domEvent);
return true;
},
managePriorityKeyDown: function(keyString, domKeyEvent, repeat) {
var keyProcessed = false;
if (this.hasCompleter()) {
keyProcessed = this.getCompleterWidget().managePriorityKeyDown(keyString, domKeyEvent, repeat);
}
this._updateCapsLockWarning();
if (keyProcessed) {
return true;
} else {
return $super.managePriorityKeyDown.call(this, keyString, domKeyEvent, repeat);
}
},
_checkValue: function(text, newTextPart) {
if (this._dialogType !== 'Input' && this._dialogType !== 'InputArray') {
return newTextPart;
}
if (!this._scroll || this._dataTypeWithNoScroll) {
newTextPart = this.checkValueDisplayWidth(text, newTextPart);
}
if (this._maxLength > 0) {
if (this.getUserInterfaceWidget().isCharLengthSemantics()) {
newTextPart = newTextPart.substr(0, this._maxLength - text.length);
} else {
newTextPart = this.checkValueByteCount(text, newTextPart, this._maxLength);
}
}
return newTextPart;
},
canAutoNext: function() {
var text = this.getValue();
if (!this._scroll || this._dataTypeWithNoScroll) {
var displayWidth = this._vmWidth;
var maxLength = this.getUserInterfaceWidget().isCharLengthSemantics() ? this._maxLength : -1;
var codepoints = Array.from(text);
if (text.displayWidth() >= displayWidth || (maxLength > 0 && codepoints.length >= maxLength)) {
return true;
}
}
if (this._maxLength > 0) {
if (!this.getUserInterfaceWidget().isCharLengthSemantics()) {
return text.countBytes() >= this._maxLength;
}
return text.length >= this._maxLength;
}
return false;
},
manageKeyDown: function(keyString, domKeyEvent, repeat) {
var widgetText = this.getValue();
var res = this._checkValue('', widgetText.removeUnknownChar());
if (res === widgetText) {
this._elementState.backup(this._inputElement);
}
return $super.manageKeyDown.call(this, keyString, domKeyEvent);
},
manageKeyUp: function(keyString, domKeyEvent) {
$super.manageKeyUp.call(this, keyString, domKeyEvent);
this._verifyWidgetValue(domKeyEvent);
this.emit(context.constants.widgetEvents.keyUp, domKeyEvent, true);
},
_verifyWidgetValue: function(domEvent) {
var widgetText = this._elementState.getValue();
var inputValue = this._inputElement.value;
if (widgetText.length >= inputValue.length) {
if (!this.isPictureDefined() || !window.isAndroid()) {
this._elementState.backup(this._inputElement);
}
return;
} else {
var newPart = this._elementState.newPart(inputValue);
var res = '';
if (!window.isMobile() && domEvent.isComposing) {
return;
} else {
res = this._checkValue(widgetText, newPart.removeUnknownChar());
}
if (res !== newPart) {
this._elementState.restore(this._inputElement, res);
this.setValue(this._inputElement.value);
this._elementState.setRestored(true);
return;
}
}
},
_onInput: function(event) {
$super._onInput.call(this);
if (this._completerWidget &&
this.getValue() &&
this.getValue() === this._oldValue &&
!this._completerWidget.isVisible()) {
this._completerWidget.show();
}
if (!this._processingKeyEvent) {
this._verifyWidgetValue(event);
if (!this._elementState.isRestored()) {
this.emit(context.constants.widgetEvents.keyUp, event, true);
}
}
if (!window.browserInfo.isIE || this.getValue() !== this._oldValue || this.hasVMFocus()) {
this.emit(context.constants.widgetEvents.change, true);
}
},
_onBlur: function(event) {
this.emit(context.constants.widgetEvents.blur, event);
},
setReadOnly: function(readonly) {
this._realReadOnly = readonly;
if (this._isReadOnly !== readonly) {
$super.setReadOnly.call(this, readonly);
this._setInputReadOnly(this._realReadOnly || this._notEditable || !this._enabled);
}
},
_setInputReadOnly: function(readonly) {
this._setElementAttribute('readonly', readonly ? 'readonly' : null, "_inputElement");
},
setMaxLength: function(maxlength) {
if (maxlength) {
this._maxLength = maxlength;
this._setElementAttribute('maxlength', maxlength + 1, "_inputElement");
}
},
getMaxLength: function() {
return this._maxLength;
},
setScroll: function(scroll) {
var scrollModified = this._scroll !== scroll;
this._scroll = scroll;
if (scrollModified && !this._scroll && this._dataTypeWithValueCheck && this._maxLength > 0) {
var widgetText = this.getValue();
var res = this._checkValue("", widgetText);
if (res !== widgetText) {
this.setValue(res);
}
}
},
setTextAlign: function(align) {
this._textAlign = align;
this.setStyle('>input', {
'text-align': align
});
this.setStyle('>input:focus', {
'text-align': align
});
},
setCols: function(cols) {
this._inputElement.setAttribute('size', cols);
},
getTextAlign: function() {
return this.getStyle('>input', 'text-align');
},
isNumber: function() {
var regex = /SMALLINT|INTEGER|BIGINT|INT|DECIMAL|MONEY|SMALLFLOAT|FLOAT/g;
var match = regex.exec(this.getDisplayFormat());
return Boolean(match);
},
getDisplayFormat: function() {
return this._displayFormat;
},
setDisplayFormat: function(format) {
this._displayFormat = format;
},
traditionalDisplay: function(letterSpacing, fieldHeight, heightPadding) {
var layoutInfo = this.getLayoutInformation();
if (layoutInfo) {
var left = layoutInfo.getGridX() - 1;
var top = (layoutInfo.getGridY()) * (fieldHeight + 2 * heightPadding) + heightPadding;
var width = layoutInfo.getGridWidth() + 1;
var height = layoutInfo.getGridHeight() * fieldHeight;
var style = this._element.parentElement.style;
layoutInfo.getHostElement().toggleClass(layoutInfo.className, true);
style.left = 'calc(' + left + 'ch + 1ch / 2 + ' + left + ' * ' + letterSpacing + ')';
style.top = top + 'px';
style.width = 'calc(' + width + 'ch + ' + width + ' * ' + letterSpacing + ')';
style.height = height + 'px';
}
},
setValue: function(value, fromVM, cursorPosition) {
$super.setValue.call(this, value, fromVM, cursorPosition);
if (window.browserInfo.isSafari) {
this._inputElement.value = value + ' ';
}
this._inputElement.value = value;
if (cursorPosition && this.isEnabled()) {
this._inputElement.selectionStart = this._inputElement.selectionEnd = cursorPosition;
}
this._elementState.backup(this._inputElement);
},
getValue: function() {
if (this._inputElement) {
var result = this._inputElement.value;
if (this.isEditing()) {
if (this.getTextTransform() === 'up') {
result = result.toLocaleUpperCase();
}
if (this.getTextTransform() === 'down') {
result = result.toLocaleLowerCase();
}
} else if (this._editingTime === 0 && this._vmValue) {
result = this._valueStack[this._valueStackCursor];
}
return result;
}
return null;
},
setCursors: function(cursor, cursor2) {
if (!cursor2) {
cursor2 = cursor;
}
if (cursor2 && cursor2 < 0) {
cursor2 = this.getValue() && this.getValue().length || 0;
}
if (this.isInTable() && !this.isEnabled()) {
cursor = cursor2 = 0;
}
this._inputElement.setCursorPosition(cursor, cursor2);
this._elementState.backupCursorPos(this._inputElement);
},
getCursors: function() {
var cursors = {
start: 0,
end: 0
};
if (this._inputElement && this._inputElement.value) {
try {
cursors.start = this._inputElement.selectionStart;
cursors.end = this._inputElement.selectionEnd;
} catch (ignore) {
}
}
return cursors;
},
setIsPassword: function(isPassword) {
if (isPassword) {
this._inputElement.setAttribute('type', 'password');
} else {
this._inputElement.setAttribute('type', 'text');
this.setType(this._inputType);
}
this.toggleClass("gbc_isPassword", Boolean(isPassword));
},
isPassword: function() {
return this._inputElement.getAttribute('type') === 'password';
},
_updateCapsLockWarning: function() {
if (this.isPassword()) {
if (!window.browserInfo.isSafari) {
this.removeClass("capsOn");
if (window._capsLock) {
this.addClass("capsOn");
}
}
}
},
_isMaxLength: function() {
return this._maxLength !== -1 && this._inputElement.value.length >= this._maxLength &&
this._inputElement.selectionStart === this._inputElement.selectionEnd;
},
setType: function(valType) {
this._inputType = valType;
if (!this.isPassword()) {
this._inputElement.setAttribute('type', valType);
if (window.browserInfo.isFirefox) {
this._inputElement.setAttribute('step', valType === 'number' ? 'any' : null);
}
}
},
getType: function() {
return this._inputType;
},
setFocus: function(fromMouse) {
$super.setFocus.call(this, fromMouse);
if (context.__wrapper.isGMA() && !this._showVirtualKeyboardOnFocus) {
if (this._canShowVirtualKeyboard) {
this._setInputReadOnly(this._realReadOnly || this._notEditable || !this._enabled);
} else {
this._setInputReadOnly(true);
}
}
this._inputElement.domFocus();
this._updateCapsLockWarning();
if (this.isFakePlaceholder() && this._title &&
((window.isMobile() && context.ThemeService.getValue("gbc-Edit-mobile-comment-to-placeholder") === '1') ||
(!window.isMobile() && context.ThemeService.getValue("gbc-Edit-desktop-comment-to-placeholder") === '1'))) {
this.setPlaceHolder(this._title, true);
}
},
setDialogType: function(dialogType) {
var oldDialogType = this._dialogType;
$super.setDialogType.call(this, dialogType);
if (dialogType === 'Display' || dialogType === 'DisplayArray') {
return;
}
this._canShowVirtualKeyboard = oldDialogType === this._dialogType;
},
loseVMFocus: function() {
$super.loseVMFocus.call(this);
if (this.isFakePlaceholder()) {
this.setPlaceHolder("", true);
}
},
loseFocus: function() {
$super.loseFocus.call(this);
if (this.isFakePlaceholder()) {
this.setPlaceHolder("", true);
}
if (this._completerWidget) {
this._completerWidget.loseFocus();
}
},
hasCompleter: function() {
return this.getCompleterWidget() !== null;
},
getCompleterWidget: function() {
return this._completerWidget;
},
addCompleterWidget: function() {
if (!this._completerWidget) {
this._completerWidget = cls.WidgetFactory.createWidget('Completer', this.getBuildParameters());
this._completerWidget.addCompleterWidget(this);
this._completerCurrentChildrenChangeHandler = this._completerWidget.onCurrentChildrenChange(function(value) {
this.setEditing(this._oldValue !== value);
this.setValue(value);
}.bind(this));
}
},
setEnabled: function(enabled) {
var oldEnabled = this._enabled;
var bEnabled = Boolean(enabled);
if (this._dialogType !== 'Display' && this._dialogType !== 'DisplayArray' && this._dialogType !== false) {
this._canShowVirtualKeyboard = !enabled || oldEnabled === bEnabled;
}
$super.setEnabled.call(this, enabled);
if (this._textAlign) {
this.setTextAlign(this._textAlign);
}
},
setTitle: function(title) {
$super.setTitle.call(this, title);
if (title === "") {
this._inputElement.removeAttribute("aria-label");
this._title = null;
} else {
this._inputElement.setAttribute("aria-label", title);
this._title = title;
}
},
setShowVirtualKeyboardOnFocus: function(show) {
this._showVirtualKeyboardOnFocus = show;
},
getClipboardAuthorizedAction: function() {
if (this._parentWidget instanceof cls.ComboBoxWidget) {
return this._parentWidget.getClipboardAuthorizedAction();
}
return $super.getClipboardAuthorizedAction.call(this);
}
};
});
cls.WidgetFactory.registerBuilder('Edit', cls.EditWidget);
cls.WidgetFactory.registerBuilder('EditWidget', cls.EditWidget);
});