appypod-rattail/fields/group.py

399 lines
18 KiB
Python
Raw Normal View History

2013-08-21 06:54:56 -05:00
# ------------------------------------------------------------------------------
# This file is part of Appy, a framework for building applications in the Python
# language. Copyright (C) 2007 Gaetan Delannay
# Appy is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 3 of the License, or (at your option) any later
# version.
# Appy 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
# Appy. If not, see <http://www.gnu.org/licenses/>.
# ------------------------------------------------------------------------------
from appy.px import Px
from appy.gen import utils as gutils
# ------------------------------------------------------------------------------
class Group:
'''Used for describing a group of fields within a page.'''
def __init__(self, name, columns=['100%'], wide=True, style='section2',
hasLabel=True, hasDescr=False, hasHelp=False,
hasHeaders=False, group=None, colspan=1, align='center',
valign='top', css_class='', master=None, masterValue=None,
cellpadding=1, cellspacing=1, cellgap='0.6em', label=None,
translated=None):
self.name = name
# In its simpler form, field "columns" below can hold a list or tuple
# of column widths expressed as strings, that will be given as is in
# the "width" attributes of the corresponding "td" tags. Instead of
# strings, within this list or tuple, you may give Column instances
# (see below).
self.columns = columns
self._setColumns()
# If field "wide" below is True, the HTML table corresponding to this
# group will have width 100%. You can also specify some string value,
# which will be used for HTML param "width".
if wide == True:
self.wide = '100%'
elif isinstance(wide, str):
2013-08-21 06:54:56 -05:00
self.wide = wide
else:
self.wide = ''
# If style = 'fieldset', all widgets within the group will be rendered
# within an HTML fieldset. If style is 'section1' or 'section2', widgets
# will be rendered after the group title.
self.style = style
# If hasLabel is True, the group will have a name and the corresponding
# i18n label will be generated.
self.hasLabel = hasLabel
# If hasDescr is True, the group will have a description and the
# corresponding i18n label will be generated.
self.hasDescr = hasDescr
# If hasHelp is True, the group will have a help text associated and the
# corresponding i18n label will be generated.
self.hasHelp = hasHelp
# If hasheaders is True, group content will begin with a row of headers,
# and a i18n label will be generated for every header.
self.hasHeaders = hasHeaders
self.nbOfHeaders = len(columns)
# If this group is himself contained in another group, the following
# attribute is filled.
self.group = Group.get(group)
# If the group is rendered into another group, we can specify the number
# of columns that this group will span.
self.colspan = colspan
self.align = align
self.valign = valign
self.cellpadding = cellpadding
self.cellspacing = cellspacing
# Beyond standard cellpadding and cellspacing, cellgap can define an
# additional horizontal gap between cells in a row. So this value does
# not add space before the first cell or after the last one.
self.cellgap = cellgap
if style == 'tabs':
# Group content will be rendered as tabs. In this case, some
# param combinations have no sense.
self.hasLabel = self.hasDescr = self.hasHelp = False
# The rendering is forced to a single column
self.columns = self.columns[:1]
# Inner field/group labels will be used as tab labels.
2013-08-21 06:54:56 -05:00
self.css_class = css_class
self.master = master
self.masterValue = gutils.initMasterValue(masterValue)
if master: master.slaves.append(self)
self.label = label # See similar attr of Type class.
# If a translated name is already given here, we will use it instead of
# trying to translate the group label.
self.translated = translated
def _setColumns(self):
'''Standardizes field "columns" as a list of Column instances. Indeed,
the initial value for field "columns" may be a list or tuple of
Column instances or strings.'''
for i in range(len(self.columns)):
columnData = self.columns[i]
if not isinstance(columnData, Column):
self.columns[i] = Column(self.columns[i])
@staticmethod
def get(groupData):
'''Produces a Group instance from p_groupData. User-defined p_groupData
can be a string or a Group instance; this method returns always a
Group instance.'''
res = groupData
if res and isinstance(res, str):
2013-08-21 06:54:56 -05:00
# Group data is given as a string. 2 more possibilities:
# (a) groupData is simply the name of the group;
# (b) groupData is of the form <groupName>_<numberOfColumns>.
groupElems = groupData.rsplit('_', 1)
if len(groupElems) == 1:
res = Group(groupElems[0])
else:
try:
nbOfColumns = int(groupElems[1])
except ValueError:
nbOfColumns = 1
width = 100.0 / nbOfColumns
res = Group(groupElems[0], ['%.2f%%' % width] * nbOfColumns)
return res
def getMasterData(self):
'''Gets the master of this group (and masterValue) or, recursively, of
containing groups when relevant.'''
if self.master: return (self.master, self.masterValue)
if self.group: return self.group.getMasterData()
def generateLabels(self, messages, classDescr, walkedGroups,
2013-09-24 05:26:31 -05:00
content='fields'):
2013-08-21 06:54:56 -05:00
'''This method allows to generate all the needed i18n labels related to
this group. p_messages is the list of i18n p_messages (a PoMessages
instance) that we are currently building; p_classDescr is the
2013-09-24 05:26:31 -05:00
descriptor of the class where this group is defined. The type of
content in this group is specified by p_content.'''
# A part of the group label depends on p_content.
gp = (content == 'searches') and 'searchgroup' or 'group'
2013-08-21 06:54:56 -05:00
if self.hasLabel:
msgId = '%s_%s_%s' % (classDescr.name, gp, self.name)
messages.append(msgId, self.name)
if self.hasDescr:
msgId = '%s_%s_%s_descr' % (classDescr.name, gp, self.name)
messages.append(msgId, ' ', nice=False)
if self.hasHelp:
msgId = '%s_%s_%s_help' % (classDescr.name, gp, self.name)
messages.append(msgId, ' ', nice=False)
if self.hasHeaders:
for i in range(self.nbOfHeaders):
msgId = '%s_%s_%s_col%d' % (classDescr.name, gp, self.name, i+1)
messages.append(msgId, ' ', nice=False)
walkedGroups.add(self)
if self.group and (self.group not in walkedGroups) and \
not self.group.label:
# We remember walked groups for avoiding infinite recursion.
self.group.generateLabels(messages, classDescr, walkedGroups,
2013-09-24 05:26:31 -05:00
content=content)
2013-08-21 06:54:56 -05:00
2013-09-24 05:26:31 -05:00
def insertInto(self, elems, uiGroups, page, className, content='fields'):
2013-08-21 06:54:56 -05:00
'''Inserts the UiGroup instance corresponding to this Group instance
2013-09-24 05:26:31 -05:00
into p_elems, the recursive structure used for displaying all
elements in a given p_page (fields, searches, transitions...) and
returns this UiGroup instance.'''
2013-08-21 06:54:56 -05:00
# First, create the corresponding UiGroup if not already in p_uiGroups.
if self.name not in uiGroups:
2013-09-24 05:26:31 -05:00
uiGroup = uiGroups[self.name] = UiGroup(self, page, className,
content=content)
# Insert the group at the higher level (ie, directly in p_elems)
2013-08-21 06:54:56 -05:00
# if the group is not itself in a group.
if not self.group:
2013-09-24 05:26:31 -05:00
elems.append(uiGroup)
2013-08-21 06:54:56 -05:00
else:
2013-09-24 05:26:31 -05:00
outerGroup = self.group.insertInto(elems, uiGroups, page,
className, content=content)
outerGroup.addElement(uiGroup)
2013-08-21 06:54:56 -05:00
else:
uiGroup = uiGroups[self.name]
return uiGroup
class Column:
'''Used for describing a column within a Group like defined above'''
2013-08-21 06:54:56 -05:00
def __init__(self, width, align="left"):
self.width = width
self.align = align
class UiGroup:
2013-09-24 05:26:31 -05:00
'''On-the-fly-generated data structure that groups all elements
(fields, searches, transitions...) sharing the same Group instance, that
the currently logged user can see.'''
2013-08-21 06:54:56 -05:00
# PX that renders a help icon for a group.
pxHelp = Px('''<acronym title="obj.translate('help', field=field)"><img
src=":url('help')"/></acronym>''')
# PX that renders the content of a group (which is referred as var "field").
2013-08-21 06:54:56 -05:00
pxContent = Px('''
<table var="cellgap=field.cellgap" width=":field.wide"
align=":ztool.flipLanguageDirection(field.align, dir)"
id=":tagId" name=":tagName" class=":groupCss"
cellspacing=":field.cellspacing" cellpadding=":field.cellpadding">
<!-- Display the title of the group if not rendered a fieldset. -->
<tr if="(field.style != 'fieldset') and field.hasLabel">
<td colspan=":len(field.columnsWidths)" class=":field.style"
align=":dleft">
<x>::_(field.labelId)</x><x if="field.hasHelp">:field.pxHelp</x>
</td>
</tr>
<tr if="(field.style != 'fieldset') and field.hasDescr">
<td colspan=":len(field.columnsWidths)"
class="discreet">::_(field.descrId)</td>
</tr>
<!-- The column headers -->
<tr>
<th for="colNb in range(len(field.columnsWidths))"
2014-05-14 10:26:46 -05:00
align=":ztool.flipLanguageDirection(field.columnsAligns[colNb], dir)"
2013-08-21 06:54:56 -05:00
width=":field.columnsWidths[colNb]">::field.hasHeaders and \
_('%s_col%d' % (field.labelId, (colNb+1))) or ''</th>
</tr>
<!-- The rows of widgets -->
2013-09-24 05:26:31 -05:00
<tr valign=":field.valign" for="row in field.elements">
2015-02-17 01:58:04 -06:00
<td for="field in row" colspan=":field.colspan|1"
2013-08-21 06:54:56 -05:00
style=":not loop.field.last and ('padding-right:%s'% cellgap) or ''">
<x if="field">
<x if="field.type == 'group'">:field.pxView</x>
<x if="field.type != 'group'">:field.pxRender</x>
</x>
</td>
</tr>
</table>''')
2013-09-24 05:26:31 -05:00
# PX that renders a group of fields (the group is refered as var "field").
2013-08-21 06:54:56 -05:00
pxView = Px('''
<x var="tagCss=field.master and ('slave*%s*%s' % \
(field.masterName, '*'.join(field.masterValue))) or '';
2013-08-21 06:54:56 -05:00
widgetCss=field.css_class;
groupCss=tagCss and ('%s %s' % (tagCss, widgetCss)) or widgetCss;
tagName=field.master and 'slave' or '';
tagId='%s_%s' % (zobj.id, field.name)">
2013-08-21 06:54:56 -05:00
<!-- Render the group as a fieldset if required -->
<fieldset if="field.style == 'fieldset'">
<legend if="field.hasLabel">
<i>::_(field.labelId)></i><x if="field.hasHelp">:field.pxHelp</x>
</legend>
<div if="field.hasDescr" class="discreet">::_(field.descrId)</div>
<x>:field.pxContent</x>
</fieldset>
<!-- Render the group as a section if required -->
<x if="field.style not in ('fieldset', 'tabs')">:field.pxContent</x>
<!-- Render the group as tabs if required -->
<x if="field.style == 'tabs'" var2="tabsCount=len(field.elements)">
2013-08-21 06:54:56 -05:00
<table width=":field.wide" class=":groupCss" id=":tagId" name=":tagName">
<!-- First row: the tabs. -->
<tr valign="middle"><td style="border-bottom: 1px solid #ff8040">
<table class="tabs" cellpadding="0" cellspacing="0">
<tr valign="middle">
<x for="sub in field.elements"
var2="nb = loop.sub.nb + 1;
suffix='%s_%d_%d' % (field.name, nb, tabsCount);
tabId='tab_%s' % suffix">
2013-08-21 06:54:56 -05:00
<td><img src=":url('tabLeft')" id=":'%s_left' % tabId"/></td>
<td style=":url('tabBg', bg=True)" class="tab" id=":tabId">
<a onclick=":'showTab(%s)' % q(suffix)"
class="clickable">:_(sub.labelId)</a>
2013-08-21 06:54:56 -05:00
</td>
<td><img id=":'%s_right' % tabId" src=":url('tabRight')"/></td>
</x>
</tr>
</table>
</td></tr>
<!-- Other rows: the fields -->
<tr for="sub in field.elements"
var2="nb=loop.sub.nb + 1"
id=":'tabcontent_%s_%d_%d' % (field.name, nb, tabsCount)"
style=":(nb == 1) and 'display:table-row' or 'display:none'">
<td var="field=sub">
2013-08-21 06:54:56 -05:00
<x if="field.type == 'group'">:field.pxView</x>
<x if="field.type != 'group'">:field.pxRender</x>
</td>
</tr>
</table>
<script type="text/javascript">:'initTab(%s,%s)' % \
(q('tab_%s' % field.name), q('%s_1_%d' % (field.name, tabsCount)))
2013-08-21 06:54:56 -05:00
</script>
</x>
</x>''')
# PX that renders a group of searches
2013-08-21 06:54:56 -05:00
pxViewSearches = Px('''
<x var="collapse=field.getCollapseInfo(field.labelId, req)">
2013-08-21 06:54:56 -05:00
<!-- Group name, prefixed by the expand/collapse icon -->
<div class="portletGroup"><x>:collapse.px</x>
2013-08-21 06:54:56 -05:00
<x if="not field.translated">:_(field.labelId)</x>
<x if="field.translated">:field.translated</x>
</div>
<!-- Group content -->
<div id=":collapse.id" style=":'padding-left: 10px; %s' % collapse.style">
<x for="searches in field.elements">
2013-08-21 06:54:56 -05:00
<x for="elem in searches">
<!-- An inner group within this group -->
<x if="elem.type== 'group'" var2="field=elem">:field.pxViewSearches</x>
2013-08-21 06:54:56 -05:00
<!-- A search -->
<x if="elem.type!= 'group'" var2="search=elem">:search.pxView</x>
2013-08-21 06:54:56 -05:00
</x>
</x>
</div></x>''')
2013-08-21 06:54:56 -05:00
2013-09-24 05:26:31 -05:00
# PX that renders a group of transitions.
pxViewTransitions = Px('''
<!-- Render a group of transitions, as a one-column table -->
<table>
<x for="row in uiGroup.elements">
<x for="transition in row"><tr><td>:transition.pxView</td></tr></x>
</x>
</table>''')
# What PX to use, depending on group content?
pxByContent = {'fields': pxView, 'searches': pxViewSearches,
'transitions': pxViewTransitions}
def __init__(self, group, page, className, content='fields'):
'''A UiGroup can group various kinds of elements: fields, searches,
transitions..., The type of content that one may find in this group
is given in p_content.
* p_group is the Group instance corresponding to this UiGroup;
* p_page is the Page instance where the group is rendered (for
transitions, it corresponds to a virtual page
"workflow");
* p_className is the name of the class that holds the elements to
group.'''
2013-08-21 06:54:56 -05:00
self.type = 'group'
2013-09-24 05:26:31 -05:00
# All p_group attributes become self attributes. This is required
# because a UiGroup, in some PXs, must behave like a Field (ie, have
# the same attributes, like "master".
for name, value in group.__dict__.items():
2013-08-21 06:54:56 -05:00
if not name.startswith('_'):
setattr(self, name, value)
2013-09-24 05:26:31 -05:00
self.group = group
2013-08-21 06:54:56 -05:00
self.columnsWidths = [col.width for col in group.columns]
self.columnsAligns = [col.align for col in group.columns]
# Names of i18n labels for this group
2013-08-21 06:54:56 -05:00
labelName = self.name
2013-09-24 05:26:31 -05:00
prefix = className
2013-08-21 06:54:56 -05:00
if group.label:
if isinstance(group.label, str): prefix = group.label
2013-09-24 05:26:31 -05:00
else: # It is a tuple (className, name)
2013-08-21 06:54:56 -05:00
if group.label[1]: labelName = group.label[1]
if group.label[0]: prefix = group.label[0]
2013-09-24 05:26:31 -05:00
gp = (content == 'searches') and 'searchgroup' or 'group'
2013-08-21 06:54:56 -05:00
self.labelId = '%s_%s_%s' % (prefix, gp, labelName)
self.descrId = self.labelId + '_descr'
self.helpId = self.labelId + '_help'
# The name of the page where the group lies
self.page = page.name
# The elements (fields or sub-groups) contained in the group, that the
# current user may see. They will be inserted by m_addElement below.
if self.style != 'tabs':
# In most cases, "elements" will be a list of lists for rendering
# them as a table.
self.elements = [[]]
else:
# If the group is a tab, elements will be stored as a simple list.
self.elements = []
2013-09-24 05:26:31 -05:00
# PX to use for rendering this group.
self.px = self.pxByContent[content]
2013-08-21 06:54:56 -05:00
2013-09-24 05:26:31 -05:00
def addElement(self, element):
'''Adds p_element into self.elements. We try first to add p_element into
the last row. If it is not possible, we create a new row.'''
if self.style == 'tabs':
self.elements.append(element)
return
2013-08-21 06:54:56 -05:00
# Get the last row
2013-09-24 05:26:31 -05:00
lastRow = self.elements[-1]
2013-08-21 06:54:56 -05:00
numberOfColumns = len(self.columnsWidths)
# Compute the number of columns already filled in the last row.
filledColumns = 0
2013-09-24 05:26:31 -05:00
for rowElem in lastRow: filledColumns += rowElem.colspan
2013-08-21 06:54:56 -05:00
freeColumns = numberOfColumns - filledColumns
2013-09-24 05:26:31 -05:00
if freeColumns >= element.colspan:
# We can add the element in the last row.
lastRow.append(element)
2013-08-21 06:54:56 -05:00
else:
if freeColumns:
# Terminate the current row by appending empty cells
for i in range(freeColumns): lastRow.append('')
# Create a new row
2013-09-24 05:26:31 -05:00
self.elements.append([element])
def getCollapseInfo(self, id, request):
'''Returns a Collapsible instance, that determines if this group,
represented as an expandable menu item, is collapsed or expanded.'''
return gutils.Collapsible(id, request)
2013-08-21 06:54:56 -05:00
# ------------------------------------------------------------------------------