Przeglądaj źródła

additional fixes and refactor for hl compilation

Nicolas Cannasse 1 rok temu
rodzic
commit
eb32f5a82c

+ 5 - 1
hide/Element.hx

@@ -2,4 +2,8 @@ package hide;
 
 typedef Element = #if hl hide.tools.vdom.JQuery #else js.jquery.JQuery #end;
 typedef Event = #if hl hide.tools.vdom.Event #else js.jquery.Event #end;
-typedef HTMLElement = #if hl hide.tools.vdom.Dom #else js.html.Element #end;
+typedef HTMLElement = #if hl hide.tools.vdom.Dom #else js.html.Element #end;
+
+function getVal( e : Element ) {
+	return #if js e.val() #else e.getValue() #end;
+}

+ 2 - 25
hide/Ide.hx

@@ -784,29 +784,6 @@ class Ide extends hide.tools.IdeData {
 		}).appendTo(window.window.document.body).click();
 	}
 
-	public function loadPrefab<T:hrt.prefab.Prefab>( file : String, ?cl : Class<T>, ?checkExists ) : T {
-		if( file == null )
-			return null;
-		var l = hrt.prefab.Library.create(file.split(".").pop().toLowerCase());
-		try {
-			var path = getPath(file);
-			if( checkExists && !sys.FileSystem.exists(path) )
-				return null;
-			l.loadData(parseJSON(sys.io.File.getContent(path)));
-		} catch( e : Dynamic ) {
-			error("Invalid prefab "+file+" ("+e+")");
-			throw e;
-		}
-		if( cl == null )
-			return cast l;
-		return l.get(cl);
-	}
-
-	public function savePrefab( file : String, f : hrt.prefab.Prefab ) {
-		var content = f.saveData();
-		sys.io.File.saveContent(getPath(file), toJSON(content));
-	}
-
 	public function filterPrefabs( callb : hrt.prefab.Prefab -> Bool ) {
 		var exts = Lambda.array({iterator : @:privateAccess hrt.prefab.Library.registeredExtensions.keys });
 		exts.push("prefab");
@@ -868,11 +845,11 @@ class Ide extends hide.tools.IdeData {
 		window.title = title != null ? title : ((isCDB ? "CastleDB" : "HIDE") + " - " + projectDir);
 	}
 
-	public function runCommand(cmd, ?callb) {
+	public function runCommand(cmd, ?callb:String->Void) {
 		var c = cmd.split("%PROJECTDIR%").join(projectDir);
 		var slash = isWindows ? "\\" : "/";
 		c = c.split("/").join(slash);
-		js.node.ChildProcess.exec(c, callb);
+		js.node.ChildProcess.exec(c, function(e:js.node.ChildProcess.ChildProcessExecError,_,_) callb(e == null ? null : e.message));
 	}
 
 	public function initMenu() {

+ 9 - 5
hide/comp/Dropdown.hx

@@ -30,7 +30,7 @@ class Dropdown extends Component {
 		this.options = options;
 		this.orderedOptions = options.copy();
 		filterInput = root.find("#filter").first();
-
+		#if js
 		optionsCont = root.find(".options").first();
 		for( o in options ) {
 			var el = new Element('<div tabindex="-1" class="dropdown-option">
@@ -66,7 +66,7 @@ class Dropdown extends Component {
 			return options.findIndex(o -> o.id == id1) - options.findIndex(o -> o.id == id2);
 		}
 
-		filterInput.on("input", (e : js.jquery.Event) -> {
+		filterInput.on("input", (e : Element.Event) -> {
 			var v = filterInput.val();
 			if (v != null) {
 				for( o in optionsCont.children().elements() ) {
@@ -119,9 +119,12 @@ class Dropdown extends Component {
 			if( !removed && element[0].isConnected )
 				remove();
 		});
-
+		#else
+		super(parent, root);
+		#end
 	}
 
+	#if js
 	function reflow() {
 		var offset = anchor.offset();
 		var popupHeight =  element.get(0).offsetHeight;
@@ -139,6 +142,7 @@ class Dropdown extends Component {
 		element.offset(offset);
 		element.width(popupWidth);
 	}
+	#end
 
 	var removed = false;
 	override function remove() {
@@ -197,7 +201,7 @@ class Dropdown extends Component {
 		return getMatchingScore(text, filter) >= 0;
 	}
 
-	function onKey( e : js.jquery.Event ) {
+	function onKey( e : Element.Event ) {
 		if( e.altKey )
 			return true;
 		var children = optionsCont.children();
@@ -257,4 +261,4 @@ class Dropdown extends Component {
 
 	public dynamic function onSelect(val: String) {}
 	public dynamic function onClose() {}
-}
+}

+ 1 - 1
hide/comp/ScriptEditor.hx

@@ -6,7 +6,7 @@ typedef GlobalsDef = haxe.DynamicAccess<{
 	var contexts : Array<String>;
 	var events : String;
 	var evalTo : String;
-	var allowGlobalsDefine : Bool;
+	var allowGlobalsDefine : Null<Bool>;
 	var cdbEnums : Array<String>;
 }>;
 

+ 7 - 3
hide/comp/cdb/Cell.hx

@@ -798,9 +798,9 @@ class Cell {
 
 			// Select whole content of contenteditable div
 			{
-				var range = #if hl ide.createElement("range") #else js.Browser.document.createRange() #end;
-				range.selectNodeContents(i.get(0));
 				#if js
+				var range = js.Browser.document.createRange();
+				range.selectNodeContents(i.get(0));
 				var sel = js.Browser.window.getSelection();
 				sel.removeAllRanges();
 				sel.addRange(range);
@@ -1030,10 +1030,12 @@ class Cell {
 			};
 			#end
 		case TFile:
+			#if js
 			ide.chooseFile(["*"], function(file) {
 				setValue(file);
 				closeEdit();
 			}, false, currentValue);
+			#end
 		case TFlags(values):
 			var div = new Element("<div>").addClass("flagValues");
 			div.click(function(e) e.stopPropagation()).dblclick(function(e) e.stopPropagation());
@@ -1048,7 +1050,7 @@ class Cell {
 				if( mask & (1<<i) == 0 ) continue;
 				var f = new Element("<input>").attr("type", "checkbox").prop("checked", val & (1 << i) != 0).change(function(e) {
 					val &= ~(1 << i);
-					if( e.getThis().prop("checked") ) val |= 1 << i;
+					if( e.getThis().is(":checked") ) val |= 1 << i;
 					e.stopPropagation();
 				});
 				var line = new Element("<label>");
@@ -1130,6 +1132,7 @@ class Cell {
 			}
 
 			if( file == null ) {
+				#if js
 				ide.chooseImage(function(path) {
 					if( path == null ) {
 						closeEdit();
@@ -1140,6 +1143,7 @@ class Cell {
 					closeEdit();
 					edit();
 				},true);
+				#end
 				return;
 			}
 

+ 2 - 2
hide/comp/cdb/Cursor.hx

@@ -138,7 +138,7 @@ class Cursor {
 			var index = lines.index(line.element);
 			var targetLine = lines.get(hxd.Math.imax(index + dy,0));
 			if( targetLine == null || targetLine == line.element.get(0) ) return;
-			dy = allLines.index(targetLine) - allLines.index(line.element);
+			dy = allLines.index(new Element(targetLine)) - allLines.index(line.element);
 		}
 
 		if( !shift )
@@ -231,7 +231,7 @@ class Cursor {
 				}
 			}
 		}
-		var e = line.element[0];
+		var e = line.element.get(0);
 		if( e != null ) untyped e.scrollIntoViewIfNeeded();
 	}
 

+ 8 - 6
hide/comp/cdb/Editor.hx

@@ -119,7 +119,7 @@ class Editor extends Component {
 			element.mousedown(onMouseDown);
 			keys = new hide.ui.Keys(element);
 		} else {
-			cdbTable.element.off("mousedown", onMouseDown);
+			cdbTable.element.off("mousedown" #if js, onMouseDown #end);
 			cdbTable.element.mousedown(onMouseDown);
 			keys = cdbTable.keys;
 		}
@@ -904,7 +904,7 @@ class Editor extends Component {
 			var commands = [for (h in hooks) if (h.sheets.has(s)) h.cmd];
 			function runRec(i: Int) {
 				runningHooks = true;
-				ide.runCommand(commands[i], (e, stdout, stderr) -> {
+				ide.runCommand(commands[i], (e) -> {
 					if (e != null) {
 						ide.error('Hook error:\n$e');
 						hookEnd();
@@ -1472,7 +1472,7 @@ class Editor extends Component {
 				// If user input a comaprison character, switch to expression mode for
 				// the current filter
 				for (c in Editor.COMPARISON_EXPR_CHARS) {
-					if (StringTools.contains(e.getThis().val(), c) && !searchExp) {
+					if (StringTools.contains(Element.getVal(e.getThis()), c) && !searchExp) {
 						searchExp = true;
 						var searchTypeBtn = searchBox.find(".search-type");
 						searchTypeBtn.toggleClass("fa-superscript", searchExp);
@@ -1482,7 +1482,7 @@ class Editor extends Component {
 					}
 				}
 
-				filters[index].text = e.getThis().val();
+				filters[index].text = Element.getVal(e.getThis());
 				filters[index].isExpr = e.getThis().next().hasClass("fa-superscript");
 
 				// Slow table refresh protection
@@ -1665,7 +1665,7 @@ class Editor extends Component {
 			return;
 		if( table.displayMode == Properties ) {
 			var ins = table.element.find("select.insertField");
-			var options = [for( o in ins.find("option").elements() ) o.val()];
+			var options = [for( o in ins.find("option").elements() ) Element.getVal(o)];
 			ins.attr("size", options.length);
 			options.shift();
 			ins.focus();
@@ -1682,7 +1682,7 @@ class Editor extends Component {
 				case K.DOWN if( index < options.length - 1 ):
 					ins.val(options[++index]);
 				case K.ENTER:
-					@:privateAccess table.insertProperty(ins.val());
+					@:privateAccess table.insertProperty(Element.getVal(ins));
 				default:
 				}
 				e.stopPropagation();
@@ -2152,7 +2152,9 @@ class Editor extends Component {
 				return;
 			categories = [for(s in wstr.split(",")) { var t = StringTools.trim(s); if(t.length > 0) t; }];
 			setFunc(categories.length > 0 ? categories : null);
+			#if editor
 			ide.initMenu();
+			#end
 		}}];
 
 		for(name in getCategories(base)) {

+ 23 - 12
hide/comp/cdb/Table.hx

@@ -394,17 +394,18 @@ class Table extends Component {
 
 		var syncLevel : Int = -1;
 		function sync() {
-			data.hidden = title == null ? null : isSepHidden(sindex);
+			data.hidden = title == null ? false : isSepHidden(sindex);
 			toggle.css({ display : title == null ? "none" : "" });
 			toggle.text(data.hidden ? "🡆" : "🡇");
 			content.text(title == null ? "" : title+(data.hidden ? " ("+getLines().length+")" : ""));
 			sep.toggleClass("sep-hidden", data.hidden == true);
+			var def = #if js null #else 0 #end;
 			if( syncLevel != sepInfo.level ) {
-				sep.removeClass("seplevel-"+(syncLevel == null ? 0 : syncLevel));
+				sep.removeClass("seplevel-"+(syncLevel == def ? 0 : syncLevel));
 				syncLevel = sepInfo.level;
-				sep.addClass('seplevel-'+(syncLevel == null ? 0 : syncLevel));
+				sep.addClass('seplevel-'+(syncLevel == def ? 0 : syncLevel));
 			}
-			sep.attr("level", syncLevel == null ? 0 : sepInfo.level);
+			sep.attr("level", syncLevel == def ? 0 : sepInfo.level);
 		}
 
 		sep.contextmenu(function(e) {
@@ -459,7 +460,10 @@ class Table extends Component {
 									var level = t.sep.level;
 									if( level == null ) level = 0;
 									level += delta;
-									t.sep.level = level == 0 ? js.Lib.undefined : level;
+									if( level == 0 )
+										Reflect.deleteField(t.sep,"level")
+									else
+										t.sep.level = level;
 									for( s in t.subs )
 										deltaRec(s);
 								}
@@ -491,6 +495,7 @@ class Table extends Component {
 					editor.refresh();
 				}}
 			];
+			#if js
 			if( sepInfo.path != null )
 				opts.unshift({
 					label : "Open",
@@ -498,6 +503,7 @@ class Table extends Component {
 						ide.openFile(sepInfo.path);
 					},
 				});
+			#end
 			new hide.comp.ContextMenu(opts);
 		});
 
@@ -505,13 +511,16 @@ class Table extends Component {
 			if( !canInsert() ) return;
 			content.empty();
 			J("<input>").appendTo(content).focus().val(title == null ? "" : title).blur(function(e) {
-				title = e.getThis().val();
+				title = Element.getVal(e.getThis());
 				e.getThis().remove();
 				if( title == "" ) title = null;
 				editor.beginChanges();
 				var sep = sheet.separators[sindex];
 				var prevTitle = sep.title;
-				sep.title = title == null ? js.Lib.undefined : title;
+				if( title == null )
+					Reflect.deleteField(sep,"title");
+				else
+					sep.title = title;
 				if( prevTitle != null ) {
 					if( title == null ) {
 						if( sep.level == null ) sep.level = 0;
@@ -519,7 +528,8 @@ class Table extends Component {
 					}
 				} else if( title != null && sep.level > 0 ) {
 					sep.level--;
-					if( sep.level == 0 ) sep.level = js.Lib.undefined;
+					if( sep.level == 0 )
+						Reflect.deleteField(sep,"level");
 				}
 				editor.endChanges();
 				sync();
@@ -531,7 +541,7 @@ class Table extends Component {
 			}).keypress(function(e) {
 				e.stopPropagation();
 			}).keydown(function(e) {
-				if( e.keyCode == 13 ) { JTHIS.blur(); e.preventDefault(); } else if( e.keyCode == 27 ) content.text(title);
+				if( e.keyCode == 13 ) { e.getThis().blur(); e.preventDefault(); } else if( e.keyCode == 27 ) content.text(title);
 				e.stopPropagation();
 			});
 		});
@@ -543,7 +553,7 @@ class Table extends Component {
 				var s = sheet.separators[sindex - 1 - i];
 				if( s.title != null && (s.level == null || s.level == level) ) {
 					if( isSepHidden(sindex - 1 - i) ) {
-						sep[0].style.display = "none";
+						sep.hide();
 						data.hidden = true;
 					}
 					break;
@@ -568,7 +578,7 @@ class Table extends Component {
 			var subs = element.find("tr.separator");
 			function toggleRec( t : SepTree ) {
 				var sid = sheet.separators.indexOf(t.sep);
-				subs.get(sid).style.display = hidden ? "none" : "";
+				subs.eq(sid).toggle(!hidden);
 				if( !hidden ) {
 					if( isSepHidden(sid) ) return;
 					for( l in getLines(sid) )
@@ -621,6 +631,7 @@ class Table extends Component {
 		var ids = [];
 		if( id != null ) ids.push(id);
 		var pos = scopes.length;
+		var scope : Null<Int> = scope;
 		while( true ) {
 			pos -= scope;
 			if( pos < 0 ) {
@@ -712,7 +723,7 @@ class Table extends Component {
 		else if( !canInsert )
 			end.remove();
 		sel.change(function(e) {
-			var v = sel.val();
+			var v = Element.getVal(sel);
 			if( v == "" )
 				return;
 			sel.val("");

+ 23 - 1
hide/tools/IdeData.hx

@@ -78,7 +78,7 @@ class IdeData {
 	function get_appPath() {
 		if( appPath != null )
 			return appPath;
-		var path = js.Node.process.argv[0].split("\\").join("/").split("/");
+		var path = #if hl Sys.programPath() #else js.Node.process.argv[0] #end.split("\\").join("/").split("/");
 		path.pop();
 		var hidePath = path.join("/");
 		if( !sys.FileSystem.exists(hidePath + "/package.json") ) {
@@ -221,5 +221,27 @@ class IdeData {
 		return str;
 	}
 
+	public function loadPrefab<T:hrt.prefab.Prefab>( file : String, ?cl : Class<T>, ?checkExists ) : T {
+		if( file == null )
+			return null;
+		var l = hrt.prefab.Library.create(file.split(".").pop().toLowerCase());
+		try {
+			var path = getPath(file);
+			if( checkExists && !sys.FileSystem.exists(path) )
+				return null;
+			l.loadData(parseJSON(sys.io.File.getContent(path)));
+		} catch( e : Dynamic ) {
+			error("Invalid prefab "+file+" ("+e+")");
+			throw e;
+		}
+		if( cl == null )
+			return cast l;
+		return l.get(cl);
+	}
+
+	public function savePrefab( file : String, f : hrt.prefab.Prefab ) {
+		var content = f.saveData();
+		sys.io.File.saveContent(getPath(file), toJSON(content));
+	}
 
 }

+ 4 - 5
hide/ui/View.hx

@@ -16,6 +16,8 @@ class View<T> extends hide.comp.Component {
 	var container : golden.Container;
 	var containerView : golden.ContentItem;
 	public var fullScreen(get,set) : Bool;
+	var contentWidth(get,never) : Int;
+	var contentHeight(get,never) : Int;
 	#end
 	var watches : Array<{ keep : Bool, path : String, callb : Void -> Void }> = [];
 	public var keys(get,null) : Keys;
@@ -24,9 +26,6 @@ class View<T> extends hide.comp.Component {
 	public var config(get, null) : Config;
 	public var viewClass(get, never) : String;
 	public var defaultOptions(get,never) : ViewOptions;
-
-	var contentWidth(get,never) : Int;
-	var contentHeight(get,never) : Int;
 	var needRebuild : Bool;
 
 	public function new(state:T) {
@@ -223,6 +222,8 @@ class View<T> extends hide.comp.Component {
 		return Type.getClassName(Type.getClass(this)) + (this.state == null ? "" : "("+haxe.Json.stringify(this.state)+")");
 	}
 
+	function get_defaultOptions() return viewClasses.get(Type.getClassName(Type.getClass(this))).options;
+
 	#if !hl
 
 	public function isActive() {
@@ -278,8 +279,6 @@ class View<T> extends hide.comp.Component {
 
 	function get_contentWidth() return container.width;
 	function get_contentHeight() return container.height;
-	function get_defaultOptions() return viewClasses.get(Type.getClassName(Type.getClass(this))).options;
-
 	function get_fullScreen() return container != null && container.getElement().is(".fullScreen");
 	function set_fullScreen(v) {
 		if( fullScreen == v )

+ 3 - 1
hide/view/CdbTable.hx

@@ -189,9 +189,11 @@ class CdbTable extends hide.ui.View<{}> {
 		return [for( s in getSheets() ) s.name].join("|");
 	}
 
+	#if js
 	override function onActivate() {
 		if( editor != null ) editor.focus();
 	}
+	#end
 
 	function setEditor(index:Int) {
 		var sheets = getSheets();
@@ -278,7 +280,7 @@ class CdbTable extends hide.ui.View<{}> {
 				tab.addClass("ignore-loc-" + ignoreCount);
 				if( ignoreCount > 0 ) {
 					tab.addClass("has-loc-ignored");
-					tab.append(' ($ignoreCount)');
+					tab.get(0).textContent += ' ($ignoreCount)';
 				}
 			}
 		}