123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
'use strict';
modulum('TextEditWidget', ['FieldWidgetBase', 'WidgetFactory'],
function(context, cls) {
cls.TextEditWidget = context.oo.Class(cls.FieldWidgetBase, function($super) {
return {
__name: 'TextEditWidget',
_hasHTMLContent: false,
_sanitize: null,
_htmlFilter: null,
_wantReturns: true,
_wantTabs: false,
__dataContentPlaceholderSelector: cls.WidgetBase.selfDataContent,
_canShowVirtualKeyboard: null,
_showVirtualKeyboardOnFocus: null,
_virtualKeyboardDisplayedHandler: null,
_realReadOnly: false,
_initLayout: function() {
if (!this._ignoreLayout) {
this._layoutInformation = new cls.LayoutInformation(this);
this._layoutInformation.shouldFillStack = true;
this._layoutEngine = new cls.LeafLayoutEngine(this);
this._layoutEngine._shouldFillHeight = true;
this._layoutInformation.getSizePolicyConfig().dynamic = cls.SizePolicy.Initial();
this._layoutInformation.forcedMinimalWidth = 20;
this._layoutInformation.forcedMinimalHeight = 20;
}
},
_initElement: function() {
$super._initElement.call(this);
this._inputElement = this._element.getElementsByTagName('textarea')[0];
this._initListeners();
this._canShowVirtualKeyboard = true;
this._realReadOnly = false;
if (context.__wrapper.isGMA()) {
this._virtualKeyboardDisplayedHandler = this._appWidget.when(context.constants.widgetEvents.virtualKeyboardDisplayed,
function() {
this._canShowVirtualKeyboard = true;
}.bind(this));
}
this._showVirtualKeyboardOnFocus = false;
},
_initListeners: function() {
this._inputElement.on('input.TextEditWidget', this._onInput.bind(this));
this._inputElement.on('mousedown.TextEditWidget', cls.WidgetBase._onSelect.bind(this));
},
_unloadListeners: function() {
this._inputElement.off('mousedown.TextEditWidget');
this._inputElement.off('input.TextEditWidget');
},
destroy: function() {
this._unloadListeners();
if (this._htmlFilter) {
this._htmlFilter.destroy();
this._htmlFilter = 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);
this.emit(context.constants.widgetEvents.click, domEvent);
return true;
},
managePriorityKeyDown: function(keyString, domKeyEvent, repeat) {
var keyProcessed = false;
if (this.isEnabled()) {
switch (keyString) {
case "up":
case "down":
case "end":
case "home":
domKeyEvent.gbcDontPreventDefault = true;
keyProcessed = true;
break;
case "enter":
case "return":
if (!this.isNotEditable() && this._wantReturns) {
domKeyEvent.gbcDontPreventDefault = true;
keyProcessed = true;
}
break;
case "tab":
if (!this.isNotEditable() && this._wantTabs) {
keyProcessed = true;
this._insertTab();
}
break;
}
}
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._maxLength > 0 && !this.getUserInterfaceWidget().isCharLengthSemantics()) {
newTextPart = this.checkValueByteCount(text, newTextPart, this._maxLength);
}
return newTextPart;
},
manageKeyDown: function(keyString, domKeyEvent, repeat) {
if (!this._hasHTMLContent) {
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);
if (!this._hasHTMLContent) {
this._verifyWidgetValue(domKeyEvent);
}
this.emit(context.constants.widgetEvents.keyUp, domKeyEvent, true);
},
_verifyWidgetValue: function(event) {
var widgetText = this._elementState.getValue();
var inputValue = this._inputElement.value;
if (widgetText.length >= inputValue.length) {
this._elementState.backup(this._inputElement);
return;
} else {
var newPart = this._elementState.newPart(inputValue);
var res = '';
if (!window.isMobile() && event.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() {
$super._onInput.call(this);
if (!this._hasHTMLContent && !this._processingKeyEvent) {
this._verifyWidgetValue(event);
}
if (!window.browserInfo.isIE || this.getValue() !== this._oldValue || this.hasVMFocus()) {
this.emit(context.constants.widgetEvents.change, true);
}
},
_insertTab: function() {
var s = this._inputElement.selectionStart;
var value = this._inputElement.value.substring(0, this._inputElement.selectionStart) + "\t" + this._inputElement.value
.substring(this._inputElement.selectionEnd);
if (!this.getMaxLength() || value.length <= this.getMaxLength()) {
this.setValue(value);
this._inputElement.selectionEnd = s + 1;
}
},
setValue: function(value, fromVM, cursorPosition) {
$super.setValue.call(this, value, fromVM, cursorPosition);
this._setValue(value);
if (cursorPosition && this.isEnabled()) {
this._inputElement.selectionStart = this._inputElement.selectionEnd = cursorPosition;
}
},
_setValue: function(value) {
if (this._hasHTMLContent) {
if (this._sanitize) {
this._inputElement.innerHTML = this._htmlFilter.sanitize(value);
} else {
this._inputElement.innerHTML = value;
}
} else {
this._inputElement.value = value;
this._elementState.backup(this._inputElement);
}
},
getValue: function() {
if (this._hasHTMLContent === true) {
return this._inputElement.innerHTML;
} else if (this._editingTime === 0) {
return this._valueStack[this._valueStackCursor] || "";
} else {
var result = this._inputElement.value;
if (this.isEditing()) {
if (this.getTextTransform() === 'up') {
result = result.toLocaleUpperCase();
}
if (this.getTextTransform() === 'down') {
result = result.toLocaleLowerCase();
}
}
return result;
}
},
setReadOnly: function(readonly) {
this._realReadOnly = readonly;
$super.setReadOnly.call(this, readonly);
this._setInputReadOnly(this._realReadOnly || this._notEditable || !this._enabled);
},
_setInputReadOnly: function(readonly) {
if (readonly || this._isReadOnly) {
if (this._hasHTMLContent) {
this._inputElement.setAttribute('contenteditable', false);
} else {
this._inputElement.setAttribute('readonly', 'readonly');
}
} else {
if (this._hasHTMLContent) {
this._inputElement.setAttribute('contenteditable', true);
} else {
this._inputElement.removeAttribute('readonly');
}
}
},
setMaxLength: function(maxlength) {
if (maxlength) {
this._maxLength = maxlength;
this._setElementAttribute('maxlength', maxlength, "_inputElement");
}
},
getMaxLength: function() {
return this._inputElement.getIntAttribute('maxlength');
},
setCursors: function(cursor, cursor2) {
if (!cursor2) {
cursor2 = cursor;
} else if (cursor2 === -1) {
cursor2 = cursor;
}
this._inputElement.setCursorPosition(cursor, cursor2);
},
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;
},
setTextAlign: function(align) {
$super.setTextAlign.call(this, align);
this.setStyle(">textarea", {
"text-align": align
});
},
setHtmlControl: function(jcontrol) {
if (this._htmlFilter === null) {
this._htmlFilter = cls.WidgetFactory.createWidget('HtmlFilterWidget', this.getBuildParameters());
}
if (this.isEnabled()) {
jcontrol.setAttribute('contenteditable', true);
}
jcontrol.innerHTML = this.getValue();
this._unloadListeners();
this._inputElement.replaceWith(jcontrol);
this._hasHTMLContent = true;
this._inputElement = jcontrol;
this._initListeners();
if (this.hasFocus()) {
this.setFocus();
}
},
setSanitize: function(sanitize) {
this._sanitize = sanitize;
},
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();
$super.setFocus.call(this, fromMouse);
},
setRows: function(rows) {
this._inputElement.setAttribute('rows', rows || 1);
},
setWrapPolicy: function(format) {
this._inputElement.toggleClass('breakword', format === 'anywhere');
},
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);
this._setInputReadOnly(!enabled || this._notEditable || this._realReadOnly);
},
setWantTabs: function(wantTabs) {
this._wantTabs = wantTabs;
},
setWantReturns: function(wantReturns) {
this._wantReturns = wantReturns;
},
setScrollBars: function(scrollBars) {
this.addClass("scrollbars-" + scrollBars);
},
setShowVirtualKeyboardOnFocus: function(show) {
this._showVirtualKeyboardOnFocus = show;
},
getClipboardValue: function(ignoreSelection) {
if (ignoreSelection) {
return this.getValue();
}
if (this._hasHTMLContent) {
var txt = cls.ClipboardHelper.getSelection();
return txt.length > 0 ? txt : null;
}
return this._elementState.hasSelectedText() ? this._elementState.getSelectedText() : null;
},
getClipboardAuthorizedAction: function() {
return {
paste: true,
copy: true,
cut: !this._hasHTMLContent
};
}
};
});
cls.WidgetFactory.registerBuilder('TextEdit', cls.TextEditWidget);
});