Added JSOn events helpers functions
authorThierry Florac <thierry.florac@onf.fr>
Tue, 10 Apr 2018 15:24:06 +0200
changeset 305 1235a0c99263
parent 304 9076b2adce67
child 306 0a7df4cf44bf
Added JSOn events helpers functions
src/pyams_skin/event.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_skin/event.py	Tue Apr 10 15:24:06 2018 +0200
@@ -0,0 +1,122 @@
+#
+# Copyright (c) 2008-2018 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_utils.interfaces import ICacheKeyValue
+
+# import packages
+
+
+def get_json_refresh_event(object_id, content):
+    """Get JSON response for basic refresh event"""
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshContent',
+            'object_id': object_id,
+            'content': content
+        }
+    }
+
+
+def get_json_form_refresh_event(context, request, form_factory):
+    """Get JSON response for form refresh event"""
+    form = form_factory(context, request)
+    form.update()
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshContent',
+            'object_id': form.id,
+            'content': form.render()
+        }
+    }
+
+
+def get_json_widget_refresh_event(context, request, form_factory, widget):
+    """Get JSON response for form widget refresh event"""
+    form = form_factory(context, request)
+    form.update()
+    widget = form.widgets[widget]
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshWidget',
+            'parent_id': form.id,
+            'widget_name': widget.name,
+            'content': widget.render()
+        }
+    }
+
+
+def get_json_table_refresh_event(context, request, table_factory):
+    """Get JSON response for table refresh event"""
+    table = table_factory(context, request)
+    table.update()
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshTable',
+            'object_id': table.id,
+            'table': table.render()
+        }
+    }
+
+
+def get_json_switched_table_refresh_event(context, request, table_factory):
+    """Get JSON response for switched table refresh event"""
+    table = table_factory(context, request)
+    table.update()
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshSwitchedTable',
+            'object_id': table.id,
+            'table': table.render()
+        }
+    }
+
+
+def get_json_table_row_refresh_event(context, request, table_factory, table_item):
+    """Get JSON response for table row refresh event"""
+    table = table_factory(context, request)
+    table.update()
+    row = table.setUpRow(table_item)
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshRow',
+            'object_id': '{0}::{1}'.format(table.id, ICacheKeyValue(table_item)),
+            'row': table.renderRow(row)
+        }
+    }
+
+
+def get_json_table_cell_refresh_event(context, request, object_id, col_name, cell_content=None, column_factory=None):
+    """Get JSON response for table cell refresh event"""
+    if cell_content is None:
+        column = column_factory(context, request, None)
+        cell_content = column.renderCell(context)
+    return {
+        'event': 'myams.refresh',
+        'options': {
+            'handler': 'MyAMS.skin.refreshRowCell',
+            'object_id': object_id,
+            'col_name': col_name,
+            'cell': cell_content
+        }
+    }