-
Notifications
You must be signed in to change notification settings - Fork 215
/
TextBoxBase.py
117 lines (90 loc) · 3.76 KB
/
TextBoxBase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright 2006 James Tauber and contributors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from pyjamas import DOM
from pyjamas.ui.FocusWidget import FocusWidget
from pyjamas.ui import Event
class TextBoxBase(FocusWidget):
ALIGN_CENTER = "center"
ALIGN_JUSTIFY = "justify"
ALIGN_LEFT = "left"
ALIGN_RIGHT = "right"
def __init__(self, element):
self.changeListeners = []
self.clickListeners = []
self.currentEvent = None
FocusWidget.__init__(self, element)
self.sinkEvents(Event.ONCHANGE)
def addChangeListener(self, listener):
self.changeListeners.append(listener)
def addClickListener(self, listener):
self.clickListeners.append(listener)
def cancelKey(self):
if self.currentEvent is not None:
DOM.eventPreventDefault(self.currentEvent)
def getCursorPos(self):
element = self.getElement()
try:
return element.selectionStart
except:
return 0
def getName(self):
return DOM.getAttribute(self.getElement(), "name")
def getSelectedText(self):
start = self.getCursorPos()
length = self.getSelectionLength()
text = self.getText()
return text[start:start + length]
def getSelectionLength(self):
element = self.getElement()
try:
return element.selectionEnd - element.selectionStart
except:
return 0
def getText(self):
return DOM.getAttribute(self.getElement(), "value")
def onBrowserEvent(self, event):
FocusWidget.onBrowserEvent(self, event)
type = DOM.eventGetType(event)
if type == "change":
for listener in self.changeListeners:
if hasattr(listener, 'onChange'): listener.onChange(self)
else: listener(self)
def removeChangeListener(self, listener):
self.changeListeners.remove(listener)
def removeClickListener(self, listener):
self.clickListeners.remove(listener)
def selectAll(self):
length = len(self.getText())
if length > 0:
self.setSelectionRange(0, length)
def setCursorPos(self, pos):
self.setSelectionRange(pos, 0)
def setKey(self, key):
if self.currentEvent is not None:
DOM.eventSetKeyCode(self.currentEvent, key)
def setName(self, name):
DOM.setAttribute(self.getElement(), "name", name)
def setSelectionRange(self, pos, length):
if length < 0:
# throw new IndexOutOfBoundsException("Length must be a positive integer. Length: " + length);
console.error("Length must be a positive integer. Length: " + length)
if (pos < 0) or (length + pos > len(self.getText())):
#throw new IndexOutOfBoundsException("From Index: " + pos + " To Index: " + (pos + length) + " Text Length: " + getText().length());
console.error("From Index: " + pos + " To Index: " + (pos + length) + " Text Length: " + len(self.getText()))
element = self.getElement()
element.setSelectionRange(pos, pos + length)
def setText(self, text):
DOM.setAttribute(self.getElement(), "value", text)
def setTextAlignment(self, align):
DOM.setStyleAttribute(self.getElement(), "textAlign", align)