@@ -86,35 +86,16 @@ return /******/ (function(modules) { // webpackBootstrap
86
86
function _inherits ( subClass , superClass ) { if ( typeof superClass !== "function" && superClass !== null ) { throw new TypeError ( "Super expression must either be null or a function, not " + typeof superClass ) ; } subClass . prototype = Object . create ( superClass && superClass . prototype , { constructor : { value : subClass , enumerable : false , writable : true , configurable : true } } ) ; if ( superClass ) Object . setPrototypeOf ? Object . setPrototypeOf ( subClass , superClass ) : subClass . __proto__ = superClass ; }
87
87
88
88
var defaultOptions = {
89
- ref : 'list' ,
90
- model : 'items' ,
91
- onStart : 'handleStart' ,
92
- onEnd : 'handleEnd' ,
93
- onAdd : 'handleAdd' ,
94
- onUpdate : 'handleUpdate' ,
95
- onRemove : 'handleRemove' ,
96
- onSort : 'handleSort' ,
97
- onFilter : 'handleFilter' ,
98
- onMove : 'handleMove'
89
+ ref : 'list'
99
90
} ;
100
91
101
- var _nextSibling = null ;
102
- var _activeWrapperComponent = null ;
92
+ var store = {
93
+ nextSibling : null ,
94
+ activeComponent : null
95
+ } ;
103
96
104
97
var refName = 'sortableComponent' ;
105
98
106
- var getModelItems = function getModelItems ( wrapperComponent ) {
107
- var model = wrapperComponent . sortableOptions . model ;
108
- var sortableComponent = wrapperComponent . refs [ refName ] ;
109
- var _sortableComponent$st = sortableComponent . state ;
110
- var state = _sortableComponent$st === undefined ? { } : _sortableComponent$st ;
111
- var _sortableComponent$pr = sortableComponent . props ;
112
- var props = _sortableComponent$pr === undefined ? { } : _sortableComponent$pr ;
113
-
114
- var items = state [ model ] || props [ model ] || [ ] ;
115
- return items . slice ( ) ; // returns a shallow copy of the items array
116
- } ;
117
-
118
99
var extend = function extend ( target ) {
119
100
for ( var _len = arguments . length , sources = Array ( _len > 1 ? _len - 1 : 0 ) , _key = 1 ; _key < _len ; _key ++ ) {
120
101
sources [ _key - 1 ] = arguments [ _key ] ;
@@ -141,101 +122,74 @@ return /******/ (function(modules) { // webpackBootstrap
141
122
var SortableMixin = function SortableMixin ( ) {
142
123
var options = arguments . length <= 0 || arguments [ 0 ] === undefined ? defaultOptions : arguments [ 0 ] ;
143
124
return function ( Component ) {
144
- return function ( _React$Component ) {
145
- _inherits ( _class2 , _React$Component ) ;
125
+ var _class , _temp2 ;
126
+
127
+ return _temp2 = _class = function ( _React$Component ) {
128
+ _inherits ( _class , _React$Component ) ;
146
129
147
- function _class2 ( ) {
130
+ function _class ( ) {
148
131
var _Object$getPrototypeO ;
149
132
150
133
var _temp , _this , _ret ;
151
134
152
- _classCallCheck ( this , _class2 ) ;
135
+ _classCallCheck ( this , _class ) ;
153
136
154
137
for ( var _len2 = arguments . length , args = Array ( _len2 ) , _key2 = 0 ; _key2 < _len2 ; _key2 ++ ) {
155
138
args [ _key2 ] = arguments [ _key2 ] ;
156
139
}
157
140
158
- return _ret = ( _temp = ( _this = _possibleConstructorReturn ( this , ( _Object$getPrototypeO = Object . getPrototypeOf ( _class2 ) ) . call . apply ( _Object$getPrototypeO , [ this ] . concat ( args ) ) ) , _this ) , _this . state = {
141
+ return _ret = ( _temp = ( _this = _possibleConstructorReturn ( this , ( _Object$getPrototypeO = Object . getPrototypeOf ( _class ) ) . call . apply ( _Object$getPrototypeO , [ this ] . concat ( args ) ) ) , _this ) , _this . state = {
159
142
sortableInstance : null
160
143
} , _this . sortableOptions = extend ( { } , defaultOptions , options ) , _temp ) , _possibleConstructorReturn ( _this , _ret ) ;
161
144
}
162
145
163
- _createClass ( _class2 , [ {
146
+ _createClass ( _class , [ {
164
147
key : 'componentDidMount' ,
165
148
value : function componentDidMount ( ) {
166
149
var _this2 = this ;
167
150
168
- var sortableComponent = this . refs [ refName ] ;
169
- var emitEvent = function emitEvent ( type , evt ) {
170
- var methodName = _this2 . sortableOptions [ type ] ;
171
- var method = sortableComponent [ methodName ] ;
172
- method && method . call ( sortableComponent , evt , _this2 . state . sortableInstance ) ;
173
- } ;
174
-
175
- var copyOptions = extend ( { } , this . sortableOptions ) ;
176
-
177
151
[ // Bind callbacks
178
152
'onStart' , 'onEnd' , 'onAdd' , 'onSort' , 'onUpdate' , 'onRemove' , 'onFilter' , 'onMove' ] . forEach ( function ( name ) {
179
- copyOptions [ name ] = function ( evt ) {
153
+ _this2 . sortableOptions [ name ] = function ( evt ) {
180
154
if ( name === 'onStart' ) {
181
- _nextSibling = evt . item . nextElementSibling ;
182
- _activeWrapperComponent = _this2 ;
155
+ store . nextSibling = evt . item . nextElementSibling ;
156
+ store . activeComponent = _this2 ;
183
157
} else if ( name === 'onAdd' || name === 'onUpdate' ) {
184
- evt . from . insertBefore ( evt . item , _nextSibling ) ;
158
+ evt . from . insertBefore ( evt . item , store . nextSibling ) ;
185
159
186
160
var oldIndex = evt . oldIndex ;
187
161
var newIndex = evt . newIndex ;
188
- var newState = { } ;
189
- var remoteState = { } ;
190
- var items = getModelItems ( _this2 ) ;
162
+ var items = _this2 . props . items ;
163
+ var remoteItems = [ ] ;
191
164
192
165
if ( name === 'onAdd' ) {
193
- var remoteItems = getModelItems ( _activeWrapperComponent ) ;
166
+ remoteItems = store . activeComponent . props . items ;
194
167
var item = remoteItems . splice ( oldIndex , 1 ) [ 0 ] ;
195
168
items . splice ( newIndex , 0 , item ) ;
196
-
197
- remoteState [ _activeWrapperComponent . sortableOptions . model ] = remoteItems ;
198
169
} else {
199
170
items . splice ( newIndex , 0 , items . splice ( oldIndex , 1 ) [ 0 ] ) ;
200
171
}
201
172
202
- newState [ _this2 . sortableOptions . model ] = items ;
203
-
204
- if ( copyOptions . stateHandler ) {
205
- sortableComponent [ copyOptions . stateHandler ] ( newState ) ;
206
- } else {
207
- sortableComponent . setState ( newState ) ;
208
- }
173
+ // Called by any change to the list (add / update / remove)
174
+ _this2 . props . onSort ( items ) ;
209
175
210
- if ( _activeWrapperComponent !== _this2 ) {
211
- _activeWrapperComponent . refs [ refName ] . setState ( remoteState ) ;
176
+ if ( store . activeComponent !== _this2 ) {
177
+ store . activeComponent . props . onSort ( remoteItems ) ;
212
178
}
213
179
}
214
180
215
181
setTimeout ( function ( ) {
216
- emitEvent ( name , evt ) ;
182
+ _this2 . props [ name ] && _this2 . props [ name ] ( evt , _this2 . state . sortableInstance ) ;
217
183
} , 0 ) ;
218
184
} ;
219
185
} ) ;
220
- this . populatedOptions = copyOptions ;
221
- this . initSortable ( sortableComponent ) ;
222
- }
223
- } , {
224
- key : 'componentWillReceiveProps' ,
225
- value : function componentWillReceiveProps ( nextProps ) {
226
- var sortableComponent = this . refs [ refName ] ;
227
- var model = this . sortableOptions . model ;
228
- var items = nextProps [ model ] ;
229
186
230
- if ( items ) {
231
- var newState = { } ;
232
- newState [ model ] = items ;
233
- sortableComponent . setState ( newState ) ;
234
- }
187
+ var sortableComponent = this . refs [ refName ] ;
188
+ this . initSortable ( sortableComponent ) ;
235
189
}
236
190
} , {
237
191
key : 'componentDidUpdate' ,
238
- value : function componentDidUpdate ( prevProps ) {
192
+ value : function componentDidUpdate ( prevProps , prevState ) {
239
193
var model = this . sortableOptions . model ;
240
194
var prevItems = prevProps [ model ] ;
241
195
var currItems = this . props [ model ] ;
@@ -253,7 +207,7 @@ return /******/ (function(modules) { // webpackBootstrap
253
207
value : function initSortable ( sortableComponent ) {
254
208
this . destroySortable ( ) ;
255
209
var domNode = _reactDom2 . default . findDOMNode ( sortableComponent . refs [ this . sortableOptions . ref ] || sortableComponent ) ;
256
- var sortableInstance = _sortablejs2 . default . create ( domNode , this . populatedOptions ) ;
210
+ var sortableInstance = _sortablejs2 . default . create ( domNode , this . sortableOptions ) ;
257
211
this . setState ( { sortableInstance : sortableInstance } ) ;
258
212
}
259
213
} , {
@@ -271,8 +225,11 @@ return /******/ (function(modules) { // webpackBootstrap
271
225
}
272
226
} ] ) ;
273
227
274
- return _class2 ;
275
- } ( _react2 . default . Component ) ;
228
+ return _class ;
229
+ } ( _react2 . default . Component ) , _class . propTypes = {
230
+ items : _react2 . default . PropTypes . array . isRequired ,
231
+ onSort : _react2 . default . PropTypes . func . isRequired
232
+ } , _temp2 ;
276
233
} ;
277
234
} ;
278
235
0 commit comments