Archived
0

Изменение внутреннего строения веб апи

This commit is contained in:
2017-03-14 02:06:37 +03:00
parent e96ffbc568
commit 9a5ca4c2a7
3 changed files with 55 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
group = 'asys' group = 'asys'
version = '0.13-SNAPSHOT' version = '0.14-SNAPSHOT'
buildscript { buildscript {
repositories { repositories {

View File

@@ -18,9 +18,10 @@ public class IndexHandler implements HttpHandler {
public IndexHandler(WebinterfaceImpl webinterface) { public IndexHandler(WebinterfaceImpl webinterface) {
try { try {
InputStream inputStream = getClass().getResourceAsStream("/index.html"); InputStream inputStream = getClass().getResourceAsStream("/index.html");
WebinterfaceImpl.Module module = webinterface.getModule("modules");
htmlTemplate = IOUtils.toString(inputStream, "UTF-8") htmlTemplate = IOUtils.toString(inputStream, "UTF-8")
.replace("<{moduleScript}>", webinterface.javascriptToTags()) .replace("<{moduleScript}>", module.javascriptToTags())
.replace("<{moduleStylesheet}>", webinterface.stylesheetToTags()); .replace("<{moduleStylesheet}>", module.stylesheetToTags());
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException("Error load htmlTemplate", e); throw new RuntimeException("Error load htmlTemplate", e);
} }

View File

@@ -9,51 +9,83 @@ import asys.webinterface.api.Webinterface;
import java.util.*; import java.util.*;
public class WebinterfaceImpl implements Webinterface { public class WebinterfaceImpl implements Webinterface {
// moduleName, links private Map<String, Module> modules = new HashMap<>();
private Map<String, List<String>> javascripts = new HashMap<>();
private Map<String, List<String>> stylesheets = new HashMap<>();
private Map<String, List<MainMenuItem>> menuItems = new HashMap<>(); private Map<String, List<MainMenuItem>> menuItems = new HashMap<>();
public class MainMenuItem { public class MainMenuItem {
public String title, link; public String title, link;
public MainMenuItem(String title, String link) { public MainMenuItem(String title, String link) {
this.title = title; this.title = title;
this.link = link; this.link = link;
} }
} }
public class Module {
List<String> javascripts = new ArrayList<>();
List<String> stylesheets = new ArrayList<>();
public String javascriptToTags() {
StringJoiner sj = new StringJoiner("\n");
for (String link : javascripts) {
sj.add(String.format("<script src=\"%s\"></script>", link));
}
return sj.toString();
}
public String stylesheetToTags() {
StringJoiner sj = new StringJoiner("\n");
for (String link : stylesheets) {
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
}
return sj.toString();
}
}
@Override @Override
public void addJavascript(String moduleName, String link) { public void addJavascript(String moduleName, String link) {
List<String> list; Module module;
if (javascripts.containsKey(moduleName)) { if (modules.containsKey(moduleName)) {
list = javascripts.get(moduleName); module = modules.get(moduleName);
} else { } else {
list = new ArrayList<>(); module = new Module();
javascripts.put(moduleName, list); modules.put(moduleName, module);
} }
list.add(link);
module.javascripts.add(link);
} }
@Override @Override
public void removeJavascript(String moduleName) { public void removeJavascript(String moduleName) {
javascripts.remove(moduleName); if (modules.containsKey(moduleName)) {
Module module = modules.get(moduleName);
module.javascripts.clear();
}
} }
@Override @Override
public void addStylesheet(String moduleName, String link) { public void addStylesheet(String moduleName, String link) {
List<String> list; Module module;
if (stylesheets.containsKey(moduleName)) { if (modules.containsKey(moduleName)) {
list = stylesheets.get(moduleName); module = modules.get(moduleName);
} else { } else {
list = new ArrayList<>(); module = new Module();
stylesheets.put(moduleName, list); modules.put(moduleName, module);
} }
list.add(link);
module.stylesheets.add(link);
} }
@Override @Override
public void removeStylesheet(String moduleName) { public void removeStylesheet(String moduleName) {
stylesheets.remove(moduleName); if (modules.containsKey(moduleName)) {
Module module = modules.get(moduleName);
module.stylesheets.clear();
}
} }
@Override @Override
@@ -88,27 +120,7 @@ public class WebinterfaceImpl implements Webinterface {
return sj.toString(); return sj.toString();
} }
public String javascriptToTags() { public Module getModule(String moduleName) {
StringJoiner sj = new StringJoiner("\n"); return this.modules.get(moduleName);
for (List<String> items : javascripts.values()) {
for (String link : items) {
sj.add(String.format("<script src=\"%s\"></script>", link));
}
}
return sj.toString();
}
public String stylesheetToTags() {
StringJoiner sj = new StringJoiner("\n");
for (List<String> items : javascripts.values()) {
for (String link : items) {
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
}
}
return sj.toString();
} }
} }