netdisk.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /**
  2. * @fileOverview
  3. *
  4. * 保存文件到网盘的功能
  5. *
  6. * @author: techird
  7. * @copyright: Baidu FEX, 2014
  8. */
  9. KityMinder.registerUI('menu/save/netdisk', function(minder) {
  10. var $menu = minder.getUI('menu/menu');
  11. var $save = minder.getUI('menu/save/save');
  12. var $netdiskfinder = minder.getUI('widget/netdiskfinder');
  13. var $eve = minder.getUI('eve');
  14. var $doc = minder.getUI('doc');
  15. var ret = $eve.setup({});
  16. var notice = minder.getUI('widget/notice');
  17. /* extension => protocol */
  18. var supports = {};
  19. minder.getSupportedProtocols().forEach(function(protocol) {
  20. if (protocol.encode && protocol.decode) {
  21. supports[protocol.fileExtension] = protocol;
  22. }
  23. });
  24. // 删除不稳定两种格式
  25. delete supports['.mm'];
  26. delete supports['.xmind'];
  27. /* 网盘面板 */
  28. var $panel = $($save.createSub('netdisk', true)).addClass('netdisk-save-panel');
  29. var $finder = $netdiskfinder.generate($panel, function(file) {
  30. return supports[file.extension];
  31. });
  32. var $selects = $('<div class="netdisk-save-select"></div>')
  33. .appendTo($panel);
  34. $('<label>')
  35. .text(minder.getLang('ui.saveas'))
  36. .appendTo($selects);
  37. /* 文件名 */
  38. var $filename = $('<input>')
  39. .addClass('fui-widget fui-selectable')
  40. .attr('type', 'text')
  41. .attr('placeholder', minder.getLang('ui.filename'))
  42. .attr('title', minder.getLang('ui.filename'))
  43. .on('keydown', function(e) {
  44. if (e.keyCode == 27) $menu.toggle();
  45. if (e.keyCode == 13) save();
  46. })
  47. .appendTo($selects);
  48. /* 文件格式 */
  49. var $format = $('<select>')
  50. .attr('title', minder.getLang('ui.fileformat'))
  51. .appendTo($selects);
  52. for (var ext in supports) {
  53. var protocol = supports[ext];
  54. if (!protocol.encode) return;
  55. $('<option>')
  56. .text(protocol.fileDescription + '(' + protocol.fileExtension + ')')
  57. .val(ext)
  58. .appendTo($format);
  59. }
  60. $format.val('.km');
  61. $format.on('change', normalizeFilename);
  62. /* 保存按钮 */
  63. var $saveBtn = $('<button></button>')
  64. .addClass('save-button')
  65. .text(minder.getLang('ui.save'))
  66. .click(save)
  67. .appendTo($selects);
  68. $menu.on('show', setFilename);
  69. $finder.on('fileclick', function(file) {
  70. $finder.select(file.path);
  71. $filename.val(file.filename);
  72. });
  73. ret.quickSave = quickSave;
  74. window.onbeforeunload = function() {
  75. var noask = ret.mute || window.location.href.indexOf('noask') > 0;
  76. if (!$doc.checkSaved(true) && !noask)
  77. return minder.getLang('ui.unsavedcontent', '* ' + $doc.current().title);
  78. };
  79. var autoSaveDuration = minder.getOptions('autoSave');
  80. if (autoSaveDuration !== false) {
  81. autoSaveDuration = isNaN(autoSaveDuration) ? 3000 : (autoSaveDuration * 1000);
  82. autoSave();
  83. }
  84. var autoSaveTimer = 0;
  85. function autoSave() {
  86. function lazySave(doc) {
  87. if (doc.saved) return;
  88. clearTimeout(autoSaveTimer);
  89. autoSaveTimer = setTimeout(saveCurrent, autoSaveDuration);
  90. }
  91. $doc.on('docchange', lazySave);
  92. }
  93. // 快速保存
  94. function quickSave() {
  95. var doc = $doc.current();
  96. if (doc.source != 'netdisk' && !$menu.isVisible()) {
  97. $menu.$tabs.select(2);
  98. $save.$tabs.select(0);
  99. return $menu.show();
  100. } else {
  101. saveCurrent();
  102. }
  103. }
  104. function saveCurrent() {
  105. var doc = $doc.current();
  106. if (doc.source != 'netdisk') return Promise.resolve();
  107. var $title = minder.getUI('topbar/title').$title;
  108. $filename.val(doc.title);
  109. return doSave(doc.path, doc.protocol, doc, $title, 'leaveTheMenu');
  110. }
  111. function normalizeFilename() {
  112. var filename = $filename.val();
  113. var info = fio.file.anlysisPath(filename);
  114. var ext = info.extension;
  115. if (ext != $format.val()) {
  116. if (ext in supports) {
  117. $filename.val(info.name + $format.val());
  118. } else {
  119. $filename.val(filename + $format.val());
  120. }
  121. $filename[0].select();
  122. }
  123. return $filename.val();
  124. }
  125. function getSaveContext() {
  126. var filename = normalizeFilename();
  127. var path = $finder.pwd() + filename;
  128. var doc = $doc.current();
  129. var protocol = supports[$format.val()];
  130. var exist = $finder.select(path); // 目标路径存在
  131. var match = doc.path == path; // 目标路径正是当前文档
  132. var duplicated = exist && !match;
  133. return {
  134. filename: filename,
  135. path: path,
  136. doc: doc,
  137. protocol: protocol,
  138. exist: exist,
  139. match: match,
  140. duplicated: duplicated
  141. };
  142. }
  143. function save() {
  144. var ctx = getSaveContext();
  145. if (ctx.match || !ctx.exist ||
  146. ctx.duplicated && window.confirm(minder.getLang('ui.overrideconfirm', ctx.filename))) {
  147. doSave(ctx.path, ctx.protocol.name, ctx.doc, $panel);
  148. }
  149. }
  150. var saving = 0;
  151. function doSave(path, protocol, doc, $mask, leaveTheMenu, msg) {
  152. if (saving) return;
  153. saving = true;
  154. $doc.lock();
  155. if ($mask) $mask.addClass('loading');
  156. function upload(data) {
  157. return fio.file.write({
  158. path: path,
  159. content: data,
  160. ondup: fio.file.DUP_OVERWRITE
  161. });
  162. }
  163. function finish(file) {
  164. if (!file.modifyTime) throw new Error('File Save Error');
  165. if (!leaveTheMenu) {
  166. $menu.hide();
  167. }
  168. doc.path = file.path;
  169. doc.title = file.filename;
  170. doc.source = 'netdisk';
  171. doc.protocol = protocol;
  172. $doc.save(doc);
  173. $doc.unlock();
  174. //notice.info(msg || minder.getLang('ui.save_success', doc.title, file.modifyTime.toLocaleTimeString()));
  175. setTimeout(function() {
  176. $finder.list($finder.pwd(), true);
  177. }, 1499);
  178. }
  179. function error(e) {
  180. notice.error('err_save', e);
  181. }
  182. return minder.exportData(protocol).then(upload).then(finish, error).then(function() {
  183. if ($mask) $mask.removeClass('loading');
  184. saving = false;
  185. });
  186. }
  187. function setFilename() {
  188. var doc = $doc.current();
  189. switch (doc.source) {
  190. case 'netdisk':
  191. setFilenameForNetDiskSource(doc);
  192. break;
  193. default:
  194. setFilenameForOtherSource(doc);
  195. break;
  196. }
  197. $filename[0].select();
  198. }
  199. function setFilenameInputValue(filename) {
  200. $filename.val(filename);
  201. normalizeFilename(filename);
  202. }
  203. function setFilenameForNetDiskSource(doc) {
  204. if (!fio.user.current()) return;
  205. var path = doc.path;
  206. var pathInfo = fio.file.anlysisPath(path);
  207. // 选中当前文件
  208. if ($finder.pwd() != pathInfo.parentPath) {
  209. $finder.list(pathInfo.parentPath).then(function() {
  210. $finder.select(path);
  211. });
  212. } else {
  213. $finder.select(path);
  214. }
  215. setFilenameInputValue(pathInfo.filename);
  216. }
  217. function setFilenameForOtherSource(doc) {
  218. setFilenameInputValue(doc.title);
  219. $finder.select(null);
  220. }
  221. return ret;
  222. });