MidCOM svn: r12661 - in trunk/midcom/midcom.helper.datamanager2: static/privilege widget

w_i midcom-commits at lists.midgard-project.org
Thu Oct 4 19:27:24 CEST 2007


Author: w_i
Date: Thu Oct  4 19:27:23 2007
New Revision: 12661
URL: http://trac.midgard-project.org/software/changeset/12661

Log:
Added new privilege widget for the new acl-editor

Added:
   trunk/midcom/midcom.helper.datamanager2/static/privilege/
   trunk/midcom/midcom.helper.datamanager2/static/privilege/jquery.privilege.css
   trunk/midcom/midcom.helper.datamanager2/static/privilege/jquery.privilege.js
   trunk/midcom/midcom.helper.datamanager2/static/privilege/privileges.gif   (contents, props changed)
   trunk/midcom/midcom.helper.datamanager2/widget/privilegeselection.php

Added: trunk/midcom/midcom.helper.datamanager2/static/privilege/jquery.privilege.css
==============================================================================
--- (empty file)
+++ trunk/midcom/midcom.helper.datamanager2/static/privilege/jquery.privilege.css	Thu Oct  4 19:27:23 2007
@@ -0,0 +1,52 @@
+.privilege_val {
+    width: 16px;
+    height: 16px;
+    overflow: hidden;
+    text-indent: -999em;
+    cursor: pointer;
+	display: none;
+}
+.privilege_val a {
+	width: 16px;
+    height: 16px;
+    display: block;
+    display: block;
+    background: url(privileges.gif) no-repeat top left;
+}
+
+.privilege_val a.inherited,
+.privilege_val a.inherited:hover
+{
+	background-position:  0 0px;
+}
+div.privilege_val a.inherited-allow,
+div.privilege_val a.inherited-allow:hover
+{
+	background-position:  0 -16px;
+}
+.privilege_val a.inherited-deny,
+.privilege_val a.inherited-deny:hover
+{
+	background-position:  0 -32px;
+}
+.privilege_val a.allow,
+.privilege_val a.allow:hover
+{
+	background-position:  0 -48px;
+}
+.privilege_val a.deny,
+.privilege_val a.deny:hover
+{
+	background-position:  0 -64px;
+}
+
+div.privilege_actions,
+div.privilege_action
+{
+    display: inline;
+    margin-right: 3px;
+}
+div.privilege_action
+{
+    cursor: pointer;
+}

Added: trunk/midcom/midcom.helper.datamanager2/static/privilege/jquery.privilege.js
==============================================================================
--- (empty file)
+++ trunk/midcom/midcom.helper.datamanager2/static/privilege/jquery.privilege.js	Thu Oct  4 19:27:23 2007
@@ -0,0 +1,187 @@
+/**
+ * background mover - jQuery plugin
+ */
+
+/**
+ * Moves elements background depending on given parameters
+ *   
+ * @example jQuery('a.image-link').moveBackground({startPos: 0, endPos: -16});
+ * @cat plugin
+ * @type jQuery 
+ *
+ */
+jQuery.fn.moveBackground = function(settings) {
+	settings = jQuery.extend({
+		direction: "down",
+		startPos: 0,
+		endPos: 0,
+		time: null,
+		startLeft: null,
+		startTop: null,
+		callback: null,
+		callbackArgs: [],
+	}, settings);
+
+	var element = this[0];
+	var direction = settings.direction;
+	var startPos = settings.startPos;
+	var endPos = settings.endPos;
+	var time = settings.time;
+
+	if(direction == "down" || direction == "d")
+	{
+		endPos -= 1;
+	}
+	
+	var left = settings.startLeft != null ? settings.startLeft : 0;
+	var top = settings.startTop != null ? settings.startTop : 0;
+	
+	if(direction == "down" || direction == "d") {
+		top = startPos;
+	}
+	
+	function anim()
+	{
+		var leftPos = "px";
+		var topPos = "px";
+		
+		if(direction == "down" || direction == "d")
+		{
+			if (endPos < top) {
+				top -= 1;
+			}
+			
+			if (top != endPos) {
+				setTimeout(anim, time);
+			} else {
+				if(settings.callback != null) {
+					element.style.backgroundPosition = "";
+					settings.callback.call(settings.callbackArgs[0], settings.callbackArgs);
+				}
+				return this;
+			}
+		}
+		
+		leftPos = left + leftPos;
+		topPos = top + topPos;
+		
+		var posStr = leftPos + " " + topPos;
+
+		element.style.backgroundPosition = posStr;
+	}
+
+	anim();
+};
+
+/**
+ * privilege renderer - jQuery plugin
+ */
+
+/**
+ * Create a multiface checkbox interface out of a simple form structure.
+ *   
+ * @example jQuery('#select-holder').render_privilege();
+ * @cat plugin
+ * @type jQuery 
+ *
+ */
+jQuery.fn.render_privilege = function(settings) {
+	settings = jQuery.extend({
+		imageWidth: 16,
+		imageHeight: 16,
+		maxIndex: 3,
+		animate: false,
+	}, settings);
+	
+    return this.each(function() {
+        var div = jQuery("<div/>").insertAfter( this );
+		
+		var list_menu = jQuery(this).find("select")[0];
+		var nextValue = 0;
+		var selected_index = 0;
+		
+		jQuery(this).find('select').each(function(){
+		    jQuery(this).bind('onchange', function(e, val){
+                div.find('div.privilege_val').trigger('click', []);
+            });
+		});
+		
+        jQuery(this).find("select option").each(function(){
+			//var classes = [ 'inherited', 'inherited-allow', 'inherited-deny', 'allow', 'deny'];
+			var classes = [ null, 'allow', 'deny', 'inherited'];
+			var block_style = this.selected ? "style='display: block;'" : "";
+            div.append( "<div class='privilege_val' " + block_style + "><a class='" + classes[this.value] + "' href='#" + this.value + "' title='" + this.innerHTML + "'>" + this.innerHTML + "</a></div>" );
+        });
+
+        var selects = div.find('div.privilege_val').click(click);
+        
+        function click() {
+            selected_index = selects.index(this) + 1;
+
+			var href = jQuery(this).find('a')[0].href;
+			var currentValue = href.charAt(href.length-1);
+			
+			if(prevValue == undefined) {
+				var prevValue = currentValue;
+			} else {
+				prevValue = currentValue;
+			}
+			
+			nextHref = jQuery(selects[(selected_index >= settings.maxIndex ? settings.maxIndex : selected_index)]).find('a')[0].href;
+			nextValue = nextHref.charAt(nextHref.length-1);
+			
+			if(selected_index == settings.maxIndex) {
+			 	selected_index = 0;
+			 	var startPos = 0;
+			} else {
+				var startPos = 0 - (prevValue * settings.imageHeight);
+			}
+
+			var endPos = 0 - (nextValue * settings.imageHeight);
+			
+			if(settings.animate == true) {
+				jQuery(jQuery(this).find('a')[0]).moveBackground({ startPos: startPos, endPos: endPos, time: 25, callback: showNext, callbackArgs: [this,selects[selected_index]] });
+			} else {
+				showNext([this]);
+			}
+			
+			list_menu.selectedIndex = selected_index;
+        	
+			prevValue = currentValue;
+
+            return false;
+        }
+
+		function showNext(args)
+		{
+			jQuery(args[0]).hide();
+			jQuery(selects[selected_index]).show();			
+		}
+
+    }).hide();
+};
+
+
+jQuery.fn.privilege_actions = function(privilege_key) {
+    return this.each(function() {
+        var row = this;
+        var actions_holder = jQuery('#privilege_row_actions_' + privilege_key, row);
+        var clear_action = jQuery('<div class="privilege_action" />').insertAfter( actions_holder );
+        var clear_action_icon = jQuery('<img src="' + MIDCOM_STATIC_URL + '/stock-icons/16x16/cancel.png" />').attr({
+            alt: "Clear privileges",
+            border: 0
+        }).appendTo(clear_action);
+        
+        clear_action.bind('click', function(e){
+            jQuery('select', row).each(function(i,n){
+                jQuery(n).val(3);
+                jQuery(n).trigger('onchange', [0]);
+                jQuery(row).hide();
+            });
+        });
+    });
+};
+
+// fix ie6 background flicker problem.
+if ( jQuery.browser.msie == true )
+    document.execCommand('BackgroundImageCache', false, true);

Added: trunk/midcom/midcom.helper.datamanager2/static/privilege/privileges.gif
==============================================================================
Binary file. No diff available.

Added: trunk/midcom/midcom.helper.datamanager2/widget/privilegeselection.php
==============================================================================
--- (empty file)
+++ trunk/midcom/midcom.helper.datamanager2/widget/privilegeselection.php	Thu Oct  4 19:27:23 2007
@@ -0,0 +1,206 @@
+<?php
+/**
+ * @package midcom.helper.datamanager2
+ * @author The Midgard Project, http://www.midgard-project.org
+ * @version $Id: privilege.php 10966 2007-06-15 07:00:37Z adrenalin $
+ * @copyright The Midgard Project, http://www.midgard-project.org
+ * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
+ */
+
+/**
+ * Datamanager 2 privilege selection
+ *
+ * As with all subclasses, the actual initialization is done in the initialize() function,
+ * not in the constructor, to allow for error handling.
+ *
+ * This widget supports the privilege type only. It shows a menu rendered as multi state checkbox having
+ * allowed, denied and inherited as options and default selected according to the type's value.
+ *
+ * The widget will not display itself if the user does not have privileges permission on 
+ * the storage object.
+ * 
+ * @package midcom.helper.datamanager2
+ */
+class midcom_helper_datamanager2_widget_privilegeselection extends midcom_helper_datamanager2_widget
+{
+    /**
+     * The QF Element added to the form. Saved for freezing/unfreezing.
+     * 
+     * @var Array
+     * @access protected
+     */
+    var $_element = null;
+    
+    /**
+     * Holds the privileges to be included in the selection widget
+     * 
+     * @var Array
+     * @access protected
+     */
+    var $_privilege_options = null;
+
+    /**
+     * Holds the javascript to render the privilege selection widget
+     * 
+     * @var String
+     * @access protected
+     */    
+    var $_jscript = '';
+    
+    /**
+     * The initialization event handler validates the base type.
+     *
+     * @return bool Indicating Success
+     */
+    function _on_initialize()
+    {
+        if (! is_a($this->_type, 'midcom_helper_datamanager2_type_privilege'))
+        {
+            debug_push_class(__CLASS__, __FUNCTION__);
+            debug_add("Warning, the field {$this->name} is not of type privilege.", MIDCOM_LOG_WARN);
+            debug_pop();
+            return false;
+        }
+        
+        $this->_privilege_options = array(
+            MIDCOM_PRIVILEGE_INHERIT => $this->_l10n->get('widget privilege: inherit'),
+            MIDCOM_PRIVILEGE_ALLOW => $this->_l10n->get('widget privilege: allow'),
+            MIDCOM_PRIVILEGE_DENY => $this->_l10n->get('widget privilege: deny'),
+        );
+        
+        $_MIDCOM->enable_jquery();
+
+        $_MIDCOM->add_link_head(
+            array(
+                'rel' => 'stylesheet',
+                'type' => 'text/css',
+                'href' => MIDCOM_STATIC_URL . '/midcom.helper.datamanager2/privilege/jquery.privilege.css'
+            )
+        );
+        
+        $_MIDCOM->add_jsfile(MIDCOM_STATIC_URL . '/midcom.helper.datamanager2/privilege/jquery.privilege.js');
+        
+        return true;
+    }
+
+    /**
+     * Adds the elements to the form if we have the permissions.
+     */
+    function add_elements_to_form()
+    {
+        if (   $this->_type->storage->object
+            && ! $this->_type->storage->object->can_do('midgard:privileges'))
+        {
+            return;
+        }
+
+        $elements = array();
+
+        $select_attributes = Array
+        (
+            'class' => 'dropdown privilegeselection',
+            'id'    => "{$this->_namespace}{$this->name}",
+        );
+        $this->_element =& HTML_QuickForm::createElement('select', $this->name, '',
+            $this->_privilege_options, $select_attributes);
+        
+        $this->_jscript = '<script type="text/javascript">';
+        $this->_jscript .= 'jQuery("#' . $this->_namespace . $this->name . '_holder").render_privilege();';
+        $this->_jscript .= '</script>';
+
+        $elements[] =& HTML_QuickForm::createElement
+        (
+            'static',
+            "{$this->_namespace}{$this->name}_holder_start",
+            '',
+            "<div id=\"{$this->_namespace}{$this->name}_holder\">"
+        );      
+        $elements[] =& $this->_element;        
+        $elements[] =& HTML_QuickForm::createElement
+        (
+            'static',
+            "{$this->_namespace}{$this->name}_holder_end",
+            '',
+            "</div>"
+        );
+        $elements[] =& HTML_QuickForm::createElement
+        (
+            'static',
+            "{$this->_namespace}{$this->name}_initscripts",
+            '',
+            $this->_jscript
+        );
+            
+        $group =& $this->_form->addGroup
+        (
+            $elements, 
+            $this->name, 
+            '', 
+            '',
+            Array('class' => 'privilegeselection')
+        );
+    }
+
+    function get_default()
+    {
+        $key = $this->_type->get_value();
+        if (! $key)
+        {
+            reset($this->_privilege_options);
+            $key = key($this->_privilege_options);
+        }
+
+        return array($this->name => $key);
+    }
+    
+    function sync_type_with_widget($results)
+    {
+        if (   $this->_type->storage->object
+            && ! $this->_type->storage->object->can_do('midgard:privileges'))
+        {
+            return;
+        }
+
+        $this->_type->set_value($results[$this->name]);
+    }
+
+    function is_frozen()
+    {
+        if (    (   $this->_type->storage->object
+                 && ! $this->_type->storage->object->can_do('midgard:privileges'))
+             || ! $this->_element)
+        {
+            return false;
+        }
+        else
+        {        
+            return $this->_element->isFrozen();
+        } 
+    }
+    
+    function freeze()
+    {
+        if (    (   $this->_type->storage->object
+                 && ! $this->_type->storage->object->can_do('midgard:privileges'))
+             || ! $this->_element)
+        {
+            return;
+        }
+        
+        $this->_element->freeze();
+    }
+    
+    function unfreeze()
+    {
+        if (    (   $this->_type->storage->object
+                 && ! $this->_type->storage->object->can_do('midgard:privileges'))
+             || ! $this->_element)
+        {
+            return;
+        }
+         
+        $this->_element->unfreeze();
+    }
+}
+
+?>
\ No newline at end of file


More information about the midcom-commits mailing list