Added generic functions to handle elements visibility
authortflorac@dagon.home
Thu, 05 Apr 2018 22:35:31 +0200
changeset 297 a3d2ef66e7ea
parent 296 b2b043bf066a
child 298 399c586fc1a5
Added generic functions to handle elements visibility
src/pyams_skin/container.py
src/pyams_skin/resources/js/myams.js
src/pyams_skin/table.py
--- a/src/pyams_skin/container.py	Tue Apr 03 15:47:05 2018 +0200
+++ b/src/pyams_skin/container.py	Thu Apr 05 22:35:31 2018 +0200
@@ -24,6 +24,7 @@
 # import packages
 from pyams_skin.table import BaseTable
 from pyams_template.template import template_config
+from pyramid.exceptions import NotFound
 from pyramid.view import view_config
 from zope.interface import implementer
 
@@ -53,12 +54,43 @@
     translate = request.localizer.translate
     name = request.params.get('object_name')
     if not name:
-        return {'status': 'message',
-                'messagebox': {'status': 'error',
-                               'content': translate(_("No provided object_name argument!"))}}
+        return {
+            'status': 'message',
+            'messagebox': {
+                'status': 'error',
+                'content': translate(_("No provided object_name argument!"))
+            }
+        }
     if name not in request.context:
-        return {'status': 'message',
-                'messagebox': {'status': 'error',
-                               'content': translate(_("Given plug-in name doesn't exist!"))}}
+        return {
+            'status': 'message',
+            'messagebox': {
+                'status': 'error',
+                'content': translate(_("Given plug-in name doesn't exist!"))
+            }
+        }
     del request.context[name]
     return {'status': 'success'}
+
+
+def set_element_visibility(request, interface):
+    """Set container element visibility
+
+    :param request: original browser request; request should contain a parameter called
+        "object_name" which contains the name of the element which should be switched.
+        A NotFound exception is raised if argument is not provided or if given argument
+        doesn't match an existing element.
+    :param interface: container interface to which request's context should be adapted
+    :return: a JSON object containing a boolean "visible" property defining new element visibility.
+    """
+    container = interface(request.context, None)
+    if container is None:
+        raise NotFound()
+    object_name = request.params.get('object_name')
+    if not object_name:
+        raise NotFound()
+    element = container.get(str(object_name))
+    if element is None:
+        raise NotFound()
+    element.visible = not element.visible
+    return {'visible': element.visible}
--- a/src/pyams_skin/resources/js/myams.js	Tue Apr 03 15:47:05 2018 +0200
+++ b/src/pyams_skin/resources/js/myams.js	Thu Apr 05 22:35:31 2018 +0200
@@ -2,7 +2,7 @@
  * MyAMS
  * « My Application Management Skin »
  *
- * $Tag$ (rev. 1)
+ * $Tag: 0.1.14 $ (rev. 1)
  * A bootstrap based application/administration skin
  *
  * Custom administration and application skin tools
@@ -4826,10 +4826,9 @@
 		/**
 		 * Delete an element from a container table
 		 *
-		 * @param element
 		 * @returns {Function}
 		 */
-		deleteElement: function(element) {
+		deleteElement: function() {
 			return function() {
 				var link = $(this);
 				MyAMS.skin.bigBox({
@@ -4861,6 +4860,27 @@
 					}
 				});
 			};
+		},
+
+        /**
+		 * Switch element visibility
+         */
+        switchElementVisibility: function() {
+        	return function() {
+        		var source = $(this);
+        		var element = source.parents('tr').first();
+        		var container = element.parents('table');
+        		ams.ajax.post(container.data('ams-location') + '/' +
+							  container.data('ams-visibility-target'),
+					{object_name: element.data('ams-element-name')},
+					function(result, status) {
+        				if (result.visible) {
+        					$('i', source).attr('class', 'fa fa-fw fa-eye');
+						} else {
+        					$('i', source).attr('class', 'fa fa-fw fa-eye-slash text-danger');
+						}
+					});
+			}
 		}
 	};
 
@@ -5197,6 +5217,21 @@
 			MyAMS.initContent(widget);
 			return widget;
 		},
+
+        /**
+		 * Replace given table with new content
+		 * If table is located inside a switched fieldset, fieldset is opened
+		 *
+         * @param changes
+         */
+		refreshSwitchedTable: function(changes) {
+			var widget = ams.skin.refreshTable(changes);
+			var legend = widget.siblings('legend');
+			if (legend.parents('fieldset:first').hasClass('switched')) {
+				legend.click();
+			}
+		},
+
 		/**
 		 * Replace given row with new content
 		 */
--- a/src/pyams_skin/table.py	Tue Apr 03 15:47:05 2018 +0200
+++ b/src/pyams_skin/table.py	Thu Apr 05 22:35:31 2018 +0200
@@ -304,6 +304,33 @@
         return '#'
 
 
+class VisibilitySwitcherColumn(JsActionColumn):
+    """Visibility switcher column"""
+
+    cssClasses = {'th': 'action',
+                  'td': 'action switcher'}
+
+    visible_icon_class = 'fa fa-fw fa-eye'
+    hidden_icon_class = 'fa fa-fw fa-eye-slash text-danger'
+
+    url = 'MyAMS.container.switchElementVisibility'
+    weight = 5
+
+    def get_icon(self, item):
+        if item.visible:
+            icon_class = self.visible_icon_class
+        else:
+            icon_class = self.hidden_icon_class
+        return '<i class="{0}"></i>'.format(icon_class)
+
+    def renderCell(self, item):
+        permission = self.permission
+        if permission and self.request.has_permission(permission, context=item):
+            return self.get_icon(item)
+        else:
+            return super(VisibilitySwitcherColumn, self).renderCell(item)
+
+
 @adapter_config(name='actions', context=(Interface, IPyAMSLayer, ITableWithActions), provides=IColumn)
 class MenuActionsColumn(I18nColumn, Column):
     """Menu actions column"""