upgradedoctemplates.ui.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /**
  2. * 升级文档模板内容。
  3. * Upgrade doc template content.
  4. *
  5. * @param int $docID
  6. * @param object $options
  7. * @access public
  8. * @return bool|string
  9. */
  10. async function upgradeTemplateContent(docID, options)
  11. {
  12. const template = await zui.fetchData(options.processUrl, [{docID}]);
  13. if(!template || !template.data) return false;
  14. const oldContent = template.data.content;
  15. const snap = await zui.Editor.htmlToSnap(oldContent);
  16. if(!snap) return false;
  17. let result;
  18. await $.post(zui.formatString(options.processUrl, {docID: docID}), {type: 'html'}, (res) =>
  19. {
  20. result = res.result === 'success' ? true : res.message;
  21. }, 'json');
  22. return result;
  23. }
  24. /**
  25. * 处理wiki类型的文档模板。
  26. * Process templates of wiki.
  27. *
  28. * @param object $idList
  29. * @access public
  30. * @return object
  31. */
  32. async function upgradeWikiTemplates(idList)
  33. {
  34. let result;
  35. await $.post($.createLink('upgrade', 'ajaxUpgradeWikiTemplates'), {wikis: idList.join(',')}, (res) =>
  36. {
  37. result = res.result === 'success' ? true : res.message;
  38. }, 'json');
  39. return result;
  40. }
  41. /**
  42. * 依次处理文档模板内容。
  43. * Process doc template content.
  44. *
  45. * @param object $idList
  46. * @param object $options
  47. * @access public
  48. * @return object
  49. */
  50. async function upgradeDocTemplates(idList, options)
  51. {
  52. await zui.Editor.loadModule();
  53. const {onProgress} = options || {};
  54. let current = 0;
  55. const total = idList.html.length + idList.wiki.length;
  56. for(const id of idList['html'])
  57. {
  58. await upgradeTemplateContent(id, {processUrl: $.createLink('upgrade', 'ajaxUpgradeDocTemplate', 'docID={docID}')});
  59. await zui.delay(50);
  60. current++;
  61. onProgress(current, total);
  62. }
  63. if(idList.wiki.length)
  64. {
  65. await upgradeWikiTemplates(idList.wiki);
  66. current += idList.wiki.length;
  67. }
  68. onProgress(current, total);
  69. return idList;
  70. }
  71. /**
  72. * 开始升级文档模板。
  73. * Start upgrade doc templates.
  74. *
  75. * @param object $event
  76. * @param object $idList
  77. * @param string $upgradingDocTemplatesText
  78. * @param string $nextText
  79. * @access public
  80. * @return void
  81. */
  82. window.startUpgradeDocTemplates = function(event, idList, upgradingDocTemplatesText, nextText)
  83. {
  84. const $btn = $('#upgradeDocTemplatesBtn');
  85. if($btn.hasClass('is-finished')) return;
  86. event.preventDefault();
  87. const $progressBar = $('#upgradeDocTemplatesProgress').addClass('active').find('.progress-bar');
  88. $btn.attr('disabled', 'disabled').addClass('disabled').removeClass('primary').find('.text').text(upgradingDocTemplatesText);
  89. upgradeDocTemplates(idList,
  90. {
  91. onProgress: (current, total) =>
  92. {
  93. $progressBar.css('width', (100 * current / total) + '%');
  94. $btn.find('.text').text(`${upgradingDocTemplatesText} (${current}/${total})`);
  95. }
  96. }).then(() =>
  97. {
  98. $btn.removeAttr('disabled').removeClass('disabled').addClass('primary is-finished').find('.text').text(nextText);
  99. $('#upgradeDocTemplatesProgress').removeClass('active')
  100. });
  101. };