src/pyams_portal/slot.py
changeset 0 6f99128c6d48
child 5 670b7956c689
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_portal/slot.py	Wed Jun 17 09:58:33 2015 +0200
@@ -0,0 +1,159 @@
+#
+# Copyright (c) 2008-2015 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+
+__docformat__ = 'restructuredtext'
+
+
+# import standard library
+
+# import interfaces
+from pyams_portal.interfaces import ISlotConfiguration, IPortalTemplateConfiguration, IPortalTemplate, IPortalPage
+
+# import packages
+from persistent import Persistent
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.schema.fieldproperty import FieldProperty
+
+
+PORTAL_SLOTS_KEY = 'pyams_portal.slots'
+
+
+@implementer(ISlotConfiguration)
+class SlotConfiguration(Persistent, Contained):
+    """Portal slot class"""
+
+    slot_name = FieldProperty(ISlotConfiguration['slot_name'])
+    visible = FieldProperty(ISlotConfiguration['visible'])
+    _inherit_parent = FieldProperty(ISlotConfiguration['inherit_parent'])
+    _xs_width = FieldProperty(ISlotConfiguration['xs_width'])
+    _sm_width = FieldProperty(ISlotConfiguration['sm_width'])
+    _md_width = FieldProperty(ISlotConfiguration['md_width'])
+    _lg_width = FieldProperty(ISlotConfiguration['lg_width'])
+    _css_class = FieldProperty(ISlotConfiguration['css_class'])
+
+    def __init__(self, slot_name, **kwargs):
+        self.slot_name = slot_name
+        self.xs_width = 12
+        self.sm_width = 12
+        self.md_width = 12
+        self.lg_width = 12
+        for key, value in kwargs.items():
+            setattr(self, key, value)
+
+    @property
+    def template(self):
+        if IPortalTemplate.providedBy(self.__parent__):
+            return self.__parent__
+        else:
+            return IPortalPage(self.__parent__).template
+
+    @property
+    def can_inherit(self):
+        return IPortalPage.providedBy(self.__parent__)
+
+    @property
+    def inherit_parent(self):
+        return self._inherit_parent if self.can_inherit else False
+
+    @inherit_parent.setter
+    def inherit_parent(self, value):
+        self._inherit_parent = value
+
+    @property
+    def xs_width(self):
+        if self.inherit_parent:
+            config = IPortalTemplateConfiguration(self.template)
+            return config.get_slot_configuration(self.slot_name).xs_width
+        else:
+            return self._xs_width
+        
+    @xs_width.setter
+    def xs_width(self, value):
+        self._xs_width = value
+
+    @property
+    def sm_width(self):
+        if self.inherit_parent:
+            config = IPortalTemplateConfiguration(self.template)
+            return config.get_slot_configuration(self.slot_name).sm_width
+        else:
+            return self._sm_width
+        
+    @sm_width.setter
+    def sm_width(self, value):
+        self._sm_width = value
+
+    @property
+    def md_width(self):
+        if self.inherit_parent:
+            config = IPortalTemplateConfiguration(self.template)
+            return config.get_slot_configuration(self.slot_name).md_width
+        else:
+            return self._md_width
+        
+    @md_width.setter
+    def md_width(self, value):
+        self._md_width = value
+
+    @property
+    def lg_width(self):
+        if self.inherit_parent:
+            config = IPortalTemplateConfiguration(self.template)
+            return config.get_slot_configuration(self.slot_name).lg_width
+        else:
+            return self._lg_width
+        
+    @lg_width.setter
+    def lg_width(self, value):
+        self._lg_width = value
+
+    @property
+    def css_class(self):
+        if self.inherit_parent:
+            config = IPortalTemplateConfiguration(self.template)
+            return config.get_slot_configuration(self.slot_name).css_class
+        else:
+            return self._css_class
+
+    @css_class.setter
+    def css_class(self, value):
+        self._css_class = value
+
+    def get_css_class(self, device=None):
+        if not device:
+            device = ('xs', 'sm', 'md', 'lg')
+        elif isinstance(device, str):
+            device = (device, )
+        result = [self.css_class or '']
+        for attr in device:
+            width = getattr(self, attr + '_width')
+            result.append('col-{0}-{1}'.format(attr, width))
+        return ' '.join(result)
+
+    def get_width(self, device=None):
+        if not device:
+            device = ('xs', 'sm', 'md', 'lg')
+        elif isinstance(device, str):
+            device = (device, )
+        result = {}
+        for attr in device:
+            result[attr] = getattr(self, attr + '_width')
+        return result
+
+    def set_width(self, width, device=None):
+        if not device:
+            device = ('xs', 'sm', 'md', 'lg')
+        elif isinstance(device, str):
+            device = (device, )
+        for attr in device:
+            setattr(self, attr + '_width', width)