theme.tpl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <style lang="scss">
  2. .page-theme {
  3. &:last-child {
  4. margin-bottom: 55px;
  5. }
  6. h2 {
  7. font-size: 28px;
  8. line-height: 28px;
  9. margin: 0;
  10. }
  11. ul {
  12. list-style: none;
  13. padding: 0;
  14. margin: 0;
  15. display: flex;
  16. flex-wrap: wrap;
  17. justify-content: space-between;
  18. }
  19. .theme-card {
  20. display: inline-block;
  21. height: 150px;
  22. height: 16vw;
  23. max-height: 230px;
  24. flex: 0 0 24%;
  25. cursor: default;
  26. vertical-align: bottom;
  27. }
  28. .theme-section {
  29. margin-bottom: 20px;
  30. }
  31. .second-section {
  32. margin-top: 60px;
  33. }
  34. }
  35. </style>
  36. <template>
  37. <div class="page-container page-theme">
  38. <section class="theme-section">
  39. <h2><%= 1 ></h2>
  40. <ul>
  41. <li class="theme-card" v-for="item in officialTheme" :key="item.name">
  42. <theme-card
  43. type="official"
  44. :config="item"
  45. @action="onAction"
  46. ></theme-card>
  47. </li>
  48. </ul>
  49. </section>
  50. <section class="theme-section second-section">
  51. <h2><%= 2 > ({{userThemeCount}}/{{maxUserTheme}})</h2>
  52. <ul>
  53. <li class="theme-card" v-if="showUserUpload">
  54. <theme-card
  55. type="upload"
  56. :config="{name: 'upload'}"
  57. @action="onAction"
  58. ></theme-card>
  59. </li>
  60. <li class="theme-card" v-for="item in displayUserTheme" :key="item.name">
  61. <theme-card
  62. type="user"
  63. :config="item"
  64. @action="onAction"
  65. ></theme-card>
  66. </li>
  67. </ul>
  68. </section>
  69. <el-dialog :visible.sync="copyDialogVisible">
  70. <el-form :model="copyForm" ref="copyForm" :rules="copyFormRule">
  71. <el-form-item label="<%= 3 >" prop="name">
  72. <el-input v-model="copyForm.name"></el-input>
  73. </el-form-item>
  74. </el-form>
  75. <div slot="footer" class="dialog-footer">
  76. <el-button @click="closeCopyForm">{{getActionDisplayName('cancel')}}</el-button>
  77. <el-button type="primary" @click="copyToUser">{{getActionDisplayName('confirm')}}</el-button>
  78. </div>
  79. </el-dialog>
  80. </div>
  81. </template>
  82. <script>
  83. import ThemeCard from '../../components/theme/theme-card.vue';
  84. import ThemeList from '../../components/theme/theme-list.js';
  85. import { saveUserThemeToLocal, loadUserThemeFromLocal } from '../../components/theme/localstorage';
  86. import { getActionDisplayName } from '../../components/theme-configurator/utils/utils';
  87. const maxUserTheme = 8;
  88. export default {
  89. components: {
  90. ThemeCard
  91. },
  92. mounted() {
  93. this.userTheme = loadUserThemeFromLocal();
  94. if (!Array.isArray(this.userTheme)) {
  95. this.userTheme = [];
  96. saveUserThemeToLocal(this.userTheme);
  97. }
  98. },
  99. data() {
  100. return {
  101. officialTheme: this.padEmpeyTheme(ThemeList),
  102. userTheme: [],
  103. maxUserTheme,
  104. copyDialogVisible: false,
  105. copyForm: {},
  106. copyFormRule: {
  107. name: [{
  108. validator: this.validateCopyName,
  109. trigger: 'blur'
  110. }]
  111. }
  112. };
  113. },
  114. computed: {
  115. userThemeCount() {
  116. return this.userTheme.length;
  117. },
  118. showUserUpload() {
  119. return this.userThemeCount < maxUserTheme;
  120. },
  121. displayUserTheme() {
  122. return this.padEmpeyTheme(this.userTheme, this.showUserUpload ? 1 : 0);
  123. }
  124. },
  125. methods: {
  126. getActionDisplayName(key) {
  127. return getActionDisplayName(key);
  128. },
  129. validateCopyName(rule, value, callback) {
  130. if (!value) {
  131. callback(new Error(this.getActionDisplayName('require-them-name')));
  132. } else if (this.filterUserThemeByName(value).length > 0) {
  133. callback(new Error(this.getActionDisplayName('duplicate-them-name')));
  134. } else {
  135. callback();
  136. }
  137. },
  138. filterUserThemeByName(name, include = true) {
  139. return this.userTheme.filter((theme) => (include ? theme.name === name : theme.name !== name));
  140. },
  141. padEmpeyTheme(theme, add = 0) {
  142. if (!theme.length) return [];
  143. const pad = 4 - ((theme.length + add) % 4);
  144. if (pad < 4) return theme.concat(Array(pad).fill({}));
  145. return theme;
  146. },
  147. onAction(name, item) {
  148. switch (name) {
  149. case 'copy':
  150. this.openCopyForm(item.theme);
  151. break;
  152. case 'upload':
  153. this.openCopyForm(item);
  154. break;
  155. case 'rename':
  156. this.openRenameForm(item.name);
  157. break;
  158. case 'delete':
  159. this.$confirm(this.getActionDisplayName('confirm-delete-theme'), this.getActionDisplayName('notice'), {
  160. confirmButtonText: this.getActionDisplayName('confirm'),
  161. cancelButtonText: this.getActionDisplayName('cancel'),
  162. type: 'warning'
  163. }).then(() => {
  164. this.deleteUserThemeByName(item.name);
  165. }).catch(() => {});
  166. break;
  167. default:
  168. return;
  169. }
  170. },
  171. deleteUserThemeByName(name) {
  172. this.userTheme = this.filterUserThemeByName(name, false);
  173. this.saveToLocal();
  174. },
  175. openRenameForm(name) {
  176. this.copyForm.oldname = name;
  177. this.copyDialogVisible = true;
  178. },
  179. openCopyForm(theme) {
  180. if (this.userTheme.length >= 8) {
  181. this.$message.error(this.getActionDisplayName('max-user-theme'));
  182. return;
  183. }
  184. this.copyForm.theme = theme;
  185. this.copyDialogVisible = true;
  186. },
  187. closeCopyForm() {
  188. this.copyDialogVisible = false;
  189. this.$nextTick(() => {
  190. this.copyForm = {};
  191. });
  192. },
  193. copyToUser() {
  194. this.$refs.copyForm.validate((valid) => {
  195. if (valid) {
  196. const { theme, name, oldname } = this.copyForm;
  197. if (theme) {
  198. // copy
  199. this.userTheme.push({
  200. update: Date.now(),
  201. name,
  202. theme
  203. });
  204. } else {
  205. // rename
  206. this.userTheme.forEach((config) => {
  207. if (config.name === oldname) {
  208. config.update = Date.now();
  209. config.name = name;
  210. }
  211. });
  212. }
  213. this.saveToLocal();
  214. this.closeCopyForm();
  215. }
  216. });
  217. },
  218. saveToLocal() {
  219. saveUserThemeToLocal(this.userTheme);
  220. }
  221. }
  222. };
  223. </script>