Package pyjamas :: Package ui :: Module FormPanel
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.FormPanel

  1  # Copyright 2006 James Tauber and contributors 
  2  # Copyright (C) 2009 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
  3  # 
  4  # Licensed under the Apache License, Version 2.0 (the "License"); 
  5  # you may not use this file except in compliance with the License. 
  6  # You may obtain a copy of the License at 
  7  # 
  8  #     http://www.apache.org/licenses/LICENSE-2.0 
  9  # 
 10  # Unless required by applicable law or agreed to in writing, software 
 11  # distributed under the License is distributed on an "AS IS" BASIS, 
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 13  # See the License for the specific language governing permissions and 
 14  # limitations under the License. 
 15  import sys 
 16  from __pyjamas__ import JS 
 17  from pyjamas import Factory 
 18  if sys.platform not in ['mozilla', 'ie6', 'opera', 'oldmoz', 'safari']: 
 19      from __pyjamas__ import get_main_frame 
 20   
 21  from pyjamas import DOM 
 22  from __pyjamas__ import JS 
 23   
 24  from SimplePanel import SimplePanel 
 25  from pyjamas.ui import Event 
 26  from EventObject import EventObject 
 27   
28 -class FormSubmitEvent(EventObject):
29 - def __init__(self, source):
30 EventObject.__init__(self, source) 31 self.cancel = False # ?
32
33 - def isCancelled(self):
34 return self.cancel
35
36 - def setCancelled(self, cancel):
37 self.cancel = cancel
38
39 -class FormSubmitCompleteEvent(EventObject):
40 - def __init__(self, source, results):
41 EventObject.__init__(self, source) 42 self.results = results
43 - def getResults(self):
44 return self.results
45 46 FormPanel_formId = 0 47
48 -class FormPanel(SimplePanel):
49 ENCODING_MULTIPART = "multipart/form-data" 50 ENCODING_URLENCODED = "application/x-www-form-urlencoded" 51 METHOD_GET = "get" 52 METHOD_POST = "post" 53
54 - def __init__(self, target = None, **kwargs):
55 global FormPanel_formId 56 57 if hasattr(target, "getName"): 58 target = target.getName() 59 60 if kwargs.has_key('Element'): 61 element = kwargs.pop('Element') 62 else: 63 element = DOM.createForm() 64 65 self.formHandlers = [] 66 self.iframe = None 67 self.__formAction = None 68 69 FormPanel_formId += 1 70 formName = "FormPanel_" + str(FormPanel_formId) 71 DOM.setAttribute(element, "target", formName) 72 DOM.setInnerHTML(element, """<iframe name='%s' src="javascript:''">"""\ 73 % formName) 74 self.iframe = DOM.getFirstChild(element) 75 76 DOM.setIntStyleAttribute(self.iframe, "width", 0) 77 DOM.setIntStyleAttribute(self.iframe, "height", 0) 78 DOM.setIntStyleAttribute(self.iframe, "border", 0) 79 80 if target is not None: 81 kwargs['Target'] = target 82 83 SimplePanel.__init__(self, element, **kwargs) 84 85 try: 86 self.sinkEvents(Event.ONLOAD) 87 except: 88 # MSHTML doesn't have form.onload, 89 # it has onreadystatechange. 90 pass
91
92 - def addFormHandler(self, handler):
93 self.formHandlers.append(handler)
94
95 - def getAction(self):
96 return DOM.getAttribute(self.getElement(), "action")
97 98 # FormPanelImpl.getEncoding
99 - def getEncoding(self):
100 elem = self.getElement() 101 if hasattr(elem, 'enctype'): 102 return elem.enctype 103 return elem.encoding
104
105 - def getMethod(self):
106 return DOM.getAttribute(self.getElement(), "method")
107
108 - def getTarget(self):
109 return DOM.getAttribute(self.getElement(), "target")
110 111 # FormPanelImpl.getTextContents
112 - def getTextContents(self, iframe):
113 try: 114 if not iframe.contentDocument: 115 return None 116 return DOM.getInnerHTML(iframe.contentDocument.body) 117 except: 118 return None
119
120 - def _onload(self, form, event, something):
121 print form, event, something 122 if not self.__formAction: 123 return 124 self._listener.onFrameLoad()
125
126 - def _onsubmit(self, form, event, something):
127 print form, event, something 128 try: 129 event = get_main_frame().gobject_wrap(event) # webkit HACK! 130 form = get_main_frame().gobject_wrap(form) # webkit HACK! 131 except: 132 pass 133 134 if self.iframe: 135 self.__formAction = form.action 136 return self._listener.onFormSubmit()
137 138 # FormPanelImpl.hookEvents
139 - def hookEvents(self, iframe, form, listener):
140 # TODO: might have to fix this, use DOM.set_listener() 141 self._listener = listener 142 if iframe: 143 wf = mf = get_main_frame() 144 self._onload_listener = mf.addEventListener(iframe, "load", 145 self._onload) 146 147 self._onsubmit_listener = mf.addEventListener(form, "onsubmit", 148 self._onsubmit)
149
150 - def onFormSubmit(self):
151 event = FormSubmitEvent(self) 152 for handler in self.formHandlers: 153 handler.onSubmit(event) 154 155 return not event.isCancelled()
156
157 - def onFrameLoad(self):
158 event = FormSubmitCompleteEvent(self, self.getTextContents(self.iframe)) 159 for handler in self.formHandlers: 160 handler.onSubmitComplete(event)
161
162 - def removeFormHandler(self, handler):
163 self.formHandlers.remove(handler)
164
165 - def setAction(self, url):
166 DOM.setAttribute(self.getElement(), "action", url)
167 168 # FormPanelImpl.setEncoding
169 - def setEncoding(self, encodingType):
170 form = self.getElement() 171 if hasattr(form, 'enctype'): 172 form.enctype = encodingType 173 form.encoding = encodingType
174
175 - def setMethod(self, method):
176 DOM.setAttribute(self.getElement(), "method", method)
177
178 - def submit(self):
179 event = FormSubmitEvent(self) 180 for handler in self.formHandlers: 181 handler.onSubmit(event) 182 183 if event.isCancelled(): 184 return 185 186 self.submitImpl(self.getElement(), self.iframe)
187 188 # FormPanelImpl.submit
189 - def submitImpl(self, form, iframe):
190 if iframe: 191 self.__formAction = form.action 192 form.submit()
193
194 - def onAttach(self):
195 SimplePanel.onAttach(self) 196 self.hookEvents(self.iframe, self.getElement(), self)
197
198 - def onDetach(self):
199 SimplePanel.onDetach(self) 200 self.unhookEvents(self.iframe, self.getElement())
201
202 - def setTarget(self, target):
203 DOM.setAttribute(self.getElement(), "target", target)
204 205 # FormPanelImpl.unhookEvents
206 - def unhookEvents(self, iframe, form):
207 # these might be wrong, need testing. 208 iframe.removeEventListener("load", self._onload_listener, True) 209 form.removeEventListener("onsubmit", self._onsubmit_listener, True)
210 211 Factory.registerClass('pyjamas.ui.FormPanel', FormPanel) 212