AdminCustomizeContainer.js 9.3 KB

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