瀏覽代碼

added cdb view config to limit view/edit

Nicolas Cannasse 5 年之前
父節點
當前提交
92dc0f628c
共有 9 個文件被更改,包括 118 次插入31 次删除
  1. 8 2
      bin/cdb.css
  2. 8 4
      bin/cdb.less
  3. 11 2
      hide/comp/cdb/Cell.hx
  4. 10 0
      hide/comp/cdb/ConfigView.hx
  5. 3 3
      hide/comp/cdb/Cursor.hx
  6. 31 6
      hide/comp/cdb/Editor.hx
  7. 4 4
      hide/comp/cdb/SubTable.hx
  8. 40 9
      hide/comp/cdb/Table.hx
  9. 3 1
      hide/view/CdbTable.hx

+ 8 - 2
bin/cdb.css

@@ -14,7 +14,8 @@
 .cdb:focus .cdb-sheet tr.cursorLine {
   background-color: #262d35;
 }
-.cdb:focus .cdb-sheet tr.cursorLine td.start {
+.cdb:focus .cdb-sheet tr.cursorLine td.start,
+.cdb:focus .cdb-sheet tr.cursorLine td.t_readonly {
   background-color: #313a48;
 }
 .cdb:focus .cdb-sheet tr.selected,
@@ -22,7 +23,9 @@
   background-color: #313a48;
 }
 .cdb:focus .cdb-sheet tr.selected td.start,
-.cdb:focus .cdb-sheet td.selected td.start {
+.cdb:focus .cdb-sheet td.selected td.start,
+.cdb:focus .cdb-sheet tr.selected td.t_readonly,
+.cdb:focus .cdb-sheet td.selected td.t_readonly {
   background-color: #313a48;
 }
 .cdb .cdb-sheet {
@@ -139,6 +142,9 @@
 .cdb .cdb-sheet td.t_tilepos {
   text-align: center;
 }
+.cdb .cdb-sheet td.t_readonly {
+  background-color: #333;
+}
 .cdb .cdb-sheet th.t_bool,
 .cdb .cdb-sheet th.t_int,
 .cdb .cdb-sheet th.t_float {

+ 8 - 4
bin/cdb.less

@@ -19,14 +19,14 @@
 
 		tr.cursorLine {
 			background-color: #262d35;
-			td.start {
+			td.start, td.t_readonly {
 				background-color: #313a48;
 			}
 		}
 
 		tr.selected, td.selected {
 			background-color : #313a48;
-			td.start {
+			td.start, td.t_readonly {
 				background-color: #313a48;
 			}
 		}
@@ -163,6 +163,10 @@
 			text-align : center;
 		}
 
+		td.t_readonly {
+			background-color: #333;
+		}
+
 		th.t_bool,
 		th.t_int,
 		th.t_float {
@@ -224,9 +228,9 @@
 				max-height: 32px;
 				max-width: 32px;
 			}
-			&.parent-sub-table {
+			//&.parent-sub-table {
 				//background-color: #2b3036;
-			}
+			//}
 		}
 
 		td.t_enum {

+ 11 - 2
hide/comp/cdb/Cell.hx

@@ -33,12 +33,19 @@ class Cell extends Component {
 		case TString if( column.kind == Script ):
 			element.click(function(_) edit());
 		default:
-			element.dblclick(function(_) edit());
+			if( canEdit() )
+				element.dblclick(function(_) edit());
+			else
+				root.addClass("t_readonly");
 		}
 	}
 
+	public function canEdit() {
+		return table.canEditColumn(column.name);
+	}
+
 	function get_table() return line.table;
-	function get_columnIndex() return table.sheet.columns.indexOf(column);
+	function get_columnIndex() return table.columns.indexOf(column);
 	inline function get_value() return currentValue;
 
 	public function refresh() {
@@ -261,6 +268,8 @@ class Cell extends Component {
 	}
 
 	public function edit() {
+		if( !canEdit() )
+			return;
 		switch( column.type ) {
 		case TString if( column.kind == Script ):
 			open();

+ 10 - 0
hide/comp/cdb/ConfigView.hx

@@ -0,0 +1,10 @@
+package hide.comp.cdb;
+
+typedef SheetView = {
+	var insert : Bool;
+	var ?show : Array<String>;
+	var ?edit : Array<String>;
+	var sub : haxe.DynamicAccess<SheetView>;
+}
+
+typedef ConfigView = haxe.DynamicAccess<SheetView>;

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

@@ -105,7 +105,7 @@ class Cursor {
 		}
 		if( dx > 0 ) {
 			x += dx;
-			var max = table.sheet.columns.length;
+			var max = table.columns.length;
 			if( x >= max ) x = max - 1;
 		}
 		if( dy > 0 ) {
@@ -136,7 +136,7 @@ class Cursor {
 			y = table.lines.length - 1;
 			select = null;
 		}
-		var max = table.sheet.props.isProps ? 1 : table.sheet.columns.length;
+		var max = table.sheet.props.isProps ? 1 : table.columns.length;
 		if( x >= max ) {
 			x = max - 1;
 			select = null;
@@ -174,7 +174,7 @@ class Cursor {
 		if( table == null )
 			return null;
 		var x1 = if( x < 0 ) 0 else x;
-		var x2 = if( x < 0 ) table.sheet.columns.length-1 else if( select != null ) select.x else x1;
+		var x2 = if( x < 0 ) table.columns.length-1 else if( select != null ) select.x else x1;
 		var y1 = y;
 		var y2 = if( select != null ) select.y else y1;
 		if( x2 < x1 ) {

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

@@ -38,6 +38,7 @@ class Editor extends Component {
 	var api : EditorApi;
 	var undoState : Array<UndoState> = [];
 	var currentValue : Any;
+	public var view : ConfigView;
 	public var config : hide.Config;
 	public var cursor : Cursor;
 	public var keys : hide.ui.Keys;
@@ -47,6 +48,7 @@ class Editor extends Component {
 		super(null,null);
 		this.api = api;
 		this.config = config;
+		view = cast this.config.get("cdb.view");
 		currentValue = api.copy();
 		undo = new hide.ui.UndoHistory();
 	}
@@ -170,7 +172,7 @@ class Editor extends Component {
 			var obj = cursor.table.lines[y].obj;
 			var out = {};
 			for( x in sel.x1...sel.x2+1 ) {
-				var c = cursor.table.sheet.columns[x];
+				var c = cursor.table.columns[x];
 				var v = Reflect.field(obj, c.name);
 				if( v != null )
 					Reflect.setField(out, c.name, v);
@@ -180,13 +182,14 @@ class Editor extends Component {
 		clipboard = {
 			data : data,
 			text : Std.string([for( o in data ) cursor.table.sheet.objToString(o,true)]),
-			schema : [for( x in sel.x1...sel.x2+1 ) cursor.table.sheet.columns[x]],
+			schema : [for( x in sel.x1...sel.x2+1 ) cursor.table.columns[x]],
 		};
 		ide.setClipboard(clipboard.text);
 	}
 
 	function onPaste() {
 		var text = ide.getClipboard();
+		var columns = cursor.table.columns;
 		var sheet = cursor.table.sheet;
 		if( clipboard == null || text != clipboard.text ) {
 			if( cursor.x < 0 || cursor.y < 0 ) return;
@@ -206,7 +209,9 @@ class Editor extends Component {
 			}
 			beginChanges();
 			for( x in x1...x2+1 ) {
-				var col = sheet.columns[x];
+				var col = columns[x];
+				if( !cursor.table.canEditColumn(col.name) )
+					continue;
 				var value : Dynamic = null;
 				switch( col.type ) {
 				case TId:
@@ -235,13 +240,19 @@ class Editor extends Component {
 		var posX = cursor.x < 0 ? 0 : cursor.x;
 		var posY = cursor.y < 0 ? 0 : cursor.y;
 		for( obj1 in clipboard.data ) {
-			if( posY == sheet.lines.length )
+			if( posY == sheet.lines.length ) {
+				if( !cursor.table.canInsert() ) break;
 				sheet.newLine();
+			}
 			var obj2 = sheet.lines[posY];
 			for( cid in 0...clipboard.schema.length ) {
 				var c1 = clipboard.schema[cid];
-				var c2 = sheet.columns[cid + posX];
+				var c2 = columns[cid + posX];
 				if( c2 == null ) continue;
+
+				if( !cursor.table.canEditColumn(c2.name) )
+					continue;
+
 				var f = base.getConvFunction(c1.type, c2.type);
 				var v : Dynamic = Reflect.field(obj1, c1.name);
 				if( f == null )
@@ -275,6 +286,10 @@ class Editor extends Component {
 		if( cursor.x < 0 ) {
 			// delete lines
 			var y = sel.y2;
+			if( !cursor.table.canInsert() ) {
+				endChanges();
+				return;
+			}
 			while( y >= sel.y1 ) {
 				var line = cursor.table.lines[y];
 				line.table.sheet.deleteLine(line.index);
@@ -288,6 +303,8 @@ class Editor extends Component {
 				var line = cursor.table.lines[y];
 				for( x in sel.x1...sel.x2+1 ) {
 					var c = line.columns[x];
+					if( !line.cells[x].canEdit() )
+						continue;
 					var old = Reflect.field(line.obj, c.name);
 					var def = base.getDefault(c,false);
 					if( old == def )
@@ -340,7 +357,7 @@ class Editor extends Component {
 					return { sheet : t.sheet.name, parent : tp == null ? null : {
 						sheet : makeParent(tp),
 						line : t.sheet.parent.line,
-						column : t.sheet.parent.column,
+						column : tp.columns.indexOf(tp.sheet.columns[t.sheet.parent.column]),
 					} };
 				}
 				makeParent(tables[i]);
@@ -581,6 +598,8 @@ class Editor extends Component {
 	}
 
 	public function insertLine( table : Table, index = 0 ) {
+		if( !table.canInsert() )
+			return;
 		if( table.displayMode == Properties ) {
 			var ins = table.element.find("select.insertField");
 			var options = [for( o in ins.find("option").elements() ) o.val()];
@@ -615,6 +634,8 @@ class Editor extends Component {
 	}
 
 	public function popupColumn( table : Table, col : cdb.Data.Column, ?cell : Cell ) {
+		if( view != null )
+			return;
 		var indexColumn = table.sheet.columns.indexOf(col);
 		var menu : Array<hide.comp.ContextMenu.ContextMenuItem> = [
 			{ label : "Edit", click : function () editColumn(table.sheet, col) },
@@ -673,6 +694,8 @@ class Editor extends Component {
 	}
 
 	public function popupLine( line : Line ) {
+		if( view != null && !line.table.view.insert )
+			return;
 		var sheet = line.table.sheet;
 		var sepIndex = sheet.separators.indexOf(line.index);
 		new hide.comp.ContextMenu([
@@ -742,6 +765,8 @@ class Editor extends Component {
 	}
 
 	public function popupSheet( ?sheet : cdb.Sheet, ?onChange : Void -> Void ) {
+		if( view != null )
+			return;
 		if( sheet == null ) sheet = this.sheet;
 		if( onChange == null ) onChange = function() {}
 		var index = base.sheets.indexOf(sheet);

+ 4 - 4
hide/comp/cdb/SubTable.hx

@@ -25,15 +25,15 @@ class SubTable extends Table {
 		var group;
 		if( mode == Properties ) {
 			var count = cell.columnIndex + 1;
-			if (count < 3 && cell.table.sheet.columns.length >= 8)
+			if (count < 3 && cell.table.columns.length >= 8)
 				count += 2; // fix when a lot of columns but the subproperty is on the left
 			group = new Element("<td>").attr("colspan",""+(count+1)).appendTo(insertedTR);
-			var remain = cell.table.sheet.columns.length - count;
+			var remain = cell.table.columns.length - count;
 			if( remain > 0 )
 				new Element("<td>").attr("colspan", "" + remain).appendTo(insertedTR);
 		} else {
 			new Element("<td>").appendTo(insertedTR);
-			group = new Element("<td>").attr("colspan",""+cell.table.sheet.columns.length).appendTo(insertedTR);
+			group = new Element("<td>").attr("colspan",""+cell.table.columns.length).appendTo(insertedTR);
 		}
 		slider = new Element("<div>").appendTo(group);
 		slider.hide();
@@ -81,7 +81,7 @@ class SubTable extends Table {
 			name : psheet.name, // same
 			lines : lines, // ref
 			separators : [], // none
-		},key, { sheet : sheet, column : cell.columnIndex, line : index });
+		},key, { sheet : sheet, column : cell.table.sheet.columns.indexOf(c), line : index });
 	}
 
 	public function show( ?immediate ) {

+ 40 - 9
hide/comp/cdb/Table.hx

@@ -15,6 +15,9 @@ class Table extends Component {
 	public var lines : Array<Line>;
 	public var displayMode(default,null) : DisplayMode;
 
+	public var columns : Array<cdb.Data.Column>;
+	public var view : ConfigView.SheetView;
+
 	public function new(editor, sheet, root, mode) {
 		super(null,root);
 		this.displayMode = mode;
@@ -22,9 +25,30 @@ class Table extends Component {
 		this.sheet = sheet;
 		@:privateAccess editor.tables.push(this);
 		root.addClass("cdb-sheet");
+		if( editor.view != null ) {
+			var cname = parent == null ? null : sheet.parent.sheet.columns[sheet.parent.column].name;
+			if( parent == null )
+				view = editor.view.get(sheet.name);
+			else if( parent.view.sub != null )
+				view = parent.view.sub.get(cname);
+			if( view == null ) {
+				if( parent != null && parent.canEditColumn(cname) )
+					view = { insert : true, edit : [for( c in sheet.columns ) c.name], sub : {} };
+				else
+					view = { insert : false, edit : [], sub : {} };
+			}
+		}
 		refresh();
 	}
 
+	public function canInsert() {
+		return view == null || view.insert;
+	}
+
+	public function canEditColumn( name : String ) {
+		return view == null || (view.edit != null && view.edit.indexOf(name) >= 0);
+	}
+
 	public function close() {
 		element.remove();
 		dispose();
@@ -36,6 +60,7 @@ class Table extends Component {
 
 	public function refresh() {
 		element.empty();
+		columns = view == null || view.show == null ? sheet.columns : [for( c in sheet.columns ) if( view.show.indexOf(c.name) >= 0 ) c];
 		switch( displayMode ) {
 		case Table:
 			refreshTable();
@@ -77,7 +102,7 @@ class Table extends Component {
 			var l = J("<tr>");
 			var head = J("<td>").addClass("start").text("" + index);
 			head.appendTo(l);
-			var line = new Line(this, sheet.columns, index, l);
+			var line = new Line(this, columns, index, l);
 			head.mousedown(function(e) {
 				if( e.which == 3 ) {
 					editor.popupLine(line);
@@ -95,9 +120,8 @@ class Table extends Component {
 			line;
 		}];
 
-		var colCount = sheet.columns.length;
-		for( cindex in 0...sheet.columns.length ) {
-			var c = sheet.columns[cindex];
+		var colCount = columns.length;
+		for( c in columns ) {
 			var col = J("<th>");
 			col.text(c.name);
 			col.addClass( "t_"+c.type.getName().substr(1).toLowerCase() );
@@ -111,7 +135,7 @@ class Table extends Component {
 				}
 			});
 			col.dblclick(function(_) {
-				editor.editColumn(sheet, c);
+				if( editor.view == null ) editor.editColumn(sheet, c);
 			});
 			cols.append(col);
 
@@ -149,7 +173,7 @@ class Table extends Component {
 			});
 			element.append(l);
 		} else if( sheet.lines.length == 0 ) {
-			var l = J('<tr><td colspan="${sheet.columns.length + 1}"><input type="button" value="Insert Line"/></td></tr>');
+			var l = J('<tr><td colspan="${columns.length + 1}"><input type="button" value="Insert Line"/></td></tr>');
 			l.find("input").click(function(_) {
 				editor.insertLine(this);
 				editor.cursor.set(this);
@@ -203,7 +227,7 @@ class Table extends Component {
 
 		var available = [];
 		var props = sheet.lines[0];
-		for( c in sheet.columns ) {
+		for( c in columns ) {
 
 			if( c.opt && props != null && !Reflect.hasField(props,c.name) && displayMode != AllProperties ) {
 				available.push(c);
@@ -239,9 +263,16 @@ class Table extends Component {
 		end = new Element("<td>").attr("colspan", "2").appendTo(end);
 		var sel = new Element("<select class='insertField'>").appendTo(end);
 		new Element("<option>").attr("value", "").text("--- Choose ---").appendTo(sel);
+		var canInsert = false;
 		for( c in available )
-			J("<option>").attr("value",c.name).text(c.name).appendTo(sel);
-		J("<option>").attr("value","$new").text("New property...").appendTo(sel);
+			if( canEditColumn(c.name) ) {
+				J("<option>").attr("value",c.name).text(c.name).appendTo(sel);
+				canInsert = true;
+			}
+		if( editor.view == null )
+			J("<option>").attr("value","$new").text("New property...").appendTo(sel);
+		else if( !canInsert )
+			end.remove();
 		sel.change(function(e) {
 			var v = sel.val();
 			if( v == "" )

+ 3 - 1
hide/view/CdbTable.hx

@@ -6,6 +6,7 @@ class CdbTable extends hide.ui.View<{}> {
 	var editor : hide.comp.cdb.Editor;
 	var currentSheet : String;
 	var tabCache : String;
+	var view : hide.comp.cdb.ConfigView;
 
 	public function new( ?state ) {
 		super(state);
@@ -19,6 +20,7 @@ class CdbTable extends hide.ui.View<{}> {
 		});
 		undo = editor.undo;
 		currentSheet = this.config.get("cdb.currentSheet");
+		view = cast this.config.get("cdb.view");
 	}
 
 	function syncTabs() {
@@ -29,7 +31,7 @@ class CdbTable extends hide.ui.View<{}> {
 	}
 
 	function getSheets() {
-		return [for( s in ide.database.sheets ) if( !s.props.hide ) s];
+		return [for( s in ide.database.sheets ) if( !s.props.hide && (view == null || view.exists(s.name)) ) s];
 	}
 
 	function getTabCache() {