WME LevelReset

Script version of the WME LevelReset tool, to make relocking segments to their appropriate lock level easy & quick.

Version vom 02.12.2015. Aktuellste Version

Dieses Skript sollte nicht direkt installiert werden. Es handelt sich hier um eine Bibliothek für andere Skripte, welche über folgenden Befehl in den Metadaten eines Skriptes eingebunden wird // @require https://update.greasyfork.org/scripts/14414/91193/WME%20LevelReset.js

// ==UserScript==
// @name           WME LevelReset
// @namespace      [email protected]
// @version        0.1
// @description    Script version of the WME LevelReset tool, to make relocking segments to their appropriate lock level easy & quick.
// @author         Broos Gert '2015
// @match          https://editor-beta.waze.com/*editor/*
// @match          https://www.waze.com/*editor/*
// @icon		   data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAA+VBMVEX///93PgHX19fTgQfFZgLLcwTrxYDDgA3nqBj5+fmwr6+Yl5f8/PzExMTl5eX114vv7+/e3t68vLzOzs6saRKARQSLTgeioqK2tbX72XfU1NT515fxz4b54b3RmySYWAv31aTpwIHgrn9/f3/75qPZsEvuuC/utx3psVP13KizbhXuuVj745bfoEzzwzDxwDXTjknpxqDPfhzWih7PhUaObErowqDJchrmqCfprRjbmUvblCLZjAv71WnhnyTfmA7hrmbjsm7qxpPv06vYljj305776MvLkD3XkjFwcHCMi4v6zk/6z1P2wVDYqzr3y3j2xWnrrl761X3u0VhGAAABv0lEQVQ4jZWTXXuaMBiGY7bZQUhIoBaKsIK0KkVqtd+2tJ2gnVJs9f//mAW78uHYwe6TXE+em/flJAD8D0RVdF3HTKqvGcaMAiAQVYd1vaEASikhhFKA1ZoeA8Iwct2lCAnAxl/zdcAMbeGipbtwMQM62xFEFUJtoWEIsbh0CVTF3QGqqrjax2cq4kkkFQFjTJD2eYeXBoa4uoEoBOU/RhBUWHWHJukUCZ9JQFCnWkVAQJRQniREyvGPANA/YzazRhBKwjSOg+DZmdoRZ+r8XAfxr5eo1AfzuW1HljXfYkX2zJ5b8TQXXtbWzPff38x2hvn27qf+zFrHubC39tppGoabjczZHIZpmra9/jgXTn2vnSTJaxgecsLwNRkmsueflgV5eLZarU4y+Lk6G9YIg8HxB4PBYEfY3woZQ0529rjQ3y+Evid3ez9K9LpmWTjqe2b3Ti5xlwlHhRDYzdvvFW5NOyiEAy48Pu2VeHps2sFBIUwi5/6hWeLh3okmhdCajJyLLxUunNGktS0lgdLW+agz/lZh3Bmdt6ggZS/NUBqX152brxVuOteXDZVRafsUrxq1XGHIBb6CwHoY4Tt+A1eiQ8S/AAv7AAAAAElFTkSuQmCC
// @grant          GM_info
// @license        MIT/BSD/X11
// ==/UserScript==
// initialize LevelReset and do some checks
function LevelReset_bootstrap() {
    LevelReset_init();
}

function LevelReset_init() {
    // Check initialisation
    if (typeof Waze == 'undefined' || typeof I18n == 'undefined') {
        setTimeout(LevelReset_init, 660);
        console.log('LevelReset: Waze object unavailable, map still loading');
        return;
    }

    console.log('LevelReset: Waze - ', Waze);

    function onScreen(obj) {
        if (obj.geometry) {
            return(Waze.map.getExtent().intersectsBounds(obj.geometry.getBounds()));
        }
        return(false);  
    } 

    var UpdateObject = require("Waze/Action/UpdateObject"),
        strt = '',
        fwy_lvl = 5,
        rmp_lvl = 4,
        maj_lvl = 3,
        min_lvl = 2,
        pri_lvl = 1,
        absolute = false,
        fwy_cnt = 0,
        rmp_cnt = 0,
        maj_cnt = 0,
        min_cnt = 0,
        pri_cnt = 0,
        userlevel = Waze.loginManager.user.normalizedLevel,
        relockTab = document.createElement('li'),
        userInfo = document.getElementById('user-info'),
        navTabs = userInfo.querySelector('.nav-tabs'),
        tabContent = userInfo.querySelector('.tab-content'),
        relockContent = document.createElement('div'),
        relockTitle = document.createElement('h4'),
        relockSubTitle = document.createElement('h4'),
        relockScanbutton = document.createElement('input'),
        relockAllbutton = document.createElement('input'),
        relockSub = document.createElement('p'),
        resultsCntr = document.createElement('div'),
        warningCntr = document.createElement('div'),
        readable = {'pri':'Primary Streets (L2)','min':'Minor Highways (L3)', 'maj':'Major Highways (L4)',  'rmp':'Ramps (L5)', 'fwy':'Freeways (L6)'};

    // Begin building
    relockContent.id = 'sidepanel-relockTab';
    relockContent.className = 'tab-pane';
    relockTitle.appendChild(document.createTextNode('Relock segments'));
    relockTitle.style.marginBottom = '5px';
    relockContent.appendChild(relockTitle);
    relockTab.innerHTML = '<a href="#sidepanel-relockTab" data-toggle="tab" title="Relock segments">Re - <span class="icon-lock" id="lockcolor" style="color:green"></span></a>';

    // fill tab
    relockSub.innerHTML = 'Your on-screen area is automatically scanned when you load or pan around. Pressing the lock behind each type will relock only those results, or you can choose to relock all.<br/><br/>You can only relock segments lower or equal to your current editor level.';
    relockSub.style.cssText = 'font-size:85%';
    relockSubTitle.innerHTML = 'Results';
    relockScanbutton.type = 'button';
    relockScanbutton.value = 'Rescan area';
    relockScanbutton.style.cssText = 'margin: 10px 3px 0 0';
    relockScanbutton.onclick = function() {
        scanArea();
    };
    relockAllbutton.type = 'button';
    relockAllbutton.value = 'Relock All';
    relockAllbutton.style.cssText = 'margin: 10px 3px 0 5px';
    relockAllbutton.onclick = function() {
        relockAll();
    };

    // Remember to save
    warningCntr.style.cssText = 'margin-top:15px;border:1px solid #FFBB00;background:#FFE599;color:#664C00;border-radius:5px;padding:10px 5px;text-align:center;font-weight:bold;display:none';
    warningCntr.innerHTML = 'Remember to save these changes!';
    warningCntr.id = 'warningCntr';

    // add to stage
    navTabs.appendChild(relockTab);
    tabContent.appendChild(relockContent);
    relockContent.appendChild(relockSub);
    relockContent.appendChild(relockSubTitle);
    relockContent.appendChild(resultsCntr);
    relockContent.appendChild(relockScanbutton);
    relockContent.appendChild(relockAllbutton);
    relockContent.appendChild(warningCntr);

    // Some functions
    function relock(obj, key) {
        $.each(obj[key], function( id, value ) {
            Waze.model.actionManager.add(value);
        });
        scanArea();
       
        $('#warningCntr:hidden').fadeIn('fast');
    }

    function relockAll() {
        $('#warningCntr:hidden').fadeIn('fast');
    }

    function scanArea() {
        // Object with array of roadtypes, to collect each wrongly locked segment, for later use        
        var relockObject = {'pri':[], 'min':[], 'maj':[], 'rmp':[], 'fwy':[]};
        var foundBadlocks = false;
        var count = 0;

        // Do a count on how many segments are in need of a correct lock (limit to 150 to save CPU)
        // Count also depends on the users editor level
        Object.forEach(Waze.model.segments.objects, function(k, v) {
            if (count < 150 && v.type == "segment" && onScreen(v) && v.isGeometryEditable()) {
                // Primary (Lock 2)
                if (v.attributes.roadType == 2 && (v.attributes.lockRank < pri_lvl || (absolute && v.attributes.lockRank != pri_lvl))) {    
                    strt = Waze.model.streets.get(v.attributes.primaryStreetID);
                    relockObject.pri.push(new UpdateObject(v, {lockRank: pri_lvl}));
                    count++;
                    foundBadlocks = true;
                }
                // Minor Highway
                if (v.attributes.roadType == 7 && (v.attributes.lockRank < min_lvl || (absolute && v.attributes.lockRank != min_lvl))) {
                    strt = Waze.model.streets.get(v.attributes.primaryStreetID);
                    relockObject.min.push(new UpdateObject(v, {lockRank: min_lvl}));
                    count++;
                    foundBadlocks = true;
                }
                // Major Highway
                if (v.attributes.roadType == 6 && (v.attributes.lockRank < maj_lvl || (absolute && v.attributes.lockRank != maj_lvl))) {
                    strt = Waze.model.streets.get(v.attributes.primaryStreetID);
                    relockObject.maj.push(new UpdateObject(v, {lockRank: maj_lvl}));
                    count++;
                    foundBadlocks = true;
                }   
                // Ramps
                if (v.attributes.roadType == 4 && (v.attributes.lockRank < rmp_lvl || (absolute && v.attributes.lockRank != rmp_lvl))) {
                    strt = Waze.model.streets.get(v.attributes.primaryStreetID);
                    relockObject.rmp.push(new UpdateObject(v, {lockRank: rmp_lvl}));
                    count++;
                    foundBadlocks = true;
                }
                // Freeways
                if (v.attributes.roadType == 3 && (v.attributes.lockRank < fwy_lvl || (absolute && v.attributes.lockRank != fwy_lvl))) {
                    strt = Waze.model.streets.get(v.attributes.primaryStreetID);
                    relockObject.fwy.push(new UpdateObject(v, {lockRank: fwy_lvl}));
                    count++;
                    foundBadlocks = true;
                }
            }
        });

        // Build result to users tabpanel
        resultsCntr.innerHTML = '';
        var lvlCnt = 2; // start at 2 (pri)

        $.each(relockObject, function( key, value ) {
            var __cntr = document.createElement('div'),
                __keyLeft = document.createElement('div'),
                __lckRight = document.createElement('div'),
                __cntRight = document.createElement('div'),
                __cleardiv = document.createElement("div");                

            // Begin building
            __keyLeft.style.cssText = 'float:left';
            __keyLeft.innerHTML = readable[key];
            __lckRight.className = ((value.length !==0) ? 'icon-lock' : '');
            __cntRight.style.cssText = 'float:right';
            __cntRight.innerHTML =  ((value.length !==0) ? '<b>'+value.length+'</b>' : '-');
            __cleardiv.style.cssText ='clear:both;';

            // only add relock function if the editor's level allows it...
            if (userlevel >= lvlCnt) {
                __lckRight.style.cssText = 'width:15px;float:right;padding:3px 0 0 8px;cursor:pointer;' + ((value.length!== 0) ? 'color:red' : '' );
                __lckRight.onclick = function() {
                    relock(relockObject, key);
                };
            } else {
                // Grey out options to make it more visible
                __lckRight.className = '';
                __keyLeft.style.cssText = 'float:left;color:#777';
                __cntRight.style.cssText = 'float:right;color:#777';
                __lckRight.style.cssText = 'float:right;padding:3px 0 0 8px;color:#777;width:15px';
            }

            // Add to stage
            __cntr.appendChild(__keyLeft);
            __cntr.appendChild(__lckRight);
            __cntr.appendChild(__cntRight);
            __cntr.appendChild(__cleardiv);
            resultsCntr.appendChild(__cntr);
            lvlCnt++;
        });

        // Color the small lock icon red, if errors are found, so people can decide what to do...
        if (foundBadlocks)
            $('#lockcolor').css('color', 'red');
        else
            $('#lockcolor').css('color', 'green');
    }

    // Do a default scan once at startup
    scanArea();

    // Register some eventlisteners
    Waze.map.events.register("moveend", null, scanArea);
    Waze.model.actionManager.events.register("afteraction", null, scanArea);
    Waze.model.actionManager.events.register("afterundoaction", null, scanArea);
    Waze.model.actionManager.events.register("noActions", null, scanArea);
}
setTimeout(LevelReset_bootstrap, 3000);