Skip to content

Commit

Permalink
👏 add admin edit template
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Sep 27, 2017
1 parent d1de335 commit 1789d8a
Show file tree
Hide file tree
Showing 8 changed files with 181 additions and 12 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.otale</groupId>
<artifactId>tale</artifactId>
<version>1.3.1-beta</version>
<version>1.3.1</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
106 changes: 106 additions & 0 deletions src/main/java/com/tale/controller/admin/TemplateController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.tale.controller.admin;

import com.blade.ioc.annotation.Inject;
import com.blade.kit.StringKit;
import com.blade.mvc.Const;
import com.blade.mvc.annotation.*;
import com.blade.mvc.http.HttpMethod;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.blade.mvc.ui.RestResponse;
import com.tale.controller.BaseController;
import com.tale.extension.Commons;
import com.tale.service.ContentsService;
import com.tale.service.SiteService;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
* 编辑模板
* <p>
* Created by biezhi on 2017/2/21.
*/
@Slf4j
@Path("admin/template")
public class TemplateController extends BaseController {

@Inject
private ContentsService contentsService;

@Inject
private SiteService siteService;

@Route(value = "", method = HttpMethod.GET)
public String index(Request request) {
String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme();
try {
List<String> files = Files.list(Paths.get(themePath))
.map(path -> path.getFileName().toString())
.filter(path -> path.endsWith(".html"))
.collect(Collectors.toList());

List<String> partial = Files.list(Paths.get(themePath + File.separatorChar + "partial"))
.map(path -> path.getFileName().toString())
.filter(path -> path.endsWith(".html"))
.map(fileName -> "partial/" + fileName)
.collect(Collectors.toList());

List<String> statics = Files.list(Paths.get(themePath + File.separatorChar + "static"))
.map(path -> path.getFileName().toString())
.filter(path -> path.endsWith(".js") || path.endsWith(".css"))
.map(fileName -> "static/" + fileName)
.collect(Collectors.toList());

files.addAll(partial);
files.addAll(statics);

request.attribute("tpls", files);
} catch (IOException e) {
log.error("找不到模板路径");
}
return "admin/tpl_list";
}

@GetRoute("content")
public void getContent(@Param String fileName, Response response) {
try {
String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme();
String filePath = themePath + File.separatorChar + fileName;
String content = Files.readAllLines(Paths.get(filePath)).stream().collect(Collectors.joining("\n"));
response.text(content);
} catch (IOException e) {
log.error("获取模板文件失败", e);
}
}

@Route(value = "save", method = HttpMethod.POST)
@JSON
public RestResponse saveTpl(@Param String fileName, @Param String content) {
if (StringKit.isBlank(fileName)) {
return RestResponse.fail("缺少参数,请重试");
}
String themePath = Const.CLASSPATH + File.separatorChar + "templates" + File.separatorChar + "themes" + File.separatorChar + Commons.site_theme();
String filePath = themePath + File.separatorChar + fileName;
try {
if (Files.exists(Paths.get(filePath))) {
byte[] rf_wiki_byte = content.getBytes("UTF-8");
Files.write(Paths.get(filePath), rf_wiki_byte);
} else {
Files.createFile(Paths.get(filePath));
byte[] rf_wiki_byte = content.getBytes("UTF-8");
Files.write(Paths.get(filePath), rf_wiki_byte);
}
return RestResponse.ok();
} catch (Exception e) {
log.error("写入文件失败", e);
return RestResponse.fail("写入文件失败: " + e.getMessage());
}
}

}
4 changes: 2 additions & 2 deletions src/main/java/com/tale/controller/admin/ThemeController.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String index(Request request) {
*/
@GetRoute(value = "setting")
public String setting(Request request) {
String currentTheme = TaleConst.OPTIONS.get("site_theme", "default");
String currentTheme = Commons.site_theme();
String key = "theme_" + currentTheme + "_options";

String option = optionsService.getOption(key);
Expand All @@ -94,7 +94,7 @@ public RestResponse saveSetting(Request request) {
Map<String, List<String>> query = request.parameters();

// theme_milk_options => { }
String currentTheme = TaleConst.OPTIONS.get("site_theme", "default");
String currentTheme = Commons.site_theme();
String key = "theme_" + currentTheme + "_options";

Map<String, String> options = new HashMap<>();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/tale/extension/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,7 @@ public static String comments_num(String noComment, String value) {
* @return
*/
public static String theme_option(String key) {
String theme = Commons.site_option("site_theme", "default");
String theme = Commons.site_theme();
return TaleConst.OPTIONS.get("theme_" + theme + "_options")
.filter(StringKit::isNotBlank)
.map((String json) -> {
Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/templates/admin/article_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ <h4 class="page-title">

<input type="hidden" id="attach_url" value="${attach_url}" />

<form id="articleForm" role="form" novalidate="novalidate">
<form id="articleForm">
<input type="hidden" name="categories" id="categories"/>
<input type="hidden" name="cid" value="${contents.cid ?! ''}" id="cid"/>
<input type="hidden" name="status" value="${contents.status ?! 'publish'}" id="status"/>
Expand All @@ -101,12 +101,12 @@ <h4 class="page-title">
<input type="hidden" name="fmtType" id="fmtType" value="${contents.fmtType ?! 'markdown'}"/>

<div class="form-group col-md-6" style="padding: 0 10px 0 0;">
<input type="text" class="form-control" placeholder="请输入文章标题(必须)" name="title" required
aria-required="true" value="${contents.title ?! ''}"/>
<input class="form-control" placeholder="请输入文章标题(必须)" name="title" required
value="${contents.title ?! ''}"/>
</div>

<div class="form-group col-md-6" style="padding: 0 0 0 10px;">
<input type="text" class="form-control" placeholder="自定义访问路径,如:my-first-article 默认为文章id" name="slug"
<input class="form-control" placeholder="自定义访问路径,如:my-first-article 默认为文章id" name="slug"
value="${contents.slug ?! ''}"/>
</div>

Expand Down
10 changes: 7 additions & 3 deletions src/main/resources/templates/admin/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,26 @@
<a href="/admin/article" class="waves-effect #if(active=='article') active #end"><i class="fa fa-list" aria-hidden="true"></i><span> 文章管理 </span></a>
</li>

<li #if(active=='page') class="active" #end>
<a href="/admin/page" class="waves-effect #if(active=='page') active #end"><i class="fa fa-file-text" aria-hidden="true"></i><span> 页面管理 </span></a>
</li>

<li #if(active=='attach') class="active" #end>
<a href="/admin/attach" class="waves-effect #if(active=='attach') active #end"><i class="fa fa-cloud-upload" aria-hidden="true"></i><span> 文件管理 </span></a>
</li>

<li class="has_sub">
<a href="javascript:void(0)" class="waves-effect #if(has_sub=='other') active subdrop #end"><i class="fa fa-cubes"></i><span> 其他管理 </span><span class="pull-right"><i class="fa fa-plus"></i></span></a>
<ul class="list-unstyled">
<li #if(active=='page') class="active" #end>
<a href="/admin/page" class="waves-effect #if(active=='page') active #end"><i class="fa fa-file-text" aria-hidden="true"></i><span> 页面管理 </span></a>
</li>
<li #if(active=='comments') class="active" #end>
<a href="/admin/comments" class="waves-effect #if(active=='comments') active #end"><i class="fa fa-comments" aria-hidden="true"></i><span> 评论管理 </span></a>
</li>
<li #if(active=='category') class="active" #end>
<a href="/admin/category" class="waves-effect #if(active=='category') active #end"><i class="fa fa-tags" aria-hidden="true"></i><span> 分类/标签 </span></a>
</li>
<li #if(active=='template') class="active" #end>
<a href="/admin/template" class="waves-effect #if(active=='template') active #end"><i class="fa fa-hashtag"></i><span> 编辑模板 </span></a>
</li>
</ul>
</li>

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/admin/page_list.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include('./header.html',{has_sub:'other', active:'page', title:'页面管理'})
#include('./header.html',{active:'page', title:'页面管理'})
<div class="row">
<div class="col-sm-12">
<h4 class="page-title">文章管理</h4>
Expand Down
59 changes: 59 additions & 0 deletions src/main/resources/templates/admin/tpl_list.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include('./header.html',{active:'template', title:'编辑模板'})
<div class="row">
<div class="col-sm-12">
<h4 class="page-title">编辑模板</h4>
</div>
<div class="col-md-12">

<select id="tplSelect" onchange="showContent(this)">
<option selected value="">请选择模板文件</option>
#for(tpl : tpls)
<option value="${tpl}">${tpl}</option>
#end
</select>

<button class="btn btn-primary waves-effect waves-light m-b-5" onclick="saveTpl()">保存修改</button>

<div>
<textarea id="tpl-preview" rows="20" cols="100%" placeholder="选择模板后进行修改"></textarea>
</div>
</div>
</div>

#include('./footer.html')
<script>
var tale = new $.tale();

function showContent(obj) {
var fileName = obj.value;
if (fileName && fileName != '') {
$.get('/admin/template/content', {fileName: fileName}, function (data) {
$('#tpl-preview').val(data);
});
} else {
$('#tpl-preview').val('');
}
}

function saveTpl() {
var fileName = $('#tplSelect').val();
if (fileName && fileName != '') {
tale.post({
url: '/admin/template/save',
data: {
fileName: fileName,
content: $('#tpl-preview').val()
},
success: function (result) {
if (result && result.success) {
tale.alertOk('模板保存成功');
} else {
tale.alertError(result.msg || '模板保存失败');
}
}
});
}
}
</script>
</body>
</html>

0 comments on commit 1789d8a

Please sign in to comment.