TouchDevelop/editor/actionProperties.ts

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

2015-02-05 00:41:44 +03:00
///<reference path='refs.ts'/>
module TDev
{
export class ActionProperties
extends SideTab
{
private theAction:AST.ActionHeader;
private lastName = "";
private asAction: boolean;
2015-02-05 00:41:44 +03:00
constructor() {
super()
}
2015-05-22 03:37:36 +03:00
private actionName = HTML.mkTextInputWithOk("text", lf("function name"),() => {
2015-02-17 18:40:46 +03:00
this.updateActionName();
this.theAction.notifyChange();
TheEditor.refreshDecl();
});
2015-05-22 03:37:36 +03:00
private privateAction = HTML.mkTickCheckBox(Ticks.actionPropPrivate, lf("private function"));
private testAction = HTML.mkTickCheckBox(Ticks.actionPropTest, lf("test function"));
2015-02-05 00:41:44 +03:00
private queryAction = HTML.mkTickCheckBox(Ticks.actionPropTest, lf("read only"));
private offlineAction = HTML.mkTickCheckBox(Ticks.actionPropTest, lf("offline available"));
private syncBox:HTMLElement;
private inParms = div("paramList", []);
private outParms = div("paramList", []);
public getTick() { return Ticks.sideActionHeaderInit; }
public init(e:Editor)
{
super.init(e);
this.actionName.id = "renameBox";
2015-05-22 03:37:36 +03:00
this.syncBox = HTML.mkTickCheckBox(Ticks.actionPropAtomic, lf("'atomic' function"), (v) => {
2015-02-05 00:41:44 +03:00
this.theAction.action.isAtomic = v;
this.theAction.notifyChange();
e.updateTutorial();
});
2015-05-20 15:24:21 +03:00
this.testAction.appendChild(Editor.mkHelpLink("testing"));
this.syncBox.appendChild(Editor.mkHelpLink("async actions"));
2015-02-05 00:41:44 +03:00
}
public editedStmt():AST.Stmt { return this.theAction; }
private isActive() { return !!this.theAction; }
private updateActionName()
{
if (!this.theAction) return;
var nam = TheEditor.tutorializeName(this.actionName.value);
if (nam == this.lastName) return;
this.lastName = nam;
if (this.theAction.getName() == this.lastName) return;
var name = Script.freshName(this.lastName);
this.theAction.action.setName(name);
if (this.theAction.action.modelParameter) {
var mk = <AST.RecordEntryKind> this.theAction.action.modelParameter.local.getKind();
var df = mk.record;
if (df) {
df.setName(Script.freshName(this.theAction.getName() + " page data"))
}
}
}
private mkParam(kind:string, lbl:string, t:Ticks)
{
var e = new DeclEntry(lbl)
e.makeIntoAddButton();
e.description = lf("create a new parameter");
var ee = e.mkBox();
HTML.setTickCallback(ee, t, () => {
var blk = this.theAction.inParameters;
if (kind == "output")
blk = this.theAction.outParameters;
var empt = blk.emptyStmt()
TheEditor.initIds(empt, true)
var newCh = blk.stmts.concat([empt]);
blk.setChildren(newCh);
this.updateActionName();
this.theAction.notifyChange();
TheEditor.refreshDecl();
// Jump directly into the parameter definition and edit its name.
TheEditor.editNode(empt);
TheEditor.calculator.inlineEdit(TheEditor.calculator.expr.tokens[0]);
2015-02-05 00:41:44 +03:00
});
return ee;
}
private syncParms()
{
this.actionName.value = this.theAction.getName();
HTML.setCheckboxValue(this.privateAction, this.theAction.action.isPrivate);
HTML.setCheckboxValue(this.testAction, this.theAction.action.isTest());
HTML.setCheckboxValue(this.queryAction, this.theAction.action.isQuery);
HTML.setCheckboxValue(this.offlineAction, this.theAction.action.isOffline);
HTML.setCheckboxValue(this.syncBox, this.theAction.action.isAtomic)
}
static functionBox(labl:string, key:string, t:Ticks, f:()=>void)
{
var b = HTML.mkButton(labl, () => { tick(t); f() });
KeyboardMgr.instance.btnShortcut(b, key)
return b;
/*
var e = new DeclEntry(labl);
e.icon = icon;
e.description = desc;
return e.mkBox().withClick(f);
*/
}
static copyCutRefs(kind:string, decl:AST.Decl)
{
var cp = !TheEditor.isDebuggerMode() && TheEditor.widgetEnabled("copyPaste");
return div(null,
cp ? ActionProperties.functionBox(lf("cut"), "Ctrl-X, Shift-Del", Ticks.sideCut, () => TheEditor.cutDecl(decl)) : undefined,
cp ? ActionProperties.functionBox(lf("copy"), "Ctrl-C, Ctrl-Ins", Ticks.sideCopy, () => TheEditor.copyDecl(decl)) : undefined,
ActionProperties.functionBox(lf("delete"), "Del", Ticks.sideDelete, () => TheEditor.cutDecl(decl, true)),
TheEditor.widgetEnabled("moveToLibrary") ? ActionProperties.functionBox(lf("move to library"), "Ctrl-M", Ticks.sideMoveToLibrary, () => TheEditor.moveDeclToLibrary(decl)) : undefined,
TheEditor.widgetEnabled("findReferences") ? ActionProperties.functionBox(lf("find references"), "Ctrl-G", Ticks.sideFindRefs, () => TheEditor.findRefs(decl)) : undefined,
dbg ? ActionProperties.functionBox(lf("see text (dbg)"), "", Ticks.noEvent, () => ModalDialog.showText(decl.serialize())) : undefined
);
}
public edit(aa:AST.Stmt)
{
var a = <AST.ActionHeader> aa;
this.theAction = a;
this.lastName = "";
TheEditor.refreshDecl();
this.syncParms();
var ev = this.theAction.action.isEvent();
var tp = this.theAction.action.isActionTypeDef();
var pa = this.theAction.action.parent;
var icl = pa !== undefined && pa.isCloud;
this.actionName.readOnly = ev;
var act = this.theAction.action;
var showSettings = TheEditor.widgetEnabled("actionSettings");
2015-06-18 13:43:41 +03:00
var showAtomic = showSettings && TheEditor.widgetEnabled("scriptPropertiesPropertyAtomic");
this.asAction = this.theAction.getName() == "main" && TheEditor.widgetEnabled("forceMainAsAction");
var singleReturnValue = TheEditor.widgetEnabled("singleReturnValue");
if (this.asAction && !Script.isLibrary && !Script.isDocsTopic()) {
this.actionName.readOnly = true;
this.theAction.action.isPrivate = false;
}
2015-02-05 00:41:44 +03:00
var propDivs =
[div("prop-spacer"),
div(null, span("varLabel", lf("function"))),
2015-02-05 00:41:44 +03:00
this.actionName,
!showSettings || ev || this.asAction ? null : this.privateAction,
2015-02-05 00:41:44 +03:00
!showSettings || ev ? null : div("formHint",
Script.isLibrary ? lf("Private functions cannot be called from outside the library. ") : lf("Private actions do not get a run button. ")),
2015-06-18 13:43:41 +03:00
!showAtomic || a.action.isPage() || ev || !asyncEnabled ? null : this.syncBox,
ev || this.asAction ? null : this.mkParam("input", lf("add input parameter"), Ticks.sideActionAddInput),
ev || act.isPage() || this.asAction || (singleReturnValue && this.theAction.outParameters.count() > 0) ? null : this.mkParam("output", lf("add output parameter"), Ticks.sideActionAddOutput),
2015-05-22 03:37:36 +03:00
ActionProperties.copyCutRefs(lf("the current function"), this.theAction.action),
2015-02-05 00:41:44 +03:00
2015-05-20 15:24:21 +03:00
!TheEditor.widgetEnabled("testAction") || ev || tp ? null : this.testAction,
2015-02-05 00:41:44 +03:00
showSettings && icl ? this.offlineAction : null,
showSettings && icl ? this.queryAction : null,
Browser.EditorSettings.changeSkillLevelDiv(this.editor, Ticks.changeSkillActionProperties, "formHint marginBottom"),
2015-02-05 00:41:44 +03:00
];
this.setChildren(propDivs);
}
public bye()
{
if (!this.theAction) return;
this.theAction.action.isPrivate = HTML.getCheckboxValue(this.privateAction) && !this.asAction;
2015-02-05 00:41:44 +03:00
this.theAction.action._isTest = HTML.getCheckboxValue(this.testAction);
this.theAction.action.isOffline = HTML.getCheckboxValue(this.offlineAction);
this.theAction.action.isQuery = HTML.getCheckboxValue(this.queryAction);
this.theAction.action.isAtomic = HTML.getCheckboxValue(this.syncBox);
this.updateActionName();
this.theAction.notifyChange();
this.theAction = null;
TheEditor.refreshDecl();
TheEditor.queueNavRefresh();
}
}
}