<commit_before>"use strict";

require('lazy-some').bindToPrototypeIn(Array);

myOwn.wScreens={}

myOwn.messages=changing(myOwn.messages, {
    chpass:'<PASSWORD>',
    exit:'exit',
    proced:'proced',
    user:'user',
});

myOwn.es=changing(myOwn.es, {
    chpass:'<PASSWORD>',
    exit:'salir',
    proced:'proceder',
    user:'usuario',
});

myOwn.wScreens.table = function(addrParams){
    setTimeout(function(){
        var layout = document.getElementById('main_layout');
        my.tableGrid(addrParams.table||addrParams.name,layout);
    },10);
}

myOwn.wScreens.proc = function(addrParams){
    var procDef=my.config.procedures.find(function(proc){
        return proc.action == (addrParams.proc||addrParams.name);
    });
    console.log('procDef');
    console.log(addrParams);
    console.log(procDef);
    var params={};
    var button = html.button(my.messages.proced).create();
    var divResult = html.div().create();
    divResult.style.minHeight='40px';
    main_layout.appendChild(html.table({class:"table-param-screen"},procDef.parameters.map(function(parameterDef){
        var control = html.td({"typed-controls-direct-input":true}).create();
        control.style.minWidth='200px';
        control.style.backgroundColor='white';
        TypedControls.adaptElement(control,changing({typeName:'text'}, parameterDef));
        if('defaultValue' in parameterDef){
            params[parameterDef.name] = parameterDef.defaultValue;
            control.setTypedValue(parameterDef.defaultValue);
        }
        control.addEventListener('update', function(){
            params[parameterDef.name] = control.getTypedValue();
        });
        return html.tr([ html.td(parameterDef.label||parameterDef.name),control]);
    }).concat(
        html.tr([html.td(), html.td([button])])
    )).create());
    main_layout.appendChild(divResult);
    button.onclick=function(){
        var my_ajax_actionFun = procDef.action.split('/').reduce(function(o, part){ return o[part]; },my.ajax);
        console.log('x',params);
        button.disabled=true;
        my_ajax_actionFun(params).then(function(result){
            divResult.textContent = result;
            divResult.style.backgroundColor = '#5F5';
        },function(err){
            divResult.textContent = err.message;
            divResult.style.backgroundColor = 'orange';
        }).then(function(){
            button.disabled=false;
        });
    }
}

myOwn.UriSearchToObject = function UriSearchToObject(locationSearch){
    var parts=locationSearch.split('&');
    var addrParams={}
    parts.forEach(function(pair){
        if(pair[0]==='?'){
            pair=pair.substr(1);
        }
        var eq = pair.indexOf('=');
        if(eq !== -1){
            var varName=pair.substr(0, eq);
            addrParams[varName] = decodeURIComponent(pair.substr(eq+1));
        }
    });
    return addrParams;
}

myOwn.showPage = function showPage(pageDef){
    my.prepareFloating3dots();
    my.prepareRulerToggle();
    var addrParams=my.UriSearchToObject(location.search);
    if(addrParams.i){
        addrParams.i=addrParams.i.split(',');
    }else{
        addrParams.i=[];
    }
    var menu = my.displayMainMenu(addrParams);
    var w=addrParams.w;
    if(!w && menu && menu.selectedItem){
        addrParams = changing(addrParams, menu.selectedItem);
        w=menu.selectedItem.menuType;
    }
    if(typeof my.wScreens[w] === 'function'){
        my.wScreens[w].call(my, addrParams);
    }
    var rightMenu = document.getElementById('right-menu');
    rightMenu.onclick=function(){
        my.rightMenu();
    }
};

myOwn.createForkeableButton = function createForkeableButton(menu, label){
    var be = this;
    var href;
    if(!menu.w && menu.menuType){
        href = 'menu?i=' + menu.parents.concat(menu.name).join(',');
        if(menu.section){
            href += '&section='+menu.section;
        }
    }else{
        href = 'menu?' + my.paramsToUriPart(menu);
    }
    var button=html.a({"class": "menu-item", "menu-type":menu.menuType||menu.w, "menu-name":menu.name||'noname', href: href}, label || menu.label || menu.name).create();
    menu.button = button;
    button.onclick=function(event){
        if(!event.ctrlKey){
            history.pushState(null, null, href);
            my.showPage();
            event.preventDefault();
        }
    };
    return button;
};

function encodeMinimalURIComponent(text){
    return text
        .replace(/=/g, '%3D')
        .replace(/\?/g, '%3F')
        .replace(/&/g, '%26')
        .replace(/%/g, '%25');
}

myOwn.paramsToUriPart = function paramsToUriPart(params){
    return likeAr(params).map(function(value, name){
        if(value!=null){
            return name+'='+encodeMinimalURIComponent(value);
        }
    }).join('&');
}

myOwn.replaceAddrParams = function replaceAddrParams(params){
    history.replaceState(null, null, 'menu?'+my.paramsToUriPart(param));
}

myOwn.displayMenu = function displayMenu(layout, menu, addrParams, parents){
    var be = this;
    var next = false;
    var selectedItem = null;
    var newMenuLine;
    var elements=[];
    var depth = parents.length;
    var skin=be.config.config.skin;
    var skinUrl=(skin?skin+'/':'');
    if(depth===0){
        elements.push(html.img({id: "main-logo", src: skinUrl+"img/logo.png"}));
    }
    elements = elements.concat(menu.map(function(menuItem){
        menuItem.parents = parents;
        var button = my.createForkeableButton(menuItem);
        if(menuItem.name == addrParams.i[depth]){
            button.setAttribute('menu-selected', 'yes');
            selectedItem = menuItem;
        }
        return button;
    }))
    if(depth===0){
        elements.push(html.span({id: "right-menu"}, [
            html.span({id: "active-user"}, my.config.username||"user"),
            html.img({class: "right-menu", src: skinUrl+"img/three-dot-menu.png",id: "right-menu-icon"}),
        ]));
    }
    var menuLine=html.div({id: "main-top-bar"+(depth||''), class: depth?"sub-menu-bar":"top-menu-bar"}, elements).create();
    layout.appendChild(menuLine);
    var innerSelectedItem = selectedItem;
    if(selectedItem && selectedItem.menuType === 'menu'){
        var subMenu = my.displayMenu(layout, selectedItem.menuContent, addrParams, parents.concat(selectedItem.name));
        var realign = function(){
            subMenu.menuLine.style.paddingLeft = selectedItem.button.offsetLeft + 'px';
        };<commit_msg>Permite tener una opción de menú seleccionada por defecto (cuando no se especificó nada en la URL)<commit_after>"use strict";

require('lazy-some').bindToPrototypeIn(Array);

myOwn.wScreens={}

myOwn.messages=changing(myOwn.messages, {
    chpass:'<PASSWORD>',
    exit:'exit',
    proced:'proced',
    user:'user',
});

myOwn.es=changing(myOwn.es, {
    chpass:'<PASSWORD>',
    exit:'salir',
    proced:'proceder',
    user:'usuario',
});

myOwn.wScreens.table = function(addrParams){
    setTimeout(function(){
        var layout = document.getElementById('main_layout');
        my.tableGrid(addrParams.table||addrParams.name,layout);
    },10);
}

myOwn.wScreens.proc = function(addrParams){
    var procDef=my.config.procedures.find(function(proc){
        return proc.action == (addrParams.proc||addrParams.name);
    });
    console.log('procDef');
    console.log(addrParams);
    console.log(procDef);
    var params={};
    var button = html.button(my.messages.proced).create();
    var divResult = html.div().create();
    divResult.style.minHeight='40px';
    main_layout.appendChild(html.table({class:"table-param-screen"},procDef.parameters.map(function(parameterDef){
        var control = html.td({"typed-controls-direct-input":true}).create();
        control.style.minWidth='200px';
        control.style.backgroundColor='white';
        TypedControls.adaptElement(control,changing({typeName:'text'}, parameterDef));
        if('defaultValue' in parameterDef){
            params[parameterDef.name] = parameterDef.defaultValue;
            control.setTypedValue(parameterDef.defaultValue);
        }
        control.addEventListener('update', function(){
            params[parameterDef.name] = control.getTypedValue();
        });
        return html.tr([ html.td(parameterDef.label||parameterDef.name),control]);
    }).concat(
        html.tr([html.td(), html.td([button])])
    )).create());
    main_layout.appendChild(divResult);
    button.onclick=function(){
        var my_ajax_actionFun = procDef.action.split('/').reduce(function(o, part){ return o[part]; },my.ajax);
        console.log('x',params);
        button.disabled=true;
        my_ajax_actionFun(params).then(function(result){
            divResult.textContent = result;
            divResult.style.backgroundColor = '#5F5';
        },function(err){
            divResult.textContent = err.message;
            divResult.style.backgroundColor = 'orange';
        }).then(function(){
            button.disabled=false;
        });
    }
}

myOwn.UriSearchToObject = function UriSearchToObject(locationSearch){
    var parts=locationSearch.split('&');
    var addrParams={}
    parts.forEach(function(pair){
        if(pair[0]==='?'){
            pair=pair.substr(1);
        }
        var eq = pair.indexOf('=');
        if(eq !== -1){
            var varName=pair.substr(0, eq);
            addrParams[varName] = decodeURIComponent(pair.substr(eq+1));
        }
    });
    return addrParams;
}

myOwn.showPage = function showPage(pageDef){
    my.prepareFloating3dots();
    my.prepareRulerToggle();
    var addrParams=my.UriSearchToObject(location.search);
    if(addrParams.i){
        addrParams.i=addrParams.i.split(',');
    }else{
        addrParams.i=[];
    }
    var menu = my.displayMainMenu(addrParams);
    var w=addrParams.w;
    if(!w && menu && menu.selectedItem){
        addrParams = changing(addrParams, menu.selectedItem);
        w=menu.selectedItem.menuType;
    }
    if(typeof my.wScreens[w] === 'function'){
        my.wScreens[w].call(my, addrParams);
    }
    var rightMenu = document.getElementById('right-menu');
    rightMenu.onclick=function(){
        my.rightMenu();
    }
};

myOwn.createForkeableButton = function createForkeableButton(menu, label){
    var be = this;
    var href;
    if(!menu.w && menu.menuType){
        href = 'menu?i=' + menu.parents.concat(menu.name).join(',');
        if(menu.section){
            href += '&section='+menu.section;
        }
    }else{
        href = 'menu?' + my.paramsToUriPart(menu);
    }
    var button=html.a({"class": "menu-item", "menu-type":menu.menuType||menu.w, "menu-name":menu.name||'noname', href: href}, label || menu.label || menu.name).create();
    menu.button = button;
    button.onclick=function(event){
        if(!event.ctrlKey){
            history.pushState(null, null, href);
            my.showPage();
            event.preventDefault();
        }
    };
    return button;
};

function encodeMinimalURIComponent(text){
    return text
        .replace(/=/g, '%3D')
        .replace(/\?/g, '%3F')
        .replace(/&/g, '%26')
        .replace(/%/g, '%25');
}

myOwn.paramsToUriPart = function paramsToUriPart(params){
    return likeAr(params).map(function(value, name){
        if(value!=null){
            return name+'='+encodeMinimalURIComponent(value);
        }
    }).join('&');
}

myOwn.replaceAddrParams = function replaceAddrParams(params){
    history.replaceState(null, null, 'menu?'+my.paramsToUriPart(param));
}

myOwn.displayMenu = function displayMenu(layout, menu, addrParams, parents){
    var be = this;
    var next = false;
    var selectedItem = null;
    var newMenuLine;
    var elements=[];
    var depth = parents.length;
    var skin=be.config.config.skin;
    var skinUrl=(skin?skin+'/':'');
    if(depth===0){
        elements.push(html.img({id: "main-logo", src: skinUrl+"img/logo.png"}));
    }
    elements = elements.concat(menu.map(function(menuItem){
        menuItem.parents = parents;
        var button = my.createForkeableButton(menuItem);
        if(menuItem.name == addrParams.i[depth] || !addrParams.i[depth] && menuItem.selectedByDefault){
            button.setAttribute('menu-selected', 'yes');
            selectedItem = menuItem;
        }
        return button;
    }))
    if(depth===0){
        elements.push(html.span({id: "right-menu"}, [
            html.span({id: "active-user"}, my.config.username||"user"),
            html.img({class: "right-menu", src: skinUrl+"img/three-dot-menu.png",id: "right-menu-icon"}),
        ]));
    }
    var menuLine=html.div({id: "main-top-bar"+(depth||''), class: depth?"sub-menu-bar":"top-menu-bar"}, elements).create();
    layout.appendChild(menuLine);
    var innerSelectedItem = selectedItem;
    if(selectedItem && selectedItem.menuType === 'menu'){
        var subMenu = my.displayMenu(layout, selectedItem.menuContent, addrParams, parents.concat(selectedItem.name));
        var realign = function(){
            subMenu.menuLine.style.paddingLeft = selectedItem.button.offsetLeft + 'px';
        };
{'fraction_non_alphanumeric': 0.16527469159451383, 'fraction_numerical': 0.0035246341276530535, 'mean_word_length': 3.042118302880149, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6679379', 'n_tokens_mistral': 4043, 'n_tokens_neox': 4034, 'n_words': 610}