|
1 |
| - |
2 | 1 | 'use strict';
|
3 | 2 |
|
4 |
| -const Theme = module.exports; |
| 3 | +const nconf = require.main.require('nconf'); |
| 4 | +const meta = require.main.require('./src/meta'); |
| 5 | +const _ = require.main.require('lodash'); |
| 6 | +const user = require.main.require('./src/user'); |
| 7 | + |
| 8 | +const controllers = require('./controllers'); |
| 9 | + |
| 10 | +const library = module.exports; |
| 11 | + |
| 12 | +const defaults = { |
| 13 | + enableQuickReply: 'on', |
| 14 | + centerHeaderElements: 'off', |
| 15 | + mobileTopicTeasers: 'off', |
| 16 | + stickyToolbar: 'on', |
| 17 | + autohideBottombar: 'on', |
| 18 | + openSidebars: 'off', |
| 19 | + chatModals: 'off', |
| 20 | +}; |
| 21 | + |
| 22 | +library.init = async function (params) { |
| 23 | + const { router, middleware } = params; |
| 24 | + const routeHelpers = require.main.require('./src/routes/helpers'); |
| 25 | + |
| 26 | + routeHelpers.setupAdminPageRoute(router, '/admin/plugins/theme-quickstart', [], controllers.renderAdminPage); |
| 27 | + |
| 28 | + routeHelpers.setupPageRoute(router, '/user/:userslug/theme', [ |
| 29 | + middleware.exposeUid, |
| 30 | + middleware.ensureLoggedIn, |
| 31 | + middleware.canViewUsers, |
| 32 | + middleware.checkAccountPermissions, |
| 33 | + ], controllers.renderThemeSettings); |
| 34 | + |
| 35 | + if (nconf.get('isPrimary') && process.env.NODE_ENV === 'production') { |
| 36 | + setTimeout(buildSkins, 0); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +async function buildSkins() { |
| 41 | + try { |
| 42 | + const plugins = require.main.require('./src/plugins'); |
| 43 | + await plugins.prepareForBuild(['client side styles']); |
| 44 | + for (const skin of meta.css.supportedSkins) { |
| 45 | + // eslint-disable-next-line no-await-in-loop |
| 46 | + await meta.css.buildBundle(`client-${skin}`, true); |
| 47 | + } |
| 48 | + require.main.require('./src/meta/minifier').killAll(); |
| 49 | + } catch (err) { |
| 50 | + console.error(err.stack); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +library.addAdminNavigation = async function (header) { |
| 55 | + header.plugins.push({ |
| 56 | + route: '/plugins/theme-quickstart', |
| 57 | + icon: 'fa-paint-brush', |
| 58 | + name: 'Theme Quick Start', |
| 59 | + }); |
| 60 | + return header; |
| 61 | +}; |
| 62 | + |
| 63 | +library.addProfileItem = async (data) => { |
| 64 | + data.links.push({ |
| 65 | + id: 'theme', |
| 66 | + route: 'theme', |
| 67 | + icon: 'fa-paint-brush', |
| 68 | + name: '[[themes/harmony:settings.title]]', |
| 69 | + visibility: { |
| 70 | + self: true, |
| 71 | + other: false, |
| 72 | + moderator: false, |
| 73 | + globalMod: false, |
| 74 | + admin: false, |
| 75 | + }, |
| 76 | + }); |
| 77 | + |
| 78 | + return data; |
| 79 | +}; |
5 | 80 |
|
6 |
| -Theme.defineWidgetAreas = async function (areas) { |
| 81 | +library.defineWidgetAreas = async function (areas) { |
7 | 82 | const locations = ['header', 'sidebar', 'footer'];
|
8 | 83 | const templates = [
|
9 | 84 | 'categories.tpl', 'category.tpl', 'topic.tpl', 'users.tpl',
|
@@ -63,3 +138,51 @@ Theme.defineWidgetAreas = async function (areas) {
|
63 | 138 |
|
64 | 139 | return areas;
|
65 | 140 | };
|
| 141 | + |
| 142 | +library.loadThemeConfig = async function (uid) { |
| 143 | + const [themeConfig, userConfig] = await Promise.all([ |
| 144 | + meta.settings.get('harmony'), |
| 145 | + user.getSettings(uid), |
| 146 | + ]); |
| 147 | + |
| 148 | + const config = { ...defaults, ...themeConfig, ...(_.pick(userConfig, Object.keys(defaults))) }; |
| 149 | + config.enableQuickReply = config.enableQuickReply === 'on'; |
| 150 | + config.centerHeaderElements = config.centerHeaderElements === 'on'; |
| 151 | + config.mobileTopicTeasers = config.mobileTopicTeasers === 'on'; |
| 152 | + config.stickyToolbar = config.stickyToolbar === 'on'; |
| 153 | + config.autohideBottombar = config.autohideBottombar === 'on'; |
| 154 | + config.openSidebars = config.openSidebars === 'on'; |
| 155 | + config.chatModals = config.chatModals === 'on'; |
| 156 | + return config; |
| 157 | +}; |
| 158 | + |
| 159 | +library.getThemeConfig = async function (config) { |
| 160 | + config.theme = await library.loadThemeConfig(config.uid); |
| 161 | + config.openDraftsOnPageLoad = false; |
| 162 | + return config; |
| 163 | +}; |
| 164 | + |
| 165 | +library.getAdminSettings = async function (hookData) { |
| 166 | + if (hookData.plugin === 'harmony') { |
| 167 | + hookData.values = { |
| 168 | + ...defaults, |
| 169 | + ...hookData.values, |
| 170 | + }; |
| 171 | + } |
| 172 | + return hookData; |
| 173 | +}; |
| 174 | + |
| 175 | +library.saveUserSettings = async function (hookData) { |
| 176 | + Object.keys(defaults).forEach((key) => { |
| 177 | + if (hookData.data.hasOwnProperty(key)) { |
| 178 | + hookData.settings[key] = hookData.data[key] || undefined; |
| 179 | + } |
| 180 | + }); |
| 181 | + return hookData; |
| 182 | +}; |
| 183 | + |
| 184 | +library.filterMiddlewareRenderHeader = async function (hookData) { |
| 185 | + hookData.templateData.bootswatchSkinOptions = await meta.css.getSkinSwitcherOptions(hookData.req.uid); |
| 186 | + return hookData; |
| 187 | +}; |
| 188 | + |
0 commit comments