@@ -259,46 +259,106 @@ $(function() {
259259 } ) ;
260260 } ) ;
261261
262- $ ( ".questions" ) . sortable ( {
263- items : '.question' ,
264- connectWith : ".questions" ,
265- handle : ".drag-handle" ,
266- distance : 20 ,
267- update : function ( e , ui ) {
268- var section_id = $ ( this ) . closest ( ".form-section-div" ) . attr ( 'data-id' ) ;
269- var data = $ ( this ) . sortable ( 'serialize' ) ;
270- data = data + "&form_section_id=" + section_id ;
271- $ . ajax ( {
272- url : '<%= sort_questions_admin_form_questions_path ( @form ) %> ' ,
273- type : "PATCH" ,
274- data : data
275- } ) ;
276- }
277- } ) ;
278-
279- $ ( ".question-options" ) . sortable ( {
280- items : '.question-option' ,
281- update : function ( e , ui ) {
282- $ . ajax ( {
283- url : $ ( this ) . parent ( ) . data ( "url" ) ,
284- type : "PATCH" ,
285- data : $ ( this ) . sortable ( 'serialize' )
286- } ) ;
287- }
288- } ) ;
289-
290- $ ( ".sorting-div" ) . sortable ( {
291- distance : 20 ,
292- handle : ".drag-handle" ,
293- update : function ( e , ui ) {
294- var url = ui . item . data ( "url" ) ;
295- $ . ajax ( {
296- url : url ,
297- type : "PATCH" ,
298- data : $ ( this ) . sortable ( 'serialize' )
299- } ) ;
300- }
301- } ) ;
262+ // Initialize sortable for questions with better error handling
263+ try {
264+ $ ( ".questions" ) . sortable ( {
265+ items : '.question' ,
266+ connectWith : ".questions" ,
267+ handle : ".drag-handle" ,
268+ distance : 20 ,
269+ helper : 'clone' ,
270+ tolerance : 'pointer' ,
271+ update : function ( e , ui ) {
272+ try {
273+ var section_id = $ ( this ) . closest ( ".form-section-div" ) . attr ( 'data-id' ) ;
274+ var data = $ ( this ) . sortable ( 'serialize' ) ;
275+ if ( ! data ) {
276+ console . error ( "Failed to serialize sortable data" ) ;
277+ return ;
278+ }
279+ data = data + "&form_section_id=" + section_id ;
280+ $ . ajax ( {
281+ url : '<%= sort_questions_admin_form_questions_path ( @form ) %> ' ,
282+ type : "PATCH" ,
283+
284+ error : function ( xhr , status , error ) {
285+ console . error ( "Sort update failed:" , error ) ;
286+ ui . item . animate ( { left : 0 } , 300 ) ; // Visual feedback on failure
287+ }
288+ } ) ;
289+ } catch ( err ) {
290+ console . error ( "Error during sort update:" , err ) ;
291+ }
292+ } ,
293+ start : function ( e , ui ) {
294+ ui . placeholder . height ( ui . item . height ( ) ) ;
295+ }
296+ } ) . disableSelection ( ) ;
297+ } catch ( err ) {
298+ console . error ( "Failed to initialize question sortable:" , err ) ;
299+ }
300+
301+ // Initialize sortable for question options
302+ try {
303+ $ ( ".question-options" ) . sortable ( {
304+ items : '.question-option' ,
305+ handle : '.drag-handle' ,
306+ placeholder : 'sort-placeholder' ,
307+ update : function ( e , ui ) {
308+ try {
309+ var url = $ ( this ) . parent ( ) . data ( "url" ) ;
310+ if ( ! url ) {
311+ console . error ( "Missing URL for question options sort" ) ;
312+ return ;
313+ }
314+ $ . ajax ( {
315+ url : url ,
316+ type : "PATCH" ,
317+ data : $ ( this ) . sortable ( 'serialize' ) ,
318+ error : function ( xhr , status , error ) {
319+ console . error ( "Option sort update failed:" , error ) ;
320+ ui . item . animate ( { left : 0 } , 300 ) ;
321+ }
322+ } ) ;
323+ } catch ( err ) {
324+ console . error ( "Error during option sort update:" , err ) ;
325+ }
326+ }
327+ } ) . disableSelection ( ) ;
328+ } catch ( err ) {
329+ console . error ( "Failed to initialize option sortable:" , err ) ;
330+ }
331+
332+ // Initialize sortable for sections
333+ try {
334+ $ ( ".sorting-div" ) . sortable ( {
335+ distance : 20 ,
336+ handle : ".drag-handle" ,
337+ placeholder : 'sort-placeholder' ,
338+ update : function ( e , ui ) {
339+ try {
340+ var url = ui . item . data ( "url" ) ;
341+ if ( ! url ) {
342+ console . error ( "Missing URL for section sort" ) ;
343+ return ;
344+ }
345+ $ . ajax ( {
346+ url : url ,
347+ type : "PATCH" ,
348+ data : $ ( this ) . sortable ( 'serialize' ) ,
349+ error : function ( xhr , status , error ) {
350+ console . error ( "Section sort update failed:" , error ) ;
351+ ui . item . animate ( { left : 0 } , 300 ) ;
352+ }
353+ } ) ;
354+ } catch ( err ) {
355+ console . error ( "Error during section sort update:" , err ) ;
356+ }
357+ }
358+ } ) . disableSelection ( ) ;
359+ } catch ( err ) {
360+ console . error ( "Failed to initialize section sortable:" , err ) ;
361+ }
302362
303363 $ ( '.form-builder' ) . on ( "click" , '.form-add-question-option' , function ( event ) {
304364 event . preventDefault ( ) ;
0 commit comments