AdminCustomizeContainer.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { Container } from 'unstated';
  2. import loggerFactory from '@alias/logger';
  3. import { toastError } from '../util/apiNotification';
  4. // eslint-disable-next-line no-unused-vars
  5. const logger = loggerFactory('growi:services:AdminCustomizeContainer');
  6. /**
  7. * Service container for admin customize setting page (Customize.jsx)
  8. * @extends {Container} unstated Container
  9. */
  10. export default class AdminCustomizeContainer extends Container {
  11. constructor(appContainer) {
  12. super();
  13. this.appContainer = appContainer;
  14. this.state = {
  15. retrieveError: null,
  16. currentTheme: '',
  17. currentLayout: '',
  18. currentBehavior: '',
  19. isEnabledTimeline: false,
  20. isSavedStatesOfTabChanges: false,
  21. isEnabledAttachTitleHeader: false,
  22. currentRecentCreatedLimit: 10,
  23. currentHighlightJsStyleId: '',
  24. isHighlightJsStyleBorderEnabled: false,
  25. currentCustomizeTitle: '',
  26. currentCustomizeHeader: '',
  27. currentCustomizeCss: '',
  28. currentCustomizeScript: '',
  29. /* eslint-disable quote-props, no-multi-spaces */
  30. highlightJsCssSelectorOptions: {
  31. 'github': { name: '[Light] GitHub', border: false },
  32. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  33. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  34. 'xcode': { name: '[Light] Xcode', border: true },
  35. 'vs': { name: '[Light] Vs', border: true },
  36. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  37. 'hybrid': { name: '[Dark] Hybrid', border: false },
  38. 'monokai': { name: '[Dark] Monokai', border: false },
  39. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  40. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  41. },
  42. /* eslint-enable quote-props, no-multi-spaces */
  43. };
  44. }
  45. /**
  46. * Workaround for the mangling in production build to break constructor.name
  47. */
  48. static getClassName() {
  49. return 'AdminCustomizeContainer';
  50. }
  51. /**
  52. * retrieve customize data
  53. */
  54. async retrieveCustomizeData() {
  55. try {
  56. const response = await this.appContainer.apiv3.get('/customize-setting/');
  57. const { customizeParams } = response.data;
  58. this.setState({
  59. currentTheme: customizeParams.themeType,
  60. currentLayout: customizeParams.layoutType,
  61. currentBehavior: customizeParams.behaviorType,
  62. isEnabledTimeline: customizeParams.isEnabledTimeline,
  63. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  64. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  65. currentRecentCreatedLimit: customizeParams.recentCreatedLimit,
  66. currentHighlightJsStyleId: customizeParams.styleName,
  67. isHighlightJsStyleBorderEnabled: customizeParams.styleBorder,
  68. currentCustomizeTitle: customizeParams.customizeTitle,
  69. currentCustomizeHeader: customizeParams.customizeHeader,
  70. currentCustomizeCss: customizeParams.customizeCss,
  71. currentCustomizeScript: customizeParams.customizeScript,
  72. });
  73. // search style name from object for display
  74. this.setState({ currentHighlightJsStyleName: this.state.highlightJsCssSelectorOptions[customizeParams.styleName].name });
  75. }
  76. catch (err) {
  77. logger.error(err);
  78. toastError(new Error('Failed to fetch data'));
  79. }
  80. }
  81. /**
  82. * Switch layoutType
  83. */
  84. switchLayoutType(lauoutName) {
  85. this.setState({ currentLayout: lauoutName });
  86. }
  87. /**
  88. * Switch themeType
  89. */
  90. switchThemeType(themeName) {
  91. // can't choose theme when kibela
  92. if (this.state.currentLayout === 'kibela') {
  93. return;
  94. }
  95. this.setState({ currentTheme: themeName });
  96. }
  97. /**
  98. * Switch behaviorType
  99. */
  100. switchBehaviorType(behaviorName) {
  101. this.setState({ currentBehavior: behaviorName });
  102. }
  103. /**
  104. * Switch enabledTimeLine
  105. */
  106. switchEnableTimeline() {
  107. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  108. }
  109. /**
  110. * Switch savedStatesOfTabChanges
  111. */
  112. switchSavedStatesOfTabChanges() {
  113. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  114. }
  115. /**
  116. * Switch enabledAttachTitleHeader
  117. */
  118. switchEnabledAttachTitleHeader() {
  119. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  120. }
  121. /**
  122. * Switch recentCreatedLimit
  123. */
  124. switchRecentCreatedLimit(value) {
  125. this.setState({ currentRecentCreatedLimit: value });
  126. }
  127. /**
  128. * Switch highlightJsStyle
  129. */
  130. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  131. this.setState({ currentHighlightJsStyleId: styleId });
  132. this.setState({ currentHighlightJsStyleName: styleName });
  133. // recommended settings are applied
  134. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  135. }
  136. /**
  137. * Switch highlightJsStyleBorder
  138. */
  139. switchHighlightJsStyleBorder() {
  140. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  141. }
  142. /**
  143. * Change customize Title
  144. */
  145. changeCustomizeTitle(inputValue) {
  146. this.setState({ currentCustomizeTitle: inputValue });
  147. }
  148. /**
  149. * Change customize Html header
  150. */
  151. changeCustomizeHeader(inputValue) {
  152. this.setState({ currentCustomizeHeader: inputValue });
  153. }
  154. /**
  155. * Change customize css
  156. */
  157. changeCustomizeCss(inputValue) {
  158. this.setState({ currentCustomizeCss: inputValue });
  159. }
  160. /**
  161. * Change customize script
  162. */
  163. changeCustomizeScript(inpuValue) {
  164. this.setState({ currentCustomizeScript: inpuValue });
  165. }
  166. /**
  167. * Update layout
  168. * @memberOf AdminCustomizeContainer
  169. * @return {Array} Appearance
  170. */
  171. async updateCustomizeLayoutAndTheme() {
  172. const response = await this.appContainer.apiv3.put('/customize-setting/layoutTheme', {
  173. layoutType: this.state.currentLayout,
  174. themeType: this.state.currentTheme,
  175. });
  176. const { customizedParams } = response.data;
  177. return customizedParams;
  178. }
  179. /**
  180. * Update behavior
  181. * @memberOf AdminCustomizeContainer
  182. * @return {string} Behavior
  183. */
  184. async updateCustomizeBehavior() {
  185. const response = await this.appContainer.apiv3.put('/customize-setting/behavior', {
  186. behaviorType: this.state.currentBehavior,
  187. });
  188. const { customizedParams } = response.data;
  189. return customizedParams;
  190. }
  191. /**
  192. * Update function
  193. * @memberOf AdminCustomizeContainer
  194. * @return {string} Functions
  195. */
  196. async updateCustomizeFunction() {
  197. const response = await this.appContainer.apiv3.put('/customize-setting/function', {
  198. isEnabledTimeline: this.state.isEnabledTimeline,
  199. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  200. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  201. recentCreatedLimit: this.state.currentRecentCreatedLimit,
  202. });
  203. const { customizedParams } = response.data;
  204. return customizedParams;
  205. }
  206. /**
  207. * Update code highlight
  208. * @memberOf AdminCustomizeContainer
  209. * @return {Array} Code highlight
  210. */
  211. async updateHighlightJsStyle() {
  212. const response = await this.appContainer.apiv3.put('/customize-setting/highlight', {
  213. highlightJsStyle: this.state.currentHighlightJsStyleId,
  214. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  215. });
  216. const { customizedParams } = response.data;
  217. return customizedParams;
  218. }
  219. /**
  220. * Update customTitle
  221. * @memberOf AdminCustomizeContainer
  222. * @return {string} Customize title
  223. */
  224. async updateCustomizeTitle() {
  225. const response = await this.appContainer.apiv3.put('/customize-setting/customize-title', {
  226. customizeTitle: this.state.currentCustomizeTitle,
  227. });
  228. const { customizedParams } = response.data;
  229. return customizedParams;
  230. }
  231. /**
  232. * Update customHeader
  233. * @memberOf AdminCustomizeContainer
  234. * @return {string} Customize html header
  235. */
  236. async updateCustomizeHeader() {
  237. const response = await this.appContainer.apiv3.put('/customize-setting/customize-header', {
  238. customizeHeader: this.state.currentCustomizeHeader,
  239. });
  240. const { customizedParams } = response.data;
  241. return customizedParams;
  242. }
  243. /**
  244. * Update customCss
  245. * @memberOf AdminCustomizeContainer
  246. * @return {string} Customize css
  247. */
  248. async updateCustomizeCss() {
  249. const response = await this.appContainer.apiv3.put('/customize-setting/customize-css', {
  250. customizeCss: this.state.currentCustomizeCss,
  251. });
  252. const { customizedParams } = response.data;
  253. return customizedParams;
  254. }
  255. /**
  256. * Update customize script
  257. * @memberOf AdminCustomizeContainer
  258. * @return {string} Customize scripts
  259. */
  260. async updateCustomizeScript() {
  261. const response = await this.appContainer.apiv3.put('/customize-setting/customize-script', {
  262. customizeScript: this.state.currentCustomizeScript,
  263. });
  264. const { customizedParams } = response.data;
  265. return customizedParams;
  266. }
  267. }