|
| 1 | +/* eslint no-underscore-dangle: ["error", { "allowAfterThis": true, "allow": ["__willBeBranch", "__willBeLeaf", "__tree"] }] */ |
| 2 | + |
| 3 | +qx.Class.define("qxapp.component.widget.InputsMapper", { |
| 4 | + extend: qx.ui.core.Widget, |
| 5 | + |
| 6 | + construct: function(nodeModel, mapper) { |
| 7 | + this.base(); |
| 8 | + |
| 9 | + let widgetLayout = new qx.ui.layout.VBox(5); |
| 10 | + this._setLayout(widgetLayout); |
| 11 | + |
| 12 | + this.setNodeModel(nodeModel); |
| 13 | + this.setMapper(mapper); |
| 14 | + |
| 15 | + let tree = this.__tree = new qx.ui.tree.VirtualTree(null, "label", "children").set({ |
| 16 | + openMode: "none" |
| 17 | + }); |
| 18 | + this._add(tree, { |
| 19 | + flex: 1 |
| 20 | + }); |
| 21 | + tree.getSelection().addListener("change", this.__onTreeSelectionChanged, this); |
| 22 | + |
| 23 | + let that = this; |
| 24 | + tree.setDelegate({ |
| 25 | + createItem: () => new qxapp.component.widget.inputs.NodeOutputListItem(), |
| 26 | + bindItem: (c, item, id) => { |
| 27 | + c.bindDefaultProperties(item, id); |
| 28 | + // c.bindProperty("key", "key", null, item, id); |
| 29 | + c.bindProperty("isDir", "isDir", null, item, id); |
| 30 | + c.bindProperty("isRoot", "isRoot", null, item, id); |
| 31 | + }, |
| 32 | + configureItem: item => { |
| 33 | + item.set({ |
| 34 | + droppable: true |
| 35 | + }); |
| 36 | + item.addListener("dragover", e => { |
| 37 | + item.set({ |
| 38 | + droppable: item.getIsDir() |
| 39 | + }); |
| 40 | + let compatible = false; |
| 41 | + if (e.supportsType("osparc-mapping")) { |
| 42 | + const from = e.getRelatedTarget(); |
| 43 | + const to = e.getCurrentTarget(); |
| 44 | + const fromKey = from.getNodeKey(); |
| 45 | + if (to.getIsRoot()) { |
| 46 | + // root |
| 47 | + compatible = from.getIsDir() && that.__willBeBranch(fromKey); |
| 48 | + } else { |
| 49 | + // non root |
| 50 | + compatible = to.getIsDir() && !from.getIsDir() && that.__willBeLeaf(fromKey); |
| 51 | + } |
| 52 | + } |
| 53 | + if (!compatible) { |
| 54 | + e.preventDefault(); |
| 55 | + } |
| 56 | + }); |
| 57 | + item.addListener("drop", e => { |
| 58 | + if (e.supportsType("osparc-mapping")) { |
| 59 | + const from = e.getRelatedTarget(); |
| 60 | + const fromNodeKey = from.getNodeKey(); |
| 61 | + const fromPortKey = from.getPortKey(); |
| 62 | + let data = { |
| 63 | + key: from.getModel(), |
| 64 | + label: from.getLabel(), |
| 65 | + nodeKey: from.getNodeKey(), |
| 66 | + portKey: from.getPortKey(), |
| 67 | + isDir: from.getIsDir() |
| 68 | + }; |
| 69 | + const willBeBranch = that.__willBeBranch(fromNodeKey); |
| 70 | + if (willBeBranch) { |
| 71 | + data["children"] = []; |
| 72 | + } |
| 73 | + let newItem = qx.data.marshal.Json.createModel(data, true); |
| 74 | + const to = e.getCurrentTarget(); |
| 75 | + to.getModel().getChildren() |
| 76 | + .push(newItem); |
| 77 | + if (willBeBranch) { |
| 78 | + const nodeInstanceUUID = null; |
| 79 | + const itemProps = qxapp.data.Store.getInstance().getItem(nodeInstanceUUID, fromPortKey, newItem.getKey()); |
| 80 | + if (itemProps) { |
| 81 | + let form = new qxapp.component.form.Auto(itemProps); |
| 82 | + let propsWidget = new qxapp.component.form.renderer.PropForm(form); |
| 83 | + newItem["propsWidget"] = propsWidget; |
| 84 | + } |
| 85 | + } |
| 86 | + to.setOpen(true); |
| 87 | + tree.focus(); |
| 88 | + } |
| 89 | + }); |
| 90 | + } |
| 91 | + }); |
| 92 | + |
| 93 | + let data = { |
| 94 | + label: nodeModel.getLabel(), |
| 95 | + isRoot: true, |
| 96 | + children: [] |
| 97 | + }; |
| 98 | + let model = qx.data.marshal.Json.createModel(data, true); |
| 99 | + tree.setModel(model); |
| 100 | + |
| 101 | + this.addListener("keypress", function(keyEvent) { |
| 102 | + let treeSelection = this.__tree.getSelection(); |
| 103 | + if (treeSelection.length < 1) { |
| 104 | + return; |
| 105 | + } |
| 106 | + let selectedItem = treeSelection.toArray()[0]; |
| 107 | + if (selectedItem.getIsRoot && selectedItem.getIsRoot()) { |
| 108 | + return; |
| 109 | + } |
| 110 | + switch (keyEvent.getKeyIdentifier()) { |
| 111 | + case "F2": { |
| 112 | + let treeItemRenamer = new qxapp.component.widget.TreeItemRenamer(selectedItem); |
| 113 | + treeItemRenamer.addListener("LabelChanged", e => { |
| 114 | + let newLabel = e.getData()["newLabel"]; |
| 115 | + selectedItem.setLabel(newLabel); |
| 116 | + }, this); |
| 117 | + treeItemRenamer.center(); |
| 118 | + treeItemRenamer.open(); |
| 119 | + break; |
| 120 | + } |
| 121 | + case "Delete": { |
| 122 | + let branches = this.__tree.getModel().getChildren(); |
| 123 | + // branch |
| 124 | + let removed = branches.remove(selectedItem); |
| 125 | + if (!removed) { |
| 126 | + // leaf |
| 127 | + let br = branches.toArray(); |
| 128 | + for (let i=0; i<br.length; i++) { |
| 129 | + let branch = br[i]; |
| 130 | + removed = branch.getChildren().remove(selectedItem); |
| 131 | + if (removed) { |
| 132 | + break; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + break; |
| 137 | + } |
| 138 | + } |
| 139 | + }, this); |
| 140 | + }, |
| 141 | + |
| 142 | + properties: { |
| 143 | + nodeModel: { |
| 144 | + check: "qxapp.data.model.NodeModel", |
| 145 | + nullable: false |
| 146 | + }, |
| 147 | + |
| 148 | + mapper: { |
| 149 | + nullable: false |
| 150 | + } |
| 151 | + }, |
| 152 | + |
| 153 | + members: { |
| 154 | + __tree: null, |
| 155 | + |
| 156 | + __willBeBranch: function(candidate) { |
| 157 | + let isBranch = false; |
| 158 | + const maps = this.getMapper().maps; |
| 159 | + if (Object.prototype.hasOwnProperty.call(maps, "branch")) { |
| 160 | + if (maps["branch"] === candidate) { |
| 161 | + isBranch = true; |
| 162 | + } |
| 163 | + } |
| 164 | + const isDefault = candidate === this.getNodeModel().getKey(); |
| 165 | + return isDefault || isBranch; |
| 166 | + }, |
| 167 | + |
| 168 | + __willBeLeaf: function(candidate) { |
| 169 | + let isLeave = false; |
| 170 | + const maps = this.getMapper().maps; |
| 171 | + if (Object.prototype.hasOwnProperty.call(maps, "leaf")) { |
| 172 | + if (maps["leaf"] === candidate) { |
| 173 | + isLeave = true; |
| 174 | + } |
| 175 | + } |
| 176 | + return isLeave; |
| 177 | + }, |
| 178 | + |
| 179 | + __onTreeSelectionChanged: function() { |
| 180 | + // remove all but the tree |
| 181 | + while (this._getChildren().length > 1) { |
| 182 | + this._removeAt(1); |
| 183 | + } |
| 184 | + let selectedItems = this.__tree.getSelection(); |
| 185 | + if (selectedItems.length < 1) { |
| 186 | + return; |
| 187 | + } |
| 188 | + let selectedItem = selectedItems.toArray()[0]; |
| 189 | + if (Object.prototype.hasOwnProperty.call(selectedItem, "propsWidget")) { |
| 190 | + this._add(selectedItem["propsWidget"]); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | +}); |
0 commit comments