Skip to content

Commit

Permalink
🦄 add theme setting
Browse files Browse the repository at this point in the history
  • Loading branch information
hellokaton committed Mar 15, 2017
1 parent 0265110 commit 00aacab
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 26 deletions.
15 changes: 14 additions & 1 deletion src/main/java/com/tale/controller/admin/ThemeController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tale.controller.admin;

import com.blade.Blade;
import com.blade.ioc.annotation.Inject;
import com.blade.kit.FileKit;
import com.blade.kit.StringKit;
Expand All @@ -18,6 +19,7 @@
import com.tale.exception.TipException;
import com.tale.ext.Commons;
import com.tale.init.TaleConst;
import com.tale.init.TaleLoader;
import com.tale.service.LogService;
import com.tale.service.OptionsService;
import org.slf4j.Logger;
Expand Down Expand Up @@ -55,6 +57,10 @@ public String index(Request request) {
themeDto.setHasSetting(true);
}
themes.add(themeDto);
try {
String themePath = "/templates/themes/" + f.getName();
TaleLoader.loadTheme(themePath);
} catch (Exception e){}
}
}
request.attribute("current_theme", Commons.site_theme());
Expand Down Expand Up @@ -114,12 +120,19 @@ public RestResponse saveSetting(Request request) {
public RestResponse activeTheme(Request request, @QueryParam String site_theme) {
try {
optionsService.saveOption("site_theme", site_theme);
optionsService.deleteOption("theme_option_");

TaleConst.OPTIONS.put("site_theme", site_theme);
BaseController.THEME = "themes/" + site_theme;

String themePath = "/templates/themes/" + site_theme;
try {
TaleLoader.loadTheme(themePath);
} catch (Exception e){}
logService.save(LogActions.THEME_SETTING, site_theme, request.address(), this.getUid());
return RestResponse.ok();
} catch (Exception e) {
String msg = "激活主题失败";
String msg = "主题启用失败";
if (e instanceof TipException) {
msg = e.getMessage();
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/com/tale/ext/Theme.java
Original file line number Diff line number Diff line change
Expand Up @@ -561,4 +561,13 @@ public static String comments_num(String noComment, String value){
return contents.getComments_num() > 0 ? String.format(value, contents.getComments_num()) : noComment;
}

/**
* 返回主题设置选项
*
* @param key
* @return
*/
public static String theme_option(String key){
return TaleConst.OPTIONS.get("theme_option_" + key);
}
}
5 changes: 5 additions & 0 deletions src/main/java/com/tale/init/TaleLoader.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.tale.init;

import com.blade.Blade;
import com.blade.config.BConfig;
import com.blade.kit.FileKit;
import com.tale.controller.admin.AttachController;
Expand Down Expand Up @@ -44,6 +45,10 @@ public static void loadThemes(BConfig bConfig) {
}
}

public static void loadTheme(String themePath){
Blade.$().embedServer().addStatic(themePath + "/style.css", themePath + "/screenshot.png", themePath + "/static");
}

public static void loadPlugins(BConfig bConfig) {
File pluginDir = new File(AttachController.CLASSPATH + "plugins");
if (pluginDir.exists() && pluginDir.isDirectory()) {
Expand Down
31 changes: 27 additions & 4 deletions src/main/resources/templates/admin/themes.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ <h4 class="page-title">主题管理</h4>
<div class="col-md-12">
#for(theme : themes)
<div class="col-md-3 text-center m-t-10">
<img class="attach-img" src="/templates/themes/${theme.name}/screenshot.png" width="200" height="200" title="${theme.name}"/>
<div class="clearfix">
<img class="attach-img bx-shadow" src="/templates/themes/${theme.name}/screenshot.png" width="200" height="200" title="${theme.name}"/>
<div class="clearfix m-t-10">
主题:<span>${theme.name}</span> #if(theme.name == current_theme) <mark>(当前主题)</mark> #end
</div>
<div class="clearfix m-t-10">
#if(theme.hasSetting)
#if(theme.name == current_theme && theme.hasSetting)
<a href="/admin/themes/setting"><span>主题设置</span></a>
#end
#if(theme.name != current_theme)
<button type="button" class="btn btn-danger btn-sm waves-effect waves-light m-t-5">
<button onclick="activeTheme('${theme.name}');" type="button" class="btn btn-danger btn-sm waves-effect waves-light m-t-5">
<i class="fa fa-check-circle"></i> <span>启用该主题</span>
</button>
#end
Expand All @@ -30,6 +30,29 @@ <h4 class="page-title">主题管理</h4>

var tale = new $.tale();

/**
* 启用主题
* @param themeName
*/
function activeTheme(themeName) {
tale.alertConfirm({
title:'确定启用该主题吗?',
then: function () {
tale.post({
url : '/admin/themes/active',
data: {site_theme: themeName},
success: function (result) {
if(result && result.success){
tale.alertOkAndReload('主题启用成功');
} else {
tale.alertError(result.msg || '主题启用失败');
}
}
});
}
});
}

</script>
</body>
</html>
6 changes: 3 additions & 3 deletions src/main/resources/templates/comm/ts_base_layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
/**
* 保存全局设置
*/
function saveSetting() {
var param = $('#global-form').serialize();
function saveThemeOptions(formId) {
var param = $('#' + formId).serialize();
tale.post({
url : '/admin/setting',
url : '/admin/themes/setting',
data: param,
success: function (result) {
if(result && result.success){
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/templates/themes/default/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
<div class="footer-social">
<div class="footer-container clearfix">
<div class="social-list">
#tag social("weibo")
<a class="social weibo" target="blank" href="http://weibo.com/${site_option('social_weibo')}">微博</a>
#if(null != theme_option('social_weibo'))
<a class="social weibo" target="blank" href="http://weibo.com/${theme_option('social_weibo')}">微博</a>
#end

#tag social("zhihu")
#if(null != theme_option('social_zhihu'))
<a class="social zhihu" target="blank"
href="https://www.zhihu.com/people/${site_option('social_zhihu')}">知乎</a>
href="https://www.zhihu.com/people/${theme_option('social_zhihu')}">知乎</a>
#end

<a class="social rss" target="blank" href="/feed">RSS</a>

#tag social("github")
#if(null != theme_option('social_github'))
<a class="social github" target="blank"
href="https://github.com/${site_option('social_github')}">Github</a>
href="https://github.com/${theme_option('social_github')}">Github</a>
#end

#tag social("twitter")
<a class="social twitter" target="blank" href="https://twitter.com/${site_option('social_twitter')}">Twitter</a>
#if(null != theme_option('social_twitter'))
<a class="social twitter" target="blank" href="https://twitter.com/${theme_option('social_twitter')}">Twitter</a>
#end
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/themes/default/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<header id="header" class="header bg-white">
<div class="navbar-container">
<a href="${site_url()}" class="navbar-logo">
<img src="${theme_url('/static/img/logo.png')}" alt="${site_title()}"/>
<img src="${theme_option('logo_url') ?! theme_url('/static/img/logo.png')}" alt="${site_title()}"/>
</a>
<div class="navbar-menu">
<a href="${site_url('/archives')}">归档</a>
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/templates/themes/default/post.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h1 class="post-title" itemprop="name headline">
<p class="post-tags">${show_tags()}</p>
${show_content()}
<p class="post-info">
本文由 <a href="">biezhi</a> 创作采用 <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank"
本文由 <a href="">${theme_option('author') ?! 'biezhi'}</a> 创作采用 <a href="https://creativecommons.org/licenses/by/4.0/" target="_blank"
rel="external nofollow">知识共享署名4.0</a> 国际许可协议进行许可<br>本站文章除注明转载/出处外均为本站原创或翻译转载前请务必署名<br>最后编辑时间为:
${modified('yyyy/MM/dd HH:mm')}
</p>
Expand Down
Binary file modified src/main/resources/templates/themes/default/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 6 additions & 8 deletions src/main/resources/templates/themes/default/setting.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,23 @@ <h4 class="page-title">主题设置</h4>
<h3 class="panel-title">基本设置</h3>
</div>
<div class="panel-body">
<form class="form-horizontal" role="form">
<form id="option-from" class="form-horizontal" role="form">
<div class="form-group">
<label class="col-md-3 control-label">主题LOGO</label>
<div class="col-md-9">
<input type="text" class="form-control" name="theme_option_social_weibo" value="${options.theme_option_social_weibo ?! ''}"
placeholder="微博账号,不输入则不显示">
<input type="text" class="form-control" name="theme_option_logo_url" value="${options.theme_option_logo_url ?! ''}"
placeholder="请输入主题LOGO外链">
</div>
</div>
<div class="form-group">
<label class="col-md-3 control-label">主题作者</label>
<div class="col-md-9">
<input type="text" class="form-control" name="theme_option_social_zhihu" value="${options.theme_option_social_zhihu ?! ''}"
placeholder="知乎账号,不输入则不显示">
<input type="text" class="form-control" name="theme_option_author" value="${options.theme_option_author ?! ''}" placeholder="您的博客昵称">
</div>
</div>
<div class="clearfix pull-right">
<button type="button" class="btn btn-primary waves-effect waves-light"
onclick="saveIndiviSetting()">
onclick="saveThemeOptions('option-from')">
保存设置
</button>
</div>
Expand Down Expand Up @@ -70,8 +69,7 @@ <h3 class="panel-title">个性化设置</h3>
</div>
</div>
<div class="clearfix pull-right">
<button type="button" class="btn btn-inverse waves-effect waves-light"
onclick="saveIndiviSetting()">
<button type="button" class="btn btn-inverse waves-effect waves-light" onclick="saveThemeOptions('indivi-form')">
保存设置
</button>
</div>
Expand Down

0 comments on commit 00aacab

Please sign in to comment.