@@ -3,7 +3,8 @@ let teams = JSON.parse(document.getElementById('_teams').textContent);
33let leftover_participants = JSON . parse ( document . getElementById ( '_leftover_participants' ) . textContent ) ;
44
55addNewTeam ( ) ;
6- distributeTeams ( ) ;
6+ confirmBeforeAction ( '.clear-teams-form' , 'submit' , 'Do you really want to clear all teams and re-distribute them?' ) ;
7+ confirmBeforeAction ( '.distribute-teams-form' , 'submit' , 'Some participants have not been assigned to a team. Do you still want to proceed?' ) ;
78
89function allowDrop ( ev ) {
910 ev . preventDefault ( ) ;
@@ -24,11 +25,20 @@ function drop(ev) {
2425 if ( ev . target . nodeName == 'LI' ) {
2526 targetElement = ev . target . parentElement . id ;
2627 ev . target . parentElement . appendChild ( movingElement ) ;
27- } else {
28+ } else if ( ev . target . nodeName == 'SPAN' ) {
29+ targetElement = ev . target . parentElement . parentElement . id ;
30+ ev . target . parentElement . parentElement . appendChild ( movingElement ) ;
31+ } else if ( ev . target . nodeName == 'H5' ) {
32+ targetElement = ev . target . nextElementSibling . id ;
33+ ev . target . nextElementSibling . appendChild ( movingElement ) ;
34+ } else if ( ev . target . nodeName == 'DIV' ) {
35+ targetElement = ev . target . children [ 1 ] . id ;
36+ ev . target . children [ 1 ] . appendChild ( movingElement ) ;
37+ }
38+ else {
2839 targetElement = ev . target . id ;
2940 ev . target . appendChild ( movingElement ) ;
3041 }
31-
3242 changeTeamData ( movingElement , movingElementParent . id , targetElement ) ;
3343 changeTeamScores ( movingElement , movingElementParent . id , targetElement ) ;
3444}
@@ -41,35 +51,34 @@ function changeTeamScores(movedElement, movedElementParentId, targetElementId){
4151 if ( targetElementId != 'leftover_participants' ) {
4252 let teamScoreSpan = document . getElementById ( targetElementId + '_score' ) ;
4353 teamScoreSpan . innerText = parseInt ( teamScoreSpan . innerText ) + movedLevel ;
44- }
45-
46- if ( movedElementParentId . includes ( 'team' ) ) {
4754 let removeScoreSpan = document . getElementById ( movedElementParentId + '_score' ) ;
48- removeScoreSpan . innerText = parseInt ( removeScoreSpan . innerText ) - movedLevel ;
55+ if ( removeScoreSpan != null ) {
56+ removeScoreSpan . innerText = parseInt ( removeScoreSpan . innerText ) - movedLevel ;
57+ }
4958 }
5059}
5160
5261function changeTeamData ( movedElement , movedElementParentId , targetElementId ) {
5362 /* Removes the participant from the team the participant was previously
5463 part of and adds it to the new team in the team data object which is used
5564 to create or edit the teams */
56- let team = movedElementParentId . includes ( 'team' )
57- ? teams [ movedElementParentId ]
58- : leftover_participants ;
59- let targetTeam = targetElementId . includes ( 'team' )
60- ? teams [ targetElementId ]
61- : leftover_participants ;
65+ if ( movedElementParentId == targetElementId ) {
66+ return ;
67+ }
68+ let team = movedElementParentId . includes ( 'leftover_participants' )
69+ ? leftover_participants
70+ : teams [ movedElementParentId ] ;
71+ let targetTeam = targetElementId . includes ( 'leftover_participants' )
72+ ? leftover_participants
73+ : teams [ targetElementId ] ;
6274 let userid = movedElement . dataset . userid ;
6375 let user = team . filter ( x => x . userid == userid ) [ 0 ] ;
64- targetTeam . push ( user )
65-
66- if ( movedElementParentId . includes ( 'team' ) ) {
67- teams [ movedElementParentId ] = teams [ movedElementParentId ]
68- . filter ( x => x . userid != userid ) ;
76+ if ( movedElementParentId . includes ( 'leftover_participants' ) ) {
77+ leftover_participants = leftover_participants . filter ( x => x . userid != userid ) ;
6978 } else {
70- leftover_participants = leftover_participants
71- . filter ( x => x . userid != userid ) ;
79+ teams [ movedElementParentId ] = teams [ movedElementParentId ] . filter ( x => x . userid != userid ) ;
7280 }
81+ targetTeam . push ( user ) ;
7382 $ ( 'input[name="teams"]' ) . val ( JSON . stringify ( teams ) ) ;
7483}
7584
@@ -92,11 +101,10 @@ function addNewTeam(){
92101 } ) ;
93102}
94103
95- function distributeTeams ( ) {
96- $ ( '.distribute-teams-form' ) . on ( 'submit' , function ( event ) {
104+ function confirmBeforeAction ( className , action , msg ) {
105+ $ ( className ) . on ( action , function ( event ) {
97106 if ( leftover_participants . length > 0 ) {
98- let confirm_msg = 'Some participants have not been assigned to a team. Do you still want to proceed?' ;
99- let confirmation = window . confirmconfirm_msg ( confirm_msg ) ;
107+ let confirmation = window . confirm ( msg ) ;
100108 if ( ! confirmation ) {
101109 event . preventDefault ( ) ;
102110 }
0 commit comments