AdminCustomizeContainer.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. // TODO GW-575 set data from apiV3
  16. currentTheme: appContainer.config.themeType,
  17. currentLayout: appContainer.config.layoutType,
  18. currentBehavior: appContainer.config.behaviorType,
  19. isEnabledTimeline: appContainer.config.isEnabledTimeline,
  20. isSavedStatesOfTabChanges: appContainer.config.isSavedStatesOfTabChanges,
  21. isEnabledAttachTitleHeader: appContainer.config.isEnabledAttachTitleHeader,
  22. currentRecentCreatedLimit: appContainer.config.recentCreatedLimit,
  23. currentHighlightJsStyleId: appContainer.config.highlightJsStyle,
  24. isHighlightJsStyleBorderEnabled: appContainer.config.highlightJsStyleBorder,
  25. currentCustomizeHeader: appContainer.config.customizeHeader,
  26. currentCustomizeCss: appContainer.config.customizeCss,
  27. currentCustomizeScript: appContainer.config.customizeScript,
  28. /* eslint-disable quote-props, no-multi-spaces */
  29. highlightJsCssSelectorOptions: {
  30. 'github': { name: '[Light] GitHub', border: false },
  31. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  32. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  33. 'xcode': { name: '[Light] Xcode', border: true },
  34. 'vs': { name: '[Light] Vs', border: true },
  35. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  36. 'hybrid': { name: '[Dark] Hybrid', border: false },
  37. 'monokai': { name: '[Dark] Monokai', border: false },
  38. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  39. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  40. },
  41. /* eslint-enable quote-props, no-multi-spaces */
  42. };
  43. this.init();
  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 init() {
  55. // TODO GW-575 fetch data with apiV3
  56. try {
  57. // search style name from object for display
  58. this.setState({ currentHighlightJsStyleName: this.state.highlightJsCssSelectorOptions[this.state.currentHighlightJsStyleId].name });
  59. }
  60. catch (err) {
  61. logger.error(err);
  62. toastError(new Error('Failed to fetch data'));
  63. }
  64. }
  65. /**
  66. * Fetch highLight theme
  67. */
  68. async fetchHighLightTheme() {
  69. const response = await this.appContainer.apiv3.get('/customize-setting/');
  70. this.setState({
  71. highlightJsCssSelectorOptions: response.data.highlightJsCssSelectorOptions,
  72. });
  73. }
  74. /**
  75. * Switch layoutType
  76. */
  77. switchLayoutType(lauoutName) {
  78. this.setState({ currentLayout: lauoutName });
  79. }
  80. /**
  81. * Switch themeType
  82. */
  83. switchThemeType(themeName) {
  84. // can't choose theme when kibela
  85. if (this.state.currentLayout === 'kibela') {
  86. return;
  87. }
  88. this.setState({ currentTheme: themeName });
  89. }
  90. /**
  91. * Switch behaviorType
  92. */
  93. switchBehaviorType(behaviorName) {
  94. this.setState({ currentBehavior: behaviorName });
  95. }
  96. /**
  97. * Switch enabledTimeLine
  98. */
  99. switchEnableTimeline() {
  100. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  101. }
  102. /**
  103. * Switch savedStatesOfTabChanges
  104. */
  105. switchSavedStatesOfTabChanges() {
  106. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  107. }
  108. /**
  109. * Switch enabledAttachTitleHeader
  110. */
  111. switchEnabledAttachTitleHeader() {
  112. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  113. }
  114. /**
  115. * Switch recentCreatedLimit
  116. */
  117. switchRecentCreatedLimit(value) {
  118. this.setState({ currentRecentCreatedLimit: value });
  119. }
  120. /**
  121. * Switch highlightJsStyle
  122. */
  123. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  124. this.setState({ currentHighlightJsStyleId: styleId });
  125. this.setState({ currentHighlightJsStyleName: styleName });
  126. // recommended settings are applied
  127. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  128. }
  129. /**
  130. * Switch highlightJsStyleBorder
  131. */
  132. switchHighlightJsStyleBorder() {
  133. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  134. }
  135. /**
  136. * Change customize Html header
  137. */
  138. changeCustomizeHeader(inputValue) {
  139. this.setState({ currentCustomizeHeader: inputValue });
  140. }
  141. /**
  142. * Change customize css
  143. */
  144. changeCustomizeCss(inputValue) {
  145. this.setState({ currentCustomizeCss: inputValue });
  146. }
  147. /**
  148. * Change customize script
  149. */
  150. changeCustomizeScript(inpuValue) {
  151. this.setState({ currentCustomizeScript: inpuValue });
  152. }
  153. /**
  154. * Update layout
  155. * @memberOf AdminCustomizeContainer
  156. * @return {Array} Appearance
  157. */
  158. async updateCustomizeLayoutAndTheme() {
  159. const response = await this.appContainer.apiv3.put('/customize-setting/layoutTheme', {
  160. layoutType: this.state.currentLayout,
  161. themeType: this.state.currentTheme,
  162. });
  163. const { customizedParams } = response.data;
  164. return customizedParams;
  165. }
  166. /**
  167. * Update behavior
  168. * @memberOf AdminCustomizeContainer
  169. * @return {string} Behavior
  170. */
  171. async updateCustomizeBehavior() {
  172. const response = await this.appContainer.apiv3.put('/customize-setting/behavior', {
  173. behaviorType: this.state.currentBehavior,
  174. });
  175. const { customizedParams } = response.data;
  176. return customizedParams;
  177. }
  178. /**
  179. * Update function
  180. * @memberOf AdminCustomizeContainer
  181. * @return {string} Functions
  182. */
  183. async updateCustomizeFunction() {
  184. const response = await this.appContainer.apiv3.put('/customize-setting/function', {
  185. isEnabledTimeline: this.state.isEnabledTimeline,
  186. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  187. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  188. recentCreatedLimit: this.state.currentRecentCreatedLimit,
  189. });
  190. const { customizedParams } = response.data;
  191. return customizedParams;
  192. }
  193. /**
  194. * Update code highlight
  195. * @memberOf AdminCustomizeContainer
  196. * @return {Array} Code highlight
  197. */
  198. async updateHighlightJsStyle() {
  199. // TODO GW-515 create apiV3
  200. }
  201. /**
  202. * Update customHeader
  203. * @memberOf AdminCustomizeContainer
  204. * @return {string} Customize html header
  205. */
  206. async updateCustomizeHeader() {
  207. const response = await this.appContainer.apiv3.put('/customize-setting/customize-header', {
  208. customizeHeader: this.state.currentCustomizeHeader,
  209. });
  210. const { customizedParams } = response.data;
  211. return customizedParams;
  212. }
  213. /**
  214. * Update customCss
  215. * @memberOf AdminCustomizeContainer
  216. * @return {string} Customize css
  217. */
  218. async updateCustomizeCss() {
  219. const response = await this.appContainer.apiv3.put('/customize-setting/customize-css', {
  220. customizeCss: this.state.currentCustomizeCss,
  221. });
  222. const { customizedParams } = response.data;
  223. return customizedParams;
  224. }
  225. /**
  226. * Update customize script
  227. * @memberOf AdminCustomizeContainer
  228. * @return {string} Customize scripts
  229. */
  230. async updateCustomizeScript() {
  231. const response = await this.appContainer.apiv3.put('/customize-setting/customize-script', {
  232. customizeScript: this.state.currentCustomizeScript,
  233. });
  234. const { customizedParams } = response.data;
  235. return customizedParams;
  236. }
  237. }