|
|
@ -19,6 +19,46 @@ |
|
|
|
})(function (/** Sortable */Sortable) { |
|
|
|
})(function (/** Sortable */Sortable) { |
|
|
|
'use strict'; |
|
|
|
'use strict'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _nextSibling; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _activeComponent; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var _defaultOptions = { |
|
|
|
|
|
|
|
ref: 'list', |
|
|
|
|
|
|
|
model: 'items', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
animation: 100, |
|
|
|
|
|
|
|
onStart: 'handleStart', |
|
|
|
|
|
|
|
onEnd: 'handleEnd', |
|
|
|
|
|
|
|
onAdd: 'handleAdd', |
|
|
|
|
|
|
|
onUpdate: 'handleUpdate', |
|
|
|
|
|
|
|
onRemove: 'handleRemove', |
|
|
|
|
|
|
|
onSort: 'handleSort', |
|
|
|
|
|
|
|
onFilter: 'handleFilter' |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _getModelName(component) { |
|
|
|
|
|
|
|
return component.sortableOptions && component.sortableOptions.model || _defaultOptions.model; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _getModelItems(component) { |
|
|
|
|
|
|
|
return component.state[_getModelName(component)].slice(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _extend(dst, src) { |
|
|
|
|
|
|
|
for (var key in src) { |
|
|
|
|
|
|
|
if (src.hasOwnProperty(key)) { |
|
|
|
|
|
|
|
dst[key] = src[key]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return dst; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Simple and easy mixin-wrapper for rubaxa/Sortable library, in order to |
|
|
|
* Simple and easy mixin-wrapper for rubaxa/Sortable library, in order to |
|
|
@ -27,7 +67,8 @@ |
|
|
|
* @mixin |
|
|
|
* @mixin |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
var SortableMixin = { |
|
|
|
var SortableMixin = { |
|
|
|
sortableMixinVersion: '0.0.0', |
|
|
|
sortableMixinVersion: '0.1.0', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @type {Sortable} |
|
|
|
* @type {Sortable} |
|
|
@ -36,83 +77,59 @@ |
|
|
|
_sortableInstance: null, |
|
|
|
_sortableInstance: null, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Sortable options |
|
|
|
|
|
|
|
* @returns {object} |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
getDefaultProps: function () { |
|
|
|
|
|
|
|
return { |
|
|
|
|
|
|
|
sortable: { |
|
|
|
|
|
|
|
ref: 'list', |
|
|
|
|
|
|
|
model: 'items', |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
animation: 100, |
|
|
|
|
|
|
|
onStart: 'handleStart', |
|
|
|
|
|
|
|
onEnd: 'handleEnd', |
|
|
|
|
|
|
|
onAdd: 'handleAdd', |
|
|
|
|
|
|
|
onUpdate: 'handleUpdate', |
|
|
|
|
|
|
|
onRemove: 'handleRemove', |
|
|
|
|
|
|
|
onSort: 'handleSort', |
|
|
|
|
|
|
|
onFilter: 'handleFilter' |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
componentDidMount: function () { |
|
|
|
componentDidMount: function () { |
|
|
|
var nextSibling, |
|
|
|
var options = _extend(_extend({}, _defaultOptions), this.sortableOptions || {}), |
|
|
|
sortableProps = this.props.sortable, |
|
|
|
copyOptions = _extend({}, options), |
|
|
|
sortableOptions = {}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
callMethod = function (/** string */type, /** Event */evt) { |
|
|
|
emitEvent = function (/** string */type, /** Event */evt) { |
|
|
|
var method = this[sortableProps[type]]; |
|
|
|
var method = this[options[type]]; |
|
|
|
method && method.call(this, evt, this._sortableInstance); |
|
|
|
method && method.call(this, evt, this._sortableInstance); |
|
|
|
}.bind(this); |
|
|
|
}.bind(this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Pass through unrecognized options
|
|
|
|
|
|
|
|
for (var key in sortableProps) { |
|
|
|
|
|
|
|
sortableOptions[key] = sortableProps[key]; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Bind callbacks so that "this" refers to the component
|
|
|
|
// Bind callbacks so that "this" refers to the component
|
|
|
|
'onEnd onAdd onUpdate onRemove onFilter'.split(' ').forEach(function (/** string */name) { |
|
|
|
'onStart onEnd onAdd onSort onUpdate onRemove onFilter'.split(' ').forEach(function (/** string */name) { |
|
|
|
if (sortableProps[name]) { |
|
|
|
copyOptions[name] = function (evt) { |
|
|
|
sortableOptions[name] = callMethod.bind(this, name); |
|
|
|
if (name === 'onStart') { |
|
|
|
} |
|
|
|
_nextSibling = evt.item.nextElementSibling; |
|
|
|
}.bind(this)); |
|
|
|
_activeComponent = this; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
else if (name === 'onAdd' || name === 'onUpdate') { |
|
|
|
sortableOptions.onStart = function (/** Event */evt) { |
|
|
|
evt.from.insertBefore(evt.item, _nextSibling); |
|
|
|
nextSibling = evt.item.nextSibling; |
|
|
|
|
|
|
|
callMethod('onStart', evt); |
|
|
|
var newState = {}, |
|
|
|
}.bind(this); |
|
|
|
remoteState = {}, |
|
|
|
|
|
|
|
oldIndex = evt.oldIndex, |
|
|
|
|
|
|
|
newIndex = evt.newIndex, |
|
|
|
sortableOptions.onSort = function (/** Event */evt) { |
|
|
|
items = _getModelItems(this), |
|
|
|
evt.from.insertBefore(evt.item, nextSibling || null); |
|
|
|
remoteItems, |
|
|
|
|
|
|
|
item; |
|
|
|
var modelName = sortableProps.model, |
|
|
|
|
|
|
|
newState = {}, |
|
|
|
if (name === 'onAdd') { |
|
|
|
items = this.state[modelName]; |
|
|
|
remoteItems = _getModelItems(_activeComponent); |
|
|
|
|
|
|
|
item = remoteItems.splice(oldIndex, 1)[0]; |
|
|
|
if (items) { |
|
|
|
items.splice(newIndex, 0, item); |
|
|
|
items = items.slice(); // clone
|
|
|
|
|
|
|
|
items.splice(evt.newIndex, 0, items.splice(evt.oldIndex, 1)[0]); |
|
|
|
remoteState[_getModelName(_activeComponent)] = remoteItems; |
|
|
|
|
|
|
|
} |
|
|
|
newState[modelName] = items; |
|
|
|
else { |
|
|
|
this.setState(newState); |
|
|
|
items.splice(newIndex, 0, items.splice(oldIndex, 1)[0]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
callMethod('onSort', evt); |
|
|
|
newState[_getModelName(this)] = items; |
|
|
|
}.bind(this); |
|
|
|
this.setState(newState); |
|
|
|
|
|
|
|
(this !== _activeComponent) && _activeComponent.setState(remoteState); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setTimeout(function () { |
|
|
|
|
|
|
|
emitEvent(name, evt); |
|
|
|
|
|
|
|
}, 0); |
|
|
|
|
|
|
|
}.bind(this); |
|
|
|
|
|
|
|
}, this); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @namespace this.refs — http://facebook.github.io/react/docs/more-about-refs.html */ |
|
|
|
/** @namespace this.refs — http://facebook.github.io/react/docs/more-about-refs.html */ |
|
|
|
if (!sortableProps.ref || this.refs[sortableProps.ref]) { |
|
|
|
this._sortableInstance = Sortable.create((this.refs[options.ref] || this).getDOMNode(), copyOptions); |
|
|
|
this._sortableInstance = Sortable.create((this.refs[sortableProps.ref] || this).getDOMNode(), sortableOptions); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|