WME HN Tool

Highlights un-nudged house numbers

目前为 2016-01-25 提交的版本。查看 最新版本

// ==UserScript==
// @name         	WME HN Tool
// @description		Highlights un-nudged house numbers
// @version      	1.30.0
// @author			SAR85
// @copyright		SAR85
// @license		 	CC BY-NC-ND
// @grant		 	none
// @include			https://www.waze.com/editor/*
// @include			https://www.waze.com/*/editor/*
// @include			https://editor-beta.waze.com/*
// @namespace		https://greasyfork.org/users/9321
// @require         https://greasyfork.org/scripts/9794-wlib/code/wLib.js?version=76145
// ==/UserScript==

/* global W */
/* global wLib */

(function () {
    'use strict';
    var hnControl,
        hnMarkerLayer,
        messageBar;
    
    /**
     * Changes the highlight status of an HN marker.
     * @param {Object} marker The HN marker to highlight.
     * @param {Boolean} highlight True to highlight, false to unhighlight.
     */
    function changeHighlight(marker, highlight) {
        var color = highlight ? '#FFAD85' : 'white';
        if (marker) {
            marker.icon.$div.find('.uneditable-number').
                    css('background-color', color);
            marker.inputWrapper.css('background-color', color);
        }
    }
    
    /**
     * Nudges all currently loaded house numbers.
     */
    function nudgeAll() {
        var i,
            n,
            currentNumber,
            count = 0,
            hnMarkers = hnMarkerLayer.markers;

        hnControl.unselectNumber(hnControl.selectedNumber);
        
        for (i = 0, n = hnMarkers.length; i < n; i++) {
            currentNumber = hnMarkers[i];
            if (currentNumber && null === currentNumber.model.updatedBy) {
                currentNumber.model.geometry.x += 0.0001;
                hnControl.selectNumber(currentNumber);
                hnControl.onDragEnd();
                hnControl.unselectNumber(hnControl.selectedNumber);      
                changeHighlight(hnMarkers[i], false);
                count++;
            }
        }
        
        messageBar.postNewMessage(count + ' house numbers nudged.', { 
            messageType: 'info' });
    }
    
    /**
     * Highlights never-edited house numbers.
     */
	function highlightUntouched(retryCount) {
		var i, n, marker, hnMarkers;
		retryCount = retryCount || 0;
		hnMarkers = hnMarkerLayer.markers;
		if (hnMarkers.length === 0) {
			if (retryCount < 1000) {
				console.debug('HN Tool: HN Markers not found. Retry #' + (retryCount + 1));
				setTimeout(function () {
					highlightUntouched(++retryCount);
				}, 10);
			} else {
				console.debug('HN Tool: HN Markers not found. Giving up.');
				return;
			}
		}
		for (i = 0, n = hnMarkers.length; i < n; i++) {
			marker = hnMarkers[i];
			if (marker.model && null === marker.model.updatedBy) {
                changeHighlight(marker, true);
			}
		}
    }
    
    /**
     * Checks for the presence of the HN map layer.
     */
	function checkForHNLayer() {
		var layers = W.map.getLayersByName('houseNumberMarkers');
		if (layers.length > 0) {
			hnMarkerLayer = layers[0];
			highlightUntouched();
		}
    }
    
    /**
     * Stores version and changes info and alerts user.
     */
	function updateAlert() {
		var hnVersion = '1.30.0',
			alertOnUpdate = true,
            versionChanges = 'WME Highlight HNs has been updated to ' +
                hnVersion + '.\n';

        versionChanges += 'Changes:\n';
        versionChanges += '[*] Improved house number detection. \n';
        versionChanges += '[*] Improved house number highlighting after edits. \n';
        versionChanges += '[*] Added house number nudging for rank 3+ editors. With the HN window open, press Ctrl+Shift+H to nudge the loaded HNs. Please do not abuse this function--you only earn 1 point per street edited anyway so edit with care. Also remember to trigger a tile update by making a nearby edit.\n';

        if (alertOnUpdate && window.localStorage &&
            window.localStorage.hnVersion !== hnVersion) {
			window.localStorage.hnVersion = hnVersion;
			alert(versionChanges);
		}
    }
    
    /**
     * Initializes the script variables.
     */
	function hnInit() {
        var hnControlPrototype = require('Waze/Control/HouseNumbers'),
            customRender = function () {
                hnControlPrototype.prototype.render.call(hnControl);
                checkForHNLayer();
            },
            customOnDragEnd = function () {
                hnControlPrototype.prototype.onDragEnd.call(hnControl);
                changeHighlight(hnControl.selectedNumber, false);
            };
            
        var editHNs = function () {
            var e = this.model.children.clone(),
                t = this.model.children.first(),
                i = t.getEntireStreet(this.dataModel),
                y = require('Waze/Control/HouseNumbers'),
                n = new y({
                    model: this.dataModel,
                    map: W.map,
                    editable: t.canEditHouseNumbers(),
                    segments: i
                });
            n.on("destroy", function () {
                this.selectionManager.select(e);
                hnControl = null;
            }, this);
            hnControl = n;
            hnControl.render = customRender;
            hnControl.onDragEnd = customOnDragEnd;
            hnControl.render();
        };
        
        W.appPresenter.sidebar.editPanel.TYPE_TO_EDITOR_CLASS.segment.prototype.
        editHouseNumbers = editHNs;
        
		messageBar = new wLib.Interface.MessageBar({
			messagePrefix: 'WME HN Tool:'
        });
        
        if (W.loginManager.user.normalizedLevel > 2) {
            new wLib.Interface.Shortcut(
                'nudgeHN', 'editing', 'CS+h', nudgeAll, this).add();
        }

		console.debug('HN Tool: Initialized.');
		updateAlert();
    }
    
    /**
     * Checks for necessary DOM and WME elements before initialization.
     */
    function hnBootstrap(count) {
        count = count || 0;
        
		if ('undefined' !== typeof wLib && window.W &&
			window.W.map && window.W.map.events &&
            window.W.map.events.register &&
            'undefined' !== typeof W.appPresenter.sidebar.editPanel.
            TYPE_TO_EDITOR_CLASS.segment.prototype.editHouseNumbers &&
            window.require) {
            console.debug('HN Tool: Initializing...');
            $.get(W.Config.api_base + "/info/version").done(function (data) {
                if (data.version === '1.2.104-4369560') {
                    hnInit();
                } else {
                    console.error(
                        'HN Tool: WME version problem. Contact SAR85.');
                }
            }).fail(function () {
                console.error('HN Tool: WME version could not be verified.');
            });
            
		} else if (count < 10) {
			console.debug('HN Tool: Bootstrap failed. Trying again...');
			window.setTimeout(function () {
				hnBootstrap(++count);
			}, 1000);
		} else {
			console.error('HN Tool: Bootstrap error.');
		}
    }
    
	console.debug('HN Tool: Bootstrap...');
    hnBootstrap();
} ());