Refactor gallery page.

pull/146/head
ruibaby 2019-04-21 14:45:33 +08:00
parent 38eadc9674
commit 36f69cab39
11 changed files with 86 additions and 37 deletions

View File

@ -50,6 +50,9 @@ public class FreeMarkerAutoConfiguration {
@Autowired @Autowired
private TagTagDirective tagTagDirective; private TagTagDirective tagTagDirective;
@Autowired
private GalleryTagDirective galleryTagDirective;
@Autowired @Autowired
private RandomMethod randomMethod; private RandomMethod randomMethod;
@ -71,6 +74,7 @@ public class FreeMarkerAutoConfiguration {
configuration.setSharedVariable("menuTag", menuTagDirective); configuration.setSharedVariable("menuTag", menuTagDirective);
configuration.setSharedVariable("tagTag", tagTagDirective); configuration.setSharedVariable("tagTag", tagTagDirective);
configuration.setSharedVariable("postTag", postTagDirective); configuration.setSharedVariable("postTag", postTagDirective);
configuration.setSharedVariable("galleryTag",galleryTagDirective);
configuration.setSharedVariable("randomMethod", randomMethod); configuration.setSharedVariable("randomMethod", randomMethod);
configuration.setSharedVariable("recentPostsMethod", recentPostsMethod); configuration.setSharedVariable("recentPostsMethod", recentPostsMethod);
configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod); configuration.setSharedVariable("recentCommentsMethod", recentCommentsMethod);

View File

@ -1,9 +1,9 @@
package run.halo.app.model.freemarker.tag; package run.halo.app.model.freemarker.tag;
import run.halo.app.service.CategoryService;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.*; import freemarker.template.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.CategoryService; import run.halo.app.service.CategoryService;
import java.io.IOException; import java.io.IOException;
@ -18,8 +18,6 @@ import java.util.Map;
@Component @Component
public class CategoryTagDirective implements TemplateDirectiveModel { public class CategoryTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method";
private final CategoryService categoryService; private final CategoryService categoryService;
public CategoryTagDirective(CategoryService categoryService) { public CategoryTagDirective(CategoryService categoryService) {
@ -30,8 +28,8 @@ public class CategoryTagDirective implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("categories", builder.build().wrap(categoryService.listAll())); env.setVariable("categories", builder.build().wrap(categoryService.listAll()));

View File

@ -1,9 +1,9 @@
package run.halo.app.model.freemarker.tag; package run.halo.app.model.freemarker.tag;
import run.halo.app.service.CommentService;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.*; import freemarker.template.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.CommentService; import run.halo.app.service.CommentService;
import java.io.IOException; import java.io.IOException;
@ -18,8 +18,6 @@ import java.util.Map;
@Component @Component
public class CommentTagDirective implements TemplateDirectiveModel { public class CommentTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method";
private final CommentService commentService; private final CommentService commentService;
public CommentTagDirective(CommentService commentService) { public CommentTagDirective(CommentService commentService) {
@ -30,8 +28,8 @@ public class CommentTagDirective implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
int top = Integer.parseInt(params.get("top").toString()); int top = Integer.parseInt(params.get("top").toString());
switch (method) { switch (method) {
case "latest": case "latest":

View File

@ -0,0 +1,49 @@
package run.halo.app.model.freemarker.tag;
import freemarker.core.Environment;
import freemarker.template.*;
import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.GalleryService;
import java.io.IOException;
import java.util.Map;
/**
* Freemarker custom tag of gallery.
*
* @author : RYAN0UP
* @date : 2019/4/21
*/
@Component
public class GalleryTagDirective implements TemplateDirectiveModel {
private final GalleryService galleryService;
public GalleryTagDirective(GalleryService galleryService) {
this.galleryService = galleryService;
}
@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(HaloConst.METHOD_KEY).toString();
switch (method) {
case "list":
env.setVariable("galleries", builder.build().wrap(galleryService.listAll()));
break;
case "listTeamVos":
env.setVariable("galleries", builder.build().wrap(null));
break;
case "count":
env.setVariable("count", builder.build().wrap(galleryService.count()));
break;
default:
break;
}
}
body.render(env.getOut());
}
}

View File

@ -1,9 +1,10 @@
package run.halo.app.model.freemarker.tag; package run.halo.app.model.freemarker.tag;
import run.halo.app.service.LinkService;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.*; import freemarker.template.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.LinkService;
import java.io.IOException; import java.io.IOException;
import java.util.Map; import java.util.Map;
@ -17,8 +18,6 @@ import java.util.Map;
@Component @Component
public class LinkTagDirective implements TemplateDirectiveModel { public class LinkTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method";
private final LinkService linkService; private final LinkService linkService;
public LinkTagDirective(LinkService linkService) { public LinkTagDirective(LinkService linkService) {
@ -29,8 +28,8 @@ public class LinkTagDirective implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("links", builder.build().wrap(linkService.listAll())); env.setVariable("links", builder.build().wrap(linkService.listAll()));

View File

@ -1,9 +1,9 @@
package run.halo.app.model.freemarker.tag; package run.halo.app.model.freemarker.tag;
import run.halo.app.service.MenuService;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.*; import freemarker.template.*;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.MenuService; import run.halo.app.service.MenuService;
import java.io.IOException; import java.io.IOException;
@ -30,8 +30,8 @@ public class MenuTagDirective implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("menus", builder.build().wrap(menuService.listAll())); env.setVariable("menus", builder.build().wrap(menuService.listAll()));

View File

@ -19,8 +19,6 @@ import java.util.Map;
@Component @Component
public class PostTagDirective implements TemplateDirectiveModel { public class PostTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method";
@Override @Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
// TODO Complete article tag directive. // TODO Complete article tag directive.

View File

@ -1,5 +1,6 @@
package run.halo.app.model.freemarker.tag; package run.halo.app.model.freemarker.tag;
import run.halo.app.model.support.HaloConst;
import run.halo.app.service.TagService; import run.halo.app.service.TagService;
import freemarker.core.Environment; import freemarker.core.Environment;
import freemarker.template.*; import freemarker.template.*;
@ -17,8 +18,6 @@ import java.util.Map;
@Component @Component
public class TagTagDirective implements TemplateDirectiveModel { public class TagTagDirective implements TemplateDirectiveModel {
private static final String METHOD_KEY = "method";
private final TagService tagService; private final TagService tagService;
public TagTagDirective(TagService tagService) { public TagTagDirective(TagService tagService) {
@ -29,8 +28,8 @@ public class TagTagDirective implements TemplateDirectiveModel {
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException { public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) throws TemplateException, IOException {
final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25); final DefaultObjectWrapperBuilder builder = new DefaultObjectWrapperBuilder(Configuration.VERSION_2_3_25);
if (params.containsKey(METHOD_KEY)) { if (params.containsKey(HaloConst.METHOD_KEY)) {
String method = params.get(METHOD_KEY).toString(); String method = params.get(HaloConst.METHOD_KEY).toString();
switch (method) { switch (method) {
case "list": case "list":
env.setVariable("tags", builder.build().wrap(tagService.listAll())); env.setVariable("tags", builder.build().wrap(tagService.listAll()));

View File

@ -38,6 +38,11 @@ public class HaloConst {
*/ */
public static final String SUFFIX_FTL = ".ftl"; public static final String SUFFIX_FTL = ".ftl";
/**
* Custom freemarker tag method key.
*/
public static final String METHOD_KEY = "method";
/** /**
* Owo map. (Unmodified map) * Owo map. (Unmodified map)
*/ */

View File

@ -7,7 +7,6 @@ import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import run.halo.app.exception.NotFoundException; import run.halo.app.exception.NotFoundException;
import run.halo.app.model.entity.Comment; import run.halo.app.model.entity.Comment;
import run.halo.app.model.entity.Gallery;
import run.halo.app.model.entity.Post; import run.halo.app.model.entity.Post;
import run.halo.app.model.enums.PostStatus; import run.halo.app.model.enums.PostStatus;
import run.halo.app.service.CommentService; import run.halo.app.service.CommentService;
@ -48,9 +47,7 @@ public class ContentPageController {
* @return template path: themes/{theme}/gallery * @return template path: themes/{theme}/gallery
*/ */
@GetMapping(value = "/gallery") @GetMapping(value = "/gallery")
public String gallery(Model model) { public String gallery() {
final List<Gallery> galleries = galleryService.listAll();
model.addAttribute("galleries", galleries);
return themeService.render("gallery"); return themeService.render("gallery");
} }

View File

@ -23,6 +23,7 @@
</ul> </ul>
</header> </header>
<section id="thumbnails"> <section id="thumbnails">
<@galleryTag method="list">
<#if galleries?size gt 0> <#if galleries?size gt 0>
<#list galleries as gallery> <#list galleries as gallery>
<article> <article>
@ -32,6 +33,7 @@
</article> </article>
</#list> </#list>
</#if> </#if>
</@galleryTag>
</section> </section>
<footer id="footer"> <footer id="footer">
<ul class="copyright"> <ul class="copyright">