home.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. 'use strcit';
  2. const Fs = require('fs');
  3. const Path = require('path');
  4. const Language = require('../../utils/language');
  5. const Event = require('../../utils/event');
  6. exports.template = Fs.readFileSync(Path.join(__dirname, '../template/home.html'), 'utf-8');
  7. exports.props = [];
  8. exports.data = function () {
  9. return {
  10. state: 'normal',
  11. languages: [],
  12. current: '',
  13. _language: '',
  14. }
  15. };
  16. exports.watch = {
  17. current () {
  18. Event.emit('language-changed', this.current);
  19. }
  20. };
  21. exports.methods = {
  22. _t (key) {
  23. return Editor.T(`i18n.${key}`);
  24. },
  25. _getLanguagePath (language) {
  26. return Path.join('resources/i18n/', `${language}.js`);
  27. },
  28. changeEdit () {
  29. if (this.state === 'edit') {
  30. this.state = 'normal';
  31. } else {
  32. this.state = 'edit';
  33. }
  34. },
  35. changeCreate () {
  36. if (this.state === 'create') {
  37. this.state = 'normal';
  38. this._language = '';
  39. } else {
  40. this.state = 'create';
  41. this._language = '';
  42. }
  43. },
  44. /**
  45. * 创建一个新的语言包
  46. * @param {string} name
  47. */
  48. createLanguage (name) {
  49. // 检查是否不存在
  50. if (!name) {
  51. return alert('创建语言失败 - 名称不能为空');
  52. }
  53. // 检查是否重名
  54. if (this.languages.indexOf(name) !== -1) {
  55. return alert('创建语言失败 - 该语言已经存在');
  56. }
  57. Language.create(name).then(() => {
  58. this.languages.push(name);
  59. if (!this.current) {
  60. this.current = this.languages[0];
  61. }
  62. Event.emit('languages-changed', this.languages);
  63. this._language = '';
  64. this.state = 'normal';
  65. }).catch(() => {
  66. this._language = '';
  67. this.state = 'normal';
  68. // todo 错误提示
  69. });
  70. },
  71. /**
  72. * 删除一个已存在的语言包
  73. * @param {string} name
  74. */
  75. deleteLanguage (name) {
  76. // 检查是否存在
  77. if (this.languages.indexOf(name) === -1) {
  78. return alert('删除语言失败 - 该语言不存在');
  79. }
  80. // 弹窗提示
  81. let code = Editor.Dialog.messageBox({
  82. type: 'warning',
  83. buttons: ['Cancel', 'OK'],
  84. title: 'Delete Language Data',
  85. message: 'Delete i18n language data, this cannot be undone!',
  86. detail: name,
  87. noLink: true
  88. });
  89. if (code === 0) {
  90. return;
  91. }
  92. // 删除 profile
  93. Language.remove(name).then(() => {
  94. let index = this.languages.indexOf(name);
  95. this.languages.splice(index, 1);
  96. Event.emit('languages-changed', this.languages);
  97. if (name === this.current) {
  98. this.current = this.languages[0] || '';
  99. }
  100. }).catch(() => {
  101. // todo 错误提示
  102. });
  103. },
  104. };
  105. exports.ready = function () {};