src/pyams_content/shared/imagemap/__init__.py
changeset 69 8c5bbc396670
child 139 99a481dc4c89
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_content/shared/imagemap/__init__.py	Fri Jan 20 15:42:51 2017 +0100
@@ -0,0 +1,108 @@
+#
+# 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
+from persistent import Persistent
+from persistent.mapping import PersistentMapping
+
+# import interfaces
+from pyams_content.component.links.interfaces import ILinkContainerTarget
+from pyams_content.shared.imagemap.interfaces import IMAGEMAP_CONTENT_TYPE, IMAGEMAP_CONTENT_NAME, \
+    IWfImageMap, IImageMap, IImageMapArea
+from zope.location.interfaces import ISublocations
+from zope.traversing.interfaces import ITraversable
+
+# import packages
+from pyams_content.shared.common import WfSharedContent, register_content_type, SharedContent
+from pyams_i18n.property import I18nFileProperty
+from pyams_utils.adapter import adapter_config, ContextAdapter
+from pyramid.threadlocal import get_current_registry
+from zope.container.contained import Contained
+from zope.interface import implementer
+from zope.lifecycleevent import ObjectModifiedEvent
+from zope.location import locate
+from zope.schema.fieldproperty import FieldProperty
+
+
+@implementer(IImageMapArea)
+class ImageMapArea(Persistent, Contained):
+    """Image map area class"""
+
+    title = FieldProperty(IImageMapArea['title'])
+    link = FieldProperty(IImageMapArea['link'])
+    area = FieldProperty(IImageMapArea['area'])
+
+
+@implementer(IWfImageMap, ILinkContainerTarget)
+class WfImageMap(WfSharedContent):
+    """Base image map"""
+
+    content_type = IMAGEMAP_CONTENT_TYPE
+    content_name = IMAGEMAP_CONTENT_NAME
+
+    _image = I18nFileProperty(IWfImageMap['image'])
+    areas = FieldProperty(IWfImageMap['areas'])
+
+    _index = 0
+
+    def __init__(self):
+        self.areas = PersistentMapping()
+
+    @property
+    def image(self):
+        return self._image
+
+    @image.setter
+    def image(self, value):
+        self._image = value
+        if value:
+            self.areas = PersistentMapping()
+
+    def add_area(self, area):
+        self._index += 1
+        key = str(self._index)
+        self.areas[key] = area
+        locate(area, self, '++areas++{0}'.format(key))
+        get_current_registry().notify(ObjectModifiedEvent(self))
+
+    def remove_area(self, key):
+        if key in self.areas:
+            del self.areas[key]
+            get_current_registry().notify(ObjectModifiedEvent(self))
+
+register_content_type(WfImageMap)
+
+
+@implementer(IImageMap)
+class ImageMap(SharedContent):
+    """Workflow managed image map class"""
+
+    content_class = WfImageMap
+
+
+@adapter_config(name='areas', context=IWfImageMap, provides=ITraversable)
+class ImapemapAreasNamespace(ContextAdapter):
+    """++areas++ namespace traverser"""
+
+    def traverse(self, name, furtherpath=None):
+        return self.context.areas[name]
+
+
+@adapter_config(name='areas', context=IWfImageMap, provides=ISublocations)
+class ImagemapAreasSublocations(ContextAdapter):
+    """Image map area sublocations"""
+
+    def sublocations(self):
+        return self.context.areas.values()