Skip to content
This repository
Browse code

Merge pull request #786 from gpitel/example_anchor_update

add: more extensive examples on linking
  • Loading branch information...
commit e0191c0cf3c2f99cf70272f7fe68078b16bf85a9 2 parents 4279d5a + ab0d6ea
C Anthony Risinger authored February 18, 2013
76  examples/anchor/Anchor.py
... ...
@@ -1,15 +1,73 @@
1 1
 import pyjd
2 2
 from pyjamas.ui.RootPanel import RootPanel
3  
-from pyjamas.ui.Image import Image
  3
+from pyjamas import DOM
4 4
 from pyjamas.ui.Anchor import Anchor
  5
+from pyjamas.ui.Hyperlink import Hyperlink
  6
+from pyjamas import Window
  7
+from pyjamas.ui.HTML import HTML
  8
+from pyjamas.ui.HTMLPanel import HTMLPanel
  9
+from pyjamas.ui.Label import Label
  10
+from pyjamas.ui.Image import Image
  11
+from pyjamas.ui.HorizontalPanel import HorizontalPanel
  12
+
5 13
 
  14
+def onClick(sender):
  15
+    Window.alert('Make service request using %s'%sender.getID())
  16
+    
6 17
 if __name__ == '__main__':
7 18
     pyjd.setup("public/Anchor.html")
8  
-
9  
-    root = RootPanel()
10  
-    image = Image('http://pyjs.org/assets/images/pyjs.128x128.png')
11  
-    anchor = Anchor(Widget=image, Href='http://pyjs.org', Title='Pyjs website')
12  
-    root.add(anchor)
13  
-
14  
-    pyjd.run()
15  
-
  19
+    # EXAMPLE 1
  20
+    a1 = Anchor(Widget = HTML('Test 1: Anchor to external site using HTML widget.'), Href='http://pyjs.org', Title = 'Test1')
  21
+    RootPanel().add(a1) 
  22
+    # EXAMPLE 2
  23
+    label = Label(text = 'Test 2: Click listener added to a label.')
  24
+    label.addClickListener(onClick)
  25
+    RootPanel().add(label) 
  26
+    # EXAMPLE 3
  27
+    a2 = Hyperlink(text = 'Hyperlink', Element = DOM.createSpan())
  28
+    a2.setID('param1')
  29
+    a2.addClickListener(onClick)
  30
+    html2=HTMLPanel("Test 3: <span id ='t3'></span> added to HTMLPanel with click listener.")
  31
+    html2.add(a2, "t3")
  32
+    RootPanel().add(html2)
  33
+    # EXAMPLE 4
  34
+    hpanel = HorizontalPanel()
  35
+    hpanel.append(HTML('Test 4:  Anchor to external site using Image widget'))
  36
+    a3 = Anchor(Widget = Image('http://pyjs.org/assets/images/pyjs.128x128.png'), Href='http://pyjs.org', Title = 'Test4')
  37
+    hpanel.append(a3)
  38
+    RootPanel().add(hpanel) 
  39
+    # EXAMPLE 5
  40
+    serverXml = \
  41
+"""
  42
+<html>
  43
+  <head>
  44
+    <title>Example 5</title>
  45
+  </head>
  46
+  <body>
  47
+        <p>Test 5: Processes server html and insert click listeners into links: 
  48
+        <span id='link1' class = 'wikilink'>link 1</span> and <span id='link2' class = 'wikilink'>link 2</span>.
  49
+        </p>
  50
+  </body>
  51
+</html>
  52
+"""
  53
+    html3 = HTMLPanel(serverXml)
  54
+    links = list()
  55
+    for elem in html3.findTags('span'):
  56
+        if DOM.getElemAttribute(elem, 'class') == 'wikilink':
  57
+            linkClass = DOM.getElemAttribute(elem, 'class')
  58
+        links.append(elem)
  59
+    if len(links) > 0:
  60
+        parent = DOM.getParent(links[0])
  61
+        for link in links:
  62
+            linkId = DOM.getElemAttribute(link, 'id')
  63
+            linkClass = DOM.getElemAttribute(link, 'class')
  64
+            linkInner = DOM.getInnerHTML(link)
  65
+            a3 = Hyperlink(text = linkInner, Element = DOM.createSpan())
  66
+            a3.addClickListener(onClick)
  67
+            a3.setID('param2')
  68
+            #todo: modify HTMLPanel to replace an element instead of add
  69
+            #html3.replace(a3, linkId)
  70
+            DOM.setInnerHTML(link, '') # clear existing text
  71
+            html3.add(a3, linkId)
  72
+    RootPanel().add(html3) 
  73
+    pyjd.run()
4  examples/anchor/__main__.py
@@ -8,8 +8,8 @@
8 8
 
9 9
 
10 10
 PACKAGE = {
11  
-    'title': 'Anchor',
12  
-    'desc': 'Demonstration of an external link',
  11
+    'title': 'Linking',
  12
+    'desc': 'Demonstration of external and internal links',
13 13
 }
14 14
 
15 15
 

0 notes on commit e0191c0

Please sign in to comment.
Something went wrong with that request. Please try again.