TouchDevelop/editor/literalEditor.ts

201 строка
7.5 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";
2015-05-13 17:53:30 +03:00
this.res.onDismiss = () => {
this.dismissed();
this.calculator.checkNextDisplay();
}
2015-05-05 20:55:08 +03:00
(<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-13 17:53:30 +03:00
public dismissed() { }
2015-05-05 20:55:08 +03:00
}
2015-05-05 21:14:40 +03:00
export class BitMatrixLiteralEditor extends LiteralEditor {
private root: HTMLElement;
2015-05-13 16:56:39 +03:00
private table: HTMLElement;
2015-05-06 01:54:38 +03:00
private plusBtn: HTMLElement;
private minusBtn: HTMLElement;
2015-06-19 16:17:27 +03:00
private okBtn: HTMLElement;
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[];
2015-05-13 17:53:30 +03:00
private animTable: HTMLTableElement;
private animToken: number;
private animCells: HTMLElement[];
private dialog: ModalDialog;
2015-05-05 21:14:40 +03:00
constructor(public calculator: Calculator, public literal: AST.Literal) {
super(calculator, literal);
this.plusBtn = HTML.mkRoundButton("svg:add,black", lf("add frame"), Ticks.noEvent,() => {
2015-05-06 00:06:41 +03:00
var v = this.serialize(this.frames + 1);
this.updateTable(v);
});
this.minusBtn = HTML.mkRoundButton("svg:minus,black", lf("remove frame"), Ticks.noEvent,() => {
2015-05-06 00:06:41 +03:00
if (this.frames > 1) {
var v = this.serialize(this.frames - 1);
this.updateTable(v);
}
});
2015-06-19 16:17:27 +03:00
this.okBtn = HTML.mkRoundButton("svg:check,black", lf("ok"), Ticks.noEvent, () => {
if (this.dialog) this.dialog.dismiss();
})
2015-06-21 01:34:28 +03:00
this.okBtn.style.position = "absolute";
this.okBtn.style.bottom = "0em";
this.okBtn.style.right = "0em";
2015-05-13 17:53:30 +03:00
this.table = div('bitmatrices');
this.animTable = <HTMLTableElement>document.createElement("table");
this.animTable.className = 'bitmatrix bitpreview';
this.animCells = [];
Util.range(0, 5).forEach(i => {
var row = HTML.tr(this.animTable, 'bitrow');
HTML.td(row, 'index');
for (var j = 0; j < 5; ++j) {
this.animCells[i * 5 + j] = HTML.td(row, 'bit');
this.animCells[i * 5 + j].appendChild(div(''));
}
});
2015-06-19 16:17:27 +03:00
this.root = div('bitmatrix',
2015-06-21 01:34:28 +03:00
div('btns', this.animTable, this.plusBtn, this.minusBtn),
this.table,
this.okBtn);
2015-05-06 00:06:41 +03:00
this.updateTable(literal.data);
}
2015-05-13 17:53:30 +03:00
public dismissed() {
if (this.animToken) clearInterval(this.animToken);
}
2015-05-06 00:06:41 +03:00
private updateTable(data: string) {
2015-05-05 21:14:40 +03:00
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 = [];
2015-05-13 17:53:30 +03:00
if (this.animToken) clearInterval(this.animToken);
2015-05-13 16:56:39 +03:00
this.table.setChildren(Util.range(0, this.frames).map(frame => {
var table = document.createElement('table');
table.className = 'bitmatrix';
table.style.width = SizeMgr.phoneMode ? '16em' : SizeMgr.portraitMode ? '13em' : '15em';
2015-05-13 16:56:39 +03:00
table.withClick(() => { });
2015-05-13 17:53:30 +03:00
var hrow = HTML.tr(table, 'bitheader');
HTML.td(hrow, '');
2015-05-15 23:11:23 +03:00
for (var j = 0; j < this.rows; ++j) {
2015-05-13 17:53:30 +03:00
HTML.td(hrow, 'index').innerText = j.toString();
2015-05-13 16:56:39 +03:00
}
2015-05-05 21:14:40 +03:00
2015-05-13 16:56:39 +03:00
// bit matrix
Util.range(0, this.rows).forEach(i => {
2015-05-13 17:53:30 +03:00
var row = HTML.tr(table, 'bitrow');
HTML.td(row, 'index').innerText = i.toString();
2015-05-13 16:56:39 +03:00
Util.range(frame * this.rows, this.rows).forEach(j => {
2015-05-13 17:53:30 +03:00
var cell = HTML.td(row, 'bit');
2015-05-13 16:56:39 +03:00
cell.title = "(" + j + ", " + i + ")";
var k = i * this.frames * this.rows + j;
this.bitCells[k] = cell;
cell.setFlag('on', !!bits[k]);
cell.withClick(() => {
cell.setFlag('on', !cell.getFlag('on'));
});
cell.appendChild(div(''));
2015-05-05 21:14:40 +03:00
});
});
2015-05-13 16:56:39 +03:00
return table;
}));
2015-05-13 17:53:30 +03:00
this.animate();
if (!this.dialog) {
this.dialog = new ModalDialog();
this.dialog.add(this.root);
this.dialog.fullWhite();
this.dialog.stretchWide();
this.dialog.setScroll();
this.dialog.onDismiss = () => this.calculator.checkNextDisplay();
this.dialog.show();
}
2015-05-05 21:14:40 +03:00
}
2015-05-13 17:53:30 +03:00
private animate() {
var af = 0;
this.animToken = window.setInterval(() => {
for (var i = 0; i < 5; ++i) {
for (var j = 0; j < 5; ++j)
this.animCells[i * 5 + j].setFlag('on', this.bitCells[i * this.frames * this.rows + af * this.rows + j].getFlag('on'));
}
af = (af + 1) % this.frames;
2015-05-13 20:38:18 +03:00
}, 400);
2015-05-13 17:53:30 +03:00
}
public editor(): HTMLElement {
return this.dialog ? div('') : 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
}