Skip to main content

module salix::demo::basic::Trees

rascal-0.43.0
org.rascalmpl.salix-core-0.3.0

Usage​

import salix::demo::basic::Trees;

Dependencies​

import salix::Core;
import salix::util::Tree;
import salix::App;
import salix::Index;
import salix::HTML;
import IO;
import Set;
import List;
import String;
import util::FileSystem;

function treesApp​

SalixApp[Model] treesApp(str id = "root") 
= makeApp(id, init, withIndex("Trees", id, view,
inlineCss=TREE_CSS), update);

function treesWebApp​

App[Model] treesWebApp()
= webApp(treesApp(),|project://salix-core/src/main/rascal|);

alias Model​

set[loc]

data Msg​

data Msg  
= toggle(loc file)
;

function init​

Model init() = {};

function update​

Model update(Msg msg, Model m) {
switch (msg) {
case toggle(loc f): {
if (f in m) {
m -= {f};
}
else {
m += {f};
}
println(m);
}
}
return m;
}

function renderFS​

void renderFS(file(loc l), Model m) {
leaf(() {
// span(onClick(toggle(l)), style(("font-weight": "bold" | l in m )), l.file);
text(l.file);
input(\type("checkbox"), checked(l in m), onClick(toggle(l)));
});
}

void renderFS(directory(loc l, set[FileSystem] kids), Model m) {
subTree(l.file, true, () {
for (FileSystem fs <- sort(kids)) {
renderFS(fs, m);
}
});
}

function view​

void view(Model m) {
h2("Dealing with trees");
tree(() {
subTree("Giant planets", true, () {
subTree("Gas giants", true, () {
leaf("Jupiter");
leaf("Saturn");
});
});
subTree("Ice giants", true, () {
leaf("Uranus");
leaf("Neptune");
});
});

h2("Filesystem");
FileSystem fs = crawl(|cwd:///src/main/rascal|);
tree(() {
renderFS(fs, m);
});
}