11var allfiles = [ ]
22var oldfiles = [ ]
33var oldfileList
4- var oldfiles
5- checkoudFile ( )
6-
7- function checkoudFile ( ) {
8- oldfiles = [ ]
9- oldfileList = document . getElementById ( "exercise_oldfileList" ) . value . split ( ' ' )
10- for ( var i = 0 ; i < oldfileList . length ; i ++ ) {
11- if ( oldfileList [ i ] . replace ( / \s / g, '' ) . length ) {
12- oldfiles . push ( oldfileList [ i ] )
13- }
14- createTextAndButton ( oldfiles )
4+ var oldfiles = [ ]
5+ oldfileList = document . getElementById ( "exercise_oldfileList" ) . value . split ( ' ' )
6+ for ( var i = 0 ; i < oldfileList . length ; i ++ ) {
7+ if ( oldfileList [ i ] . replace ( / \s / g, '' ) . length ) {
8+ oldfiles . push ( oldfileList [ i ] )
159 }
1610}
17-
11+ createTextAndButton ( oldfiles )
1812
1913function removeEle ( arr , ele ) {
20- arr = arr . split ( "," )
14+ if ( ! Array . isArray ( arr ) ) {
15+ arr = arr . split ( "," )
16+ }
2117 for ( var i = 0 ; i < arr . length ; i ++ ) {
2218 if ( arr [ i ] == ele . trim ( ) ) {
2319 arr . splice ( i , 1 ) ;
@@ -27,20 +23,19 @@ function removeEle(arr,ele){
2723}
2824
2925function addButtonListener ( allfiles ) {
30-
3126 document . querySelector ( '#fileName' ) . addEventListener ( 'click' , function ( event ) {
32- allfiles = document . getElementById ( "exercise_fileList" ) . value
33- allfiles += oldfileList
27+
3428 var text = document . getElementById ( event . target . id . match ( / \d * / g) [ 0 ] )
29+ allfiles = document . getElementById ( "exercise_fileList" ) . value
30+ oldfiles = removeEle ( oldfiles , text . innerHTML . toString ( ) . trim ( ) )
31+ allfiles += oldfiles
3532 allfiles = removeEle ( allfiles , text . innerHTML . toString ( ) . trim ( ) )
3633 var button = document . getElementById ( event . target . id )
3734 text . nextSibling . remove ( )
3835 text . remove ( )
3936 button . remove ( )
40- document . getElementById ( "exercise_fileList" ) . value = allfiles . toString ( )
41- document . getElementById ( "exercise_oldfileList" ) . value = allfiles . toString ( )
37+ document . getElementById ( "exercise_fileList" ) . value = allfiles . toString ( )
4238 } ) ;
43-
4439}
4540
4641// update file name and delete button
@@ -63,7 +58,7 @@ function createTextAndButton(allfiles){
6358$ ( "#exercise_files" ) . bind ( "change" , function ( e )
6459{
6560 allfiles = [ ]
66- checkoudFile ( )
61+
6762 for ( var i of oldfiles ) {
6863 allfiles . push ( i ) ;
6964 }
0 commit comments