|
| 1 | +/* ************************************************************************ |
| 2 | +
|
| 3 | + osparc - the simcore frontend |
| 4 | +
|
| 5 | + https://osparc.io |
| 6 | +
|
| 7 | + Copyright: |
| 8 | + 2024 IT'IS Foundation, https://itis.swiss |
| 9 | +
|
| 10 | + License: |
| 11 | + MIT: https://opensource.org/licenses/MIT |
| 12 | +
|
| 13 | + Authors: |
| 14 | + * Odei Maiz (odeimaiz) |
| 15 | +
|
| 16 | +************************************************************************ */ |
| 17 | + |
| 18 | +qx.Class.define("osparc.file.FolderContent", { |
| 19 | + extend: qx.ui.container.Stack, |
| 20 | + |
| 21 | + construct: function() { |
| 22 | + this.base(arguments); |
| 23 | + |
| 24 | + this.getChildControl("icons-layout"); |
| 25 | + this.getChildControl("table"); |
| 26 | + }, |
| 27 | + |
| 28 | + properties: { |
| 29 | + folder: { |
| 30 | + check: "qx.core.Object", |
| 31 | + init: null, |
| 32 | + nullable: true, |
| 33 | + event: "changeFolder", |
| 34 | + apply: "__applyFolder" |
| 35 | + }, |
| 36 | + |
| 37 | + mode: { |
| 38 | + check: ["list", "icons"], |
| 39 | + init: "icons", |
| 40 | + nullable: false, |
| 41 | + event: "changeMode", |
| 42 | + apply: "__reloadFolderContent" |
| 43 | + }, |
| 44 | + }, |
| 45 | + |
| 46 | + events: { |
| 47 | + "selectionChanged": "qx.event.type.Data", // tap |
| 48 | + "itemSelected": "qx.event.type.Data", // dbltap |
| 49 | + "requestDatasetFiles": "qx.event.type.Data", |
| 50 | + }, |
| 51 | + |
| 52 | + statics: { |
| 53 | + getItemButton: function() { |
| 54 | + const item = new qx.ui.form.ToggleButton().set({ |
| 55 | + iconPosition: "top", |
| 56 | + width: 100, |
| 57 | + height: 80, |
| 58 | + padding: 3 |
| 59 | + }); |
| 60 | + item.getChildControl("label").set({ |
| 61 | + rich: true, |
| 62 | + textAlign: "center", |
| 63 | + maxWidth: 100, |
| 64 | + maxHeight: 31 |
| 65 | + }); |
| 66 | + osparc.utils.Utils.setIdToWidget(item, "FolderViewerItem"); |
| 67 | + return item; |
| 68 | + }, |
| 69 | + |
| 70 | + T_POS: { |
| 71 | + TYPE: 0, |
| 72 | + NAME: 1, |
| 73 | + DATE: 2, |
| 74 | + SIZE: 3, |
| 75 | + ID: 4 |
| 76 | + } |
| 77 | + }, |
| 78 | + |
| 79 | + members: { |
| 80 | + _createChildControlImpl: function(id) { |
| 81 | + let control; |
| 82 | + switch (id) { |
| 83 | + case "table": { |
| 84 | + const tableModel = new qx.ui.table.model.Simple(); |
| 85 | + tableModel.setColumns([ |
| 86 | + "", |
| 87 | + this.tr("Name"), |
| 88 | + this.tr("Date Modified"), |
| 89 | + this.tr("Size"), |
| 90 | + this.tr("Id") |
| 91 | + ]); |
| 92 | + control = new osparc.ui.table.Table(tableModel, { |
| 93 | + // tableColumnModel: obj => new qx.ui.table.columnmodel.Resize(obj), |
| 94 | + initiallyHiddenColumns: [this.self().T_POS.ID] |
| 95 | + }); |
| 96 | + control.getTableColumnModel().setDataCellRenderer(this.self().T_POS.TYPE, new qx.ui.table.cellrenderer.Image()); |
| 97 | + control.setColumnWidth(this.self().T_POS.TYPE, 30); |
| 98 | + control.setColumnWidth(this.self().T_POS.NAME, 360); |
| 99 | + control.setColumnWidth(this.self().T_POS.DATE, 170); |
| 100 | + control.setColumnWidth(this.self().T_POS.SIZE, 70); |
| 101 | + this.bind("mode", control, "visibility", { |
| 102 | + converter: mode => mode === "list" ? "visible" : "excluded" |
| 103 | + }); |
| 104 | + const scroll = new qx.ui.container.Scroll(); |
| 105 | + scroll.add(control); |
| 106 | + this.add(scroll); |
| 107 | + break; |
| 108 | + } |
| 109 | + case "icons-layout": { |
| 110 | + control = new qx.ui.container.Composite(new qx.ui.layout.Flow(5, 5)); |
| 111 | + osparc.utils.Utils.setIdToWidget(control, "FolderViewerIconsContent"); |
| 112 | + this.bind("mode", control, "visibility", { |
| 113 | + converter: mode => mode === "icons" ? "visible" : "excluded" |
| 114 | + }); |
| 115 | + const scroll = new qx.ui.container.Scroll(); |
| 116 | + scroll.add(control); |
| 117 | + this.add(scroll); |
| 118 | + break; |
| 119 | + } |
| 120 | + } |
| 121 | + return control || this.base(arguments, id); |
| 122 | + }, |
| 123 | + |
| 124 | + __convertEntries: function(content) { |
| 125 | + const items = []; |
| 126 | + if (this.getMode() === "list") { |
| 127 | + content.forEach(entry => { |
| 128 | + const row = []; |
| 129 | + row.push(entry.getIcon ? entry.getIcon() : this.__getIcon(entry)); |
| 130 | + row.push(entry.getLabel()); |
| 131 | + row.push(entry.getLastModified ? osparc.utils.Utils.formatDateAndTime(new Date(entry.getLastModified())) : ""); |
| 132 | + row.push(entry.getSize ? osparc.utils.Utils.bytesToSize(entry.getSize()) : ""); |
| 133 | + if (entry.getItemId) { |
| 134 | + row.push(entry.getItemId()); |
| 135 | + } |
| 136 | + row.entry = entry; |
| 137 | + items.push(row); |
| 138 | + }); |
| 139 | + } else if (this.getMode() === "icons") { |
| 140 | + content.forEach(entry => { |
| 141 | + let tt = entry.getLabel(); |
| 142 | + if (entry.getSize) { |
| 143 | + tt += "<br>" + osparc.utils.Utils.bytesToSize(entry.getSize()); |
| 144 | + } |
| 145 | + if (entry.getLastModified) { |
| 146 | + tt += "<br>" + osparc.utils.Utils.formatDateAndTime(new Date(entry.getLastModified())); |
| 147 | + } |
| 148 | + const item = this.self().getItemButton().set({ |
| 149 | + label: entry.getLabel(), |
| 150 | + icon: entry.getIcon ? entry.getIcon() : this.__getIcon(entry), |
| 151 | + toolTipText: tt |
| 152 | + }); |
| 153 | + const icon = item.getChildControl("icon", true); |
| 154 | + if (icon.getSource() === "@FontAwesome5Solid/circle-notch/12") { |
| 155 | + icon.setPadding(0); |
| 156 | + icon.setMarginRight(4); |
| 157 | + icon.getContentElement().addClass("rotate"); |
| 158 | + } |
| 159 | + |
| 160 | + if (entry.getItemId) { |
| 161 | + item.itemId = entry.getItemId(); |
| 162 | + this.__attachListenersToItems(item, entry); |
| 163 | + } |
| 164 | + items.push(item); |
| 165 | + }); |
| 166 | + } |
| 167 | + return items; |
| 168 | + }, |
| 169 | + |
| 170 | + __getIcon: function(entry) { |
| 171 | + return osparc.file.FilesTree.isDir(entry) ? "@MaterialIcons/folder" : "@MaterialIcons/insert_drive_file"; |
| 172 | + }, |
| 173 | + |
| 174 | + __getEntries: function() { |
| 175 | + if (this.getFolder()) { |
| 176 | + const children = this.getFolder().getChildren().toArray(); |
| 177 | + return this.__convertEntries(children); |
| 178 | + } |
| 179 | + return []; |
| 180 | + }, |
| 181 | + |
| 182 | + __applyFolder: function(folder) { |
| 183 | + if (folder) { |
| 184 | + if (folder.getLoaded && !folder.getLoaded()) { |
| 185 | + this.fireDataEvent("requestDatasetFiles", { |
| 186 | + locationId: folder.getLocation(), |
| 187 | + datasetId: folder.getPath() |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + folder.getChildren().addListener("change", () => { |
| 192 | + this.__reloadFolderContent(); |
| 193 | + }, this); |
| 194 | + } |
| 195 | + |
| 196 | + this.__reloadFolderContent(); |
| 197 | + }, |
| 198 | + |
| 199 | + __reloadFolderContent: function() { |
| 200 | + const entries = this.__getEntries(); |
| 201 | + if (this.getMode() === "list") { |
| 202 | + const table = this.getChildControl("table"); |
| 203 | + table.setData(entries); |
| 204 | + this.__attachListenersTotable(table); |
| 205 | + } else if (this.getMode() === "icons") { |
| 206 | + const iconsLayout = this.getChildControl("icons-layout"); |
| 207 | + iconsLayout.removeAll(); |
| 208 | + const iconsGroup = new qx.ui.form.RadioGroup().set({ |
| 209 | + allowEmptySelection: true |
| 210 | + }); |
| 211 | + entries.forEach(entry => { |
| 212 | + iconsGroup.add(entry); |
| 213 | + iconsLayout.add(entry); |
| 214 | + }); |
| 215 | + } |
| 216 | + this.setSelection([this.getSelectables()[this.getMode() === "icons" ? 0 : 1]]); |
| 217 | + }, |
| 218 | + |
| 219 | + __itemTapped: function(item) { |
| 220 | + this.fireDataEvent("selectionChanged", item); |
| 221 | + }, |
| 222 | + |
| 223 | + __itemDblTapped: function(item) { |
| 224 | + this.fireDataEvent("itemSelected", item); |
| 225 | + if (osparc.file.FilesTree.isDir(item)) { |
| 226 | + this.setFolder(item); |
| 227 | + } |
| 228 | + }, |
| 229 | + |
| 230 | + __attachListenersToItems: function(btn, entry) { |
| 231 | + btn.addListener("tap", () => { |
| 232 | + this.__itemTapped(entry); |
| 233 | + }, this); |
| 234 | + btn.addListener("dbltap", () => { |
| 235 | + this.__itemDblTapped(entry); |
| 236 | + }, this); |
| 237 | + }, |
| 238 | + |
| 239 | + __attachListenersTotable: function(table) { |
| 240 | + table.addListener("cellTap", e => { |
| 241 | + const selectedRow = e.getRow(); |
| 242 | + const rowData = table.getTableModel().getRowData(selectedRow); |
| 243 | + if ("entry" in rowData) { |
| 244 | + this.__itemTapped(rowData.entry); |
| 245 | + } |
| 246 | + }, this); |
| 247 | + table.addListener("cellDbltap", e => { |
| 248 | + const selectedRow = e.getRow(); |
| 249 | + const rowData = table.getTableModel().getRowData(selectedRow); |
| 250 | + if ("entry" in rowData) { |
| 251 | + this.__itemDblTapped(rowData.entry); |
| 252 | + } |
| 253 | + }, this); |
| 254 | + } |
| 255 | + } |
| 256 | +}); |
0 commit comments