# CTK: Cherokee Toolkit
#
# Authors:
#      Alvaro Lopez Ortega <alvaro@alobbs.com>
#
# Copyright (C) 2009 Alvaro Lopez Ortega
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of version 2 of the GNU General Public
# License as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

from Widget import Widget
from Container import Container
from util import formater, props_to_str

LINK_HTML      = '<a href="%(href)s" id="%(id)s" %(props)s>%(content)s</a>'
LINK_ICON_HTML = '<div id="%(id)s"><span class="ui-icon ui-icon-%(icon)s"></span>%(link)s</div>'


class Link (Container):
    def __init__ (self, href, content=None, props={}):
        Container.__init__ (self)
        self.href  = href[:]
        self.props = props.copy()

        if 'id' in self.props:
            self.id = self.props.pop('id')

        if content:
            self += content

    def Render (self):
        render = Container.Render (self)

        props = {'id':      self.id,
                 'href':    self.href,
                 'props':   props_to_str(self.props),
                 'content': render.html}

        render.html = formater (LINK_HTML, props)
        return render


class LinkWindow (Link):
    def __init__ (self, href, content=None, props={}):
        self.props = props.copy()
        props['target'] = '_blank'

        Link.__init__ (self, href, content, props)


class LinkIcon (Link):
    def __init__ (self, href="#", icon='newwin', content=None):
        Link.__init__ (self, href)
        self.icon = icon

        if content:
            self += content

    def Render (self):
        render = Link.Render (self)

        props = {'id':   self.id,
                 'icon': self.icon,
                 'link': render.html}

        render.html = formater (LINK_ICON_HTML, props)
        return render


@@ -894,24 +894,8 @@
  '%3Ca
- href=%22%25(href)s%22
  id=
@@ -902,16 +902,25 @@
 %22%25(id)s%22
+ %25(href)s
  %25(props
@@ -1082,32 +1082,37 @@
 it__ (self, href
+=None
 , content=None, 
@@ -1156,16 +1156,37 @@
  (self)%0A
+        if href:%0A    
         
@@ -1195,17 +1195,16 @@
 lf.href 
- 
 = href%5B:
@@ -1205,16 +1205,59 @@
 href%5B:%5D%0A
+        else:%0A            self.href = None%0A
         
@@ -1464,32 +1464,135 @@
 .Render (self)%0A%0A
+        if self.href:%0A            href = 'href=%22%25s%22' %25(self.href)%0A        else:%0A            href = ''%0A%0A
         props = 
@@ -1636,29 +1636,24 @@
  'href':    
-self.
 href,%0A      

Allow CTK.Link() objects without an href property.