simditor-dropzone.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. (function (root, factory) {
  2. if (typeof define === 'function' && define.amd) {
  3. // AMD. Register as an anonymous module.
  4. define(["jquery",
  5. "simple-module",
  6. "simditor"], function ($, SimpleModule) {
  7. return (root.returnExportsGlobal = factory($, SimpleModule));
  8. });
  9. } else if (typeof exports === 'object') {
  10. // Node. Does not work with strict CommonJS, but
  11. // only CommonJS-like enviroments that support module.exports,
  12. // like Node.
  13. module.exports = factory(require("jquery"),
  14. require("simple-module"),
  15. require("simditor"));
  16. } else {
  17. root['Simditor'] = factory(jQuery,
  18. SimpleModule);
  19. }
  20. }(this, function ($, SimpleModule) {
  21. var Dropzone,
  22. __hasProp = {}.hasOwnProperty,
  23. __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
  24. Dropzone = (function(_super) {
  25. __extends(Dropzone, _super);
  26. function Dropzone() {
  27. return Dropzone.__super__.constructor.apply(this, arguments);
  28. }
  29. Dropzone.pluginName = "Dropzone";
  30. Dropzone.prototype._entered = 0;
  31. Dropzone.prototype._init = function() {
  32. this.editor = this._module;
  33. if (this.editor.uploader == null) {
  34. throw new Error("Can't work without 'simple-uploader' module");
  35. return;
  36. }
  37. $(document.body).on("dragover", function(e) {
  38. e.originalEvent.dataTransfer.dropEffect = "none";
  39. return e.preventDefault();
  40. });
  41. $(document.body).on('drop', function(e) {
  42. return e.preventDefault();
  43. });
  44. this.imageBtn = this.editor.toolbar.findButton("image");
  45. return this.editor.body.on("dragover", function(e) {
  46. e.originalEvent.dataTransfer.dropEffect = "copy";
  47. e.stopPropagation();
  48. return e.preventDefault();
  49. }).on("dragenter", (function(_this) {
  50. return function(e) {
  51. if ((_this._entered += 1) === 1) {
  52. _this.show();
  53. }
  54. e.preventDefault();
  55. return e.stopPropagation();
  56. };
  57. })(this)).on("dragleave", (function(_this) {
  58. return function(e) {
  59. if ((_this._entered -= 1) <= 0) {
  60. _this.hide();
  61. }
  62. e.preventDefault();
  63. return e.stopPropagation();
  64. };
  65. })(this)).on("drop", (function(_this) {
  66. return function(e) {
  67. var file, imageFiles, _i, _j, _len, _len1, _ref;
  68. imageFiles = [];
  69. _ref = e.originalEvent.dataTransfer.files;
  70. for (_i = 0, _len = _ref.length; _i < _len; _i++) {
  71. file = _ref[_i];
  72. if (!_this.validFile(file)) {
  73. alert("「" + file.name + "]」文件不是图片。");
  74. _this.hide();
  75. return false;
  76. }
  77. imageFiles.push(file);
  78. }
  79. for (_j = 0, _len1 = imageFiles.length; _j < _len1; _j++) {
  80. file = imageFiles[_j];
  81. _this.editor.uploader.upload(file, {
  82. inline: true
  83. });
  84. }
  85. _this.hide();
  86. e.stopPropagation();
  87. return e.preventDefault();
  88. };
  89. })(this));
  90. };
  91. Dropzone.prototype.show = function() {
  92. return this.imageBtn.setActive(true);
  93. };
  94. Dropzone.prototype.hide = function() {
  95. this.imageBtn.setActive(false);
  96. return this._entered = 0;
  97. };
  98. Dropzone.prototype.validFile = function(file) {
  99. return file.type.indexOf("image/") > -1;
  100. };
  101. return Dropzone;
  102. })(SimpleModule);
  103. Simditor.connect(Dropzone);
  104. return Simditor;
  105. }));