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

Source Code for Module library.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  from __pyjamas__ import JS 
 16  from pyjamas import Factory, DOM 
 17  import pyjd 
 18  if pyjd.is_desktop: 
 19      from __pyjamas__ import get_main_frame 
 20   
 21  from pyjamas.ui.SimplePanel import SimplePanel 
 22  from pyjamas.ui import Event 
 23  from pyjamas.ui.EventObject import EventObject 
 24   
25 -class FormSubmitEvent(EventObject):
26 - def __init__(self, source):
27 EventObject.__init__(self, source) 28 self.cancel = False # ?
29
30 - def isCancelled(self):
31 return self.cancel
32
33 - def setCancelled(self, cancel):
34 self.cancel = cancel
35
36 -class FormSubmitCompleteEvent(EventObject):
37 - def __init__(self, source, results):
38 EventObject.__init__(self, source) 39 self.results = results
40 - def getResults(self):
41 return self.results
42 43 FormPanel_formId = 0 44
45 -class FormPanel(SimplePanel):
46 ENCODING_MULTIPART = "multipart/form-data" 47 ENCODING_URLENCODED = "application/x-www-form-urlencoded" 48 METHOD_GET = "get" 49 METHOD_POST = "post" 50
51 - def __init__(self, target = None, **kwargs):
52 global FormPanel_formId 53 54 if hasattr(target, "getName"): 55 target = target.getName() 56 57 if kwargs.has_key('Element'): 58 element = kwargs.pop('Element') 59 else: 60 element = DOM.createForm() 61 62 self.formHandlers = [] 63 self.iframe = None 64 self.__formAction = None 65 66 FormPanel_formId += 1 67 formName = "FormPanel_" + str(FormPanel_formId) 68 DOM.setAttribute(element, "target", formName) 69 DOM.setInnerHTML(element, """<iframe name='%s' src="javascript:''">"""\ 70 % formName) 71 self.iframe = DOM.getFirstChild(element) 72 73 DOM.setIntStyleAttribute(self.iframe, "width", 0) 74 DOM.setIntStyleAttribute(self.iframe, "height", 0) 75 DOM.setIntStyleAttribute(self.iframe, "border", 0) 76 77 if target is not None: 78 kwargs['Target'] = target 79 80 SimplePanel.__init__(self, element, **kwargs) 81 82 try: 83 self.sinkEvents(Event.ONLOAD) 84 except: 85 # MSHTML doesn't have form.onload, 86 # it has onreadystatechange. 87 pass
88
89 - def addFormHandler(self, handler):
90 self.formHandlers.append(handler)
91
92 - def getAction(self):
93 return DOM.getAttribute(self.getElement(), "action")
94 95 # FormPanelImpl.getEncoding
96 - def getEncoding(self):
97 elem = self.getElement() 98 if hasattr(elem, 'enctype'): 99 return elem.enctype 100 return elem.encoding
101
102 - def getMethod(self):
103 return DOM.getAttribute(self.getElement(), "method")
104
105 - def getTarget(self):
106 return DOM.getAttribute(self.getElement(), "target")
107 108 # FormPanelImpl.getTextContents
109 - def getTextContents(self, iframe):
110 try: 111 if not iframe.contentDocument: 112 return None 113 return DOM.getInnerHTML(iframe.contentDocument.body) 114 except: 115 return None
116
117 - def _onload(self, form, event, something):
118 print form, event, something 119 if not self.__formAction: 120 return 121 self._listener.onFrameLoad()
122
123 - def _onsubmit(self, form, event, something):
124 print form, event, something 125 try: 126 event = get_main_frame().gobject_wrap(event) # webkit HACK! 127 form = get_main_frame().gobject_wrap(form) # webkit HACK! 128 except: 129 pass 130 131 if self.iframe is not None: 132 self.__formAction = form.action 133 return self._listener.onFormSubmit()
134 135 # FormPanelImpl.hookEvents
136 - def hookEvents(self, iframe, form, listener):
137 # TODO: might have to fix this, use DOM.set_listener() 138 self._listener = listener 139 if iframe is not None: 140 wf = mf = get_main_frame() 141 self._onload_listener = mf.addEventListener(iframe, "load", 142 self._onload) 143 144 self._onsubmit_listener = mf.addEventListener(form, "onsubmit", 145 self._onsubmit)
146
147 - def onFormSubmit(self):
148 event = FormSubmitEvent(self) 149 for handler in self.formHandlers: 150 handler.onSubmit(event) 151 152 return not event.isCancelled()
153
154 - def onFrameLoad(self):
155 event = FormSubmitCompleteEvent(self, self.getTextContents(self.iframe)) 156 for handler in self.formHandlers: 157 handler.onSubmitComplete(event)
158
159 - def removeFormHandler(self, handler):
160 self.formHandlers.remove(handler)
161
162 - def setAction(self, url):
163 DOM.setAttribute(self.getElement(), "action", url)
164 165 # FormPanelImpl.setEncoding
166 - def setEncoding(self, encodingType):
167 form = self.getElement() 168 if hasattr(form, 'enctype'): 169 form.enctype = encodingType 170 form.encoding = encodingType
171
172 - def setMethod(self, method):
173 DOM.setAttribute(self.getElement(), "method", method)
174
175 - def submit(self, sender):
176 event = FormSubmitEvent(self) 177 for handler in self.formHandlers: 178 handler.onSubmit(event) 179 180 if event.isCancelled(): 181 return 182 183 self.submitImpl(self.getElement(), self.iframe)
184 185 # FormPanelImpl.submit
186 - def submitImpl(self, form, iframe):
187 if iframe: 188 self.__formAction = form.action 189 form.submit()
190
191 - def onAttach(self):
192 SimplePanel.onAttach(self) 193 self.hookEvents(self.iframe, self.getElement(), self)
194
195 - def onDetach(self):
196 SimplePanel.onDetach(self) 197 self.unhookEvents(self.iframe, self.getElement())
198
199 - def setTarget(self, target):
200 DOM.setAttribute(self.getElement(), "target", target)
201 202 # FormPanelImpl.unhookEvents
203 - def unhookEvents(self, iframe, form):
204 # these might be wrong, need testing. 205 iframe.removeEventListener("load", self._onload_listener, True) 206 form.removeEventListener("onsubmit", self._onsubmit_listener, True)
207 208 Factory.registerClass('pyjamas.ui.FormPanel', 'FormPanel', FormPanel) 209