WME HN Tool

Highlights un-nudged house numbers

目前為 2016-01-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         	WME HN Tool
// @description		Highlights un-nudged house numbers
// @version      	1.30.1
// @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;
        
        if (!hnControl) { return; }
        
        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(currentNumber, 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.1',
			alertOnUpdate = true,
            versionChanges = 'WME Highlight HNs has been updated to ' +
                hnVersion + '.\n';

        versionChanges += 'Changes:\n';
        versionChanges += '[*] Fixed script loading problem. \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...');
            hnInit();
            /*
            $.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();
} ());