<commit_before>import React from 'react';
import PropTypes from 'prop-types';
import Promise from 'promise';
import OverlayDialog,{dialogHelper,stateHelper} from './OverlayDialog.jsx';
import assign from 'object-assign';
import {Input,Checkbox,InputReadOnly,InputSelect,ColorSelector,Radio} from './Inputs.jsx';
import DB from './DialogButton.jsx';
import Button from './Button.jsx';
import ItemList from './ItemList.jsx';
import Requests from '../util/requests.js';
import Toast from './Toast.jsx';
import Helper from '../util/helper.js';
import GuiHelpers from '../util/GuiHelpers.js';
import {readFeatureInfoFromGpx} from '../map/gpxchartsource';
import {getOverlayConfigName} from '../map/chartsourcebase'
import globalStore from "../util/globalstore";
import keys from '../util/keys';
import OverlayConfig, {getKeyFromOverlay} from '../map/overlayconfig';

const filterOverlayItem=(item,opt_itemInfo)=>{
    let rt=undefined;
    if (item.type === 'chart') {
        rt=Helper.filteredAssign({chartKey:true,type:true,opacity:true,enabled:true},item)
    }
    else {
        rt = assign({}, item);
    }
    for (let k in rt){
        if (typeof rt[k] === 'function'){
            delete rt[k];
        }
    }
    delete rt.selected;
    delete rt.index;
    delete rt.disabled;
    let dstyles=['style.lineWidth','style.lineColor'];
    if (opt_itemInfo){
        dstyles.forEach((st)=> {
            if (!opt_itemInfo[st]) delete rt[st];
        })
        if (! opt_itemInfo.hasSymbols && ! opt_itemInfo.hasLinks){
            delete rt.icons;
        }
    }
    return rt;
};
export const KNOWN_OVERLAY_EXTENSIONS=['gpx'];
const KNOWN_ICON_FILE_EXTENSIONS=['zip'];
const TYPE_LIST=[
    {label: 'overlay', value: 'overlay'},
    {label: 'chart', value: 'chart'},
    {label: 'route', value: 'route'},
    {label: 'track', value: 'track'},
    ]
const itemSort=(a,b)=>{
    let na=a.name;
    let nb=b.name;
    if (na === undefined && nb === undefined) return 0;
    if (na === undefined) return -1;
    if (nb === undefined) return 1;
    na=na.toUpperCase();
    nb=nb.toUpperCase();
    if (na < nb) return -1;
    if (na > nb) return 1;
    return 0;
}
const trackSort=(a,b)=>{
    let ta=a.time;
    let tb=b.time;
    if (ta < tb) return 1;
    if (ta > tb) return -1;
    return 0;
}
class OverlayItemDialog extends React.Component{
    constructor(props) {
        super(props);
        this.dialogHelper = dialogHelper(this);
        this.state= {
            itemsFetchCount: 0,
            itemInfo: undefined,
            loading: false
        }
        this.sortLists=['icons','chart','overlay','images','user','knownOverlays','iconFiles','route','track']
        this.stateHelper = stateHelper(this, props.current || {},'item');
        this.state.itemsFetchCount = 0;
        //we make them only a variable as we consider them to be static
        this.itemLists={icons:[{label:"--none--"}],chart:[],overlay:[],images:[],user:[],knownOverlays:[],iconFiles:[{label:"--none--"}],route:[],track:[]};
        if (props.current && props.current.url && props.current.type !== 'chart') {
            this.analyseOverlay(props.current.url);
        }
        this.getItemList('chart');
        this.getItemList('overlay');
        this.getItemList('images');
        this.getItemList('user');
        this.getItemList('route');
        this.getItemList('track');
    }
    getItemList(type){
        Requests.getJson("",{},{
            request: 'listdir',
            type: type
        })
        .then((data)=>{
                this.itemLists[type]=data.items;
                if (type == 'user' || type == 'images' || type == 'overlay') {
                    data.items.forEach((item)=>{
                        if (GuiHelpers.IMAGES.indexOf(Helper.getExt(item.name)) >= 0) {
                            let el=assign({},item);
                            el.label=el.url;
                            this.itemLists.icons.push(el);
                        }
                    })
                }
                if (type == 'chart'){
                    //prepare select list
                    data.items.forEach((item)=>{
                        item.label=item.name;
                        item.value=item.chartKey;
                    });
                }
                if (type === 'overlay'|| type === 'route' || type === 'track'){
                    data.items.forEach((item)=>{
                        item.type=type;
                        if (type === 'route'){
                            if (! item.url) item.url=globalStore.getData(keys.properties.navUrl)+
                                "?request=download&type=route&name="+encodeURIComponent(item.name)+"&extension=.gpx";
                            if (! item.name.match(/\.gpx/)) item.name+=".gpx";
                        }
                        item.label=item.name;
                        item.value=item.url;
                        if (KNOWN_OVERLAY_EXTENSIONS.indexOf(Helper.getExt(item.name))>=0){
                            this.itemLists.knownOverlays.push(item);
                        }
                        if (KNOWN_ICON_FILE_EXTENSIONS.indexOf(Helper.getExt(item.name))>=0){
                            this.itemLists.iconFiles.push(assign({},item,{label:item.url}));
                        }
                    });
                }
                this.sortLists.forEach((list)=>{
                    this.itemLists[list].sort(itemSort);
                });
                this.setState({itemsFetchCount:this.state.itemsFetchCount+1})
            })
        .catch((error)=>{
                Toast("error fetching list of "+type+": "+error);
            })
    }
    changeType(newType){
        if (newType === this.stateHelper.getValue('type')) return;
        let newState={
            type: newType,
            opacity: 1
        };
        this.stateHelper.setState(newState,true);
    }
    analyseOverlay(url){
        this.setState({loading:true,itemInfo:undefined});
        Requests.getHtmlOrText(url)
            .then((data)=>{
                try {
                    let featureInfo = readFeatureInfoFromGpx(data);
                    if (! featureInfo.hasAny){
                        Toast(url+" is no valid gpx file");
                        this.setState({loading:false,itemInfo:{}});
                        this.stateHelper.setValue('name',undefined);
                    }
                    let newState={loading:false,itemInfo: featureInfo}
                    this.setState(newState);
                }catch (e){
                    Toast(url+" is no valid gpx: "+e.message);
                    this.setState({loading:false,itemInfo:{}});
                    this.stateHelper.setValue('name',undefined);
                }
            })
            .catch((error)=>{
                Toast("unable to load "+url+": "+error)
                this.setState({loading:false,itemInfo:{}});
                this.stateHelper.setValue('name',undefined);
            })
    }
    filteredNameList(){
        let currentType=this.stateHelper.getValue('type');
        let rt=[];
        this.itemLists.knownOverlays.forEach((item)=>{<commit_msg>correctly handle inserted/type changed overlay in dialog<commit_after>import React from 'react';
import PropTypes from 'prop-types';
import Promise from 'promise';
import OverlayDialog,{dialogHelper,stateHelper} from './OverlayDialog.jsx';
import assign from 'object-assign';
import {Input,Checkbox,InputReadOnly,InputSelect,ColorSelector,Radio} from './Inputs.jsx';
import DB from './DialogButton.jsx';
import Button from './Button.jsx';
import ItemList from './ItemList.jsx';
import Requests from '../util/requests.js';
import Toast from './Toast.jsx';
import Helper from '../util/helper.js';
import GuiHelpers from '../util/GuiHelpers.js';
import {readFeatureInfoFromGpx} from '../map/gpxchartsource';
import {getOverlayConfigName} from '../map/chartsourcebase'
import globalStore from "../util/globalstore";
import keys from '../util/keys';
import OverlayConfig, {getKeyFromOverlay} from '../map/overlayconfig';

const filterOverlayItem=(item,opt_itemInfo)=>{
    let rt=undefined;
    if (item.type === 'chart') {
        rt=Helper.filteredAssign({chartKey:true,type:true,opacity:true,enabled:true},item)
    }
    else {
        rt = assign({}, item);
    }
    for (let k in rt){
        if (typeof rt[k] === 'function'){
            delete rt[k];
        }
    }
    delete rt.selected;
    delete rt.index;
    delete rt.disabled;
    let dstyles=['style.lineWidth','style.lineColor'];
    if (opt_itemInfo){
        dstyles.forEach((st)=> {
            if (!opt_itemInfo[st]) delete rt[st];
        })
        if (! opt_itemInfo.hasSymbols && ! opt_itemInfo.hasLinks){
            delete rt.icons;
        }
    }
    return rt;
};
export const KNOWN_OVERLAY_EXTENSIONS=['gpx'];
const KNOWN_ICON_FILE_EXTENSIONS=['zip'];
const TYPE_LIST=[
    {label: 'overlay', value: 'overlay'},
    {label: 'chart', value: 'chart'},
    {label: 'route', value: 'route'},
    {label: 'track', value: 'track'},
    ]
const itemSort=(a,b)=>{
    let na=a.name;
    let nb=b.name;
    if (na === undefined && nb === undefined) return 0;
    if (na === undefined) return -1;
    if (nb === undefined) return 1;
    na=na.toUpperCase();
    nb=nb.toUpperCase();
    if (na < nb) return -1;
    if (na > nb) return 1;
    return 0;
}
const trackSort=(a,b)=>{
    let ta=a.time;
    let tb=b.time;
    if (ta < tb) return 1;
    if (ta > tb) return -1;
    return 0;
}
class OverlayItemDialog extends React.Component{
    constructor(props) {
        super(props);
        this.dialogHelper = dialogHelper(this);
        this.state= {
            itemsFetchCount: 0,
            itemInfo: undefined,
            loading: false
        }
        this.sortLists=['icons','chart','overlay','images','user','knownOverlays','iconFiles','route','track']
        this.stateHelper = stateHelper(this, props.current || {},'item');
        this.state.itemsFetchCount = 0;
        //we make them only a variable as we consider them to be static
        this.itemLists={icons:[{label:"--none--"}],chart:[],overlay:[],images:[],user:[],knownOverlays:[],iconFiles:[{label:"--none--"}],route:[],track:[]};
        if (props.current && props.current.url && props.current.type !== 'chart') {
            this.analyseOverlay(props.current.url);
        }
        this.getItemList('chart');
        this.getItemList('overlay');
        this.getItemList('images');
        this.getItemList('user');
        this.getItemList('route');
        this.getItemList('track');
    }
    getItemList(type){
        Requests.getJson("",{},{
            request: 'listdir',
            type: type
        })
        .then((data)=>{
                this.itemLists[type]=data.items;
                if (type == 'user' || type == 'images' || type == 'overlay') {
                    data.items.forEach((item)=>{
                        if (GuiHelpers.IMAGES.indexOf(Helper.getExt(item.name)) >= 0) {
                            let el=assign({},item);
                            el.label=el.url;
                            this.itemLists.icons.push(el);
                        }
                    })
                }
                if (type == 'chart'){
                    //prepare select list
                    data.items.forEach((item)=>{
                        item.label=item.name;
                        item.value=item.chartKey;
                    });
                }
                if (type === 'overlay'|| type === 'route' || type === 'track'){
                    data.items.forEach((item)=>{
                        item.type=type;
                        if (type === 'route'){
                            if (! item.url) item.url=globalStore.getData(keys.properties.navUrl)+
                                "?request=download&type=route&name="+encodeURIComponent(item.name)+"&extension=.gpx";
                            if (! item.name.match(/\.gpx/)) item.name+=".gpx";
                        }
                        item.label=item.name;
                        item.value=item.url;
                        if (KNOWN_OVERLAY_EXTENSIONS.indexOf(Helper.getExt(item.name))>=0){
                            this.itemLists.knownOverlays.push(item);
                        }
                        if (KNOWN_ICON_FILE_EXTENSIONS.indexOf(Helper.getExt(item.name))>=0){
                            this.itemLists.iconFiles.push(assign({},item,{label:item.url}));
                        }
                    });
                }
                this.sortLists.forEach((list)=>{
                    this.itemLists[list].sort(itemSort);
                });
                this.setState({itemsFetchCount:this.state.itemsFetchCount+1})
            })
        .catch((error)=>{
                Toast("error fetching list of "+type+": "+error);
            })
    }
    changeType(newType){
        if (newType === this.stateHelper.getValue('type')) return;
        let newState={
            type: newType,
        };
        this.stateHelper.setState(newState);
    }
    analyseOverlay(url){
        this.setState({loading:true,itemInfo:undefined});
        Requests.getHtmlOrText(url)
            .then((data)=>{
                try {
                    let featureInfo = readFeatureInfoFromGpx(data);
                    if (! featureInfo.hasAny){
                        Toast(url+" is no valid gpx file");
                        this.setState({loading:false,itemInfo:{}});
                        this.stateHelper.setValue('name',undefined);
                    }
                    let newState={loading:false,itemInfo: featureInfo}
                    this.setState(newState);
                }catch (e){
                    Toast(url+" is no valid gpx: "+e.message);
                    this.setState({loading:false,itemInfo:{}});
                    this.stateHelper.setValue('name',undefined);
                }
            })
            .catch((error)=>{
                Toast("unable to load "+url+": "+error)
                this.setState({loading:false,itemInfo:{}});
                this.stateHelper.setValue('name',undefined);
            })
    }
    filteredNameList(){
        let currentType=this.stateHelper.getValue('type');
        let rt=[];
        this.itemLists.knownOverlays.forEach((item)=>{
{'fraction_non_alphanumeric': 0.1510731402375495, 'fraction_numerical': 0.0021532263666041536, 'mean_word_length': 1.8398422090729782, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5404708', 'n_tokens_mistral': 4143, 'n_tokens_neox': 4014, 'n_words': 745}