Editor.hx 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336
  1. package hide.comp.cdb;
  2. import hxd.Key in K;
  3. enum Direction {
  4. Left;
  5. Right;
  6. }
  7. typedef UndoSheet = {
  8. var sheet : String;
  9. var parent : { sheet : UndoSheet, line : Int, column : Int };
  10. }
  11. typedef UndoState = {
  12. var data : Any;
  13. var sheet : String;
  14. var cursor : Cursor.CursorState;
  15. var tables : Array<UndoSheet>;
  16. }
  17. typedef EditorApi = {
  18. function load( data : Any ) : Void;
  19. function copy() : Any;
  20. function save() : Void;
  21. }
  22. typedef EditorColumnProps = {
  23. var ?formula : String;
  24. var ?ignoreExport : Bool;
  25. var ?categories : Array<String>;
  26. }
  27. typedef EditorSheetProps = {
  28. var ?categories : Array<String>;
  29. }
  30. @:allow(hide.comp.cdb)
  31. class Editor extends Component {
  32. var base : cdb.Database;
  33. var currentSheet : cdb.Sheet;
  34. var existsCache : Map<String,{ t : Float, r : Bool }> = new Map();
  35. var tables : Array<Table> = [];
  36. var searchBox : Element;
  37. var displayMode : Table.DisplayMode;
  38. var clipboard : {
  39. text : String,
  40. data : Array<{}>,
  41. schema : Array<cdb.Data.Column>,
  42. };
  43. var changesDepth : Int = 0;
  44. var currentFilter : String;
  45. var api : EditorApi;
  46. var undoState : Array<UndoState> = [];
  47. var currentValue : Any;
  48. var cdbTable : hide.view.CdbTable;
  49. public var view : cdb.DiffFile.ConfigView;
  50. public var config : hide.Config;
  51. public var cursor : Cursor;
  52. public var keys : hide.ui.Keys;
  53. public var undo : hide.ui.UndoHistory;
  54. public var cursorStates : Array<UndoState> = [];
  55. public var cursorIndex : Int = 0;
  56. public var formulas : Formulas;
  57. public function new(config, api, ?cdbTable) {
  58. super(null,null);
  59. this.api = api;
  60. this.config = config;
  61. this.cdbTable = cdbTable;
  62. view = cast this.config.get("cdb.view");
  63. undo = new hide.ui.UndoHistory();
  64. }
  65. public function getCurrentSheet() {
  66. return currentSheet == null ? null : currentSheet.name;
  67. }
  68. public function show( sheet, ?parent : Element ) {
  69. if( element != null ) element.remove();
  70. element = new Element('<div>');
  71. if( parent != null )
  72. parent.append(element);
  73. currentSheet = sheet;
  74. element.attr("tabindex", 0);
  75. element.addClass("is-cdb-editor");
  76. element.data("cdb", this);
  77. element.on("blur", function(_) cursor.hide());
  78. element.on("keypress", function(e) {
  79. if( e.target.nodeName == "INPUT" )
  80. return;
  81. var cell = cursor.getCell();
  82. if( cell != null && cell.isTextInput() && !e.ctrlKey )
  83. cell.edit();
  84. });
  85. element.contextmenu(function(e) e.preventDefault());
  86. if( cdbTable == null )
  87. element.mousedown(onMouseDown);
  88. else {
  89. cdbTable.element.off("mousedown", onMouseDown);
  90. cdbTable.element.mousedown(onMouseDown);
  91. }
  92. keys = new hide.ui.Keys(element);
  93. keys.addListener(onKey);
  94. keys.register("search", function() {
  95. searchBox.show();
  96. searchBox.find("input").val("").focus().select();
  97. });
  98. keys.register("copy", onCopy);
  99. keys.register("paste", onPaste);
  100. keys.register("delete", onDelete);
  101. keys.register("cdb.showReferences", showReferences);
  102. keys.register("undo", function() undo.undo());
  103. keys.register("redo", function() undo.redo());
  104. keys.register("cdb.moveBack", () -> cursorJump(true));
  105. keys.register("cdb.moveAhead", () -> cursorJump(false));
  106. keys.register("cdb.insertLine", function() { insertLine(cursor.table,cursor.y); cursor.move(0,1,false,false); });
  107. for( k in ["cdb.editCell","rename"] )
  108. keys.register(k, function() {
  109. var c = cursor.getCell();
  110. if( c != null ) c.edit();
  111. });
  112. keys.register("cdb.closeList", function() {
  113. var c = cursor.getCell();
  114. var sub = Std.downcast(c == null ? cursor.table : c.table, SubTable);
  115. if( sub != null ) {
  116. sub.cell.element.click();
  117. return;
  118. }
  119. if( cursor.select != null ) {
  120. cursor.select = null;
  121. cursor.update();
  122. }
  123. });
  124. keys.register("cdb.gotoReference", () -> gotoReference(cursor.getCell()));
  125. base = sheet.base;
  126. if( cursor == null )
  127. cursor = new Cursor(this);
  128. else if ( !tables.contains(cursor.table) )
  129. cursor.set();
  130. if( displayMode == null ) displayMode = Table;
  131. DataFiles.load();
  132. if( currentValue == null ) currentValue = api.copy();
  133. refresh();
  134. }
  135. function onMouseDown( e : js.jquery.Event ) {
  136. switch ( e.which ) {
  137. case 4:
  138. cursorJump(true);
  139. return false;
  140. case 5:
  141. cursorJump(false);
  142. return false;
  143. }
  144. return true;
  145. }
  146. function onKey( e : js.jquery.Event ) {
  147. if( e.altKey )
  148. return false;
  149. switch( e.keyCode ) {
  150. case K.LEFT:
  151. cursor.move( -1, 0, e.shiftKey, e.ctrlKey);
  152. return true;
  153. case K.RIGHT:
  154. cursor.move( 1, 0, e.shiftKey, e.ctrlKey);
  155. return true;
  156. case K.UP:
  157. cursor.move( 0, -1, e.shiftKey, e.ctrlKey);
  158. return true;
  159. case K.DOWN:
  160. cursor.move( 0, 1, e.shiftKey, e.ctrlKey);
  161. return true;
  162. case K.TAB:
  163. cursor.move( e.shiftKey ? -1 : 1, 0, false, false);
  164. return true;
  165. case K.PGUP:
  166. cursor.move(0, -10, e.shiftKey, e.ctrlKey);
  167. return true;
  168. case K.PGDOWN:
  169. cursor.move(0, 10, e.shiftKey, e.ctrlKey);
  170. return true;
  171. case K.SPACE:
  172. e.preventDefault(); // prevent scroll
  173. case K.ESCAPE:
  174. if( currentFilter != null ) {
  175. searchFilter(null);
  176. searchBox.hide();
  177. }
  178. }
  179. return false;
  180. }
  181. public function updateFilter() {
  182. searchFilter(currentFilter);
  183. }
  184. public function setFilter( f : String ) {
  185. if( searchBox != null ) {
  186. if( f == null )
  187. searchBox.hide();
  188. else {
  189. searchBox.show();
  190. searchBox.find("input").val(f);
  191. }
  192. }
  193. searchFilter(f);
  194. }
  195. function searchFilter( filter : String ) {
  196. if( filter == "" ) filter = null;
  197. if( filter != null ) filter = filter.toLowerCase();
  198. var all = element.find("table.cdb-sheet > tbody > tr").not(".head");
  199. var seps = all.filter(".separator");
  200. var lines = all.not(".separator");
  201. all.removeClass("filtered");
  202. if( filter != null ) {
  203. for( t in lines ) {
  204. if( t.textContent.toLowerCase().indexOf(filter) < 0 )
  205. t.classList.add("filtered");
  206. }
  207. while( lines.length > 0 ) {
  208. lines = lines.filter(".list").not(".filtered").prev();
  209. lines.removeClass("filtered");
  210. }
  211. all = all.not(".filtered").not(".hidden");
  212. for( s in seps.elements() ) {
  213. var idx = all.index(s);
  214. if( idx == all.length - 1 || new Element(all.get(idx+1)).hasClass("separator") ) {
  215. s.addClass("filtered");
  216. }
  217. }
  218. }
  219. currentFilter = filter;
  220. cursor.update();
  221. }
  222. function onCopy() {
  223. var sel = cursor.getSelection();
  224. if( sel == null )
  225. return;
  226. var data = [];
  227. for( y in sel.y1...sel.y2+1 ) {
  228. var obj = cursor.table.lines[y].obj;
  229. var out = {};
  230. for( x in sel.x1...sel.x2+1 ) {
  231. var c = cursor.table.columns[x];
  232. var form = @:privateAccess formulas.getFormulaNameFromValue(obj, c);
  233. if( form != null ) {
  234. Reflect.setField(out, c.name+"__f", form);
  235. continue;
  236. }
  237. var v = Reflect.field(obj, c.name);
  238. if( v != null )
  239. Reflect.setField(out, c.name, v);
  240. }
  241. data.push(out);
  242. }
  243. clipboard = {
  244. data : data,
  245. text : Std.string([for( o in data ) cursor.table.sheet.objToString(o,true)]),
  246. schema : [for( x in sel.x1...sel.x2+1 ) cursor.table.columns[x]],
  247. };
  248. ide.setClipboard(clipboard.text);
  249. }
  250. function onPaste() {
  251. var text = ide.getClipboard();
  252. var columns = cursor.table.columns;
  253. var sheet = cursor.table.sheet;
  254. var realSheet = cursor.table.getRealSheet();
  255. var allLines = cursor.table.lines;
  256. var fullRefresh = false;
  257. var toRefresh : Array<Cell> = [];
  258. var isProps = (cursor.table.displayMode != Table);
  259. var x1 = cursor.x;
  260. var y1 = cursor.y;
  261. var x2 = cursor.select == null ? x1 : cursor.select.x;
  262. var y2 = cursor.select == null ? y1 : cursor.select.y;
  263. if( x1 > x2 ) {
  264. var tmp = x1;
  265. x1 = x2;
  266. x2 = tmp;
  267. }
  268. if( y1 > y2 ) {
  269. var tmp = y1;
  270. y1 = y2;
  271. y2 = tmp;
  272. }
  273. if( clipboard == null || text != clipboard.text ) {
  274. if( cursor.x < 0 || cursor.y < 0 ) return;
  275. function parseText(text, type : cdb.Data.ColumnType) : Dynamic {
  276. switch( type ) {
  277. case TId:
  278. if( ~/^[A-Za-z0-9_]+$/.match(text) )
  279. return text;
  280. case TString:
  281. return text;
  282. case TFile:
  283. return ide.makeRelative(text);
  284. case TInt:
  285. text = text.split(",").join("").split(" ").join("");
  286. return Std.parseInt(text);
  287. case TFloat:
  288. text = text.split(",").join("").split(" ").join("");
  289. var value = Std.parseFloat(text);
  290. if( Math.isNaN(value) )
  291. return null;
  292. return value;
  293. default:
  294. }
  295. return null;
  296. }
  297. if( isProps ) {
  298. var line = cursor.getLine();
  299. toRefresh.push(cursor.getCell());
  300. var col = line.columns[x1];
  301. if( !cursor.table.canEditColumn(col.name) )
  302. return;
  303. var value = parseText(text, col.type);
  304. if( value == null )
  305. return;
  306. beginChanges();
  307. var obj = line.obj;
  308. formulas.removeFromValue(obj, col);
  309. Reflect.setField(obj, col.name, value);
  310. } else {
  311. beginChanges();
  312. for( x in x1...x2+1 ) {
  313. var col = columns[x];
  314. if( !cursor.table.canEditColumn(col.name) )
  315. continue;
  316. var lines = y1 == y2 ? [text] : text.split("\n");
  317. for( y in y1...y2+1 ) {
  318. var text = lines[y - y1];
  319. if( text == null ) text = lines[lines.length - 1];
  320. var value = parseText(text, col.type);
  321. if( value == null ) continue;
  322. var obj = sheet.lines[y];
  323. formulas.removeFromValue(obj, col);
  324. Reflect.setField(obj, col.name, value);
  325. toRefresh.push(allLines[y].cells[x]);
  326. }
  327. }
  328. }
  329. formulas.evaluateAll(realSheet);
  330. endChanges();
  331. realSheet.sync();
  332. for( c in toRefresh ) {
  333. c.refresh(true);
  334. }
  335. refreshRefs();
  336. return;
  337. }
  338. function setValue(cliObj, destObj, clipSchema : cdb.Data.Column, destCol : cdb.Data.Column) {
  339. var form = Reflect.field(cliObj, clipSchema.name+"__f");
  340. if( form != null && destCol.type.equals(clipSchema.type) ) {
  341. formulas.setForValue(destObj, sheet, destCol, form);
  342. return;
  343. }
  344. var f = base.getConvFunction(clipSchema.type, destCol.type);
  345. var v : Dynamic = Reflect.field(cliObj, clipSchema.name);
  346. if( f == null )
  347. v = base.getDefault(destCol, sheet);
  348. else {
  349. // make a deep copy to erase references
  350. if( v != null ) v = haxe.Json.parse(haxe.Json.stringify(v));
  351. if( f.f != null )
  352. v = f.f(v);
  353. }
  354. if( v == null && !destCol.opt )
  355. v = base.getDefault(destCol, sheet);
  356. if( v == null )
  357. Reflect.deleteField(destObj, destCol.name);
  358. else
  359. Reflect.setField(destObj, destCol.name, v);
  360. }
  361. var posX = cursor.x < 0 ? 0 : cursor.x;
  362. var posY = cursor.y < 0 ? 0 : cursor.y;
  363. var data = clipboard.data;
  364. if( data.length == 0 )
  365. return;
  366. if( isProps ) {
  367. var obj1 = data[0];
  368. var line = cursor.getLine();
  369. var destCol = line.columns[posX];
  370. var obj2 = line.obj;
  371. var clipSchema = clipboard.schema[0];
  372. if( clipSchema == null || destCol == null)
  373. return;
  374. if( !cursor.table.canEditColumn(destCol.name) )
  375. return;
  376. toRefresh.push(cursor.getCell());
  377. beginChanges();
  378. setValue(obj1, obj2, clipSchema, destCol);
  379. } else {
  380. beginChanges();
  381. if( data.length == 1 && y1 != y2 )
  382. data = [for( i in y1...y2+1 ) data[0]];
  383. for( obj1 in data ) {
  384. if( posY == sheet.lines.length ) {
  385. if( !cursor.table.canInsert() ) break;
  386. sheet.newLine();
  387. fullRefresh = true;
  388. }
  389. var obj2 = sheet.lines[posY];
  390. for( cid in 0...clipboard.schema.length ) {
  391. var c1 = clipboard.schema[cid];
  392. var c2 = columns[cid + posX];
  393. if( c2 == null ) continue;
  394. if( !cursor.table.canEditColumn(c2.name) )
  395. continue;
  396. setValue(obj1, obj2, c1, c2);
  397. if( c2.type == TList || c2.type == TProperties )
  398. fullRefresh = true;
  399. if( !fullRefresh )
  400. toRefresh.push(allLines[posY].cells[cid + posX]);
  401. }
  402. posY++;
  403. }
  404. }
  405. formulas.evaluateAll(realSheet);
  406. endChanges();
  407. realSheet.sync();
  408. if( fullRefresh )
  409. refreshAll();
  410. else {
  411. for( c in toRefresh ) {
  412. c.refresh(true);
  413. }
  414. refreshRefs();
  415. }
  416. }
  417. function onDelete() {
  418. var sel = cursor.getSelection();
  419. if( sel == null )
  420. return;
  421. var hasChanges = false;
  422. beginChanges();
  423. if( cursor.x < 0 ) {
  424. // delete lines
  425. var y = sel.y2;
  426. if( !cursor.table.canInsert() ) {
  427. endChanges();
  428. return;
  429. }
  430. while( y >= sel.y1 ) {
  431. var line = cursor.table.lines[y];
  432. line.table.sheet.deleteLine(line.index);
  433. hasChanges = true;
  434. y--;
  435. }
  436. cursor.set(cursor.table, -1, sel.y1, null, false);
  437. } else {
  438. // delete cells
  439. for( y in sel.y1...sel.y2+1 ) {
  440. var line = cursor.table.lines[y];
  441. for( x in sel.x1...sel.x2+1 ) {
  442. var c = line.columns[x];
  443. if( !line.cells[x].canEdit() )
  444. continue;
  445. var old = Reflect.field(line.obj, c.name);
  446. var def = base.getDefault(c,false,cursor.table.sheet);
  447. if( old == def )
  448. continue;
  449. changeObject(line,c,def);
  450. hasChanges = true;
  451. }
  452. }
  453. }
  454. endChanges();
  455. if( hasChanges )
  456. refreshAll();
  457. }
  458. public function changeObject( line : Line, column : cdb.Data.Column, value : Dynamic ) {
  459. beginChanges();
  460. var prev = Reflect.field(line.obj, column.name);
  461. if( value == null ) {
  462. formulas.setForValue(line.obj, line.table.sheet, column, null);
  463. } else {
  464. Reflect.setField(line.obj, column.name, value);
  465. formulas.removeFromValue(line.obj, column);
  466. }
  467. line.table.getRealSheet().updateValue(column, line.index, prev);
  468. line.evaluate(); // propagate
  469. endChanges();
  470. }
  471. /**
  472. Call before modifying the database, allow to group several changes together.
  473. Allow recursion, only last endChanges() will trigger db save and undo point creation.
  474. **/
  475. public function beginChanges( ?structure : Bool ) {
  476. if( changesDepth == 0 )
  477. undoState.unshift(getState());
  478. changesDepth++;
  479. }
  480. function getState() : UndoState {
  481. return {
  482. data : currentValue,
  483. sheet : getCurrentSheet(),
  484. cursor : cursor.getState(),
  485. tables : [for( i in 1...tables.length ) {
  486. function makeParent(t:Table) : UndoSheet {
  487. var tp = t.parent;
  488. return { sheet : t.sheet.name, parent : tp == null ? null : {
  489. sheet : makeParent(tp),
  490. line : t.sheet.parent.line,
  491. column : tp.columns.indexOf(tp.sheet.columns[t.sheet.parent.column]),
  492. } };
  493. }
  494. makeParent(tables[i]);
  495. }],
  496. };
  497. }
  498. function setState( state : UndoState, doFocus : Bool ) {
  499. var cur = state.cursor;
  500. for( t in state.tables ) {
  501. function openRec(s:UndoSheet) : Table {
  502. if( s.parent != null ) {
  503. var t = openRec(s.parent.sheet);
  504. if( t != null ) {
  505. var cell = t.lines[s.parent.line].cells[t.displayMode == Properties || t.displayMode == AllProperties ? 0 : s.parent.column];
  506. if( cell.line.subTable == null && (cell.column.type == TList || cell.column.type == TProperties) )
  507. cell.open(true);
  508. return cell.line.subTable;
  509. }
  510. } else {
  511. for( tp in tables )
  512. if( tp.sheet.name == s.sheet )
  513. return tp;
  514. }
  515. return null;
  516. }
  517. openRec(t);
  518. }
  519. if( cur != null ) {
  520. var table = null;
  521. for( t in tables ) {
  522. if( t.sheet.getPath() == cur.sheet ) {
  523. table = t;
  524. break;
  525. }
  526. }
  527. if( table != null && doFocus )
  528. focus();
  529. cursor.setState(cur, table);
  530. } else
  531. cursor.set();
  532. }
  533. /**
  534. Call when changes are done, after endChanges.
  535. **/
  536. public function endChanges() {
  537. changesDepth--;
  538. if( changesDepth != 0 ) return;
  539. var newValue = api.copy();
  540. if( newValue == currentValue )
  541. return;
  542. var state = undoState[0];
  543. var newSheet = getCurrentSheet();
  544. currentValue = newValue;
  545. save();
  546. undo.change(Custom(function(undo) {
  547. var currentSheet;
  548. if( undo ) {
  549. undoState.shift();
  550. currentValue = state.data;
  551. currentSheet = state.sheet;
  552. } else {
  553. undoState.unshift(state);
  554. currentValue = newValue;
  555. currentSheet = newSheet;
  556. }
  557. pushCursorState();
  558. api.load(currentValue);
  559. DataFiles.save(true); // save reloaded data
  560. element.removeClass("is-cdb-editor");
  561. refreshAll();
  562. element.addClass("is-cdb-editor");
  563. syncSheet(currentSheet);
  564. refresh(state);
  565. save();
  566. }));
  567. }
  568. function save() {
  569. api.save();
  570. }
  571. function undoStatesEqual( s1 : UndoState, s2 : UndoState, cmpCursors = true ) {
  572. function cursorEqual(c1 : Cursor.CursorState, c2 : Cursor.CursorState) {
  573. if( c1 == c2 )
  574. return true;
  575. if( c1 == null || c2 == null )
  576. return false;
  577. return c1.sheet == c2.sheet && c1.x == c2.x && c1.y == c2.y;
  578. }
  579. function undoSheetEqual(s1 : UndoSheet, s2 : UndoSheet) {
  580. if( s1.parent == null && s2.parent == null )
  581. return s1.sheet == s2.sheet;
  582. if( s1.parent == null || s2.parent == null )
  583. return false;
  584. if ( s1.sheet != s2.sheet || s1.parent.column != s2.parent.column || s1.parent.line != s2.parent.line )
  585. return false;
  586. return undoSheetEqual(s1.parent.sheet, s2.parent.sheet);
  587. }
  588. if ( s1.sheet != s2.sheet )
  589. return false;
  590. if( s1.tables.length != s2.tables.length )
  591. return false;
  592. for( i in 0...s1.tables.length ) {
  593. if( !undoSheetEqual(s1.tables[i], s2.tables[i]) )
  594. return false;
  595. }
  596. if( !cmpCursors )
  597. return true;
  598. if( cursorEqual(s1.cursor, s2.cursor) )
  599. return true;
  600. if( s1.cursor == null || s2.cursor == null )
  601. return false;
  602. return s1.cursor.y == -1 && s2.cursor.y == -1;
  603. }
  604. public function pushCursorState() {
  605. if ( cursor == null )
  606. return;
  607. var state = getState();
  608. state.data = null;
  609. var stateBehind = (cursorStates.length <= 0) ? null : cursorStates[cursorIndex];
  610. if( stateBehind != null && undoStatesEqual(state, stateBehind) )
  611. return;
  612. var stateAhead = (cursorStates.length <= 0 || cursorIndex >= cursorStates.length - 1) ? null : cursorStates[cursorIndex + 1];
  613. if ( stateAhead != null && undoStatesEqual(state, stateAhead) ) {
  614. cursorIndex++;
  615. return;
  616. }
  617. if( cursorIndex < cursorStates.length - 1 && cursorIndex >= 0 ) {
  618. cursorStates.splice(cursorIndex + 1, cursorStates.length);
  619. }
  620. cursorStates.push(state);
  621. if( cursorIndex < cursorStates.length - 1 )
  622. cursorIndex++;
  623. }
  624. function cursorJump(back = true) {
  625. focus();
  626. if( (back && cursorIndex <= 0) || (!back && cursorIndex >= cursorStates.length - 1) )
  627. return;
  628. if( back && cursorIndex == cursorStates.length - 1)
  629. pushCursorState();
  630. if(back)
  631. cursorIndex--;
  632. else
  633. cursorIndex++;
  634. var state = cursorStates[cursorIndex];
  635. syncSheet(null, state.sheet);
  636. if( undoStatesEqual(state, getState(), false) ) {
  637. setState(state, true);
  638. if( cursor.table != null ) {
  639. for( t in tables ) {
  640. if( t.sheet.getPath() == cursor.table.sheet.getPath() )
  641. cursor.table = t;
  642. }
  643. cursor.update();
  644. }
  645. } else
  646. refresh(state);
  647. if( cdbTable != null )
  648. @:privateAccess cdbTable.syncTabs();
  649. }
  650. public static var inRefreshAll(default,null) : Bool;
  651. public static function refreshAll( eraseUndo = false ) {
  652. var editors : Array<Editor> = [for( e in new Element(".is-cdb-editor").elements() ) e.data("cdb")];
  653. DataFiles.load();
  654. inRefreshAll = true;
  655. for( e in editors ) {
  656. e.syncSheet(Ide.inst.database);
  657. e.refresh();
  658. // prevent undo over input changes
  659. if( eraseUndo ) {
  660. e.currentValue = e.api.copy();
  661. e.undo.clear();
  662. e.undoState = [];
  663. }
  664. }
  665. inRefreshAll = false;
  666. }
  667. function showReferences() {
  668. if( cursor.table == null ) return;
  669. // todo : port from old cdb
  670. }
  671. function gotoReference( c : Cell ) {
  672. if( c == null || c.value == null ) return;
  673. switch( c.column.type ) {
  674. case TRef(s):
  675. var sd = base.getSheet(s);
  676. if( sd == null ) return;
  677. var k = sd.index.get(c.value);
  678. if( k == null ) return;
  679. var index = sd.lines.indexOf(k.obj);
  680. if( index >= 0 ) openReference(sd, index, 0);
  681. default:
  682. }
  683. }
  684. function openReference( s : cdb.Sheet, line : Int, column : Int ) {
  685. ide.open("hide.view.CdbTable", {}, function(view) Std.downcast(view,hide.view.CdbTable).goto(s,line,column));
  686. }
  687. public function syncSheet( ?base, ?name ) {
  688. if( base == null ) base = this.base;
  689. this.base = base;
  690. if( name == null ) name = getCurrentSheet();
  691. // swap sheet if it was modified
  692. this.currentSheet = null;
  693. for( s in base.sheets )
  694. if( s.name == name ) {
  695. this.currentSheet = s;
  696. break;
  697. }
  698. }
  699. function isUniqueID( sheet : cdb.Sheet, obj : {}, id : String ) {
  700. var uniq = base.getSheet(sheet.name).index.get(id);
  701. return uniq == null || uniq.obj == obj;
  702. }
  703. public function refreshRefs() {
  704. base.sync();
  705. for( t in tables ) {
  706. for( l in t.lines ) {
  707. for( c in l.cells ) {
  708. switch( c.column.type ){
  709. case TRef(_):
  710. c.refresh();
  711. default:
  712. }
  713. }
  714. }
  715. }
  716. }
  717. public function refresh( ?state : UndoState ) {
  718. if( state == null )
  719. state = getState();
  720. var hasFocus = element.find(":focus").length > 0;
  721. base.sync();
  722. element.empty();
  723. element.addClass('cdb');
  724. searchBox = new Element("<div>").addClass("searchBox").appendTo(element);
  725. var txt = new Element("<input type='text'>").appendTo(searchBox).keydown(function(e) {
  726. if( e.keyCode == 27 ) {
  727. searchBox.find("i").click();
  728. return;
  729. }
  730. }).keyup(function(e) {
  731. searchFilter(e.getThis().val());
  732. });
  733. new Element("<i>").addClass("ico ico-times-circle").appendTo(searchBox).click(function(_) {
  734. searchFilter(null);
  735. searchBox.toggle();
  736. var c = cursor.save();
  737. focus();
  738. cursor.load(c);
  739. });
  740. formulas = new Formulas(this);
  741. formulas.evaluateAll(currentSheet.realSheet);
  742. var content = new Element("<table>");
  743. tables = [];
  744. new Table(this, currentSheet, content, displayMode);
  745. content.appendTo(element);
  746. setState(state, hasFocus);
  747. if( cursor.table != null ) {
  748. for( t in tables )
  749. if( t.sheet.getPath() == cursor.table.sheet.getPath() )
  750. cursor.table = t;
  751. cursor.update();
  752. }
  753. if( currentFilter != null ) {
  754. updateFilter();
  755. searchBox.show();
  756. txt.val(currentFilter);
  757. }
  758. }
  759. function quickExists(path) {
  760. var c = existsCache.get(path);
  761. if( c == null ) {
  762. c = { t : -1e9, r : false };
  763. existsCache.set(path, c);
  764. }
  765. var t = haxe.Timer.stamp();
  766. if( c.t < t - 10 ) { // cache result for 10s
  767. c.r = sys.FileSystem.exists(path);
  768. c.t = t;
  769. }
  770. return c.r;
  771. }
  772. function getLine( sheet : cdb.Sheet, index : Int ) {
  773. for( t in tables )
  774. if( t.sheet == sheet )
  775. return t.lines[index];
  776. return null;
  777. }
  778. public function getColumnProps( c : cdb.Data.Column ) {
  779. var pr : EditorColumnProps = c.editor;
  780. if( pr == null ) pr = {};
  781. return pr;
  782. }
  783. public function isColumnVisible( c : cdb.Data.Column ) {
  784. var props = getColumnProps(c);
  785. var cats = ide.projectConfig.dbCategories;
  786. return cats == null || props.categories == null || cats.filter(c -> props.categories.indexOf(c) >= 0).length > 0;
  787. }
  788. public function newColumn( sheet : cdb.Sheet, ?index : Int, ?onDone : cdb.Data.Column -> Void, ?col ) {
  789. var modal = new hide.comp.cdb.ModalColumnForm(this, sheet, col, element);
  790. modal.setCallback(function() {
  791. var c = modal.getColumn(col);
  792. if (c == null)
  793. return;
  794. beginChanges(true);
  795. var err;
  796. if( col != null )
  797. err = base.updateColumn(sheet, col, c);
  798. else
  799. err = sheet.addColumn(c, index == null ? null : index + 1);
  800. endChanges();
  801. if (err != null) {
  802. modal.error(err);
  803. return;
  804. }
  805. // perform side effects before refresh
  806. if( onDone != null )
  807. onDone(c);
  808. // if first column or subtable, refresh all
  809. if( sheet.columns.length == 1 || sheet.name.indexOf("@") > 0 )
  810. refresh();
  811. for( t in tables )
  812. if( t.sheet == sheet )
  813. t.refresh();
  814. modal.closeModal();
  815. });
  816. }
  817. public function editColumn( sheet : cdb.Sheet, col : cdb.Data.Column ) {
  818. newColumn(sheet,col);
  819. }
  820. public function insertLine( table : Table, index = 0 ) {
  821. if( !table.canInsert() )
  822. return;
  823. if( table.displayMode == Properties ) {
  824. var ins = table.element.find("select.insertField");
  825. var options = [for( o in ins.find("option").elements() ) o.val()];
  826. ins.attr("size", options.length);
  827. options.shift();
  828. ins.focus();
  829. var index = 0;
  830. ins.val(options[0]);
  831. ins.off();
  832. ins.blur(function(_) table.refresh());
  833. ins.keydown(function(e) {
  834. switch( e.keyCode ) {
  835. case K.ESCAPE:
  836. element.focus();
  837. case K.UP if( index > 0 ):
  838. ins.val(options[--index]);
  839. case K.DOWN if( index < options.length - 1 ):
  840. ins.val(options[++index]);
  841. case K.ENTER:
  842. @:privateAccess table.insertProperty(ins.val());
  843. default:
  844. }
  845. e.stopPropagation();
  846. e.preventDefault();
  847. });
  848. return;
  849. }
  850. beginChanges();
  851. table.sheet.newLine(index);
  852. endChanges();
  853. table.refresh();
  854. }
  855. public function popupColumn( table : Table, col : cdb.Data.Column, ?cell : Cell ) {
  856. if( view != null )
  857. return;
  858. var sheet = table.getRealSheet();
  859. var indexColumn = sheet.columns.indexOf(col);
  860. var menu : Array<hide.comp.ContextMenu.ContextMenuItem> = [
  861. { label : "Edit", click : function () editColumn(sheet, col) },
  862. { label : "Add Column", click : function () newColumn(sheet, indexColumn) },
  863. { label : "", isSeparator: true },
  864. { label : "Move Left", enabled: (indexColumn > 0 &&
  865. nextVisibleColumnIndex(table, indexColumn, Left) > -1), click : function () {
  866. beginChanges();
  867. var nextIndex = nextVisibleColumnIndex(table, indexColumn, Left);
  868. sheet.columns.remove(col);
  869. sheet.columns.insert(nextIndex, col);
  870. if (cursor.x == indexColumn)
  871. cursor.set(cursor.table, nextIndex, cursor.y);
  872. else if (cursor.x == nextIndex)
  873. cursor.set(cursor.table, nextIndex + 1, cursor.y);
  874. endChanges();
  875. refresh();
  876. }},
  877. { label : "Move Right", enabled: (indexColumn < sheet.columns.length - 1 &&
  878. nextVisibleColumnIndex(table, indexColumn, Right) < sheet.columns.length), click : function () {
  879. beginChanges();
  880. var nextIndex = nextVisibleColumnIndex(table, indexColumn, Right);
  881. sheet.columns.remove(col);
  882. sheet.columns.insert(nextIndex, col);
  883. if (cursor.x == indexColumn)
  884. cursor.set(cursor.table, nextIndex, cursor.y);
  885. else if (cursor.x == nextIndex)
  886. cursor.set(cursor.table, nextIndex - 1, cursor.y);
  887. endChanges();
  888. refresh();
  889. }},
  890. { label: "", isSeparator: true },
  891. { label : "Delete", click : function () {
  892. if( table.displayMode == Properties ) {
  893. beginChanges();
  894. changeObject(cell.line, col, base.getDefault(col,sheet));
  895. } else {
  896. beginChanges(true);
  897. sheet.deleteColumn(col.name);
  898. }
  899. endChanges();
  900. refresh();
  901. }}
  902. ];
  903. if( table.parent == null ) {
  904. var props = table.sheet.props;
  905. switch( col.type ) {
  906. case TString, TRef(_):
  907. menu.push({ label : "Display Name", click : function() {
  908. beginChanges();
  909. props.displayColumn = (props.displayColumn == col.name ? null : col.name);
  910. endChanges();
  911. refresh();
  912. }, checked: props.displayColumn == col.name });
  913. case TTilePos:
  914. menu.push({ label : "Display Icon", click : function() {
  915. beginChanges();
  916. props.displayIcon = (props.displayIcon == col.name ? null : col.name);
  917. endChanges();
  918. refresh();
  919. }, checked: props.displayIcon == col.name });
  920. default:
  921. }
  922. var editProps = getColumnProps(col);
  923. menu.push({ label : "Categories", menu: categoriesMenu(editProps.categories, function(cats) {
  924. beginChanges();
  925. editProps.categories = cats;
  926. col.editor = editProps;
  927. endChanges();
  928. refresh();
  929. })});
  930. switch(col.type) {
  931. case TId | TString:
  932. menu.push({ label : "Sort", click: () -> table.sortBy(col) });
  933. default:
  934. }
  935. }
  936. if( col.type == TString && col.kind == Script )
  937. menu.insert(1,{ label : "Edit all", click : function() editScripts(table,col) });
  938. if( table.displayMode == Properties ) {
  939. menu.push({ label : "Delete All", click : function() {
  940. beginChanges(true);
  941. table.sheet.deleteColumn(col.name);
  942. endChanges();
  943. refresh();
  944. }});
  945. }
  946. new hide.comp.ContextMenu(menu);
  947. }
  948. function nextVisibleColumnIndex( table : Table, index : Int, dir : Direction){
  949. var next = index;
  950. do {
  951. next += (dir == Left ? -1 : 1);
  952. }
  953. while (next >= 0 && next <= table.columns.length - 1 && !isColumnVisible(table.columns[next]));
  954. return next;
  955. }
  956. function editScripts( table : Table, col : cdb.Data.Column ) {
  957. // TODO : create single edit-all script view allowing global search & replace
  958. }
  959. function moveLine( line : Line, delta : Int ) {
  960. if( !line.table.canInsert() )
  961. return;
  962. beginChanges();
  963. var prevIndex = line.index;
  964. var distance = (delta >= 0 ? delta : -1 * delta);
  965. var index : Null<Int> = null;
  966. var currIndex : Null<Int> = line.index;
  967. for( _ in 0...distance ) {
  968. currIndex = line.table.sheet.moveLine( currIndex, delta );
  969. if( currIndex == null )
  970. break;
  971. else
  972. index = currIndex;
  973. }
  974. if( index != null ) {
  975. if (index != prevIndex) {
  976. if ( cursor.y == prevIndex ) cursor.set(cursor.table, cursor.x, index);
  977. else if ( cursor.y > prevIndex && cursor.y <= index) cursor.set(cursor.table, cursor.x, cursor.y - 1);
  978. else if ( cursor.y < prevIndex && cursor.y >= index) cursor.set(cursor.table, cursor.x, cursor.y + 1);
  979. }
  980. refresh();
  981. }
  982. endChanges();
  983. }
  984. function separatorCount( sheet : cdb.Sheet, fromLine : Int, toSep : Int ) {
  985. var count = 0;
  986. if( fromLine >= sheet.separators[toSep] ) {
  987. for( i in (toSep + 1)...sheet.separators.length ) {
  988. if( sheet.separators[i] > fromLine )
  989. break;
  990. count--;
  991. }
  992. } else {
  993. for( i in 0...(toSep + 1) ) {
  994. if( sheet.separators[i] <= fromLine )
  995. continue;
  996. count++;
  997. }
  998. }
  999. return count;
  1000. }
  1001. public function popupLine( line : Line ) {
  1002. if( !line.table.canInsert() )
  1003. return;
  1004. var sheet = line.table.sheet;
  1005. var sepIndex = sheet.separators.indexOf(line.index);
  1006. var moveSubmenu : Array<hide.comp.ContextMenu.ContextMenuItem> = new Array<hide.comp.ContextMenu.ContextMenuItem>();
  1007. if( sheet.props.separatorTitles != null ) {
  1008. for( i in 0...sheet.separators.length ) {
  1009. if( sheet.props.separatorTitles[i] == null )
  1010. continue;
  1011. var lastOfGroup = (i == sheet.separators.length - 1) ? line.table.lines.length : sheet.separators[i + 1];
  1012. if( lastOfGroup > line.index ) lastOfGroup--;
  1013. var delta = lastOfGroup - line.index + separatorCount(sheet, line.index, i);
  1014. moveSubmenu.push({
  1015. label : sheet.props.separatorTitles[i],
  1016. enabled : true,
  1017. click : moveLine.bind(line, delta)
  1018. });
  1019. }
  1020. }
  1021. new hide.comp.ContextMenu([
  1022. { label : "Move Up", enabled: (line.index > 0 || sepIndex >= 0), click : moveLine.bind(line,-1) },
  1023. { label : "Move Down", enabled: (line.index < sheet.lines.length - 1), click : moveLine.bind(line,1) },
  1024. { label : "Move to Group", enabled : sheet.props.separatorTitles != null, menu : moveSubmenu },
  1025. { label : "Insert", click : function() {
  1026. insertLine(line.table,line.index);
  1027. cursor.move(0,1,false,false);
  1028. }, keys : config.get("key.cdb.insertLine") },
  1029. { label : "Delete", click : function() {
  1030. beginChanges();
  1031. sheet.deleteLine(line.index);
  1032. endChanges();
  1033. refreshAll();
  1034. } },
  1035. { label : "Separator", enabled : !sheet.props.hide, checked : sepIndex >= 0, click : function() {
  1036. beginChanges();
  1037. if( sepIndex >= 0 ) {
  1038. sheet.separators.splice(sepIndex, 1);
  1039. if( sheet.props.separatorTitles != null ) sheet.props.separatorTitles.splice(sepIndex, 1);
  1040. } else {
  1041. sepIndex = sheet.separators.length;
  1042. for( i in 0...sheet.separators.length )
  1043. if( sheet.separators[i] > line.index ) {
  1044. sepIndex = i;
  1045. break;
  1046. }
  1047. sheet.separators.insert(sepIndex, line.index);
  1048. if( sheet.props.separatorTitles != null && sheet.props.separatorTitles.length > sepIndex )
  1049. sheet.props.separatorTitles.insert(sepIndex, null);
  1050. }
  1051. endChanges();
  1052. refresh();
  1053. } }
  1054. ]);
  1055. }
  1056. function rename( sheet : cdb.Sheet, name : String ) {
  1057. if( !base.r_ident.match(name) ) {
  1058. ide.error("Invalid sheet name");
  1059. return false;
  1060. }
  1061. var f = base.getSheet(name);
  1062. if( f != null ) {
  1063. if( f != sheet ) ide.error("Sheet name already in use");
  1064. return false;
  1065. }
  1066. beginChanges();
  1067. var old = sheet.name;
  1068. sheet.rename(name);
  1069. base.mapType(function(t) {
  1070. return switch( t ) {
  1071. case TRef(o) if( o == old ):
  1072. TRef(name);
  1073. case TLayer(o) if( o == old ):
  1074. TLayer(name);
  1075. default:
  1076. t;
  1077. }
  1078. });
  1079. for( s in base.sheets )
  1080. if( StringTools.startsWith(s.name, old + "@") )
  1081. s.rename(name + "@" + s.name.substr(old.length + 1));
  1082. endChanges();
  1083. DataFiles.save(true,[ sheet.name => old ]);
  1084. return true;
  1085. }
  1086. function categoriesMenu(categories: Array<String>, setFunc : Array<String> -> Void) {
  1087. var menu : Array<ContextMenu.ContextMenuItem> = [{ label : "Set...", click : function() {
  1088. var wstr = "";
  1089. if(categories != null)
  1090. wstr = categories.join(",");
  1091. wstr = ide.ask("Set Categories (comma separated)", wstr);
  1092. if(wstr == null)
  1093. return;
  1094. categories = [for(s in wstr.split(",")) { var t = StringTools.trim(s); if(t.length > 0) t; }];
  1095. setFunc(categories.length > 0 ? categories : null);
  1096. ide.initMenu();
  1097. }}];
  1098. for(name in getCategories(base)) {
  1099. var has = categories != null && categories.indexOf(name) >= 0;
  1100. menu.push({
  1101. label: name, checked: has, stayOpen: true, click: function() {
  1102. if(has)
  1103. categories.remove(name);
  1104. else {
  1105. if(categories == null)
  1106. categories = [];
  1107. categories.push(name);
  1108. }
  1109. has = !has;
  1110. setFunc(categories.length > 0 ? categories : null);
  1111. }
  1112. });
  1113. }
  1114. return menu;
  1115. }
  1116. public function popupSheet( ?sheet : cdb.Sheet, ?onChange : Void -> Void ) {
  1117. if( view != null )
  1118. return;
  1119. if( sheet == null ) sheet = this.currentSheet;
  1120. if( onChange == null ) onChange = function() {}
  1121. var index = base.sheets.indexOf(sheet);
  1122. var content : Array<ContextMenu.ContextMenuItem> = [
  1123. { label : "Add Sheet", click : function() { beginChanges(); var db = ide.createDBSheet(index+1); endChanges(); if( db != null ) onChange(); } },
  1124. { label : "Move Left", click : function() { beginChanges(); base.moveSheet(sheet,-1); endChanges(); onChange(); } },
  1125. { label : "Move Right", click : function() { beginChanges(); base.moveSheet(sheet,1); endChanges(); onChange(); } },
  1126. { label : "Rename", click : function() {
  1127. var name = ide.ask("New name", sheet.name);
  1128. if( name == null || name == "" || name == sheet.name ) return;
  1129. if( !rename(sheet, name) ) return;
  1130. onChange();
  1131. }},
  1132. { label : "Delete", click : function() {
  1133. beginChanges();
  1134. base.deleteSheet(sheet);
  1135. endChanges();
  1136. onChange();
  1137. }},
  1138. { label : "Categories", menu: categoriesMenu(getSheetProps(sheet).categories, function(cats) {
  1139. beginChanges();
  1140. var props = getSheetProps(sheet);
  1141. props.categories = cats;
  1142. sheet.props.editor = props;
  1143. endChanges();
  1144. onChange();
  1145. })},
  1146. { label : "", isSeparator: true },
  1147. ];
  1148. if( sheet.props.dataFiles == null )
  1149. content = content.concat([
  1150. { label : "Add Index", checked : sheet.props.hasIndex, click : function() {
  1151. beginChanges();
  1152. if( sheet.props.hasIndex ) {
  1153. for( o in sheet.getLines() )
  1154. Reflect.deleteField(o, "index");
  1155. sheet.props.hasIndex = false;
  1156. } else {
  1157. for( c in sheet.columns )
  1158. if( c.name == "index" ) {
  1159. ide.error("Column 'index' already exists");
  1160. return;
  1161. }
  1162. sheet.props.hasIndex = true;
  1163. }
  1164. endChanges();
  1165. }},
  1166. { label : "Add Group", checked : sheet.props.hasGroup, click : function() {
  1167. beginChanges();
  1168. if( sheet.props.hasGroup ) {
  1169. for( o in sheet.getLines() )
  1170. Reflect.deleteField(o, "group");
  1171. sheet.props.hasGroup = false;
  1172. } else {
  1173. for( c in sheet.columns )
  1174. if( c.name == "group" ) {
  1175. ide.error("Column 'group' already exists");
  1176. return;
  1177. }
  1178. sheet.props.hasGroup = true;
  1179. }
  1180. endChanges();
  1181. }},
  1182. ]);
  1183. if( sheet.lines.length == 0 || sheet.props.dataFiles != null )
  1184. content.push({
  1185. label : "Data Files",
  1186. checked : sheet.props.dataFiles != null,
  1187. click : function() {
  1188. var txt = ide.ask("Data Files Path", sheet.props.dataFiles);
  1189. if( txt == null ) return;
  1190. txt = StringTools.trim(txt);
  1191. beginChanges();
  1192. if( txt == "" ) {
  1193. Reflect.deleteField(sheet.props,"dataFile");
  1194. @:privateAccess sheet.sheet.lines = [];
  1195. } else {
  1196. sheet.props.dataFiles = txt;
  1197. @:privateAccess sheet.sheet.lines = null;
  1198. DataFiles.load();
  1199. }
  1200. endChanges();
  1201. refresh();
  1202. }
  1203. });
  1204. new ContextMenu(content);
  1205. }
  1206. public function close() {
  1207. for( t in tables.copy() )
  1208. t.dispose();
  1209. }
  1210. public function focus() {
  1211. if( element.is(":focus") ) return;
  1212. (element[0] : Dynamic).focus({ preventScroll : true });
  1213. }
  1214. static public function getSheetProps( s : cdb.Sheet ) {
  1215. var pr : EditorSheetProps = s.props.editor;
  1216. if( pr == null ) pr = {};
  1217. return pr;
  1218. }
  1219. static public function getCategories(db: cdb.Database) : Array<String> {
  1220. var names : Array<String> = [];
  1221. for( s in db.sheets ) {
  1222. var props = getSheetProps(s);
  1223. if(props.categories != null) {
  1224. for(n in props.categories)
  1225. if(names.indexOf(n) < 0)
  1226. names.push(n);
  1227. }
  1228. }
  1229. names.sort((a, b) -> Reflect.compare(a, b));
  1230. return names;
  1231. }
  1232. }