1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 from pyjamas import DOM
16 from pyjamas import Factory
17 from __pyjamas__ import console, JS
18 from pyjamas.ui.FocusWidget import FocusWidget
19 from pyjamas.ui.ChangeListener import ChangeHandler
20 from pyjamas.ui.InputListener import InputHandler
21 from pyjamas.ui import Event
22
23 -class TextBoxBase(FocusWidget, ChangeHandler, InputHandler):
24 ALIGN_CENTER = "center"
25 ALIGN_JUSTIFY = "justify"
26 ALIGN_LEFT = "left"
27 ALIGN_RIGHT = "right"
28
29 _props = [("name", "Name", "Name", None),
30 ("align", "Text Alignment", "TextAlign", None),
31 ]
32
33 - def __init__(self, element, **kwargs):
39
40 @classmethod
41 - def _getProps(self):
43
44 - def cancelKey(self):
47
48 - def getCursorPos(self):
49 element = self.getElement()
50 try:
51 return element.selectionStart
52 except:
53 return 0
54
56 return DOM.getAttribute(self.getElement(), "name")
57
59 start = self.getCursorPos()
60 length = self.getSelectionLength()
61 text = self.getText()
62 return text[start:start + length]
63
65 element = self.getElement()
66 try:
67 return element.selectionEnd - element.selectionStart
68 except:
69 return 0
70
71
72
73
74
75
76 - def setFocus(self, focused):
77 if (focused):
78 self.getElement().focus()
79 else:
80 self.getElement().blur()
81
83 return DOM.getAttribute(self.getElement(), "value")
84
85 - def selectAll(self):
86 length = len(self.getText())
87 if length > 0:
88 self.setSelectionRange(0, length)
89
90 - def setCursorPos(self, pos):
92
93 - def setKey(self, key):
96
97 - def setName(self, name):
98 DOM.setAttribute(self.getElement(), "name", name)
99
100 - def setSelectionRange(self, pos, length):
101 if length < 0:
102
103 console.error("Length must be a positive integer. Length: " + length)
104
105 if (pos < 0) or (length + pos > len(self.getText())):
106
107 console.error("From Index: %d " % pos + " To Index: %d " % (pos + length) + " Text Length: %d " % len(self.getText()))
108
109 element = self.getElement()
110 element.setSelectionRange(pos, pos + length)
111
112 - def setText(self, text):
113 DOM.setAttribute(self.getElement(), "value", text)
114
115 - def setTextAlignment(self, align):
116 DOM.setStyleAttribute(self.getElement(), "textAlign", align)
117
118 - def getTextAlignment(self, align):
119 return DOM.getStyleAttribute(self.getElement(), "textAlign")
120
121
122
123
124