123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- package hide.comp.cdb;
- import js.jquery.Helper.*;
- enum DisplayMode {
- Table;
- Properties;
- AllProperties;
- }
- class Table extends Component {
- public var editor : Editor;
- public var parent : Table;
- public var sheet : cdb.Sheet;
- public var lines : Array<Line>;
- public var displayMode(default,null) : DisplayMode;
- public var columns : Array<cdb.Data.Column>;
- public var view : cdb.DiffFile.SheetView;
- public function new(editor, sheet, root, mode) {
- super(null,root);
- this.displayMode = mode;
- this.editor = editor;
- this.sheet = sheet;
- saveDisplayKey = "cdb/"+sheet.name;
- @:privateAccess for( t in editor.tables )
- if( t.sheet.path == sheet.path )
- trace("Dup CDB table!");
- @:privateAccess editor.tables.push(this);
- root.addClass("cdb-sheet");
- root.addClass("s_" + sheet.name);
- if( editor.view != null ) {
- var cname = parent == null ? null : sheet.parent.sheet.columns[sheet.parent.column].name;
- if( parent == null )
- view = editor.view.get(sheet.name);
- else if( parent.view.sub != null )
- view = parent.view.sub.get(cname);
- if( view == null ) {
- if( parent != null && parent.canEditColumn(cname) )
- view = { insert : true, edit : [for( c in sheet.columns ) c.name], sub : {} };
- else
- view = { insert : false, edit : [], sub : {} };
- }
- }
- refresh();
- }
- public function getRealSheet() {
- return sheet.realSheet;
- }
- public function canInsert() {
- if( sheet.props.dataFiles != null ) return false;
- return view == null || view.insert;
- }
- public function canEditColumn( name : String ) {
- return view == null || (view.edit != null && view.edit.indexOf(name) >= 0);
- }
- public function close() {
- for( t in @:privateAccess editor.tables.copy() )
- if( t.parent == this )
- t.close();
- element.remove();
- dispose();
- }
- public function dispose() {
- editor.tables.remove(this);
- }
- public function refresh() {
- element.empty();
- columns = view == null || view.show == null ? sheet.columns : [for( c in sheet.columns ) if( view.show.indexOf(c.name) >= 0 ) c];
- switch( displayMode ) {
- case Table:
- refreshTable();
- case Properties, AllProperties:
- refreshProperties();
- }
- }
- function cloneTableHead() {
- var target = element.find('thead').first().find('.head');
- if (target.length == 0)
- return;
- var target_children = target.children();
- J(".floating-thead").remove();
- var clone = J("<div>").addClass("floating-thead");
- for (i in 0...target_children.length) {
- var targetElt = target_children.eq(i);
- var elt = targetElt.clone(true); // clone with events
- elt.width(targetElt.width());
- elt.css("max-width", targetElt.width());
- var txt = elt[0].innerHTML;
- elt.empty();
- J("<span>" + txt + "</span>").appendTo(elt);
- clone.append(elt);
- }
- J('.cdb').prepend(clone);
- }
- function refreshTable() {
- var cols = J("<thead>").addClass("head");
- J("<th>").addClass("start").appendTo(cols);
- lines = [for( index in 0...sheet.lines.length ) {
- var l = J("<tr>");
- var head = J("<td>").addClass("start").text("" + index);
- head.appendTo(l);
- var line = new Line(this, columns, index, l);
- head.mousedown(function(e) {
- if( e.which == 3 ) {
- editor.popupLine(line);
- e.preventDefault();
- return;
- }
- });
- l.click(function(e) {
- if( e.which == 3 ) {
- e.preventDefault();
- return;
- }
- editor.cursor.clickLine(line, e.shiftKey);
- });
- line;
- }];
- var colCount = columns.length;
- for( c in columns ) {
- var editProps = editor.getColumnProps(c);
- var col = J("<th>");
- col.text(c.name);
- col.addClass( "t_"+c.type.getName().substr(1).toLowerCase() );
- col.addClass( "n_" + c.name );
- col.attr("title", c.name);
- col.toggleClass("hidden", !editor.isColumnVisible(c));
- col.toggleClass("cat", editProps.categories != null);
- if(editProps.categories != null)
- for(c in editProps.categories)
- col.addClass("cat-" + c);
- if( c.documentation != null ) {
- col.attr("title", c.documentation);
- new Element('<i style="margin-left: 5px" class="ico ico-book"/>').appendTo(col);
- }
- if( sheet.props.displayColumn == c.name )
- col.addClass("display");
- col.mousedown(function(e) {
- if( e.which == 3 ) {
- editor.popupColumn(this, c);
- e.preventDefault();
- return;
- }
- });
- col.dblclick(function(_) {
- if( editor.view == null ) editor.editColumn(getRealSheet(), c);
- });
- cols.append(col);
- }
- element.append(cols);
- var tbody = J("<tbody>");
- var snext = 0, hidden = false;
- for( i in 0...lines.length+1 ) {
- while( sheet.separators[snext] == i ) {
- var sep = makeSeparator(snext, colCount);
- sep.element.appendTo(tbody);
- if( sep.hidden != null ) hidden = sep.hidden;
- snext++;
- }
- if( i == lines.length ) break;
- var line = lines[i];
- if( hidden )
- line.hide();
- else
- line.create();
- tbody.append(line.element);
- }
- element.append(tbody);
- if( colCount == 0 ) {
- var l = J('<tr><td><input type="button" value="Add a column"/></td></tr>').find("input").click(function(_) {
- editor.newColumn(sheet);
- });
- element.append(l);
- } else if( sheet.lines.length == 0 && canInsert() ) {
- var l = J('<tr><td colspan="${columns.length + 1}"><input type="button" value="Insert Line"/></td></tr>');
- l.find("input").click(function(_) {
- editor.insertLine(this);
- editor.cursor.set(this);
- });
- element.append(l);
- }
- if( sheet.parent == null ) {
- cols.ready(cloneTableHead);
- cols.on("resize", cloneTableHead);
- }
- }
- function makeSeparator( sindex : Int, colCount : Int ) : { element : Element, hidden : Null<Bool> } {
- var sep = J("<tr>").addClass("separator").append('<td colspan="${colCount+1}"><a href="#" class="toggle"></a><span></span></td>');
- var content = sep.find("span");
- var toggle = sep.find("a");
- var title = if( sheet.props.separatorTitles != null ) sheet.props.separatorTitles[sindex] else null;
- function getLines() {
- var snext = 0, sref = -1;
- var out = [];
- for( i in 0...lines.length ) {
- while( sheet.separators[snext] == i ) {
- var title = if( sheet.props.separatorTitles != null ) sheet.props.separatorTitles[snext] else null;
- if( title != null ) sref = snext;
- snext++;
- }
- if( sref == sindex )
- out.push(lines[i]);
- }
- return out;
- }
- var hidden : Bool;
- function sync() {
- hidden = title == null ? null : getDisplayState("sep/"+title) == false;
- toggle.css({ display : title == null ? "none" : "" });
- toggle.text(hidden ? "🡆" : "🡇");
- content.text(title == null ? "" : title+(hidden ? " ("+getLines().length+")" : ""));
- sep.toggleClass("sep-hidden", hidden == true);
- }
- sep.contextmenu(function(e) {
- var opts : Array<hide.comp.ContextMenu.ContextMenuItem> = [
- { label : "Expand All", click : function() {
- element.find("tr.separator.sep-hidden a.toggle").click();
- }},
- { label : "Collapse All", click : function() {
- element.find("tr.separator").not(".sep-hidden").find("a.toggle").click();
- }},
- ];
- if( sheet.props.dataFiles != null && title != null )
- opts.unshift({
- label : "Open",
- click : function() {
- ide.openFile(title);
- },
- });
- new hide.comp.ContextMenu(opts);
- });
- sep.dblclick(function(e) {
- if( !canInsert() ) return;
- content.empty();
- J("<input>").appendTo(content).focus().val(title == null ? "" : title).blur(function(_) {
- title = JTHIS.val();
- JTHIS.remove();
- if( title == "" ) title = null;
- var old = sheet.props.separatorTitles;
- var titles = sheet.props.separatorTitles;
- if( titles == null ) titles = [] else titles = titles.copy();
- while( titles.length < sindex )
- titles.push(null);
- titles[sindex] = title;
- while( titles[titles.length - 1] == null && titles.length > 0 )
- titles.pop();
- if( titles.length == 0 ) titles = null;
- editor.beginChanges();
- sheet.props.separatorTitles = titles;
- editor.endChanges();
- sync();
- }).keypress(function(e) {
- e.stopPropagation();
- }).keydown(function(e) {
- if( e.keyCode == 13 ) { JTHIS.blur(); e.preventDefault(); } else if( e.keyCode == 27 ) content.text(title);
- e.stopPropagation();
- });
- });
- sync();
- toggle.dblclick(function(e) e.stopPropagation());
- toggle.click(function(e) {
- hidden = !hidden;
- saveDisplayState("sep/"+title, !hidden);
- sync();
- for( l in getLines() ) {
- if( hidden ) l.hide() else l.create();
- }
- editor.updateFilter();
- });
- return { hidden : hidden, element : sep };
- }
- function refreshProperties() {
- lines = [];
- var available = [];
- var props = sheet.lines[0];
- var isLarge = false;
- for( c in columns ) {
- if( c.type.match(TList | TProperties) ) isLarge = true;
- if( c.opt && props != null && !Reflect.hasField(props,c.name) && displayMode != AllProperties ) {
- available.push(c);
- continue;
- }
- var v = Reflect.field(props, c.name);
- var l = new Element("<tr>").appendTo(element);
- var th = new Element("<th>").text(c.name).appendTo(l);
- var td = new Element("<td>").addClass("c").appendTo(l);
- if( c.documentation != null ) {
- th.attr("title", c.documentation);
- new Element('<i style="margin-left: 5px" class="ico ico-book"/>').appendTo(th);
- }
- var line = new Line(this, [c], lines.length, l);
- var cell = new Cell(td, line, c);
- lines.push(line);
- th.mousedown(function(e) {
- if( e.which == 3 ) {
- editor.popupColumn(this, c, cell);
- editor.cursor.clickCell(cell, false);
- e.preventDefault();
- return;
- }
- });
- }
- if( isLarge )
- element.parent().addClass("cdb-large");
- // add/edit properties
- var end = new Element("<tr>").appendTo(element);
- end = new Element("<td>").attr("colspan", "2").appendTo(end);
- var sel = new Element("<select class='insertField'>").appendTo(end);
- new Element("<option>").attr("value", "").text("--- Choose ---").appendTo(sel);
- var canInsert = false;
- for( c in available )
- if( canEditColumn(c.name) ) {
- var opt = J("<option>").attr("value",c.name).text(c.name).appendTo(sel);
- if( c.documentation != null ) opt.attr("title", c.documentation);
- canInsert = true;
- }
- if( editor.view == null )
- J("<option>").attr("value","$new").text("New property...").appendTo(sel);
- else if( !canInsert )
- end.remove();
- sel.change(function(e) {
- var v = sel.val();
- if( v == "" )
- return;
- sel.val("");
- editor.element.focus();
- if( v == "$new" ) {
- editor.newColumn(sheet, null, function(c) {
- if( c.opt ) insertProperty(c.name);
- });
- return;
- }
- insertProperty(v);
- });
- }
- function insertProperty( p : String ) {
- var props = sheet.lines[0];
- for( c in sheet.columns )
- if( c.name == p ) {
- var val = editor.base.getDefault(c, true, sheet);
- editor.beginChanges();
- Reflect.setField(props, c.name, val);
- editor.endChanges();
- refresh();
- for( l in lines )
- if( l.cells[0].column == c ) {
- l.cells[0].focus();
- break;
- }
- return true;
- }
- return false;
- }
- public function sortBy(col: cdb.Data.Column) {
- editor.beginChanges();
- var group : Array<Dynamic> = [];
- var startIndex = 0;
- function sort() {
- group.sort(function(a, b) {
- var val1 = Reflect.field(a, col.name);
- var val2 = Reflect.field(b, col.name);
- return Reflect.compare(val1, val2);
- });
- for(i in 0...group.length)
- sheet.lines[startIndex + i] = group[i];
- }
- for(i in 0...lines.length) {
- var sep = sheet.separators.indexOf(i) >= 0;
- if(sep) {
- sort();
- group = [];
- startIndex = i;
- }
- group.push(lines[i].obj);
- }
- sort();
- editor.endChanges();
- refresh();
- }
- public function toggleList( cell : Cell, ?immediate : Bool, ?make : Void -> SubTable ) {
- var line = cell.line;
- var cur = line.subTable;
- if( cur != null ) {
- cur.close();
- if( cur.cell == cell ) return; // toggle
- }
- var sub = make == null ? new SubTable(editor, cell) : make();
- sub.show(immediate);
- editor.cursor.set(sub);
- }
- public function refreshList( cell : Cell, ?make : Void -> SubTable ) {
- var line = cell.line;
- var cur = line.subTable;
- if( cur != null ) {
- cur.immediateClose();
- var sub = make == null ? new SubTable(editor, cell) : make();
- sub.show(true);
- }
- }
- function toString() {
- return "Table#"+sheet.name;
- }
- }
|