1
- define ( [ "../notjQuery" , "BaseInput" ] , function ( $ , BaseInput ) {
1
+ define ( [ "../notjQuery" , "../vendor/Sortable" , " BaseInput"] , function ( $ , Sortable , BaseInput ) {
2
2
3
3
"use strict" ;
4
4
@@ -7,6 +7,7 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
7
7
super ( input ) ;
8
8
9
9
this . separator = this . input . dataset . termSeparator || ' ' ;
10
+ this . ordered = 'maintainTermOrder' in this . input . dataset ;
10
11
this . readOnly = 'readOnlyTerms' in this . input . dataset ;
11
12
this . ignoreSpaceUntil = null ;
12
13
}
@@ -18,6 +19,16 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
18
19
$ ( this . termContainer ) . on ( 'click' , '[data-index] > input' , this . onTermClick , this ) ;
19
20
}
20
21
22
+ if ( this . ordered ) {
23
+ $ ( this . termContainer ) . on ( 'end' , this . onDrop , this ) ;
24
+
25
+ Sortable . create ( this . termContainer , {
26
+ scroll : true ,
27
+ direction : 'vertical' ,
28
+ handle : '[data-drag-initiator]'
29
+ } ) ;
30
+ }
31
+
21
32
// TODO: Compatibility only. Remove as soon as possible once Web 2.12 (?) is out.
22
33
// Or upon any other update which lets Web trigger a real submit upon auto submit.
23
34
$ ( this . input . form ) . on ( 'change' , 'select.autosubmit' , this . onSubmit , this ) ;
@@ -131,6 +142,41 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
131
142
return quoted . join ( this . separator ) . trim ( ) ;
132
143
}
133
144
145
+ addRenderedTerm ( label ) {
146
+ if ( ! this . ordered ) {
147
+ return super . addRenderedTerm ( label ) ;
148
+ }
149
+
150
+ const listItem = document . createElement ( 'li' ) ;
151
+ listItem . appendChild ( label ) ;
152
+ listItem . appendChild ( $ . render ( '<i data-drag-initiator class="icon fa-bars fa"></i>' ) ) ;
153
+ this . termContainer . appendChild ( listItem ) ;
154
+ }
155
+
156
+ insertRenderedTerm ( label ) {
157
+ if ( ! this . ordered ) {
158
+ return super . insertRenderedTerm ( label ) ;
159
+ }
160
+
161
+ const termIndex = Number ( label . dataset . index ) ;
162
+ const nextListItemLabel = this . termContainer . querySelector ( `[data-index="${ termIndex + 1 } "]` ) ;
163
+ const nextListItem = nextListItemLabel ?. parentNode || null ;
164
+ const listItem = document . createElement ( 'li' ) ;
165
+ listItem . appendChild ( label ) ;
166
+ listItem . appendChild ( $ . render ( '<i data-drag-initiator class="icon fa-bars fa"></i>' ) ) ;
167
+ this . termContainer . insertBefore ( listItem , nextListItem ) ;
168
+
169
+ return label ;
170
+ }
171
+
172
+ removeRenderedTerm ( label ) {
173
+ if ( ! this . ordered ) {
174
+ return super . removeRenderedTerm ( label ) ;
175
+ }
176
+
177
+ label . parentNode . remove ( ) ;
178
+ }
179
+
134
180
complete ( input , data ) {
135
181
data . exclude = this . usedTerms . map ( termData => termData . search ) ;
136
182
@@ -159,6 +205,30 @@ define(["../notjQuery", "BaseInput"], function ($, BaseInput) {
159
205
this . moveFocusForward ( termIndex - 1 ) ;
160
206
}
161
207
208
+ onDrop ( event ) {
209
+ if ( event . to === event . from && event . newIndex === event . oldIndex ) {
210
+ // The user dropped the term at its previous position
211
+ return ;
212
+ }
213
+
214
+ // The item is the list item, not the term's label
215
+ const label = event . item . firstChild ;
216
+
217
+ // Remove the term from the internal map, but not the DOM, as it's been moved already
218
+ const termData = this . removeTerm ( label , false ) ;
219
+ delete label . dataset . index ; // Which is why we have to take it out of the equation for now
220
+
221
+ let newIndex = 0 ; // event.newIndex is intentionally not used, as we have our own indexing
222
+ if ( event . item . previousSibling ) {
223
+ newIndex = Number ( event . item . previousSibling . firstChild . dataset . index ) + 1 ;
224
+ }
225
+
226
+ // This is essentially insertTerm() with custom DOM manipulation
227
+ this . reIndexTerms ( newIndex , 1 , true ) ; // Free up the new index
228
+ this . registerTerm ( termData , newIndex ) ; // Re-register the term with the new index
229
+ label . dataset . index = `${ newIndex } ` ; // Update the DOM, we didn't do that during removal
230
+ }
231
+
162
232
onSubmit ( event ) {
163
233
super . onSubmit ( event ) ;
164
234
0 commit comments