Made (blank) and (error) choices in list facets editable, too.

git-svn-id: http://google-refine.googlecode.com/svn/trunk@569 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
David Huynh 2010-04-29 20:18:06 +00:00
parent cf01dcd965
commit 17c9b65889
2 changed files with 79 additions and 22 deletions

View File

@ -31,10 +31,14 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
static public class Edit implements Jsonizable { static public class Edit implements Jsonizable {
final public List<String> from; final public List<String> from;
final public boolean fromBlank;
final public boolean fromError;
final public Serializable to; final public Serializable to;
public Edit(List<String> from, Serializable to) { public Edit(List<String> from, boolean fromBlank, boolean fromError, Serializable to) {
this.from = from; this.from = from;
this.fromBlank = fromBlank;
this.fromError = fromError;
this.to = to; this.to = to;
} }
@ -42,6 +46,8 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
throws JSONException { throws JSONException {
writer.object(); writer.object();
writer.key("fromBlank"); writer.value(fromBlank);
writer.key("fromError"); writer.value(fromError);
writer.key("from"); writer.key("from");
writer.array(); writer.array();
for (String s : from) { for (String s : from) {
@ -72,14 +78,22 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
for (int i = 0; i < editCount; i++) { for (int i = 0; i < editCount; i++) {
JSONObject editO = editsA.getJSONObject(i); JSONObject editO = editsA.getJSONObject(i);
JSONArray fromA = editO.getJSONArray("from"); List<String> from = null;
int fromCount = fromA.length(); if (editO.has("from") && !editO.isNull("from")) {
JSONArray fromA = editO.getJSONArray("from");
List<String> from = new ArrayList<String>(fromCount); int fromCount = fromA.length();
for (int j = 0; j < fromCount; j++) {
from.add(fromA.getString(j)); from = new ArrayList<String>(fromCount);
for (int j = 0; j < fromCount; j++) {
from.add(fromA.getString(j));
}
} else {
from = new ArrayList<String>();
} }
boolean fromBlank = editO.has("fromBlank") && editO.getBoolean("fromBlank");
boolean fromError = editO.has("fromError") && editO.getBoolean("fromError");
Serializable to = (Serializable) editO.get("to"); Serializable to = (Serializable) editO.get("to");
if (editO.has("type")) { if (editO.has("type")) {
String type = editO.getString("type"); String type = editO.getString("type");
@ -88,7 +102,7 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
} }
} }
edits.add(new Edit(from, to)); edits.add(new Edit(from, fromBlank, fromError, to));
} }
return edits; return edits;
@ -136,10 +150,21 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
Properties bindings = ExpressionUtils.createBindings(project); Properties bindings = ExpressionUtils.createBindings(project);
Map<String, Serializable> fromTo = new HashMap<String, Serializable>(); Map<String, Serializable> fromTo = new HashMap<String, Serializable>();
Serializable fromBlankTo = null;
Serializable fromErrorTo = null;
for (Edit edit : _edits) { for (Edit edit : _edits) {
for (String s : edit.from) { for (String s : edit.from) {
fromTo.put(s, edit.to); fromTo.put(s, edit.to);
} }
// the last edit wins
if (edit.fromBlank) {
fromBlankTo = edit.to;
}
if (edit.fromError) {
fromErrorTo = edit.to;
}
} }
return new RowVisitor() { return new RowVisitor() {
@ -147,41 +172,59 @@ public class MassEditOperation extends EngineDependentMassCellOperation {
Properties bindings; Properties bindings;
List<CellChange> cellChanges; List<CellChange> cellChanges;
Evaluable eval; Evaluable eval;
Map<String, Serializable> fromTo; Map<String, Serializable> fromTo;
Serializable fromBlankTo;
Serializable fromErrorTo;
public RowVisitor init( public RowVisitor init(
int cellIndex, int cellIndex,
Properties bindings, Properties bindings,
List<CellChange> cellChanges, List<CellChange> cellChanges,
Evaluable eval, Evaluable eval,
Map<String, Serializable> fromTo Map<String, Serializable> fromTo,
Serializable fromBlankTo,
Serializable fromErrorTo
) { ) {
this.cellIndex = cellIndex; this.cellIndex = cellIndex;
this.bindings = bindings; this.bindings = bindings;
this.cellChanges = cellChanges; this.cellChanges = cellChanges;
this.eval = eval; this.eval = eval;
this.fromTo = fromTo; this.fromTo = fromTo;
this.fromBlankTo = fromBlankTo;
this.fromErrorTo = fromErrorTo;
return this; return this;
} }
public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) { public boolean visit(Project project, int rowIndex, Row row, boolean includeContextual, boolean includeDependent) {
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
Cell newCell = null;
ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell); ExpressionUtils.bind(bindings, row, rowIndex, _columnName, cell);
Object v = eval.evaluate(bindings); Object v = eval.evaluate(bindings);
if (v != null) { if (ExpressionUtils.isError(v)) {
if (fromErrorTo != null) {
newCell = new Cell(fromErrorTo, (cell != null) ? cell.recon : null);
}
} else if (ExpressionUtils.isNonBlankData(v)) {
String from = v.toString(); String from = v.toString();
Serializable to = fromTo.get(from); Serializable to = fromTo.get(from);
if (to != null) { if (to != null) {
Cell newCell = new Cell(to, (cell != null) ? cell.recon : null); newCell = new Cell(to, (cell != null) ? cell.recon : null);
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell); }
cellChanges.add(cellChange); } else {
if (fromBlankTo != null) {
newCell = new Cell(fromBlankTo, (cell != null) ? cell.recon : null);
} }
} }
if (newCell != null) {
CellChange cellChange = new CellChange(rowIndex, cellIndex, cell, newCell);
cellChanges.add(cellChange);
}
return false; return false;
} }
}.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo); }.init(column.getCellIndex(), bindings, cellChanges, eval, fromTo, fromBlankTo, fromErrorTo);
} }
} }

View File

@ -249,7 +249,7 @@ ListFacet.prototype._update = function(resetScroll) {
); );
// edit link // edit link
if (renderEdit && customLabel === undefined) { if (renderEdit) {
html.push('<a href="javascript:{}" class="facet-choice-link facet-choice-edit" style="visibility: hidden">edit</a>'); html.push('<a href="javascript:{}" class="facet-choice-link facet-choice-edit" style="visibility: hidden">edit</a>');
} }
@ -377,22 +377,36 @@ ListFacet.prototype._editChoice = function(choice, choiceDiv) {
MenuSystem.showMenu(menu, function(){}); MenuSystem.showMenu(menu, function(){});
MenuSystem.positionMenuLeftRight(menu, choiceDiv); MenuSystem.positionMenuLeftRight(menu, choiceDiv);
var originalContent = choice.v.v; var originalContent;
if (choice === this._blankChoice) {
originalContent = "(blank)";
} else if (choice === this._errorChoice) {
originalContent = "(error)";
} else {
originalContent = choice.v.v;
}
var commit = function() { var commit = function() {
var text = elmts.textarea[0].value; var text = elmts.textarea[0].value;
MenuSystem.dismissAll(); MenuSystem.dismissAll();
var edit = { to : text };
if (choice === self._blankChoice) {
edit.fromBlank = true;
} else if (choice === self._errorChoice) {
edit.fromError = true;
} else {
edit.from = [ originalContent ]
}
Gridworks.postProcess( Gridworks.postProcess(
"mass-edit", "mass-edit",
{}, {},
{ {
columnName: self._config.columnName, columnName: self._config.columnName,
expression: "value", expression: "value",
edits: JSON.stringify([{ edits: JSON.stringify([ edit ])
from: [ originalContent ],
to: text
}])
}, },
{ {
// limit edits to rows constrained only by the other facets // limit edits to rows constrained only by the other facets