Skip to content

Commit

Permalink
Supporting multiple components in pages, with enhanced styling.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Feb 23, 2017
1 parent 4616b40 commit 50aee1f
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 3 deletions.
2 changes: 2 additions & 0 deletions rapidoid-commons/src/main/resources/rapidoid-classes.txt
Expand Up @@ -800,6 +800,8 @@ org.rapidoid.var.Vars
org.rapidoid.web.config.bean.AbstractRouteConfig
org.rapidoid.web.config.bean.APIConfig
org.rapidoid.web.config.bean.PageConfig
org.rapidoid.web.config.bean.PageGuiConfig
org.rapidoid.web.config.bean.PageGuiType
org.rapidoid.web.config.bean.ProxyConfig
org.rapidoid.web.config.listener.APIConfigListener
org.rapidoid.web.config.listener.GenericConfigListener
Expand Down
Expand Up @@ -23,6 +23,8 @@
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

import java.util.Map;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class PageConfig extends AbstractRouteConfig {
Expand All @@ -33,6 +35,8 @@ public class PageConfig extends AbstractRouteConfig {

public volatile String zone;

public volatile Map<String, PageGuiConfig> gui;

public PageConfig() {
}

Expand Down
@@ -0,0 +1,48 @@
package org.rapidoid.web.config.bean;

/*
* #%L
* rapidoid-web
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public class PageGuiConfig extends RapidoidThing {

public volatile PageGuiType type = PageGuiType.grid;

public volatile String caption;

public volatile String header;

public volatile String footer;

public volatile String sql;

public PageGuiConfig() {
}

public PageGuiConfig(String shortcut) {
this.sql = shortcut;
}

}
@@ -0,0 +1,32 @@
package org.rapidoid.web.config.bean;

/*
* #%L
* rapidoid-web
* %%
* Copyright (C) 2014 - 2017 Nikolche Mihajlovski and contributors
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/

import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;

@Authors("Nikolche Mihajlovski")
@Since("5.3.0")
public enum PageGuiType {

grid, form;

}
Expand Up @@ -23,16 +23,18 @@
import org.rapidoid.RapidoidThing;
import org.rapidoid.annotation.Authors;
import org.rapidoid.annotation.Since;
import org.rapidoid.commons.Err;
import org.rapidoid.gui.GUI;
import org.rapidoid.gui.Grid;
import org.rapidoid.http.Current;
import org.rapidoid.http.Req;
import org.rapidoid.http.ReqRespHandler;
import org.rapidoid.http.Resp;
import org.rapidoid.jdbc.JDBC;
import org.rapidoid.u.U;
import org.rapidoid.web.config.bean.PageConfig;
import org.rapidoid.web.config.bean.PageGuiConfig;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

Expand All @@ -48,9 +50,56 @@ public PageHandler(PageConfig page) {

@Override
public Object execute(Req req, Resp resp) {
List<Map<String, Object>> items = JDBC.query(page.sql, req.params());

Grid grid = GUI.grid(items);
if (page.sql != null) {
return sqlGrid(page.sql);
}

if (U.notEmpty(page.gui)) {
return guiModel(page.gui);
}

return GUI.N_A;
}

private Object guiModel(Map<String, PageGuiConfig> gui) {
Map<Object, Object> model = U.map();

for (Map.Entry<String, PageGuiConfig> e : gui.entrySet()) {
model.put(e.getKey(), gui(e.getValue()));
}

return model;
}

private Object gui(PageGuiConfig gui) {
Object item;

switch (gui.type) {
case grid:
item = sqlGrid(gui.sql);
break;

default:
throw Err.notReady();
}


if (U.notEmpty(gui.caption)) {
item = GUI.multi(GUI.titleBox(gui.caption), item);
}

if (U.notEmpty(gui.header) || U.notEmpty(gui.footer)) {
item = GUI.panel(item).header(gui.header).footer(gui.footer);
}

return item;
}

public Grid sqlGrid(String sql) {
Req req = req();

Grid grid = GUI.grid(JDBC.query(sql, req.params()));

String q = req.param("find", null);
if (q != null) grid.highlightRegex(Pattern.quote(q));
Expand All @@ -59,9 +108,14 @@ public Object execute(Req req, Resp resp) {
if (highlight != null) grid.highlightRegex(Pattern.quote(highlight));

String pageSize = req.param("$pageSize", null);

if (pageSize != null) grid.pageSize(U.num(pageSize));

return grid;
}

private Req req() {
return Current.request();
}

}

0 comments on commit 50aee1f

Please sign in to comment.