Archived
0

WebModule: продолжаем пытаться найти наилучшее решение расширения веб интерфейса

This commit is contained in:
2017-03-15 14:34:16 +03:00
parent e96ffbc568
commit 908bd6a7dd
7 changed files with 82 additions and 84 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

@@ -4,6 +4,8 @@
*/ */
package asys.webinterface; package asys.webinterface;
import asys.webinterface.api.HttpReqResp;
import asys.webinterface.api.WebModule;
import asys.webinterface.api.Webinterface; import asys.webinterface.api.Webinterface;
import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext; import org.osgi.framework.BundleContext;
@@ -11,6 +13,8 @@ import org.osgi.framework.ServiceRegistration;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.*;
public class Activator implements BundleActivator { public class Activator implements BundleActivator {
private final Logger logger = LoggerFactory.getLogger(Activator.class); private final Logger logger = LoggerFactory.getLogger(Activator.class);
private ServiceRegistration<?> serviceMainMenu; private ServiceRegistration<?> serviceMainMenu;
@@ -19,8 +23,7 @@ public class Activator implements BundleActivator {
@Override @Override
public void start(BundleContext context) throws Exception { public void start(BundleContext context) throws Exception {
WebinterfaceImpl webinterface = new WebinterfaceImpl(); WebinterfaceImpl webinterface = new WebinterfaceImpl();
webinterface.addMainMenuItem("modules", "Модули", "#"); webinterface.addModule(createSelfModule());
webinterface.addJavascript("modules", "/static/module.js");
logger.trace("Register service: {}", Webinterface.class); logger.trace("Register service: {}", Webinterface.class);
serviceMainMenu = context.registerService(Webinterface.class, webinterface, null); serviceMainMenu = context.registerService(Webinterface.class, webinterface, null);
@@ -29,6 +32,36 @@ public class Activator implements BundleActivator {
webServer.start(8778); webServer.start(8778);
} }
private WebModule createSelfModule() {
return new WebModule() {
@Override
public String getName() {
return "modules";
}
@Override
public List<String> getStylesheetsLinks() {
return Collections.emptyList();
}
@Override
public List<String> getJavaScriptLinks() {
return Collections.singletonList("/static/module.js");
}
@Override
public Map<String, String> getMainMenuItems() {
return new HashMap<String, String>(){{
this.put("Модули", "#");
}};
}
@Override
public void handle(HttpReqResp httpReqResp) {
}
};
}
@Override @Override
public void stop(BundleContext context) throws Exception { public void stop(BundleContext context) throws Exception {
webServer.stop(); webServer.stop();

View File

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

View File

@@ -4,83 +4,32 @@
*/ */
package asys.webinterface; package asys.webinterface;
import asys.webinterface.api.WebModule;
import asys.webinterface.api.Webinterface; 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, WebModule> webModules = 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<>();
public class MainMenuItem { @Override
public String title, link; public void addModule(WebModule webModule) {
public MainMenuItem(String title, String link) { webModules.put(webModule.getName(), webModule);
this.title = title;
this.link = link;
}
} }
@Override @Override
public void addJavascript(String moduleName, String link) { public void removeModule(String moduleName) {
List<String> list; webModules.remove(moduleName);
if (javascripts.containsKey(moduleName)) {
list = javascripts.get(moduleName);
} else {
list = new ArrayList<>();
javascripts.put(moduleName, list);
}
list.add(link);
}
@Override
public void removeJavascript(String moduleName) {
javascripts.remove(moduleName);
}
@Override
public void addStylesheet(String moduleName, String link) {
List<String> list;
if (stylesheets.containsKey(moduleName)) {
list = stylesheets.get(moduleName);
} else {
list = new ArrayList<>();
stylesheets.put(moduleName, list);
}
list.add(link);
}
@Override
public void removeStylesheet(String moduleName) {
stylesheets.remove(moduleName);
}
@Override
public void addMainMenuItem(String moduleName, String title, String link) {
List<MainMenuItem> list;
if (menuItems.containsKey(moduleName)) {
list = menuItems.get(moduleName);
} else {
list = new ArrayList<>();
menuItems.put(moduleName, list);
}
list.add(new MainMenuItem(title, link));
}
@Override
public void removeMainMenuItems(String moduleName) {
menuItems.remove(moduleName);
} }
public String mainMenuItemsToReactJS() { public String mainMenuItemsToReactJS() {
StringJoiner sj = new StringJoiner(","); StringJoiner sj = new StringJoiner(",");
for(List<MainMenuItem> listItems : menuItems.values()) { for (WebModule webModule : webModules.values()) {
for (MainMenuItem item : listItems) { for(Map.Entry<String, String> entry : webModule.getMainMenuItems().entrySet()) {
sj.add(String.format( sj.add(String.format(
"ce(MainMenuItem, {title: '%s', href: '%s'})", "ce(MainMenuItem, {title: '%s', href: '%s'})",
item.title.replace("'","\\'"), item.link.replace("'","\\'") entry.getKey().replace("'","\\'"), entry.getValue().replace("'","\\'")
)); ));
} }
} }
@@ -88,25 +37,23 @@ public class WebinterfaceImpl implements Webinterface {
return sj.toString(); return sj.toString();
} }
public String javascriptToTags() { public String javascriptToTags(String moduleName) {
StringJoiner sj = new StringJoiner("\n"); StringJoiner sj = new StringJoiner("\n");
for (List<String> items : javascripts.values()) { WebModule webModule = webModules.get(moduleName);
for (String link : items) { for (String link : webModule.getJavaScriptLinks()) {
sj.add(String.format("<script src=\"%s\"></script>", link)); sj.add(String.format("<script src=\"%s\"></script>", link));
}
} }
return sj.toString(); return sj.toString();
} }
public String stylesheetToTags() { public String stylesheetToTags(String moduleName) {
StringJoiner sj = new StringJoiner("\n"); StringJoiner sj = new StringJoiner("\n");
for (List<String> items : javascripts.values()) { WebModule webModule = webModules.get(moduleName);
for (String link : items) { for (String link : webModule.getJavaScriptLinks()) {
sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link)); sj.add(String.format("<link rel=\"stylesheet\" href=\"%s\">", link));
}
} }
return sj.toString(); return sj.toString();

View File

@@ -0,0 +1,8 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2017-03-15
*/
package asys.webinterface.api;
public interface HttpReqResp {
}

View File

@@ -0,0 +1,16 @@
/*
* DmitriyMX <mail@dmitriymx.ru>
* 2017-03-15
*/
package asys.webinterface.api;
import java.util.List;
import java.util.Map;
public interface WebModule {
String getName();
List<String> getStylesheetsLinks();
List<String> getJavaScriptLinks();
Map<String, String> getMainMenuItems();
void handle(HttpReqResp httpReqResp);
}

View File

@@ -5,12 +5,6 @@
package asys.webinterface.api; package asys.webinterface.api;
public interface Webinterface { public interface Webinterface {
void addJavascript(String moduleName, String link); void addModule(WebModule webModule);
void removeJavascript(String moduleName); void removeModule(String moduleName);
void addStylesheet(String moduleName, String link);
void removeStylesheet(String moduleName);
void addMainMenuItem(String moduleName, String title, String link);
void removeMainMenuItems(String moduleName);
} }