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; public var displayMode(default,null) : DisplayMode; public function new(editor, sheet, root, mode) { super(null,root); this.displayMode = mode; this.editor = editor; this.sheet = sheet; @:privateAccess editor.tables.push(this); root.addClass("cdb-sheet"); refresh(); } public function close() { element.remove(); dispose(); } public function dispose() { editor.tables.remove(this); } public function refresh() { element.empty(); switch( displayMode ) { case Table: refreshTable(); case Properties, AllProperties: refreshProperties(); } } public function cloneTableHead() { var target = J('.head'); if (target.length == 0) { trace("Fail clone table head"); return; } var target_children = target.children(); J(".floating-thead").remove(); var clone = J("
").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("" + txt + "").appendTo(elt); clone.append(elt); } J('.cdb').prepend(clone); } function refreshTable() { var cols = J("").addClass("head"); J("").addClass("start").appendTo(cols); lines = [for( index in 0...sheet.lines.length ) { var l = J(""); var head = J("").addClass("start").text("" + index); head.appendTo(l); var line = new Line(this, sheet.columns, index, l); l.mousedown(function(e) { if( e.which == 3 ) { head.click(); editor.popupLine(line); e.preventDefault(); return; } }).click(function(e) { editor.cursor.clickLine(line, e.shiftKey); }); line; }]; var colCount = sheet.columns.length; for( cindex in 0...sheet.columns.length ) { var c = sheet.columns[cindex]; var col = J(""); col.text(c.name); col.addClass( "t_"+c.type.getName().substr(1).toLowerCase() ); 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(_) { editor.editColumn(sheet, c); }); cols.append(col); for( index in 0...sheet.lines.length ) { var v = J("").addClass("c"); var line = lines[index]; v.appendTo(line.element); var cell = new Cell(v, line, c); v.click(function(e) { editor.cursor.clickCell(cell, e.shiftKey); e.stopPropagation(); }); switch( c.type ) { case TList, TProperties: cell.element.click(function(e) { if( e.shiftKey ) return; e.stopPropagation(); toggleList(cell); }); default: cell.element.dblclick(function(_) cell.edit()); } } } element.append(cols); var tbody = J(""); var snext = 0; for( i in 0...lines.length+1 ) { while( sheet.separators[snext] == i ) { makeSeparator(snext, colCount).appendTo(tbody); snext++; } if( i == lines.length ) break; tbody.append(lines[i].element); } element.append(tbody); if( sheet.lines.length == 0 ) { var l = J('Insert Line'); l.find("a").click(function(_) { editor.insertLine(this); editor.cursor.set(this); }); element.append(l); } cols.ready(cloneTableHead); cols.on("resize", cloneTableHead); } function makeSeparator( sindex : Int, colCount : Int ) { var sep = J("").addClass("separator").append(''); var content = sep.find("td"); var title = if( sheet.props.separatorTitles != null ) sheet.props.separatorTitles[sindex] else null; if( title != null ) content.text(title); sep.dblclick(function(e) { content.empty(); J("").appendTo(content).focus().val(title == null ? "" : title).blur(function(_) { title = JTHIS.val(); JTHIS.remove(); content.text(title); 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 == "" ? null : 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(); }).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(); }); }); return sep; } function refreshProperties() { lines = []; var available = []; var props = sheet.lines[0]; for( c in sheet.columns ) { if( c.opt && !Reflect.hasField(props,c.name) && displayMode != AllProperties ) { available.push(c); continue; } var v = Reflect.field(props, c.name); var l = new Element("").appendTo(element); var th = new Element("").text(c.name).appendTo(l); var td = new Element("").addClass("c").appendTo(l); var line = new Line(this, [c], lines.length, l); var cell = new Cell(td, line, c); lines.push(line); td.click(function(e) { editor.cursor.clickCell(cell, e.shiftKey); e.stopPropagation(); }); th.mousedown(function(e) { if( e.which == 3 ) { editor.popupColumn(this, c); editor.cursor.clickCell(cell, false); e.preventDefault(); return; } }); cell.element.dblclick(function(_) cell.edit()); } // add/edit properties var end = new Element("").appendTo(element); end = new Element("").attr("colspan", "2").appendTo(end); var sel = new Element("