| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- /**
- * @fileOverview
- *
- * 保存文件到网盘的功能
- *
- * @author: techird
- * @copyright: Baidu FEX, 2014
- */
- KityMinder.registerUI('menu/save/netdisk', function(minder) {
- var $menu = minder.getUI('menu/menu');
- var $save = minder.getUI('menu/save/save');
- var $netdiskfinder = minder.getUI('widget/netdiskfinder');
- var $eve = minder.getUI('eve');
- var $doc = minder.getUI('doc');
- var ret = $eve.setup({});
- var notice = minder.getUI('widget/notice');
- /* extension => protocol */
- var supports = {};
- minder.getSupportedProtocols().forEach(function(protocol) {
- if (protocol.encode && protocol.decode) {
- supports[protocol.fileExtension] = protocol;
- }
- });
- // 删除不稳定两种格式
- delete supports['.mm'];
- delete supports['.xmind'];
- /* 网盘面板 */
- var $panel = $($save.createSub('netdisk', true)).addClass('netdisk-save-panel');
- var $finder = $netdiskfinder.generate($panel, function(file) {
- return supports[file.extension];
- });
- var $selects = $('<div class="netdisk-save-select"></div>')
- .appendTo($panel);
- $('<label>')
- .text(minder.getLang('ui.saveas'))
- .appendTo($selects);
- /* 文件名 */
- var $filename = $('<input>')
- .addClass('fui-widget fui-selectable')
- .attr('type', 'text')
- .attr('placeholder', minder.getLang('ui.filename'))
- .attr('title', minder.getLang('ui.filename'))
- .on('keydown', function(e) {
- if (e.keyCode == 27) $menu.toggle();
- if (e.keyCode == 13) save();
- })
- .appendTo($selects);
- /* 文件格式 */
- var $format = $('<select>')
- .attr('title', minder.getLang('ui.fileformat'))
- .appendTo($selects);
- for (var ext in supports) {
- var protocol = supports[ext];
- if (!protocol.encode) return;
- $('<option>')
- .text(protocol.fileDescription + '(' + protocol.fileExtension + ')')
- .val(ext)
- .appendTo($format);
- }
- $format.val('.km');
- $format.on('change', normalizeFilename);
- /* 保存按钮 */
- var $saveBtn = $('<button></button>')
- .addClass('save-button')
- .text(minder.getLang('ui.save'))
- .click(save)
- .appendTo($selects);
- $menu.on('show', setFilename);
- $finder.on('fileclick', function(file) {
- $finder.select(file.path);
- $filename.val(file.filename);
- });
- ret.quickSave = quickSave;
- window.onbeforeunload = function() {
- var noask = ret.mute || window.location.href.indexOf('noask') > 0;
- if (!$doc.checkSaved(true) && !noask)
- return minder.getLang('ui.unsavedcontent', '* ' + $doc.current().title);
- };
- var autoSaveDuration = minder.getOptions('autoSave');
- if (autoSaveDuration !== false) {
- autoSaveDuration = isNaN(autoSaveDuration) ? 3000 : (autoSaveDuration * 1000);
- autoSave();
- }
- var autoSaveTimer = 0;
- function autoSave() {
- function lazySave(doc) {
- if (doc.saved) return;
- clearTimeout(autoSaveTimer);
- autoSaveTimer = setTimeout(saveCurrent, autoSaveDuration);
- }
- $doc.on('docchange', lazySave);
- }
- // 快速保存
- function quickSave() {
- var doc = $doc.current();
- if (doc.source != 'netdisk' && !$menu.isVisible()) {
- $menu.$tabs.select(2);
- $save.$tabs.select(0);
- return $menu.show();
- } else {
- saveCurrent();
- }
- }
- function saveCurrent() {
- var doc = $doc.current();
- if (doc.source != 'netdisk') return Promise.resolve();
- var $title = minder.getUI('topbar/title').$title;
- $filename.val(doc.title);
- return doSave(doc.path, doc.protocol, doc, $title, 'leaveTheMenu');
- }
- function normalizeFilename() {
- var filename = $filename.val();
- var info = fio.file.anlysisPath(filename);
- var ext = info.extension;
- if (ext != $format.val()) {
- if (ext in supports) {
- $filename.val(info.name + $format.val());
- } else {
- $filename.val(filename + $format.val());
- }
- $filename[0].select();
- }
- return $filename.val();
- }
- function getSaveContext() {
- var filename = normalizeFilename();
- var path = $finder.pwd() + filename;
- var doc = $doc.current();
- var protocol = supports[$format.val()];
- var exist = $finder.select(path); // 目标路径存在
- var match = doc.path == path; // 目标路径正是当前文档
- var duplicated = exist && !match;
- return {
- filename: filename,
- path: path,
- doc: doc,
- protocol: protocol,
- exist: exist,
- match: match,
- duplicated: duplicated
- };
- }
- function save() {
- var ctx = getSaveContext();
- if (ctx.match || !ctx.exist ||
- ctx.duplicated && window.confirm(minder.getLang('ui.overrideconfirm', ctx.filename))) {
- doSave(ctx.path, ctx.protocol.name, ctx.doc, $panel);
- }
- }
- var saving = 0;
- function doSave(path, protocol, doc, $mask, leaveTheMenu, msg) {
- if (saving) return;
- saving = true;
- $doc.lock();
- if ($mask) $mask.addClass('loading');
- function upload(data) {
- return fio.file.write({
- path: path,
- content: data,
- ondup: fio.file.DUP_OVERWRITE
- });
- }
- function finish(file) {
- if (!file.modifyTime) throw new Error('File Save Error');
- if (!leaveTheMenu) {
- $menu.hide();
- }
- doc.path = file.path;
- doc.title = file.filename;
- doc.source = 'netdisk';
- doc.protocol = protocol;
- $doc.save(doc);
- $doc.unlock();
- //notice.info(msg || minder.getLang('ui.save_success', doc.title, file.modifyTime.toLocaleTimeString()));
- setTimeout(function() {
- $finder.list($finder.pwd(), true);
- }, 1499);
- }
- function error(e) {
- notice.error('err_save', e);
- }
- return minder.exportData(protocol).then(upload).then(finish, error).then(function() {
- if ($mask) $mask.removeClass('loading');
- saving = false;
- });
- }
- function setFilename() {
- var doc = $doc.current();
- switch (doc.source) {
- case 'netdisk':
- setFilenameForNetDiskSource(doc);
- break;
- default:
- setFilenameForOtherSource(doc);
- break;
- }
- $filename[0].select();
- }
- function setFilenameInputValue(filename) {
- $filename.val(filename);
- normalizeFilename(filename);
- }
- function setFilenameForNetDiskSource(doc) {
- if (!fio.user.current()) return;
- var path = doc.path;
- var pathInfo = fio.file.anlysisPath(path);
- // 选中当前文件
- if ($finder.pwd() != pathInfo.parentPath) {
- $finder.list(pathInfo.parentPath).then(function() {
- $finder.select(path);
- });
- } else {
- $finder.select(path);
- }
- setFilenameInputValue(pathInfo.filename);
- }
- function setFilenameForOtherSource(doc) {
- setFilenameInputValue(doc.title);
- $finder.select(null);
- }
- return ret;
- });
|