TouchDevelop/editor/literalEditor.ts

138 строки
5.0 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 00:06:41 +03:00
var bits = (data || "").trim().split(/[\s\r\n]+/).map(s => parseInt(s));
this.rows = bits.shift() || 5;
2015-05-06 00:06:41 +03:00
this.frames = bits.shift() || 1;
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'));
});
});
});
}
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 {
var r = this.rows + " " + f;
for (var i = 0; i < this.rows; ++i) {
r += "\n";
for (var j = 0; j < f * this.rows; ++j) {
var k = i * this.rows * this.frames + j;
var s = j < this.rows * this.frames ? this.bitCells[k].getFlag("on") ? "1" : "0" : "0";
r += s + " ";
}
}
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
}