source: products/quintagroup.referencedatagridfield/trunk/quintagroup/referencedatagridfield/columns.py @ 2290

Last change on this file since 2290 was 2290, checked in by mylan, 14 years ago

Added StyledColumn? with possibility to change style on focus and blur of the input tag

  • Property svn:eol-style set to native
File size: 4.5 KB
Line 
1from plone.memoize.view import memoize
2
3from AccessControl import ClassSecurityInfo
4from Globals import InitializeClass
5from Products.DataGridField.Column import Column
6
7class HiddenColumn(Column):
8    """ Column with non-changeable text
9   
10    Useful with DataGridField.fixed_row property in some use cases.
11    """
12    security = ClassSecurityInfo()
13
14    def __init__(self, label, default=None, label_msgid=None, visible=True):
15        """ Create a column
16       
17            @param hide Hide column from displaying
18        """
19        Column.__init__(self, label, default, label_msgid)
20        self.visible = visible
21
22    security.declarePublic('getMacro')
23    def getMacro(self):
24        """ Return macro used to render this column in view/edit """
25        return "datagrid_hidden_cell"
26
27# Initializes class security
28InitializeClass(HiddenColumn)
29
30class BlockColumn(Column):
31    """ Column with possibility to block the cell data from changing,
32        based on the value of the row data.
33    """
34    security = ClassSecurityInfo()
35
36    def __init__(self, label, default=None, label_msgid=None,
37                 column_on_class=None, column_off_class=None,
38                 columns=[], invert=False, read_only=None):
39        """ Create a column, with adding class attribute depend on value presence for the cell.
40       
41            @param column_on_class set class, which will be added to the <input> tag if columns has value
42            @param column_off_class set class, which will be added to the <input> tag if columns not has value
43            @param columns list of columns names, which values will be checked for set class attribute
44            @param invert - invert the adding class logic
45            @param read_only - check-on/off readOnly attribute for the <input> tag
46        """
47        Column.__init__(self, label, default, label_msgid)
48        self.column_on_class = column_on_class
49        self.column_off_class = column_off_class
50        self.columns = columns
51        self.invert = invert
52        self.read_only = read_only
53
54    def passCondition(self, row_data):
55        """ Return calculated class attribute."""
56        res = sum([1 for c in self.columns if row_data.get(c,0)])
57        return res == len(self.columns) and not self.invert
58   
59    security.declarePublic('getAttributesData')
60    def getAttributesData(self, row_data):
61        """ Return calculated class attribute."""
62        res = {'style_class': None, 'read_only': None}
63        isPassCondition = self.passCondition(row_data)
64        if self.column_on_class or self.column_off_class:
65           res['style_class'] = isPassCondition and self.column_on_class or self.column_off_class
66        if self.read_only:
67           res['read_only'] = isPassCondition and True or False
68
69        return res
70   
71    security.declarePublic('getMacro')
72    def getMacro(self):
73        """ Return macro used to render this column in view/edit """
74        return "datagrid_block_cell"
75
76# Initializes class security
77InitializeClass(BlockColumn)
78
79class StyledColumn(Column):
80    """ Column with styling based on events."""
81    security = ClassSecurityInfo()
82
83   
84
85    def __init__(self, label, default=None, label_msgid=None,
86                 trigger_key=None, blur_handler="", focus_handler="",
87                 class_no="", class_changed="", class_not_changed=""):
88        """ Create a column
89       
90            @param trigger_key
91        """
92        Column.__init__(self, label, default, label_msgid)
93        self.trigger = trigger_key
94        self.blur_handler = blur_handler and blur_handler + "(event)" or ""
95        self.focus_handler = focus_handler and focus_handler + "(event)" or ""
96        self.class_no = class_no
97        self.class_not_changed = class_not_changed
98        self.class_changed = class_changed
99
100    security.declarePublic("getAttributes")
101    def getAttributes(self, rows):
102        blur_handler = None
103        focus_handler = None
104        sclass = self.class_no
105
106        if rows.has_key(self.trigger):
107            focus_handler = self.focus_handler
108            blur_handler = self.blur_handler
109            if bool(rows[self.trigger]):
110                sclass = self.class_changed
111            else:
112                sclass = self.class_not_changed
113
114        return {'class': sclass,
115                'onblur': blur_handler,
116                'onfocus': focus_handler}
117
118    security.declarePublic('getMacro')
119    def getMacro(self):
120        """ Return macro used to render this column in view/edit """
121        return "datagrid_styled_cell"
122
123# Initializes class security
124InitializeClass(StyledColumn)
125
Note: See TracBrowser for help on using the repository browser.