1
0

edit.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. define('views/Edit', function (require, _exports, module) {
  2. function fromList(list) {
  3. return (list || []).join('\n');
  4. }
  5. function toList(text) {
  6. return text.split('\n')
  7. .map(function (line) {
  8. return line.trim();
  9. })
  10. .filter(function (item) {
  11. return item;
  12. });
  13. }
  14. var Message = require('views/Message');
  15. var Editor = require('views/Editor');
  16. var cache = require('cache');
  17. module.exports = {
  18. props: {
  19. script: {
  20. twoWay: true,
  21. },
  22. },
  23. template: cache.get('/options/components/edit.html'),
  24. components: {
  25. Editor: Editor,
  26. },
  27. data: function () {
  28. return {
  29. canSave: false,
  30. update: false,
  31. code: '',
  32. custom: {},
  33. };
  34. },
  35. computed: {
  36. placeholders: function () {
  37. var script = this.script;
  38. return {
  39. name: script.meta.name,
  40. homepageURL: script.meta.homepageURL,
  41. updateURL: script.meta.updateURL || _.i18n('hintUseDownloadURL'),
  42. downloadURL: script.meta.downloadURL || script.lastInstallURL,
  43. };
  44. },
  45. },
  46. watch: {
  47. code: function () {
  48. this.canSave = true;
  49. },
  50. },
  51. ready: function () {
  52. var _this = this;
  53. (_this.script.id ? _.sendMessage({
  54. cmd: 'GetScript',
  55. data: _this.script.id,
  56. }) : Promise.resolve(_this.script))
  57. .then(function (script) {
  58. _this.update = script.update;
  59. _this.code = script.code;
  60. var custom = Object.assign({}, script.custom);
  61. custom.keepInclude = custom._include;
  62. custom.keepMatch = custom._match;
  63. custom.keepExclude = custom._exclude;
  64. custom.include = fromList(custom.include);
  65. custom.match = fromList(custom.match);
  66. custom.exclude = fromList(custom.exclude);
  67. _this.custom = custom;
  68. _this.$nextTick(function () {
  69. _this.canSave = false;
  70. });
  71. });
  72. },
  73. methods: {
  74. save: function () {
  75. var _this = this;
  76. var custom = _this.custom;
  77. var value = [
  78. 'name',
  79. 'run-at',
  80. 'homepageURL',
  81. 'updateURL',
  82. 'downloadURL',
  83. ].reduce(function (value, key) {
  84. value[key] = custom[key];
  85. return value;
  86. }, {
  87. _include: custom.keepInclude,
  88. _match: custom.keepMatch,
  89. _exclude: custom.keepExclude,
  90. include: toList(custom.include),
  91. match: toList(custom.match),
  92. exclude: toList(custom.exclude),
  93. });
  94. return _.sendMessage({
  95. cmd: 'ParseScript',
  96. data: {
  97. id: _this.script.id,
  98. code: _this.code,
  99. // User created scripts MUST be marked `isNew` so that
  100. // the backend is able to check namespace conflicts
  101. isNew: !_this.script.id,
  102. message: '',
  103. more: {
  104. custom: value,
  105. update: _this.update,
  106. },
  107. },
  108. })
  109. .then(function (script) {
  110. _this.script = script;
  111. _this.canSave = false;
  112. }, function (err) {
  113. new Message({text: err});
  114. });
  115. },
  116. close: function () {
  117. var _this = this;
  118. if (!_this.canSave || confirm(_.i18n('confirmNotSaved'))) {
  119. _this.script = null;
  120. }
  121. },
  122. saveClose: function () {
  123. var _this = this;
  124. _this.save().then(function () {
  125. _this.close();
  126. });
  127. },
  128. },
  129. };
  130. });