doc.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * @fileOverview
  3. *
  4. * 当前文档管理
  5. *
  6. * @author: techird
  7. * @copyright: Baidu FEX, 2014
  8. */
  9. KityMinder.registerUI('doc', function(minder) {
  10. var ret = minder.getUI('eve').setup({});
  11. var current = { saved: true };
  12. var loading = false;
  13. var notice = minder.getUI('widget/notice');
  14. var finder = minder.getUI('widget/netdiskfinder');
  15. if (finder) finder.on('mv', trackFileMove);
  16. function trackFileMove(from, to) {
  17. if (current.source != 'netdisk') return;
  18. var fromPath = from.split('/');
  19. var toPath = to.split('/');
  20. function preCommonLength(a, b) {
  21. var i = 0;
  22. while ((i in a) && (i in b) && a[i] == b[i]) i++;
  23. return (i in b) ? 0 : i;
  24. }
  25. var originPath = current.path.split('/');
  26. var clen = preCommonLength(originPath, fromPath);
  27. if (clen) {
  28. var movedPath = toPath.concat(originPath.slice(clen));
  29. current.path = movedPath.join('/');
  30. current.title = movedPath.pop();
  31. ret.fire('docchange', current);
  32. }
  33. }
  34. var locked = false;
  35. ret.lock = function() { locked = true; };
  36. ret.unlock = function() { locked = false; };
  37. /**
  38. * 加载文档
  39. *
  40. * @param {Object} doc 文档的属性,可包括:
  41. * doc.content {string} [Required] 文档内容
  42. * doc.protocol {string} [Required] 内容所使用的编码协议
  43. * doc.title {string} 文档的标题
  44. * doc.source {string} 文档的来源
  45. * doc.path {string} 文档的路径
  46. * doc.saved {bool} 文档的保存状态
  47. *
  48. * @event docload(doc)
  49. * doc - 文档解析之后的文档对象
  50. *
  51. * @return {Promise<doc>} 返回解析完之后的文档对象,解析的结果为 doc.data
  52. */
  53. function load(doc) {
  54. if (locked) return Promise.reject(new Error('doc was locked'));
  55. var restore = doc;
  56. current = doc;
  57. loading = true;
  58. return minder.importData(doc.content, doc.protocol).then(function(data) {
  59. doc.title = doc.title || minder.getMinderTitle();
  60. minder.execCommand('camera', minder.getRoot(), 300);
  61. doc.data = data;
  62. doc.json = JSON.stringify(data);
  63. ret.fire('docload', doc);
  64. ret.fire('docchange', doc);
  65. return doc;
  66. })['catch'](function(e) {
  67. current = restore;
  68. notice.error('err_doc_resolve', e);
  69. }).then(function(doc) {
  70. loading = false;
  71. if (doc)
  72. notice.info(minder.getLang('ui.load_success', doc.title));
  73. return doc;
  74. });
  75. }
  76. function save(doc) {
  77. current = doc;
  78. doc.data = minder.exportJson();
  79. doc.json = JSON.stringify(doc.data);
  80. doc.saved = true;
  81. ret.fire('docsave', doc);
  82. ret.fire('docchange', doc);
  83. }
  84. function getCurrent() {
  85. return current;
  86. }
  87. function checkSaved(noConfirm) {
  88. if (!fio.user.current()) return true;
  89. if (locked) return false;
  90. if (noConfirm) return current.saved;
  91. return current.saved || window.confirm(minder.getLang('ui.unsavedcontent', '* ' + current.title));
  92. }
  93. /* 绕开初始化时候的乱事件 */
  94. setTimeout(function() {
  95. minder.on('contentchange', function() {
  96. if (loading) return;
  97. if (current.source != 'netdisk') {
  98. current.title = minder.getMinderTitle();
  99. current.saved = false;
  100. } else {
  101. current.saved = current.json == JSON.stringify(minder.exportJson());
  102. }
  103. ret.fire('docchange', current);
  104. });
  105. }, 1000);
  106. ret.load = load;
  107. ret.save = save;
  108. ret.current = getCurrent;
  109. ret.checkSaved = checkSaved;
  110. return ret;
  111. });