Use dynamic dialog class based on image width in adding and editing forms
authorThierry Florac <thierry.florac@onf.fr>
Wed, 14 Jun 2017 15:19:38 +0200
changeset 85 f6474843036e
parent 84 7214a95eaa9b
child 86 5d179f78817b
Use dynamic dialog class based on image width in adding and editing forms
src/pyams_content/shared/imagemap/zmi/area.py
--- a/src/pyams_content/shared/imagemap/zmi/area.py	Wed Jun 14 15:19:03 2017 +0200
+++ b/src/pyams_content/shared/imagemap/zmi/area.py	Wed Jun 14 15:19:38 2017 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyams_i18n.interfaces import II18n
 
 __docformat__ = 'restructuredtext'
 
@@ -62,7 +63,20 @@
     """Imagemap area add form"""
 
     legend = _("Add new image area")
-    dialog_class = 'modal-large'
+
+    @property
+    def dialog_class(self):
+        image = II18n(self.context).query_attribute('image', request=self.request)
+        if image is not None:
+            width = image.get_image_size()[0]
+            if width > 1000:
+                return 'modal-max'
+            elif width > 800:
+                return 'modal-xlarge'
+            else:
+                return 'modal-large'
+        else:
+            return 'modal-large'
 
     fields = field.Fields(IImageMapArea)
     fields['link'].widgetFactory = SingleLinkLinkSelectFieldWidget
@@ -92,7 +106,20 @@
     """Image map area properties edit form"""
 
     legend = _("Edit image map properties")
-    dialog_class = 'modal-large'
+
+    @property
+    def dialog_class(self):
+        image = II18n(self.context.__parent__).query_attribute('image', request=self.request)
+        if image is not None:
+            width = image.get_image_size()[0]
+            if width > 1000:
+                return 'modal-max'
+            elif width > 800:
+                return 'modal-xlarge'
+            else:
+                return 'modal-large'
+        else:
+            return 'modal-large'
 
     fields = field.Fields(IImageMapArea)
     fields['link'].widgetFactory = SingleLinkLinkSelectFieldWidget