Browse Source

Prevent id duplication when editing in CDB

Clement Espeute 3 years ago
parent
commit
f6bd9bcb44
2 changed files with 39 additions and 22 deletions
  1. 37 16
      hide/comp/cdb/Cell.hx
  2. 2 6
      hide/comp/cdb/Editor.hx

+ 37 - 16
hide/comp/cdb/Cell.hx

@@ -703,7 +703,12 @@ class Cell extends Component {
 				e.stopPropagation();
 				e.stopPropagation();
 			});
 			});
 			i.keyup(function(_) try {
 			i.keyup(function(_) try {
-				editor.base.parseValue(column.type, i.val());
+				var v = editor.base.parseValue(column.type, i.val());
+
+				if (column.type == TId && !isUniqueID((v:String), true)) {
+						throw v + " is not an unique id";
+				}
+
 				setErrorMessage(null);
 				setErrorMessage(null);
 			} catch( e : Dynamic ) {
 			} catch( e : Dynamic ) {
 				setErrorMessage(StringTools.htmlUnescape(""+e));
 				setErrorMessage(StringTools.htmlUnescape(""+e));
@@ -1077,6 +1082,12 @@ class Cell extends Component {
 		new Element("<div>").addClass("error").html(msg).appendTo(element);
 		new Element("<div>").addClass("error").html(msg).appendTo(element);
 	}
 	}
 
 
+	public function isUniqueID(id : String, ignoreSelf:Bool = false) {
+		var scope = getScope();
+		var idWithScope : String = if (column.scope != null) makeId(scope, column.scope, id) else id;
+		return editor.isUniqueID(table.getRealSheet(), if(ignoreSelf) line.obj else {}, idWithScope);
+	}
+
 	function setRawValue( str : Dynamic ) {
 	function setRawValue( str : Dynamic ) {
 		var newValue : Dynamic;
 		var newValue : Dynamic;
 		if( Std.isOfType(str,String) ) {
 		if( Std.isOfType(str,String) ) {
@@ -1098,22 +1109,32 @@ class Cell extends Component {
 			var prevObj = value != null ? realSheet.index.get(isLocal ? parentID+":"+value : value) : null;
 			var prevObj = value != null ? realSheet.index.get(isLocal ? parentID+":"+value : value) : null;
 			// have we already an obj mapped to the same id ?
 			// have we already an obj mapped to the same id ?
 			var prevTarget = realSheet.index.get(isLocal ? parentID+":"+newValue : newValue);
 			var prevTarget = realSheet.index.get(isLocal ? parentID+":"+newValue : newValue);
-			editor.beginChanges();
-			if( prevObj == null || prevObj.obj == obj ) {
-				// remap
-				var m = new Map();
-				m.set(value, newValue);
-				if( isLocal ) {
-					var scope = getScope();
-					var parent = scope[scope.length - realSheet.idCol.scope];
-					editor.base.updateLocalRefs(realSheet, m, parent.obj, parent.s);
-				} else
-					editor.base.updateRefs(realSheet, m);
+			
+			if (isUniqueID(newValue, true))
+			{
+				editor.beginChanges();
+				if( prevObj == null || prevObj.obj == obj ) {
+					// remap
+					var m = new Map();
+					m.set(value, newValue);
+					if( isLocal ) {
+						var scope = getScope();
+						var parent = scope[scope.length - realSheet.idCol.scope];
+						editor.base.updateLocalRefs(realSheet, m, parent.obj, parent.s);
+					} else
+						editor.base.updateRefs(realSheet, m);
+				}
+				setValue(newValue);
+				editor.endChanges();
+				editor.refreshRefs();
+				focus();
+	
+				// Refresh display of all ids in the column manually
+				var colId = table.sheet.columns.indexOf(column);
+				for (l in table.lines) {
+					l.cells[colId].refresh(false);	
+				}
 			}
 			}
-			setValue(newValue);
-			editor.endChanges();
-			editor.refreshRefs();
-			focus();
 			/*
 			/*
 			// creates or remove a #DUP : need to refresh the whole table
 			// creates or remove a #DUP : need to refresh the whole table
 			if( prevTarget != null || (prevObj != null && (prevObj.obj != obj || table.sheet.index.get(prevValue) != null)) )
 			if( prevTarget != null || (prevObj != null && (prevObj.obj != obj || table.sheet.index.get(prevValue) != null)) )

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

@@ -1245,13 +1245,11 @@ class Editor extends Component {
 					
 					
 					if (currentValue != null) {
 					if (currentValue != null) {
 						var cell : Cell = table.lines[index].cells[colId];
 						var cell : Cell = table.lines[index].cells[colId];
-						@:privateAccess var scopes = cell.getScope();
 
 
 						var newId : String;
 						var newId : String;
 						var idWithScope : String;
 						var idWithScope : String;
 						do {
 						do {
-							currentValue+=1;
-							
+							currentValue+=1;							
 							var valStr = Std.string(currentValue);
 							var valStr = Std.string(currentValue);
 
 
 							// Pad with zeroes
 							// Pad with zeroes
@@ -1259,10 +1257,8 @@ class Editor extends Component {
 								valStr = "0" + valStr;
 								valStr = "0" + valStr;
 							}
 							}
 							newId = str.substr(0, -strIdx) + valStr;
 							newId = str.substr(0, -strIdx) + valStr;
-
-							@:privateAccess idWithScope = if (c.scope != null) cell.makeId(scopes, c.scope, newId) else newId;
 						}
 						}
-						while (!isUniqueID(table.sheet,{},idWithScope));
+						while (!cell.isUniqueID(newId,true));
 						Reflect.setField(obj, c.name, newId);
 						Reflect.setField(obj, c.name, newId);
 					}
 					}
 				}
 				}