In progress We should trawl properties for an "info" attribute, and then use that to render html into collapsable sections. Those sections should remember that they've collapsed.
| 24 | * In progress |
| 25 | * <p> |
| 26 | * We should trawl properties for an "info" attribute, and then use that to render html into collapsable sections. Those sections should remember that they've collapsed. |
| 27 | */ |
| 28 | public class BoxBrowser extends Box implements IO.Loaded { |
| 29 | |
| 30 | static public final String NAMED = "/named/"; |
| 31 | static public final String ID = "/id/"; |
| 32 | static public final String DOCUMENTATION = "/doc/"; |
| 33 | |
| 34 | private final Box root; |
| 35 | private Server s; |
| 36 | |
| 37 | |
| 38 | public interface HasMarkdownInformation { |
| 39 | String generateMarkdown(Box inside, Dict.Prop property); |
| 40 | } |
| 41 | |
| 42 | static public final Dict.Prop<BiFunction<Box, Object, String>> toMarkdown = new Dict.Prop<>("toMarkdown").set(Dict.domain, "*/attributes") |
| 43 | .doc("function that takes a (box, value) and returns a markdown string that describes that box"); |
| 44 | static public final Dict.Prop<BiFunction<Box, Object, String>> toHTML = new Dict.Prop<>("toHTML").set(Dict.domain, "*/attributes") |
| 45 | .doc("function that takes a (box, value) and returns a HTML string that describes that box"); |
| 46 | |
| 47 | static public Map<String, Object> knownObjects = new MapMaker().weakKeys().weakValues().makeMap(); |
| 48 | |
| 49 | public BoxBrowser(Box root) { |
| 50 | this.root = root; |
| 51 | } |
| 52 | |
| 53 | public void loaded() { |
| 54 | |
| 55 | this.s = root.find(ServerSupport.server, this.both()) |
| 56 | .findFirst() |
| 57 | .orElseThrow(() -> new IllegalArgumentException("need server for boxbrowser")); |
| 58 | |
| 59 | s.addDocumentRoot(fieldagent.Main.app + "/fieldbox/resources/"); |
| 60 | s.addURIHandler((uri, method, headers, params, files) -> { |
| 61 | if (uri.startsWith(NAMED)) { |
| 62 | uri = uri.substring(NAMED.length()); |
| 63 | String[] pieces = uri.split("/"); |
| 64 | return handleBy(x -> x.properties.has(Box.name) && x.properties.get(Box.name) |
| 65 | .equals(pieces[0]), pieces.length > 1 ? pieces[1] : null); |
| 66 | } else if (uri.startsWith(ID)) { |
| 67 | uri = uri.substring(ID.length()); |
| 68 | String[] pieces = uri.split("/"); |
| 69 | return handleBy(x -> x.properties.has(IO.id) && x.properties.get(IO.id) |
| 70 | .equals(pieces[0]), pieces.length > 1 ? pieces[1] : null); |
| 71 | } else if (uri.startsWith(DOCUMENTATION)) { |
| 72 | uri = uri.substring(DOCUMENTATION.length()); |
| 73 | String[] pieces = uri.split("/"); |
| 74 | Object object = knownObjects.get(pieces[0]); |
| 75 | if (object != null) { |
| 76 | return handleObject(object, pieces.length > 1 ? pieces[1] : null); |
| 77 | } |
| 78 | } |
| 79 | return null; |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | public NanoHTTPD.Response handleObject(Object object, String extraParameter) { |