Skip to content

Commit

Permalink
Upgrade the fix for issue #422 to manage it in a more flexible way.
Browse files Browse the repository at this point in the history
  • Loading branch information
asterd committed Jun 30, 2016
1 parent cc0eab5 commit d909c31
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 12 deletions.
4 changes: 2 additions & 2 deletions gradleResources/js/bsf.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function jq( myid ) {
return "#" + myid.replace( /(:|\.|\[|\]|,)/g, "\\$1" );
}

/* tree support method to convert data object to state string */
/* tree support method to convert data object to state string
function treeDataMapper(data) {
var sep = "|#*#|";
if(data && data !== "undefined") {
Expand All @@ -169,7 +169,7 @@ function treeDataMapper(data) {
data.state.selected;
}
return "";
}
}*/

BsF.substringMatcher = function(strs) {
return function findMatches(q, cb) {
Expand Down
2 changes: 1 addition & 1 deletion mavenResources/META-INF/resources/bsf/js/bsf.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 14 additions & 7 deletions src/main/java/net/bootsfaces/component/tree/TreeRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void decode(FacesContext context, UIComponent component) {
value = pair[1];
}
if (value != null && !"".equals(value.trim())) {
Node n = TreeModelUtils.mapDataToModel(value);
// Node n = TreeModelUtils.mapDataToModel(value);
Node n = (Node) BsfUtils.fromString(value);
TreeModelUtils.updateNodeById(tree.getValue(), n.getNodeId(), n);
// tree.setSubmittedValue(tree.getValue());

Expand Down Expand Up @@ -167,27 +168,33 @@ public void encodeEnd(FacesContext context, UIComponent component) throws IOExce
" data: getTreeData(), " +
// enable nodeSelected event callback
" onNodeSelected: function(event, data) { " +
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeSelected:' + treeDataMapper(data));" + // @all
//" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeSelected:' + treeDataMapper(data));" + // @all
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeSelected:' + data.serialize);" + // @all
" }," +
// enable nodeUnselected event callback
" onNodeUnselected: function(event, data) { " +
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeUnselected:' + treeDataMapper(data));" + // @all
//" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeUnselected:' + treeDataMapper(data));" + // @all
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeUnselected:' + data.serialize);" + // @all
" }," +
//enable nodeChecked event callback
" onNodeChecked: function(event, data) { " +
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeChecked:' + treeDataMapper(data));" + // @all
//" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeChecked:' + treeDataMapper(data));" + // @all
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeChecked:' + data.serialize);" + // @all
" }," +
//enable nodeUnchecked event callback
" onNodeUnchecked: function(event, data) { " +
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeUnchecked:' + treeDataMapper(data));" + // @all
//" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeUnchecked:' + treeDataMapper(data));" + // @all
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeUnchecked:' + data.serialize);" + // @all
" }," +
// enable nodeCollapsed event callback
" onNodeCollapsed: function(event, data) { " +
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeCollapsed:' + treeDataMapper(data));" + // @all
//" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeCollapsed:' + treeDataMapper(data));" + // @all
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeCollapsed:' + data.serialize);" + // @all
" }," +
// enable nodeExpanded event callback
" onNodeExpanded: function(event, data) { " +
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeExpanded:' + treeDataMapper(data));" + // @all
//" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeExpanded:' + treeDataMapper(data));" + // @all
" BsF.ajax.callAjax(this, event, '" + updateItems + "', '" + clientId + "', null, null, null, 'nodeExpanded:' + data.serialize);" + // @all
" }" +
"}); ", null);
rw.writeText("});", null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DefaultNodeImpl
implements Node, Serializable {
private static final long serialVersionUID = -6742025921003033215L;

private final List<Node> childs = new ArrayList<Node>();
private final transient List<Node> childs = new ArrayList<Node>();
private String data;
private int nodeId;
private String text;
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/net/bootsfaces/component/tree/model/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@

package net.bootsfaces.component.tree.model;

import java.io.Serializable;
import java.util.List;

public interface Node {
public interface Node
extends Serializable {
/**
* @return the node id
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ private static String renderNode(Node node) {
sb.append(renderSubnodes(node.getChilds()));
sb.append(",");
}
// SERIALIZE
sb.append("\"serialize\": \"" + BsfUtils.toString(node) + "\", ");

sb.append("\"selectable\": " + node.isSelectable() + ", ");
sb.append("\"state\": {");
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/net/bootsfaces/utils/BsfUtils.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package net.bootsfaces.utils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Iterator;
import java.util.Locale;
import java.util.MissingResourceException;
Expand Down Expand Up @@ -512,4 +518,43 @@ public static String getLabel(FacesContext context, UIComponent comp) {
}
return (String) o;
}

/**
* Read an object from a base64 string
* @param s
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object fromString(String s) {
try {
byte [] data = Base64.getDecoder().decode( s );
ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(data));
Object o = ois.readObject();
ois.close();
return o;
} catch(Exception e) {
e.printStackTrace();
return null;
}
}

/**
* Write an object to base64 string
* @param o
* @return
* @throws IOException
*/
public static String toString(Serializable o) {
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(o);
oos.close();
return Base64.getEncoder().encodeToString(baos.toByteArray());
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
}

0 comments on commit d909c31

Please sign in to comment.