Code

Updated sortableListing
authorcajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 8 Jan 2010 17:21:16 +0000 (17:21 +0000)
committercajus <cajus@594d385d-05f5-0310-b6e9-bd551577e9d8>
Fri, 8 Jan 2010 17:21:16 +0000 (17:21 +0000)
git-svn-id: https://oss.gonicus.de/repositories/gosa/trunk@15131 594d385d-05f5-0310-b6e9-bd551577e9d8

gosa-core/html/include/dragdrop.js
gosa-core/html/themes/default/style.css
gosa-core/include/class_sortableListing.inc

index 07c98e2bd3b2a7a01cd8f576d6ec133e66bff93a..e51439e3867f04a0c69d4c8e7e4b9eff5e52acf0 100644 (file)
@@ -972,4 +972,4 @@ Element.findChildren = function(element, only, recursive, tagName) {
 
 Element.offsetSize = function (element, type) {
   return element['offset' + ((type=='vertical' || type=='height') ? 'Height' : 'Width')];
-};
\ No newline at end of file
+};
index 695ecbecba8b9c3e10f6e8ac7a762fc6fd4548d2..74282e289b44ad1653d3f785b944e0b79c5d91ab 100644 (file)
@@ -1956,12 +1956,3 @@ table.sortableListTable {
     border: 0px;
 }
 
-div.dropmarker {
-      height:1px;
-      border-top:1px solid #A00;
-      color: #fff;
-      z-index:1000;
-      margin : 0 0 0 0;
-}
-
-
index 53c61c34e38f3fb4e273f73758bd6fb68d1e7d86..d023584cf3461e53f744fbe954c69b0983c0ea34 100644 (file)
@@ -31,6 +31,14 @@ class sortableListing {
 
   private $data= array();
   private $columns= 0;
+  private $deletable= false;
+  private $editable= false;
+  private $instantDelete= true;
+  private $action;
+  private $targets;
+  private $mapping;
+  private $current_mapping;
+  private $active_index;
 
   function sortableListing($data= array())
   {
@@ -54,6 +62,9 @@ class sortableListing {
     // Transfer information
     $this->data= $data;
 
+    // Create initial mapping
+    $this->mapping= array_keys($data);
+
     // Find the number of coluns
     reset($this->data);
     $first= current($this->data);
@@ -62,7 +73,6 @@ class sortableListing {
     } else {
       $this->columns= 1;
     }
-
   }
 
 
@@ -72,6 +82,24 @@ class sortableListing {
   }
 
 
+  function setInstantDelete($flag)
+  {
+    $this->instantDelete= $flag;
+  }
+
+
+  function setEditable($flag)
+  {
+    $this->editable= $flag;
+  }
+
+
+  function setDeleteable($flag)
+  {
+    $this->deleteable= $flag;
+  }
+
+
   function setHeight($height)
   {
     $this->height= $height;
@@ -98,8 +126,11 @@ class sortableListing {
 
   function render()
   {
-    $result= "<div class='sortableListContainer' style='width: ".$this->width."; height: ".$this->height."'>\n";
+    $result= "<div class='sortableListContainer' id='scroll_".$this->id."' style='width: ".$this->width."; height: ".$this->height."'>\n";
     $result.= "<table border='0' cellpadding='0' cellspacing='0' width='100%' height='100%' style='position: relative;'".(!empty($this->cssclass)?" class='".$this->cssclass."'":"").">\n";
+    $action_width= 0;
+    $edit_image= $this->editable?"<input class='center' type='image' src='images/lists/edit.png' alt='"._("Edit")."' name='%ID' title='"._("Edit this entry")."'>":"";
+    $delete_image= $this->deleteable?"<input class='center' type='image' src='images/lists/trash.png' alt='"._("Delete")."' name='%ID' title='"._("Delete this entry")."'>":"";
 
     // Do we need colspecs?
     if ($this->colspecs) {
@@ -107,6 +138,12 @@ class sortableListing {
       for ($i= 0; $i<$this->columns; $i++) {
         $result.= "  <col width='".(isset($this->colspecs[$i])?$this->colspecs[$i]:"*")."'/>\n";
       }
+
+      // Extend by another column if we've actions specified
+      $action_width= ($this->editable?20:0) + ($this->deleteable?20:0);
+      if ($action_width) {
+        $result.= "  <col width='$action_width'/>\n";
+      }
       $result.= " </colgroup>\n";
     }
 
@@ -114,36 +151,41 @@ class sortableListing {
     if ($this->header) {
       $result.= " <thead>\n  <tr>\n";
       for ($i= 0; $i<$this->columns; $i++) {
-        $result.= "   <th>".(isset($this->header[$i])?$this->header[$i]:"")."</th>\n";
+        $result.= "   <th>".(isset($this->header[$i])?$this->header[$i]:"")."</th>";
       }
-      $result.= "  </tr>\n </thead>\n";
+      if ($action_width) {
+        $result.= "<th>&nbsp;</th>";
+      }
+      $result.= "\n  </tr>\n </thead>\n";
     }
 
     // Render table body
     $result.= " <tbody id='".$this->id."'>\n";
-    foreach ($this->data as $nr => $row) {
-      $result.= "  <tr class='sortableListItem".($nr&1?'Odd':'')."' id='item_$nr'>\n";
-      foreach ($row as $column) {
+    foreach ($this->mapping as $nr => $row) {
+      $result.= "  <tr class='sortableListItem".($nr&1?'Odd':'')."' id='item_".$this->id."_$nr'>\n";
+      foreach ($this->data[$row] as $column) {
         $result.= "   <td>".htmlentities($column)."</td>\n";
       }
+      if ($action_width) {
+        $result.= "<td>".str_replace('%ID', "edit_".$this->id."_$nr", $edit_image).
+                         str_replace('%ID', "del_".$this->id."_$nr", $delete_image)."</td>";
+      }
       $result.= "  </tr>\n";
     }
 
     $result.= " </tbody>\n</table>\n</div>\n";
-    $result.= " <div id='dropmarker' class='dropmarker' style='display: none; width: ".(intval($this->width)-17)."px'\>\n";
+    $result.= " <input type='hidden' name='reorder_".$this->id."' id='reorder_".$this->id."'>\n";
 
     // Append script stuff if needed
     $result.= '<script type="text/javascript" language="javascript">';
     $result.= ' function updateOrder(){';
     $result.= '    var ampcharcode= \'%26\';';
-    $result.= '    var serializeOpts = Sortable.serialize(\''.$this->id.'\')+ unescape(ampcharcode)+"key='.$this->id.'"+unescape(ampcharcode)+"update=products";';
-    $result.= '    var options = {';
-    $result.= '        method : \'post\',';
-    $result.= '        parameters : serializeOpts';
-    $result.= '        };';
-//    $result.= '      // new Ajax.Request(\'Reorder.aspx\',options);';
+    $result.= '    var serializeOpts = Sortable.serialize(\''.$this->id.'\')+"='.$this->id.'";';
+    $result.= '    $("reorder_'.$this->id.'").value= serializeOpts;';
+    $result.= '    document.mainform.submit();';
     $result.= ' }';
-    $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:true,constraint:\'vertical\', onUpdate : updateOrder,tree:true})';
+    $result.= 'Position.includeScrollOffsets = true;';
+    $result.= ' Sortable.create(\''.$this->id.'\',{tag:\'tr\', ghosting:false,constraint:\'vertical\', scroll:\'scroll_'.$this->id.'\',onUpdate : updateOrder})';
     $result.= '</script>';
 
     return $result;
@@ -152,6 +194,73 @@ class sortableListing {
 
   function update()
   {
+    // Do not do anything if we're not posted
+    if (isset($_POST['reorder_'.$this->id])){
+
+      // Move requested?
+      $move= $_POST['reorder_'.$this->id];
+      if ($move != "") {
+        preg_match_all('/=([0-9]+)[&=]/', $move, $matches);
+        $this->action= "reorder";
+        $tmp= array();
+        foreach ($matches[1] as $id => $row) {
+          $tmp[$id]= $this->mapping[$row];
+        }
+        $this->mapping= $tmp;
+        $this->current_mapping= $matches[1];
+        return;
+      }
+
+      // Delete requested?
+      foreach ($_POST as $key => $value) {
+        if (preg_match('/^del_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
+          $this->active_index= $this->mapping[$matches[1]];
+          $this->action= "delete";
+          if ($this->instantDelete) {
+            $this->deleteEntry($this->active_index);
+          }
+        }
+      }
+
+      // Edit requested?
+      foreach ($_POST as $key => $value) {
+        if (preg_match('/^edit_'.$this->id.'_([0-9]+)_x.*$/', $key, $matches)) {
+          $this->active_index= $this->mapping[$matches[1]];
+          $this->action= "edit";
+        }
+      }
+
+    }
+  }
+
+
+  function getAction()
+  {
+    // Do not do anything if we're not posted
+    if(!isset($_POST['reorder_'.$this->id])) {
+      return;
+    }
+
+    // For reordering, return current mapping
+    if ($this->action == 'reorder') {
+      return array("targets" => $this->current_mapping, "mapping" => $this->mapping, "action" => $this->action);
+    }
+
+    // Edit and delete
+    $result= array("targets" => array($this->active_index), "action" => $this->action);
+
+    return $result;
+  }
+
+
+  function deleteEntry($id)
+  {
+    $index= array_search($id, $this->mapping);
+    if ($index !== false) {
+      unset($this->mapping[$index]);
+      $this->mappings= array_values($this->mappings);
+    }
   }
 
+
 }