AdminCustomizeContainer.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. import { Container } from 'unstated';
  2. import loggerFactory from '~/utils/logger';
  3. import { toastError } from '../util/apiNotification';
  4. import { apiv3Get, apiv3Put } from '../util/apiv3-client';
  5. // eslint-disable-next-line no-unused-vars
  6. const logger = loggerFactory('growi:services:AdminCustomizeContainer');
  7. /**
  8. * Service container for admin customize setting page (Customize.jsx)
  9. * @extends {Container} unstated Container
  10. */
  11. export default class AdminCustomizeContainer extends Container {
  12. constructor() {
  13. super();
  14. this.state = {
  15. retrieveError: null,
  16. isEnabledTimeline: false,
  17. isSavedStatesOfTabChanges: false,
  18. isEnabledAttachTitleHeader: false,
  19. pageLimitationS: null,
  20. pageLimitationM: null,
  21. pageLimitationL: null,
  22. pageLimitationXL: null,
  23. isEnabledStaleNotification: false,
  24. isAllReplyShown: false,
  25. isSearchScopeChildrenAsDefault: false,
  26. currentHighlightJsStyleId: '',
  27. isHighlightJsStyleBorderEnabled: false,
  28. currentCustomizeTitle: '',
  29. currentCustomizeHeader: '',
  30. currentCustomizeCss: '',
  31. currentCustomizeScript: '',
  32. /* eslint-disable quote-props, no-multi-spaces */
  33. highlightJsCssSelectorOptions: {
  34. 'github': { name: '[Light] GitHub', border: false },
  35. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  36. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  37. 'xcode': { name: '[Light] Xcode', border: true },
  38. 'vs': { name: '[Light] Vs', border: true },
  39. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  40. 'hybrid': { name: '[Dark] Hybrid', border: false },
  41. 'monokai': { name: '[Dark] Monokai', border: false },
  42. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  43. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  44. },
  45. /* eslint-enable quote-props, no-multi-spaces */
  46. };
  47. this.switchPageListLimitationS = this.switchPageListLimitationS.bind(this);
  48. this.switchPageListLimitationM = this.switchPageListLimitationM.bind(this);
  49. this.switchPageListLimitationL = this.switchPageListLimitationL.bind(this);
  50. this.switchPageListLimitationXL = this.switchPageListLimitationXL.bind(this);
  51. }
  52. /**
  53. * Workaround for the mangling in production build to break constructor.name
  54. */
  55. static getClassName() {
  56. return 'AdminCustomizeContainer';
  57. }
  58. /**
  59. * retrieve customize data
  60. */
  61. async retrieveCustomizeData() {
  62. try {
  63. const response = await apiv3Get('/customize-setting/');
  64. const { customizeParams } = response.data;
  65. this.setState({
  66. isEnabledTimeline: customizeParams.isEnabledTimeline,
  67. isSavedStatesOfTabChanges: customizeParams.isSavedStatesOfTabChanges,
  68. isEnabledAttachTitleHeader: customizeParams.isEnabledAttachTitleHeader,
  69. pageLimitationS: customizeParams.pageLimitationS,
  70. pageLimitationM: customizeParams.pageLimitationM,
  71. pageLimitationL: customizeParams.pageLimitationL,
  72. pageLimitationXL: customizeParams.pageLimitationXL,
  73. isEnabledStaleNotification: customizeParams.isEnabledStaleNotification,
  74. isAllReplyShown: customizeParams.isAllReplyShown,
  75. isSearchScopeChildrenAsDefault: customizeParams.isSearchScopeChildrenAsDefault,
  76. currentHighlightJsStyleId: customizeParams.styleName,
  77. isHighlightJsStyleBorderEnabled: customizeParams.styleBorder,
  78. currentCustomizeTitle: customizeParams.customizeTitle,
  79. currentCustomizeHeader: customizeParams.customizeHeader,
  80. currentCustomizeCss: customizeParams.customizeCss,
  81. currentCustomizeScript: customizeParams.customizeScript,
  82. });
  83. // search style name from object for display
  84. this.setState({ currentHighlightJsStyleName: this.state.highlightJsCssSelectorOptions[customizeParams.styleName].name });
  85. }
  86. catch (err) {
  87. this.setState({ retrieveError: err });
  88. logger.error(err);
  89. throw new Error('Failed to fetch data');
  90. }
  91. }
  92. /**
  93. * Switch enabledTimeLine
  94. */
  95. switchEnableTimeline() {
  96. this.setState({ isEnabledTimeline: !this.state.isEnabledTimeline });
  97. }
  98. /**
  99. * Switch savedStatesOfTabChanges
  100. */
  101. switchSavedStatesOfTabChanges() {
  102. this.setState({ isSavedStatesOfTabChanges: !this.state.isSavedStatesOfTabChanges });
  103. }
  104. /**
  105. * Switch enabledAttachTitleHeader
  106. */
  107. switchEnabledAttachTitleHeader() {
  108. this.setState({ isEnabledAttachTitleHeader: !this.state.isEnabledAttachTitleHeader });
  109. }
  110. /**
  111. * S: Switch pageListLimitationS
  112. */
  113. switchPageListLimitationS(value) {
  114. this.setState({ pageLimitationS: value });
  115. }
  116. /**
  117. * M: Switch pageListLimitationM
  118. */
  119. switchPageListLimitationM(value) {
  120. this.setState({ pageLimitationM: value });
  121. }
  122. /**
  123. * L: Switch pageListLimitationL
  124. */
  125. switchPageListLimitationL(value) {
  126. this.setState({ pageLimitationL: value });
  127. }
  128. /**
  129. * XL: Switch pageListLimitationXL
  130. */
  131. switchPageListLimitationXL(value) {
  132. this.setState({ pageLimitationXL: value });
  133. }
  134. /**
  135. * Switch enabledStaleNotification
  136. */
  137. switchEnableStaleNotification() {
  138. this.setState({ isEnabledStaleNotification: !this.state.isEnabledStaleNotification });
  139. }
  140. /**
  141. * Switch isAllReplyShown
  142. */
  143. switchIsAllReplyShown() {
  144. this.setState({ isAllReplyShown: !this.state.isAllReplyShown });
  145. }
  146. /**
  147. * Switch isSearchScopeChildrenAsDefault
  148. */
  149. switchIsSearchScopeChildrenAsDefault() {
  150. this.setState({ isSearchScopeChildrenAsDefault: !this.state.isSearchScopeChildrenAsDefault });
  151. }
  152. /**
  153. * Switch highlightJsStyle
  154. */
  155. switchHighlightJsStyle(styleId, styleName, isBorderEnable) {
  156. this.setState({ currentHighlightJsStyleId: styleId });
  157. this.setState({ currentHighlightJsStyleName: styleName });
  158. // recommended settings are applied
  159. this.setState({ isHighlightJsStyleBorderEnabled: isBorderEnable });
  160. this.previewHighlightJsStyle(styleId);
  161. }
  162. /**
  163. * Switch highlightJsStyleBorder
  164. */
  165. switchHighlightJsStyleBorder() {
  166. this.setState({ isHighlightJsStyleBorderEnabled: !this.state.isHighlightJsStyleBorderEnabled });
  167. }
  168. /**
  169. * Change customize Title
  170. */
  171. changeCustomizeTitle(inputValue) {
  172. this.setState({ currentCustomizeTitle: inputValue });
  173. }
  174. /**
  175. * Change customize Html header
  176. */
  177. changeCustomizeHeader(inputValue) {
  178. this.setState({ currentCustomizeHeader: inputValue });
  179. }
  180. /**
  181. * Change customize css
  182. */
  183. changeCustomizeCss(inputValue) {
  184. this.setState({ currentCustomizeCss: inputValue });
  185. }
  186. /**
  187. * Change customize script
  188. */
  189. changeCustomizeScript(inpuValue) {
  190. this.setState({ currentCustomizeScript: inpuValue });
  191. }
  192. /**
  193. * Preview hljs style
  194. * @param {string} styleId
  195. */
  196. previewHighlightJsStyle(styleId) {
  197. const styleLInk = document.querySelectorAll('#grw-hljs-container-for-demo link')[0];
  198. // replace css url
  199. // see https://regex101.com/r/gBNZYu/4
  200. styleLInk.href = styleLInk.href.replace(/[^/]+\.css$/, `${styleId}.css`);
  201. }
  202. /**
  203. * Update function
  204. * @memberOf AdminCustomizeContainer
  205. */
  206. async updateCustomizeFunction() {
  207. try {
  208. const response = await apiv3Put('/customize-setting/function', {
  209. isEnabledTimeline: this.state.isEnabledTimeline,
  210. isSavedStatesOfTabChanges: this.state.isSavedStatesOfTabChanges,
  211. isEnabledAttachTitleHeader: this.state.isEnabledAttachTitleHeader,
  212. pageLimitationS: this.state.pageLimitationS,
  213. pageLimitationM: this.state.pageLimitationM,
  214. pageLimitationL: this.state.pageLimitationL,
  215. pageLimitationXL: this.state.pageLimitationXL,
  216. isEnabledStaleNotification: this.state.isEnabledStaleNotification,
  217. isAllReplyShown: this.state.isAllReplyShown,
  218. isSearchScopeChildrenAsDefault: this.state.isSearchScopeChildrenAsDefault,
  219. });
  220. const { customizedParams } = response.data;
  221. this.setState({
  222. isEnabledTimeline: customizedParams.isEnabledTimeline,
  223. isSavedStatesOfTabChanges: customizedParams.isSavedStatesOfTabChanges,
  224. isEnabledAttachTitleHeader: customizedParams.isEnabledAttachTitleHeader,
  225. pageLimitationS: customizedParams.pageLimitationS,
  226. pageLimitationM: customizedParams.pageLimitationM,
  227. pageLimitationL: customizedParams.pageLimitationL,
  228. pageLimitationXL: customizedParams.pageLimitationXL,
  229. isEnabledStaleNotification: customizedParams.isEnabledStaleNotification,
  230. isAllReplyShown: customizedParams.isAllReplyShown,
  231. isSearchScopeChildrenAsDefault: customizedParams.isSearchScopeChildrenAsDefault,
  232. });
  233. }
  234. catch (err) {
  235. logger.error(err);
  236. throw new Error('Failed to update data');
  237. }
  238. }
  239. /**
  240. * Update code highlight
  241. * @memberOf AdminCustomizeContainer
  242. */
  243. async updateHighlightJsStyle() {
  244. try {
  245. const response = await apiv3Put('/customize-setting/highlight', {
  246. highlightJsStyle: this.state.currentHighlightJsStyleId,
  247. highlightJsStyleBorder: this.state.isHighlightJsStyleBorderEnabled,
  248. });
  249. const { customizedParams } = response.data;
  250. this.setState({
  251. highlightJsStyle: customizedParams.highlightJsStyle,
  252. highlightJsStyleBorder: customizedParams.highlightJsStyleBorder,
  253. });
  254. }
  255. catch (err) {
  256. logger.error(err);
  257. throw new Error('Failed to update data');
  258. }
  259. }
  260. /**
  261. * Update customTitle
  262. * @memberOf AdminCustomizeContainer
  263. */
  264. async updateCustomizeTitle() {
  265. try {
  266. const response = await apiv3Put('/customize-setting/customize-title', {
  267. customizeTitle: this.state.currentCustomizeTitle,
  268. });
  269. const { customizedParams } = response.data;
  270. this.setState({
  271. customizeTitle: customizedParams.customizeTitle,
  272. });
  273. }
  274. catch (err) {
  275. logger.error(err);
  276. throw new Error('Failed to update data');
  277. }
  278. }
  279. /**
  280. * Update customHeader
  281. * @memberOf AdminCustomizeContainer
  282. */
  283. async updateCustomizeHeader() {
  284. try {
  285. const response = await apiv3Put('/customize-setting/customize-header', {
  286. customizeHeader: this.state.currentCustomizeHeader,
  287. });
  288. const { customizedParams } = response.data;
  289. this.setState({
  290. currentCustomizeHeader: customizedParams.customizeHeader,
  291. });
  292. }
  293. catch (err) {
  294. logger.error(err);
  295. throw new Error('Failed to update data');
  296. }
  297. }
  298. /**
  299. * Update customCss
  300. * @memberOf AdminCustomizeContainer
  301. */
  302. async updateCustomizeCss() {
  303. try {
  304. const response = await apiv3Put('/customize-setting/customize-css', {
  305. customizeCss: this.state.currentCustomizeCss,
  306. });
  307. const { customizedParams } = response.data;
  308. this.setState({
  309. currentCustomizeCss: customizedParams.customizeCss,
  310. });
  311. }
  312. catch (err) {
  313. logger.error(err);
  314. throw new Error('Failed to update data');
  315. }
  316. }
  317. /**
  318. * Update customize script
  319. * @memberOf AdminCustomizeContainer
  320. * @return {string} Customize scripts
  321. */
  322. async updateCustomizeScript() {
  323. try {
  324. const response = await apiv3Put('/customize-setting/customize-script', {
  325. customizeScript: this.state.currentCustomizeScript,
  326. });
  327. const { customizedParams } = response.data;
  328. this.setState({
  329. currentCustomizeScript: customizedParams.customizeScript,
  330. });
  331. }
  332. catch (err) {
  333. logger.error(err);
  334. throw new Error('Failed to update data');
  335. }
  336. }
  337. }