1+ import Translation from 'src/structures/constants/translation.js' ;
12import Setting from './array.js' ;
23
34export default class MapList extends Setting {
@@ -12,23 +13,25 @@ export default class MapList extends Setting {
1213 /**
1314 * @param {Map<string, string> } val
1415 */
15- element ( val , update , { container } ) {
16+ element ( val , update , { container, key : setting } ) {
1617 const data = [ ...val . entries ( ) ] ;
1718 function invalid ( text ) {
1819 return data . some ( ( [ value ] ) => value === text ) ;
1920 }
20- function add ( value ) {
21+ function add ( value , i ) {
2122 function save ( remove ) {
2223 if ( remove ) data . splice ( data . indexOf ( value ) , 1 ) ;
2324 update ( data . filter ( ( [ key ] ) => key ) ) ;
2425 }
25- container . append ( createItem ( { value, save, invalid } ) ) ;
26+ container . append ( createItem ( { value, save, invalid, key : ` ${ setting } . ${ i } ` } ) ) ;
2627 }
2728 data . forEach ( add ) ;
29+ let entries = data . length ;
2830 return $ ( '<button class="btn btn-success glyphicon glyphicon-plus">' ) . on ( 'click' , ( ) => {
2931 const item = [ '' , '' ] ;
3032 data . push ( item ) ;
31- add ( item ) ;
33+ add ( item , entries ) ;
34+ entries += 1 ;
3235 } ) ;
3336 }
3437
@@ -46,7 +49,7 @@ export default class MapList extends Setting {
4649 '.btn { padding: 3px 6px; }' ,
4750 '.item { display: inline-flex; flex-wrap: wrap; align-items: center; padding-top: 5px; width: 100%; }' ,
4851 '.item > * { margin: 0 5px; }' ,
49- '.error *:first-child { border-color: red; }' ,
52+ '.error [id]:not([id$=".value"]) { border-color: red; }' ,
5053 '.warning { display: none; color: red; flex-basis: 100%; user-select: none; }' ,
5154 '.error .warning { display: block; }' ,
5255 ] ;
@@ -61,6 +64,7 @@ function createItem({
6164 value = [ 'key' , 'value' ] ,
6265 save,
6366 invalid,
67+ key,
6468} ) {
6569 const left = $ ( '<input type="text">' ) . val ( value [ 0 ] ) . on ( 'blur' , ( ) => {
6670 const newVal = left . val ( ) ;
@@ -69,20 +73,19 @@ function createItem({
6973 if ( isInvalid || newVal === value [ 0 ] ) return ;
7074 value [ 0 ] = newVal ;
7175 save ( ) ;
72- } ) ;
76+ } ) . attr ( 'id' , key ) ;
7377 const right = $ ( '<input type="text">' ) . val ( value [ 1 ] ) . on ( 'blur' , ( ) => {
7478 const newVal = right . val ( ) ;
7579 if ( newVal === value [ 1 ] ) return ;
7680 value [ 1 ] = newVal ;
7781 save ( ) ;
78- } ) ;
82+ } ) . attr ( 'id' , ` ${ key } .value` ) ;
7983 const button = $ ( '<button class="btn btn-danger glyphicon glyphicon-trash">' ) . on ( 'click' , ( ) => {
8084 save ( true ) ;
8185 button . parent ( ) . remove ( ) ;
8286 } ) ;
83- // TODO: translation
8487 const warning = $ ( '<div class="warning clickable">' )
85- . text ( 'Duplicate value, not updated! Click here to reset.' )
88+ . text ( Translation . Setting ( 'map.duplicate' ) )
8689 . on ( 'click' , ( ) => left . val ( value [ 0 ] )
8790 . parent ( ) . removeClass ( 'error' ) ) ;
8891 return $ ( '<div class="item">' ) . append ( left , ' : ' , right , button , warning ) ;
0 commit comments