TouchDevelop/editor/literalEditor.ts

146 строки
5.3 KiB
TypeScript
Исходник Обычный вид История

2015-05-05 20:55:08 +03:00
///<reference path='refs.ts'/>
module TDev
{
export class LiteralEditor {
public constructor(public calculator : Calculator, public literal: AST.Literal) { }
public editor(): HTMLElement { return Util.abstract(); }
2015-05-05 20:55:08 +03:00
public value(): string { return Util.abstract();}
}
export class TextLiteralEditor extends LiteralEditor {
private res: HTML.AutoExpandingTextArea;
constructor(public calculator: Calculator, public literal: AST.Literal) {
super(calculator, literal);
var opts: HTML.AutoExpandingTextAreaOptions = { showDismiss: true };
if (Browser.isDesktop && TheEditor.widgetEnabled("stringEditFullScreen"))
opts.editFullScreenAsync = (t) => EditorHost.editFullScreenAsync(
literal.languageHint ? 'inline.' + literal.languageHint : '', t);
this.res = HTML.mkAutoExpandingTextArea(opts)
this.res.div.className += " calcStringEdit";
this.res.textarea.value = literal.data;
this.res.div.id = "stringEdit";
this.res.dismiss.id = "inlineEditCloseBtn";
this.res.onDismiss = () => this.calculator.checkNextDisplay();
(<any>this.res.div).focusEditor = () => {
this.res.update();
Util.setKeyboardFocusTextArea(this.res.textarea);
};
this.res.onUpdate = () => {
TheEditor.selector.positionButtonRows();
};
}
public editor(): HTMLElement { return this.res.div; }
2015-05-05 20:55:08 +03:00
public value(): string {
return this.res.textarea.value;
}
}
2015-05-05 21:14:40 +03:00
export class BitMatrixLiteralEditor extends LiteralEditor {
private root: HTMLElement;
2015-05-06 00:06:41 +03:00
private table: HTMLTableElement;
2015-05-05 21:14:40 +03:00
private rows: number;
2015-05-06 00:06:41 +03:00
private frames: number;
2015-05-05 21:14:40 +03:00
private bitCells: HTMLElement[];
constructor(public calculator: Calculator, public literal: AST.Literal) {
super(calculator, literal);
2015-05-06 00:06:41 +03:00
this.table = document.createElement('table');
this.table.className = 'bitmatrix';
this.table.withClick(() => { });
var plusBtn = HTML.mkRoundButton("svg:add,black", "add frame", Ticks.noEvent,() => {
var v = this.serialize(this.frames + 1);
this.updateTable(v);
});
var minusBtn = HTML.mkRoundButton("svg:minus,black", "remove frame", Ticks.noEvent,() => {
if (this.frames > 1) {
var v = this.serialize(this.frames - 1);
this.updateTable(v);
}
});
this.root = div('bitmatrix', this.table, div('btns', plusBtn, minusBtn));
this.updateTable(literal.data);
}
private updateTable(data: string) {
2015-05-05 21:14:40 +03:00
function tr(parent: HTMLElement, cl: string) {
var d = document.createElement('tr');
d.className = cl;
parent.appendChild(d);
return d;
}
function td(parent: HTMLElement, cl: string) {
var d = document.createElement('td');
d.className = cl;
parent.appendChild(d);
return d;
}
2015-05-06 01:50:41 +03:00
data = (data || "").trim();
var bits = data.split(/[\s\r\n]+/).map(s => parseInt(s));
if (bits.length <= 1) {
this.rows = 5;
this.frames = 1;
} else {
this.rows = data.split('\n').length;
this.frames = Math.floor(bits.length / (this.rows * this.rows));
}
2015-05-06 00:06:41 +03:00
this.bitCells = [];
this.table.innerHTML = ""; // clear table and rebuild
var hrow = tr(this.table, 'bitheader');
2015-05-05 21:14:40 +03:00
td(hrow, '');
2015-05-06 00:06:41 +03:00
for (var j = 0; j < this.frames * this.rows; ++j) td(hrow, 'index').innerText = j.toString();
2015-05-05 21:14:40 +03:00
// bit matrix
Util.range(0, this.rows).forEach(i => {
2015-05-06 00:06:41 +03:00
var row = tr(this.table, 'bitrow');
td(row, 'index').innerText = i.toString();
Util.range(0, this.frames * this.rows).forEach(j => {
2015-05-05 21:14:40 +03:00
var cell = td(row, 'bit');
2015-05-06 00:06:41 +03:00
cell.title = "(" + i + ", " + j + ")";
var k = i * this.frames * this.rows + j;
2015-05-05 21:14:40 +03:00
this.bitCells[k] = cell;
cell.setFlag('on', !!bits[k]);
cell.withClick(() => {
cell.setFlag('on', !cell.getFlag('on'));
});
2015-05-06 01:50:41 +03:00
cell.appendChild(div(''));
2015-05-05 21:14:40 +03:00
});
});
}
public editor(): HTMLElement {
return this.root;
2015-05-05 21:14:40 +03:00
}
2015-05-06 00:06:41 +03:00
private serialize(f: number): string {
2015-05-06 01:50:41 +03:00
var r = "";
2015-05-06 00:06:41 +03:00
for (var i = 0; i < this.rows; ++i) {
2015-05-06 01:50:41 +03:00
if (i > 0) r += "\n";
2015-05-06 00:06:41 +03:00
for (var j = 0; j < f * this.rows; ++j) {
2015-05-06 01:50:41 +03:00
if (j > 0) r += " ";
2015-05-06 00:06:41 +03:00
var k = i * this.rows * this.frames + j;
var s = j < this.rows * this.frames ? this.bitCells[k].getFlag("on") ? "1" : "0" : "0";
2015-05-06 01:50:41 +03:00
r += s;
2015-05-06 00:06:41 +03:00
}
}
return r;
}
2015-05-05 21:14:40 +03:00
public value(): string {
2015-05-06 00:06:41 +03:00
return this.serialize(this.frames);
2015-05-05 21:14:40 +03:00
}
}
2015-05-05 20:55:08 +03:00
}