ScriptTable.hx 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package hide.comp.cdb;
  2. class ScriptTable extends SubTable {
  3. var script : hide.comp.ScriptEditor;
  4. override function makeSubSheet():cdb.Sheet {
  5. var sheet = cell.table.sheet;
  6. var c = cell.column;
  7. var index = cell.line.index;
  8. var key = sheet.getPath() + "@" + c.name + ":" + index;
  9. this.lines = [];
  10. return new cdb.Sheet(editor.base, {
  11. columns : [c],
  12. props : {},
  13. name : key,
  14. lines : [cell.line.obj],
  15. separators: [],
  16. }, key, { sheet : sheet, column : cell.columnIndex, line : index });
  17. }
  18. override public function close() {
  19. if( script != null ) saveValue();
  20. super.close();
  21. }
  22. function saveValue() {
  23. var code = [for( line in script.code.split("\r\n").join("\n").split("\n") ) StringTools.rtrim(line)].join("\n");
  24. cell.setValue(code);
  25. }
  26. override function refresh() {
  27. var first = script == null;
  28. element.html("<div class='cdb-script'></div>");
  29. var div = element.children("div");
  30. var ids = [];
  31. var table = cell.table;
  32. var obj = cell.line.obj;
  33. while( table != null ) {
  34. var idCol = table.getRealSheet().idCol;
  35. if( idCol != null ) {
  36. var id = Reflect.field(obj, idCol.name);
  37. if( id == null ) id = "#";
  38. ids.unshift(id);
  39. }
  40. var st = Std.downcast(table, SubTable);
  41. table = table.parent;
  42. if( st != null ) obj = st.cell.line.obj;
  43. }
  44. div.on("keypress keydown keyup", (e) -> {
  45. // let pass Ctrl+S if ObjEditor (allow save script)
  46. if( e.keyCode != "S".code || !Std.is(editor, ObjEditor) ) e.stopPropagation();
  47. });
  48. var checker = new ScriptEditor.ScriptChecker(editor.config,"cdb."+cell.getDocumentName(),[
  49. "cdb."+cell.table.sheet.name => cell.line.obj,
  50. "cdb.objID" => ids.join(":"),
  51. "cdb.groupID" => cell.line.getGroupID(),
  52. ]);
  53. script = new ScriptEditor(cell.value, checker, div);
  54. script.onSave = saveValue;
  55. script.propagateKeys = true;
  56. script.onClose = function() { close(); cell.focus(); }
  57. lines = [new Line(this,[],0,script.element)];
  58. insertedTR.addClass("code");
  59. if( first ) script.focus();
  60. }
  61. }