Skip to content

Commit 942208d

Browse files
author
Lukas Oppermann
committed
0.8.1
1 parent 41b959d commit 942208d

8 files changed

+373
-403
lines changed

dist/html5sortable.amd.js

+74-80
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ function index (element, elementList) {
143143
}
144144

145145
function isInDom (element) {
146-
if (!element || element.nodeType !== 1) {
146+
if (!(element instanceof Element)) {
147147
throw new Error('Element is not a node element.');
148148
}
149149
return element.parentNode !== null;
@@ -209,6 +209,52 @@ function _serialize (sortableContainer, customItemSerializer, customContainerSer
209209
};
210210
}
211211

212+
function _makePlaceholder (sortableElement, placeholder, placeholderClass) {
213+
if (placeholderClass === void 0) { placeholderClass = 'sortable-placeholder'; }
214+
if (!(sortableElement instanceof Element)) {
215+
throw new Error('You must provide a valid element as a sortable.');
216+
}
217+
// if placeholder is not an element
218+
if (!(placeholder instanceof Element) && placeholder !== undefined) {
219+
throw new Error('You must provide a valid element as a placeholder or set ot to undefined.');
220+
}
221+
// if no placeholder element is given
222+
if (placeholder === undefined) {
223+
if (['UL', 'OL'].includes(sortableElement.tagName)) {
224+
placeholder = document.createElement('li');
225+
}
226+
else if (['TABLE', 'TBODY'].includes(sortableElement.tagName)) {
227+
placeholder = document.createElement('tr');
228+
// set colspan to always all rows, otherwise the item can only be dropped in first column
229+
placeholder.innerHTML = '<td colspan="100"></td>';
230+
}
231+
else {
232+
placeholder = document.createElement('div');
233+
}
234+
}
235+
// add classes to placeholder
236+
if (typeof placeholderClass === 'string') {
237+
(_a = placeholder.classList).add.apply(_a, placeholderClass.split(' '));
238+
}
239+
return placeholder;
240+
var _a;
241+
}
242+
243+
function _getElementHeight (element) {
244+
if (!(element instanceof Element)) {
245+
throw new Error('You must provide a valid dom element');
246+
}
247+
// get calculated style of element
248+
var style = window.getComputedStyle(element);
249+
// pick applicable properties, convert to int and reduce by adding
250+
return ['height', 'padding-top', 'padding-bottom']
251+
.map(function (key) {
252+
var int = parseInt(style.getPropertyValue(key), 10);
253+
return isNaN(int) ? 0 : int;
254+
})
255+
.reduce(function (sum, value) { return sum + value; });
256+
}
257+
212258
/* eslint-env browser */
213259
/*
214260
* variables global to the plugin
@@ -240,45 +286,6 @@ var _removeSortableEvents = function (sortable) {
240286
removeEventListener(sortable, 'dragenter');
241287
removeEventListener(sortable, 'drop');
242288
};
243-
/**
244-
* create a placeholder element
245-
* @param {Elememnt} sortableElement a single sortable
246-
* @param {string|undefine} placeholder a string representing an html element
247-
* @param {string} placeholderClasses a string representing the classes that should be added to the placeholder
248-
*/
249-
var _makePlaceholder = function (sortableElement, placeholder, placeholderClasses) {
250-
if (placeholder === void 0) { placeholder = undefined; }
251-
if (placeholderClasses === void 0) { placeholderClasses = 'sortable-placeholder'; }
252-
if (typeof placeholder === 'string') {
253-
var tempContainer = document.createElement(sortableElement.tagName);
254-
tempContainer.innerHTML = placeholder;
255-
placeholder = tempContainer.children[0];
256-
}
257-
else {
258-
switch (sortableElement.tagName) {
259-
case 'UL':
260-
placeholder = document.createElement('li');
261-
break;
262-
case 'OL':
263-
placeholder = document.createElement('li');
264-
break;
265-
case 'TABLE':
266-
placeholder = 'tr';
267-
placeholder.innerHTML = '<td colspan="100"></td>';
268-
break;
269-
case 'TBODY':
270-
placeholder = document.createElement('tr');
271-
placeholder.innerHTML = '<td colspan="100"></td>';
272-
break;
273-
default:
274-
placeholder = document.createElement('div');
275-
}
276-
}
277-
// add classes to placeholder
278-
(_a = placeholder.classList).add.apply(_a, placeholderClasses.split(' '));
279-
return placeholder;
280-
var _a;
281-
};
282289
/**
283290
* Attach ghost to dataTransfer object
284291
* @param {Event} original event
@@ -397,21 +404,6 @@ var _listsConnected = function (curList, destList) {
397404
var _isCopyActive = function (sortable) {
398405
return addData(sortable, 'opts').copy === true;
399406
};
400-
/**
401-
* Get height of an element including padding
402-
* @param {Element} sortable a single sortable
403-
*/
404-
var _getElementHeight = function (element) {
405-
// get calculated style of element
406-
var style = window.getComputedStyle(element);
407-
// pick applicable properties, convert to int and reduce by adding
408-
return ['height', 'padding-top', 'padding-bottom']
409-
.map(function (key) {
410-
var int = parseInt(style.getPropertyValue(key), 10);
411-
return isNaN(int) ? 0 : int;
412-
})
413-
.reduce(function (prev, cur) { return prev + cur; });
414-
};
415407
/**
416408
* get handle or return item
417409
* @param {Array} items
@@ -540,32 +532,28 @@ var _reloadSortable = function (sortableElement) {
540532
* @param {object|string} options|method
541533
*/
542534
function sortable(sortableElements, options) {
535+
// get method string to see if a method is called
543536
var method = String(options);
544-
options = (function (options) {
545-
var result = {
546-
connectWith: false,
547-
acceptFrom: null,
548-
copy: false,
549-
placeholder: null,
550-
disableIEFix: false,
551-
placeholderClass: 'sortable-placeholder',
552-
draggingClass: 'sortable-dragging',
553-
hoverClass: false,
554-
debounce: 0,
555-
maxItems: 0,
556-
itemSerializer: undefined,
557-
containerSerializer: undefined
558-
};
559-
if (typeof options === 'object') {
560-
for (var option in options) {
561-
result[option] = options[option];
562-
}
563-
}
564-
return result;
565-
})(options);
537+
// merge user options with defaults
538+
options = Object.assign({
539+
connectWith: false,
540+
acceptFrom: null,
541+
copy: false,
542+
placeholder: null,
543+
disableIEFix: false,
544+
placeholderClass: 'sortable-placeholder',
545+
draggingClass: 'sortable-dragging',
546+
hoverClass: false,
547+
debounce: 0,
548+
maxItems: 0,
549+
itemSerializer: undefined,
550+
containerSerializer: undefined
551+
}, (typeof options === 'object') ? options : {});
552+
// check if the user provided a selector instead of an element
566553
if (typeof sortableElements === 'string') {
567554
sortableElements = document.querySelectorAll(sortableElements);
568555
}
556+
// if the user provided an element, return it in an array to keep the return value consistant
569557
if (sortableElements instanceof Element) {
570558
sortableElements = [sortableElements];
571559
}
@@ -591,7 +579,14 @@ function sortable(sortableElements, options) {
591579
var items = filter(sortableElement.children, options.items);
592580
var index$$1;
593581
var startList;
594-
var placeholder = _makePlaceholder(sortableElement, options.placeholder, options.placeholderClass);
582+
// create element if user defined a placeholder element as a string
583+
var customPlaceholder;
584+
if (options.placeholder !== null && options.placeholder !== undefined) {
585+
var tempContainer = document.createElement(sortableElement.tagName);
586+
tempContainer.innerHTML = options.placeholder;
587+
customPlaceholder = tempContainer.children[0];
588+
}
589+
var placeholder = _makePlaceholder(sortableElement, customPlaceholder, options.placeholderClass);
595590
addData(sortableElement, 'items', options.items);
596591
placeholderMap.set(sortableElement, placeholder);
597592
if (options.acceptFrom) {
@@ -752,8 +747,7 @@ function sortable(sortableElements, options) {
752747
// Dead zone?
753748
var deadZone = thisHeight - draggingHeight;
754749
var offsetTop = _offset(element).top;
755-
if (placeholderIndex < thisIndex &&
756-
pageY < offsetTop + deadZone) {
750+
if (placeholderIndex < thisIndex && pageY < offsetTop) {
757751
return;
758752
}
759753
if (placeholderIndex > thisIndex &&

0 commit comments

Comments
 (0)