dnd.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. exports.install = function (Vue, options) {
  3. var dropTo = '';
  4. var _ = Vue.util;
  5. Vue.directive('draggable', {
  6. bind: function () {
  7. this.data = {};
  8. var _this = this;
  9. this.dragstart = function (event) {
  10. dropTo = _this.arg;
  11. event.target.classList.add(_this.data.dragged);
  12. event.dataTransfer.effectAllowed = 'all';
  13. event.dataTransfer.setData('data', JSON.stringify(_this.data));
  14. event.dataTransfer.setData('tag', _this.arg);
  15. return false;
  16. };
  17. this.dragend = function (event) {
  18. event.target.classList.remove(_this.data.dragged);
  19. return false;
  20. };
  21. this.el.setAttribute('draggable', true);
  22. _.on(this.el, 'dragstart', this.dragstart);
  23. _.on(this.el, 'dragend', this.dragend);
  24. },
  25. update: function (value, old) {
  26. this.data = value;
  27. },
  28. unbind: function () {
  29. this.el.setAttribute('draggable', false);
  30. _.off(this.el, 'dragstart', this.dragstart);
  31. _.off(this.el, 'dragend', this.dragend);
  32. }
  33. });
  34. Vue.directive('dropzone', {
  35. acceptStatement: true,
  36. bind: function () {
  37. var self = this;
  38. this.dragenter = function (event) {
  39. if (dropTo == self.arg) {
  40. event.target.classList.add(self.arg);
  41. }
  42. return false;
  43. };
  44. this.dragover = function (event) {
  45. if (event.preventDefault) {
  46. event.preventDefault();
  47. }
  48. // XXX
  49. if (dropTo == self.arg) {
  50. event.dataTransfer.effectAllowed = 'all';
  51. event.dataTransfer.dropEffect = 'copy';
  52. } else {
  53. event.dataTransfer.effectAllowed = 'none';
  54. event.dataTransfer.dropEffect = 'none';
  55. }
  56. return false;
  57. };
  58. this.dragleave = function (event) {
  59. if (dropTo == self.arg) {
  60. event.target.classList.remove(self.arg);
  61. }
  62. return false;
  63. };
  64. this.drop = function (event) {
  65. if (event.preventDefault) {
  66. event.preventDefault();
  67. }
  68. var tag = event.dataTransfer.getData('tag');
  69. var data = event.dataTransfer.getData('data');
  70. if (dropTo == self.arg) {
  71. self.handler(tag, JSON.parse(data));
  72. event.target.classList.remove(self.arg);
  73. }
  74. return false;
  75. };
  76. _.on(this.el, 'dragenter', this.dragenter);
  77. _.on(this.el, 'dragleave', this.dragleave);
  78. _.on(this.el, 'dragover', this.dragover);
  79. _.on(this.el, 'drop', this.drop);
  80. },
  81. update: function (value, old) {
  82. var vm = this.vm;
  83. this.handler = function (tag, data) {
  84. vm.$droptag = tag;
  85. vm.$dropdata = data;
  86. var res = value(tag, data);
  87. vm.$droptag = null;
  88. vm.$dropdata = null;
  89. return res;
  90. };
  91. },
  92. unbind: function () {
  93. _.off(this.el, 'dragenter', this.dragenter);
  94. _.off(this.el, 'dragleave', this.dragleave);
  95. _.off(this.el, 'dragover', this.dragover);
  96. _.off(this.el, 'drop', this.drop);
  97. }
  98. });
  99. };