fileloader.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /**
  2. * @fileOverview
  3. *
  4. * 完成加载一个脑图文件的流程
  5. *
  6. * @author: techird
  7. * @copyright: Baidu FEX, 2014
  8. */
  9. KityMinder.registerUI('widget/fileloader', function(minder) {
  10. var doc = minder.getUI('doc');
  11. var $container = $(minder.getRenderTarget());
  12. var supports = minder.getSupportedProtocols();
  13. function getProtocolByExtension(extension) {
  14. for (var protocol in supports) {
  15. if (supports[protocol].fileExtension == extension) return supports[protocol];
  16. }
  17. return false;
  18. }
  19. function load(file) {
  20. var protocol = getProtocolByExtension(file.extension);
  21. return doc.load({
  22. source: file.source,
  23. title: file.filename,
  24. content: file.data.content,
  25. protocol: protocol.name
  26. }).then(function(json) {
  27. $container.removeClass('loading');
  28. return {
  29. file: file,
  30. json: json
  31. };
  32. });
  33. }
  34. function error(err) {
  35. var notice = minder.getUI('widget/notice');
  36. notice.error('err_localfile_read', err);
  37. $container.removeClass('loading');
  38. }
  39. return {
  40. load: function(filePromise, source) {
  41. $container.addClass('loading');
  42. return Promise.resolve(filePromise).then(load)['catch'](error);
  43. },
  44. support: function(file) {
  45. return getProtocolByExtension(file.extension);
  46. }
  47. };
  48. });