Browse Source

IconTree: rename data -> value to avoid jstree killing our data

trethaller 7 years ago
parent
commit
76f70ecee6
6 changed files with 28 additions and 31 deletions
  1. 22 18
      hide/comp/IconTree.hx
  2. 2 2
      hide/comp/SceneTree.hx
  3. 0 7
      hide/prefab/Level3D.hx
  4. 1 1
      hide/view/FileTree.hx
  5. 2 2
      hide/view/Level3D.hx
  6. 1 1
      hide/view/Prefab.hx

+ 22 - 18
hide/comp/IconTree.hx

@@ -1,7 +1,7 @@
 package hide.comp;
 
 typedef IconTreeItem<T> = {
-	var data : T;
+	var value : T;
 	var text : String;
 	@:optional var children : Bool;
 	@:optional var icon : String;
@@ -28,7 +28,7 @@ class IconTree<T:{}> extends Component {
 	public var async : Bool = false;
 
 	public dynamic function get( parent : Null<T> ) : Array<IconTreeItem<T>> {
-		return [{ data : null, text : "get()", children : true }];
+		return [{ value : null, text : "get()", children : true }];
 	}
 
 	public dynamic function onClick( e : T ) : Void {
@@ -52,21 +52,21 @@ class IconTree<T:{}> extends Component {
 	}
 
 	function makeContent(parent:IconTreeItem<T>) {
-		var content : Array<IconTreeItem<T>> = get(parent == null ? null : parent.data);
+		var content : Array<IconTreeItem<T>> = get(parent == null ? null : parent.value);
 		for( c in content ) {
 			var key = (parent == null ? "" : parent.absKey + "/") + c.text;
 			if( c.absKey == null ) c.absKey = key;
 			c.id = "titem$" + (UID++);
 			map.set(c.id, c);
-			if( Std.is(c.data, String) )
-				revMapString.set(cast c.data, c);
+			if( Std.is(c.value, String) )
+				revMapString.set(cast c.value, c);
 			else
-				revMap.set(c.data, c);
+				revMap.set(c.value, c);
 			if( c.state == null ) {
 				var s = getDisplayState(key);
 				if( s != null ) c.state = { opened : s } else c.state = {};
 			}
-			if( !async ) {
+			if( !async && c.children ) {
 				c.state.loaded = true;
 				c.children = cast makeContent(c);
 			}
@@ -87,11 +87,11 @@ class IconTree<T:{}> extends Component {
 						return true;
 					if( operation == "rename_node" ) {
 						if( node.text == value ) return true; // no change
-						return onRename(map.get(node.id).data, value);
+						return onRename(map.get(node.id).value, value);
 					}
 					if( operation == "move_node" ) {
 						if( extra.ref == null ) return true;
-						return onAllowMove(map.get(node.id).data, map.get(extra.ref.id).data);
+						return onAllowMove(map.get(node.id).value, map.get(extra.ref.id).value);
 					}
 					return false;
 				},
@@ -104,34 +104,35 @@ class IconTree<T:{}> extends Component {
 		root.on("click.jstree", function (event) {
 			var node = new Element(event.target).closest("li");
    			var i = map.get(node[0].id);
-			onClick(i.data);
+			onClick(i.value);
 		});
 		root.on("dblclick.jstree", function (event) {
 			var node = new Element(event.target).closest("li");
    			var i = map.get(node[0].id);
 			if( allowRename ) {
-				editNode(i.data);
+				editNode(i.value);
 				return;
 			}
-			onDblClick(i.data);
+			onDblClick(i.value);
 		});
 		root.on("open_node.jstree", function(event, e) {
 			var i = map.get(e.node.id);
 			saveDisplayState(i.absKey, true);
-			onToggle(i.data, true);
+			onToggle(i.value, true);
 		});
 		root.on("close_node.jstree", function(event,e) {
 			var i = map.get(e.node.id);
 			saveDisplayState(i.absKey, false);
-			onToggle(i.data, false);
+			onToggle(i.value, false);
 		});
 		root.on("refresh.jstree", function(_) {
+			trace("refresh done");
 			var old = waitRefresh;
 			waitRefresh = [];
 			for( f in old ) f();
 		});
 		root.on("move_node.jstree", function(event, e) {
-			onMove(map.get(e.node.id).data, e.parent == "#" ? null : map.get(e.parent).data, e.position);
+			onMove(map.get(e.node.id).value, e.parent == "#" ? null : map.get(e.parent).value, e.position);
 		});
 	}
 
@@ -157,7 +158,7 @@ class IconTree<T:{}> extends Component {
 		if( id == null )
 			return null;
 		var i = map.get(id.substr(0, -7)); // remove _anchor
-		return i == null ? null : i.data;
+		return i == null ? null : i.value;
 	}
 
 	public function setSelection( objects : Array<T> ) {
@@ -169,14 +170,17 @@ class IconTree<T:{}> extends Component {
 		}
 	}
 
-	public function refresh( ?onReady : Void -> Void ) {		
+	public function refresh( ?onReady : Void -> Void ) {
+		map = new Map();
+		revMap = new haxe.ds.ObjectMap();
+		revMapString = new haxe.ds.StringMap();
 		if( onReady != null ) waitRefresh.push(onReady);
 		(untyped root.jstree)('refresh',true);
 	}
 
 	public function getSelection() : Array<T> {
 		var ids : Array<String> = (untyped root.jstree)('get_selected');
-		return [for( id in ids ) map.get(id).data];
+		return [for( id in ids ) map.get(id).value];
 	}
 
 	public function revealNode(e : T) {

+ 2 - 2
hide/comp/SceneTree.hx

@@ -71,7 +71,7 @@ class SceneTree extends IconTree<String> {
 			for( i in 0...root.numChildren ) {
 				var c = root.getChildAt(i);
 				{
-					data : path+i,
+					value :path+i,
 					text : getObjectName(c),
 					icon : "fa fa-" + getIcon(c),
 					children : c.isMesh() || c.numChildren > 0,
@@ -84,7 +84,7 @@ class SceneTree extends IconTree<String> {
 			for( i in 0...materials.length ) {
 				var m = materials[i];
 				elements.push({
-					data : path+"mat:"+i,
+					value :path+"mat:"+i,
 					text : m.name == null ? "Material@"+i : m.name,
 					icon : "fa fa-photo",
 				});

+ 0 - 7
hide/prefab/Level3D.hx

@@ -2,13 +2,6 @@ package hide.prefab;
 
 class Level3D extends Prefab {
 
-	override function load(obj:Dynamic) {
-	}
-
-	override function save() {
-		return {};
-	}
-
 	override function getHideProps() {
 		return { icon : "cube", name : "Level3D", fileSource : ["l3d"] };
 	}

+ 1 - 1
hide/view/FileTree.hx

@@ -75,7 +75,7 @@ class FileTree extends FileView {
 				var ext = getExtension(fullPath);
 				var id = ( path == "" ? c : path+"/"+c );
 				content.push({
-					data : id,
+					value : id,
 					text : c,
 					icon : "fa fa-" + (isDir ? "folder" : (ext != null && ext.options.icon != null ? ext.options.icon : "file-text")),
 					children : isDir,

+ 2 - 2
hide/view/Level3D.hx

@@ -573,7 +573,7 @@ class Level3D extends FileView {
 		function makeItem(o:PrefabElement) : hide.comp.IconTree.IconTreeItem<PrefabElement> {
 			var p = o.getHideProps();
 			var r : hide.comp.IconTree.IconTreeItem<PrefabElement> = {
-				data : o,
+				value : o,
 				text : o.name,
 				icon : "fa fa-"+p.icon,
 				children : o.children.length > 0,
@@ -595,7 +595,7 @@ class Level3D extends FileView {
 
 			var registered = new Array<hide.comp.ContextMenu.ContextMenuItem>();
 			var allRegs = @:privateAccess hide.prefab.Library.registeredElements;
-			var allowed = ["model", "object", "layer"];
+			var allowed = ["model", "object", "layer", "box"];
 			for( ptype in allowed ) {
 				var pcl = allRegs.get(ptype);
 				var props = Type.createEmptyInstance(pcl).getHideProps();

+ 1 - 1
hide/view/Prefab.hx

@@ -228,7 +228,7 @@ class Prefab extends FileView {
 		function makeItem(o:PrefabElement) : hide.comp.IconTree.IconTreeItem<PrefabElement> {
 			var p = o.getHideProps();
 			return {
-				data : o,
+				value : o,
 				text : o.name,
 				icon : "fa fa-"+p.icon,
 				children : o.children.length > 0,