aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/nodes/KeyValueBox.tsx
diff options
context:
space:
mode:
authorMonika <monika_hedman@brown.edu>2019-07-01 15:39:37 -0400
committerMonika <monika_hedman@brown.edu>2019-07-01 15:39:37 -0400
commitc5391bf0d352f69211ba28fc263d27204d7b8dc4 (patch)
treeacbc632aff852e74015bd1c8eee4933a7565cb94 /src/client/views/nodes/KeyValueBox.tsx
parentee4155a168dadad182719eb55df3459d6a937a45 (diff)
parentcd78d5b3371d73cc51dcb6f3dbfdca1a3bbcf6e1 (diff)
edits made
Diffstat (limited to 'src/client/views/nodes/KeyValueBox.tsx')
-rw-r--r--src/client/views/nodes/KeyValueBox.tsx84
1 files changed, 79 insertions, 5 deletions
diff --git a/src/client/views/nodes/KeyValueBox.tsx b/src/client/views/nodes/KeyValueBox.tsx
index cd65c42bc..0e798d291 100644
--- a/src/client/views/nodes/KeyValueBox.tsx
+++ b/src/client/views/nodes/KeyValueBox.tsx
@@ -7,13 +7,23 @@ import { FieldView, FieldViewProps } from './FieldView';
import "./KeyValueBox.scss";
import { KeyValuePair } from "./KeyValuePair";
import React = require("react");
-import { NumCast, Cast, FieldValue } from "../../../new_fields/Types";
-import { Doc, Field } from "../../../new_fields/Doc";
+import { NumCast, Cast, FieldValue, StrCast } from "../../../new_fields/Types";
+import { Doc, Field, FieldResult } from "../../../new_fields/Doc";
import { ComputedField } from "../../../new_fields/ScriptField";
+import { SetupDrag } from "../../util/DragManager";
+import { Docs } from "../../documents/Documents";
+import { RawDataOperationParameters } from "../../northstar/model/idea/idea";
+import { Templates } from "../Templates";
+import { List } from "../../../new_fields/List";
+import { TextField } from "../../util/ProsemirrorCopy/prompt";
+import { RichTextField } from "../../../new_fields/RichTextField";
+import { ImageField } from "../../../new_fields/URLField";
@observer
export class KeyValueBox extends React.Component<FieldViewProps> {
private _mainCont = React.createRef<HTMLDivElement>();
+ private _keyHeader = React.createRef<HTMLTableHeaderCellElement>();
+ @observable private rows: KeyValuePair[] = [];
public static LayoutString(fieldStr: string = "data") { return FieldView.LayoutString(KeyValueBox, fieldStr); }
@observable private _keyInput: string = "";
@@ -89,8 +99,16 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
let rows: JSX.Element[] = [];
let i = 0;
+ const self = this;
for (let key of Object.keys(ids).sort()) {
- rows.push(<KeyValuePair doc={realDoc} keyWidth={100 - this.splitPercentage} rowStyle={"keyValueBox-" + (i++ % 2 ? "oddRow" : "evenRow")} key={key} keyName={key} />);
+ rows.push(<KeyValuePair doc={realDoc} ref={(function () {
+ let oldEl: KeyValuePair | undefined;
+ return (el: KeyValuePair) => {
+ if (oldEl) self.rows.splice(self.rows.indexOf(oldEl), 1);
+ oldEl = el;
+ if (el) self.rows.push(el);
+ };
+ })()} keyWidth={100 - this.splitPercentage} rowStyle={"keyValueBox-" + (i++ % 2 ? "oddRow" : "evenRow")} key={key} keyName={key} />);
}
return rows;
}
@@ -134,6 +152,60 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
document.addEventListener('pointerup', this.onDividerUp);
}
+ getTemplate = async () => {
+ let parent = Docs.StackingDocument([], { width: 800, height: 800, title: "Template" });
+ parent.singleColumn = false;
+ parent.columnWidth = 50;
+ for (let row of this.rows.filter(row => row.isChecked)) {
+ await this.createTemplateField(parent, row);
+ row.uncheck();
+ }
+ return parent;
+ }
+
+ createTemplateField = async (parent: Doc, row: KeyValuePair) => {
+ let collectionKeyProp = `fieldKey={"data"}`;
+ let metaKey = row.props.keyName;
+ let metaKeyProp = `fieldKey={"${metaKey}"}`;
+
+ let sourceDoc = await Cast(this.props.Document.data, Doc);
+ if (!sourceDoc) {
+ return;
+ }
+ let target = this.inferType(sourceDoc[metaKey], metaKey);
+
+ let template = Doc.MakeAlias(target);
+ template.proto = parent;
+ template.title = metaKey;
+ template.nativeWidth = 0;
+ template.nativeHeight = 0;
+ template.embed = true;
+ template.isTemplate = true;
+ template.templates = new List<string>([Templates.TitleBar(metaKey)]);
+ if (target.backgroundLayout) {
+ let metaAnoKeyProp = `fieldKey={"${metaKey}"} fieldExt={"annotations"}`;
+ let collectionAnoKeyProp = `fieldKey={"annotations"}`;
+ template.layout = StrCast(target.layout).replace(collectionAnoKeyProp, metaAnoKeyProp);
+ template.backgroundLayout = StrCast(target.backgroundLayout).replace(collectionKeyProp, metaKeyProp);
+ } else {
+ template.layout = StrCast(target.layout).replace(collectionKeyProp, metaKeyProp);
+ }
+ Doc.AddDocToList(parent, "data", template);
+ row.uncheck();
+ }
+
+ inferType = (field: FieldResult, metaKey: string) => {
+ let options = { width: 300, height: 300, title: metaKey };
+ if (field instanceof RichTextField || typeof field === "string" || typeof field === "number") {
+ return Docs.TextDocument(options);
+ } else if (field instanceof List) {
+ return Docs.StackingDocument([], options);
+ } else if (field instanceof ImageField) {
+ return Docs.ImageDocument("https://www.freepik.com/free-icon/picture-frame-with-mountain-image_748687.htm", options);
+ }
+ return new Doc;
+ }
+
render() {
let dividerDragger = this.splitPercentage === 0 ? (null) :
<div className="keyValueBox-dividerDragger" style={{ transform: `translate(calc(${100 - this.splitPercentage}% - 5px), 0px)` }}>
@@ -144,7 +216,9 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
<table className="keyValueBox-table">
<tbody className="keyValueBox-tbody">
<tr className="keyValueBox-header">
- <th className="keyValueBox-key" style={{ width: `${100 - this.splitPercentage}%` }}>Key</th>
+ <th className="keyValueBox-key" style={{ width: `${100 - this.splitPercentage}%` }} ref={this._keyHeader}
+ onPointerDown={SetupDrag(this._keyHeader, this.getTemplate)}
+ >Key</th>
<th className="keyValueBox-fields" style={{ width: `${this.splitPercentage}%` }}>Fields</th>
</tr>
{this.createTable()}
@@ -154,4 +228,4 @@ export class KeyValueBox extends React.Component<FieldViewProps> {
{dividerDragger}
</div>);
}
-} \ No newline at end of file
+}