Просмотр исходного кода

reintroduce forgotten dropAccept. automated tests

Adam Shaw 11 лет назад
Родитель
Сommit
42d0ef948f
2 измененных файлов с 197 добавлено и 22 удалено
  1. 29 20
      src/common/View.js
  2. 168 2
      tests/automated/external-dnd.js

+ 29 - 20
src/common/View.js

@@ -262,31 +262,40 @@ View.prototype = {
 	documentDragStart: function(ev, ui) {
 		var _this = this;
 		var dropDate = null;
+		var el;
+		var accept;
 		var dragListener;
 
 		if (this.opt('droppable')) { // only listen if this setting is on
+			el = $(ev.target);
+
+			// Test that the dragged element passes the dropAccept selector or filter function.
+			// FYI, the default is "*" (matches all)
+			accept = this.opt('dropAccept');
+			if ($.isFunction(accept) ? accept.call(el[0], el) : el.is(accept)) {
+
+				// listener that tracks mouse movement over date-associated pixel regions
+				dragListener = new DragListener(this.coordMap, {
+					cellOver: function(cell, date) {
+						dropDate = date;
+						_this.renderDrag(date);
+					},
+					cellOut: function() {
+						dropDate = null;
+						_this.destroyDrag();
+					}
+				});
 
-			// listener that tracks mouse movement over date-associated pixel regions
-			dragListener = new DragListener(this.coordMap, {
-				cellOver: function(cell, date) {
-					dropDate = date;
-					_this.renderDrag(date);
-				},
-				cellOut: function() {
-					dropDate = null;
+				// gets called, only once, when jqui drag is finished
+				$(document).one('dragstop', function(ev, ui) {
 					_this.destroyDrag();
-				}
-			});
-
-			// gets called, only once, when jqui drag is finished
-			$(document).one('dragstop', function(ev, ui) {
-				_this.destroyDrag();
-				if (dropDate) {
-					_this.trigger('drop', ev.target, dropDate, ev, ui);
-				}
-			});
-
-			dragListener.startDrag(ev); // start listening immediately
+					if (dropDate) {
+						_this.trigger('drop', el[0], dropDate, ev, ui);
+					}
+				});
+
+				dragListener.startDrag(ev); // start listening immediately
+			}
 		}
 	},
 

+ 168 - 2
tests/automated/external-dnd.js

@@ -1,7 +1,7 @@
 
 describe('external drag and drop', function() {
 
-	// TODO: fill out tests for droppable/drop/dropAccept, with RTL
+	// TODO: fill out tests for droppable/drop, with RTL
 
 	var options;
 
@@ -66,6 +66,89 @@ describe('external drag and drop', function() {
 				});
 			}, 0);
 		});
+
+		describe('dropAccept', function() {
+
+			it('works with a className that does match', function(done) {
+				options.dropAccept = '.event1';
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: getMonthCell(1, 3),
+						callback: function() {
+							expect(options.drop).toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+
+			it('prevents a classNames that doesn\'t match', function(done) {
+				options.dropAccept = '.event2';
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: getMonthCell(1, 3),
+						callback: function() {
+							expect(options.drop).not.toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+
+			it('works with a filter function that returns true', function(done) {
+				options.dropAccept = function(jqEl) {
+					expect(typeof jqEl).toBe('object');
+					expect(jqEl.length).toBe(1);
+					return true;
+				};
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: getMonthCell(1, 3),
+						callback: function() {
+							expect(options.drop).toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+
+			it('prevents a drop with a filter function that returns false', function(done) {
+				options.dropAccept = function(jqEl) {
+					expect(typeof jqEl).toBe('object');
+					expect(jqEl.length).toBe(1);
+					return false;
+				};
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: getMonthCell(1, 3),
+						callback: function() {
+							expect(options.drop).not.toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+		});
 	});
 
 	describe('in agenda view', function() {
@@ -129,10 +212,93 @@ describe('external drag and drop', function() {
 			$('#cal').fullCalendar(options);
 			setTimeout(function() { // needed for IE8
 				$('#sidebar .event1').simulate('drag-n-drop', {
-					dropTarget: $('.fc-slats tr:eq(2)') // middle is 1:00am on 2014-08-20, LOCAL TIME
+					dropTarget: $('.fc-slats tr:eq(2)') // middle is 1:00am on 2014-08-20, UTC TIME
 				});
 			}, 0);
 		});
+
+		describe('dropAccept', function() {
+
+			it('works with a className that does match', function(done) {
+				options.dropAccept = '.event1';
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: $('.fc-slats tr:eq(2)'),
+						callback: function() {
+							expect(options.drop).toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+
+			it('prevents a classNames that doesn\'t match', function(done) {
+				options.dropAccept = '.event2';
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: $('.fc-slats tr:eq(2)'),
+						callback: function() {
+							expect(options.drop).not.toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+
+			it('works with a filter function that returns true', function(done) {
+				options.dropAccept = function(jqEl) {
+					expect(typeof jqEl).toBe('object');
+					expect(jqEl.length).toBe(1);
+					return true;
+				};
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: $('.fc-slats tr:eq(2)'),
+						callback: function() {
+							expect(options.drop).toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+
+			it('prevents a drop with a filter function that returns false', function(done) {
+				options.dropAccept = function(jqEl) {
+					expect(typeof jqEl).toBe('object');
+					expect(jqEl.length).toBe(1);
+					return false;
+				};
+				options.drop = function() { };
+				spyOn(options, 'drop').and.callThrough();
+
+				$('#cal').fullCalendar(options);
+
+				setTimeout(function() { // needed for IE8
+					$('#sidebar .event1').simulate('drag-n-drop', {
+						dropTarget: $('.fc-slats tr:eq(2)'),
+						callback: function() {
+							expect(options.drop).not.toHaveBeenCalled();
+							done();
+						}
+					});
+				}, 0);
+			});
+		});
 	});
 
 });