CdbCustomTypes.hx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package hide.view;
  2. class CdbCustomTypes extends hide.ui.View<{}> {
  3. var script : hide.comp.CodeEditor;
  4. var types : Array<cdb.Data.CustomType>;
  5. var modified(default, set) : Bool;
  6. override function onDisplay() {
  7. element.addClass("script-editor");
  8. var tl = [];
  9. for( t in ide.database.getCustomTypes() )
  10. tl.push("enum " + t.name + " {\n" + ide.database.typeCasesToString(t, "\t") + "\n}");
  11. var typesStr = tl.join("\n\n");
  12. script = new hide.comp.CodeEditor(typesStr, "hx", element);
  13. script.onSave = function() {
  14. if( !modified ) return;
  15. if( types == null ) {
  16. ide.error("Can't save with errors");
  17. return;
  18. }
  19. var base = ide.database;
  20. var tpairs = base.makePairs(base.getCustomTypes(), types);
  21. // check if we can remove some types used in sheets
  22. for( p in tpairs )
  23. if( p.b == null ) {
  24. var t = p.a;
  25. for( s in base.sheets )
  26. for( c in s.columns )
  27. switch( c.type ) {
  28. case TCustom(name) if( name == t.name ):
  29. ide.error("Type "+name+" used by " + s.name + "@" + c.name+" cannot be removed");
  30. return;
  31. default:
  32. }
  33. }
  34. // add new types
  35. for( t in types )
  36. if( !Lambda.exists(tpairs,function(p) return p.b == t) )
  37. base.getCustomTypes().push(t);
  38. // update existing types
  39. for( p in tpairs ) {
  40. if( p.b == null )
  41. base.getCustomTypes().remove(p.a);
  42. else if( ide.isDebugger )
  43. base.updateType(p.a, p.b);
  44. else {
  45. try base.updateType(p.a, p.b) catch( msg : String ) {
  46. ide.error("Error while updating " + p.b.name + " : " + msg);
  47. return;
  48. }
  49. }
  50. }
  51. base.sync();
  52. // full rebuild
  53. modified = false;
  54. types = null;
  55. rebuild();
  56. ide.saveDatabase();
  57. };
  58. script.onChanged = function() {
  59. var nstr = script.code;
  60. script.clearError();
  61. var errors = [];
  62. var base = ide.database;
  63. var t = StringTools.trim(nstr);
  64. var r = ~/^enum[ \r\n\t]+([A-Za-z0-9_]+)[ \r\n\t]*\{([^}]*)\}/;
  65. var oldTMap = @:privateAccess base.tmap;
  66. var descs = [];
  67. var tmap = new Map();
  68. @:privateAccess base.tmap = tmap;
  69. types = [];
  70. while( r.match(t) ) {
  71. var name = r.matched(1);
  72. var desc = r.matched(2);
  73. if( tmap.get(name) != null )
  74. errors.push("Duplicate type " + name);
  75. var td = { name : name, cases : [] } ;
  76. tmap.set(name, td);
  77. descs.push(desc);
  78. types.push(td);
  79. t = StringTools.trim(r.matchedRight());
  80. }
  81. for( t in types ) {
  82. try
  83. t.cases = base.parseTypeCases(descs.shift())
  84. catch( msg : Dynamic )
  85. errors.push(msg);
  86. }
  87. @:privateAccess base.tmap = oldTMap;
  88. if( t != "" )
  89. errors.push("Invalid " + StringTools.htmlEscape(t));
  90. if( errors.length > 0 ) {
  91. script.setError(errors.join("\n"),1,0,0);
  92. types = null;
  93. }
  94. modified = typesStr != nstr;
  95. };
  96. }
  97. function set_modified(b) {
  98. if( modified == b )
  99. return b;
  100. modified = b;
  101. syncTitle();
  102. return b;
  103. }
  104. override function getTitle() {
  105. return "CDB Types" + (modified ?" *" : "");
  106. }
  107. static var _ = hide.ui.View.register(CdbCustomTypes);
  108. }