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 FocusWidget import FocusWidget
19 from pyjamas.ui import Event
20
21 -class TextBoxBase(FocusWidget):
22 ALIGN_CENTER = "center"
23 ALIGN_JUSTIFY = "justify"
24 ALIGN_LEFT = "left"
25 ALIGN_RIGHT = "right"
26
27 - def __init__(self, element, **kwargs):
28 self.changeListeners = []
29 self.currentEvent = None
30
31 FocusWidget.__init__(self, element, **kwargs)
32 self.sinkEvents(Event.ONCHANGE)
33
34 - def addChangeListener(self, listener):
35 self.changeListeners.append(listener)
36
37 - def cancelKey(self):
40
41 - def getCursorPos(self):
42 element = self.getElement()
43 try:
44 return element.selectionStart
45 except:
46 return 0
47
49 return DOM.getAttribute(self.getElement(), "name")
50
52 start = self.getCursorPos()
53 length = self.getSelectionLength()
54 text = self.getText()
55 return text[start:start + length]
56
58 element = self.getElement()
59 try:
60 return element.selectionEnd - element.selectionStart
61 except:
62 return 0
63
64
65
66
67
68
69 - def setFocus(self, focused):
70 if (focused):
71 self.getElement().focus()
72 else:
73 self.getElement().blur()
74
76 return DOM.getAttribute(self.getElement(), "value")
77
78 - def onBrowserEvent(self, event):
79 FocusWidget.onBrowserEvent(self, event)
80
81 type = DOM.eventGetType(event)
82 if type == "change":
83 for listener in self.changeListeners:
84 if hasattr(listener, 'onChange'): listener.onChange(self)
85 else: listener(self)
86
87 - def removeChangeListener(self, listener):
88 self.changeListeners.remove(listener)
89
90 - def selectAll(self):
91 length = len(self.getText())
92 if length > 0:
93 self.setSelectionRange(0, length)
94
95 - def setCursorPos(self, pos):
97
98 - def setKey(self, key):
99 if self.currentEvent is not None:
100 DOM.eventSetKeyCode(self.currentEvent, key)
101
102 - def setName(self, name):
103 DOM.setAttribute(self.getElement(), "name", name)
104
105 - def setSelectionRange(self, pos, length):
106 if length < 0:
107
108 console.error("Length must be a positive integer. Length: " + length)
109
110 if (pos < 0) or (length + pos > len(self.getText())):
111
112 console.error("From Index: %d " % pos + " To Index: %d " % (pos + length) + " Text Length: %d " % len(self.getText()))
113
114 element = self.getElement()
115 element.setSelectionRange(pos, pos + length)
116
117 - def setText(self, text):
118 DOM.setAttribute(self.getElement(), "value", str(text))
119
120 - def setTextAlignment(self, align):
121 DOM.setStyleAttribute(self.getElement(), "textAlign", align)
122
123
124
125