33 Ractive-decorators-sortable
44 ===========================
55
6- Version 0.1 .0.
6+ Version 0.2 .0.
77
88 This plugin adds a 'sortable' decorator to Ractive, which enables
99 elements that correspond to array members to be re-ordered using
5858
5959*/
6060
61- ( function ( global , factory ) {
61+ var sortableDecorator = ( function ( global , factory ) {
6262
6363 'use strict' ;
6464
8989 ractive ,
9090 sourceKeypath ,
9191 sourceArray ,
92- sourceIndex ,
9392 dragstartHandler ,
9493 dragenterHandler ,
9594 removeTargetClass ,
124123 errorMessage = 'The sortable decorator only works with elements that correspond to array members' ;
125124
126125 dragstartHandler = function ( event ) {
127- var storage = this . _ractive , lastDotIndex ;
126+ var context = Ractive . getContext ( this ) ;
128127
129- sourceKeypath = storage . keypath ;
128+ sourceKeypath = context . resolve ( ) ;
129+ sourceArray = context . resolve ( '../' ) ;
130130
131- // this decorator only works with array members!
132- lastDotIndex = sourceKeypath . lastIndexOf ( '.' ) ;
133-
134- if ( lastDotIndex === - 1 ) {
135- throw new Error ( errorMessage ) ;
136- }
137-
138- sourceArray = sourceKeypath . substr ( 0 , lastDotIndex ) ;
139- sourceIndex = + ( sourceKeypath . substring ( lastDotIndex + 1 ) ) ;
140-
141- if ( isNaN ( sourceIndex ) ) {
131+ if ( ! Array . isArray ( context . get ( '../' ) ) ) {
142132 throw new Error ( errorMessage ) ;
143133 }
144134
145135 event . dataTransfer . setData ( 'foo' , true ) ; // enables dragging in FF. go figure
146136
147137 // keep a reference to the Ractive instance that 'owns' this data and this element
148- ractive = storage . root ;
138+ ractive = context . ractive ;
149139 } ;
150140
151141 dragenterHandler = function ( ) {
152- var targetKeypath , lastDotIndex , targetArray , targetIndex , array , source ;
142+ var targetKeypath , targetArray , array , source , context ;
143+
144+ context = Ractive . getContext ( this ) ;
153145
154146 // If we strayed into someone else's territory, abort
155- if ( this . _ractive . root !== ractive ) {
147+ if ( context . ractive !== ractive ) {
156148 return ;
157149 }
158150
159- targetKeypath = this . _ractive . keypath ;
160-
161- // this decorator only works with array members!
162- lastDotIndex = targetKeypath . lastIndexOf ( '.' ) ;
163-
164- if ( lastDotIndex === - 1 ) {
165- throw new Error ( errorMessage ) ;
166- }
167-
168- targetArray = targetKeypath . substr ( 0 , lastDotIndex ) ;
169- targetIndex = + ( targetKeypath . substring ( lastDotIndex + 1 ) ) ;
151+ targetKeypath = context . resolve ( ) ;
152+ targetArray = context . resolve ( '../' ) ;
170153
171154 // if we're dealing with a different array, abort
172155 if ( targetArray !== sourceArray ) {
173156 return ;
174157 }
175158
176159 // if it's the same index, add droptarget class then abort
177- if ( targetIndex === sourceIndex ) {
160+ if ( targetKeypath === sourceKeypath ) {
178161 this . classList . add ( sortable . targetClass ) ;
179162 return ;
180163 }
181164
182- array = ractive . get ( sourceArray ) ;
183-
184165 // remove source from array
185- source = array . splice ( sourceIndex , 1 ) [ 0 ] ;
166+ source = ractive . get ( sourceKeypath ) ;
167+ array = Ractive . splitKeypath ( sourceKeypath ) ;
168+
169+ ractive . splice ( targetArray , array [ array . length - 1 ] , 1 ) ;
186170
187171 // the target index is now the source index...
188- sourceIndex = targetIndex ;
172+ sourceKeypath = targetKeypath ;
173+
174+ array = Ractive . splitKeypath ( sourceKeypath ) ;
189175
190176 // add source back to array in new location
191- array . splice ( sourceIndex , 0 , source ) ;
177+ ractive . splice ( targetArray , array [ array . length - 1 ] , 0 , source ) ;
192178 } ;
193179
194180 removeTargetClass = function ( ) {
199185
200186 Ractive . decorators . sortable = sortable ;
201187
202- } ) ) ;
188+ return sortable ;
189+ } ) ) ;
190+
191+ // Common JS (i.e. browserify) environment
192+ if ( typeof module !== 'undefined' && module . exports ) {
193+ module . exports = sortableDecorator ;
194+ }
0 commit comments