WME Show Alt Names

Shows alt names for selected segments

当前为 2015-04-12 提交的版本,查看 最新版本

// ==UserScript==
// @name         	WME Show Alt Names
// @description		Shows alt names for selected segments
// @version      	0.01
// @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	
// ==/UserScript==

 //OL Popup Patch
/* OL License Info
Copyright 2005-2012 OpenLayers Contributors. All rights reserved. See
authors.txt for full list.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

 1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

 2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY OPENLAYERS CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of OpenLayers Contributors.
*/
OpenLayers.Popup = OpenLayers.Class({events: null,id: "",lonlat: null,div: null,contentSize: null,size: null,contentHTML: null,backgroundColor: "",opacity: "",border: "",contentDiv: null,groupDiv: null,closeDiv: null,autoSize: false,minSize: null,maxSize: null,displayClass: "olPopup",contentDisplayClass: "olPopupContent",padding: 0,disableFirefoxOverflowHack: false,fixPadding: function() {
        if (typeof this.padding == "number") {
            this.padding = new OpenLayers.Bounds(this.padding, this.padding, this.padding, this.padding);
        }
    },panMapIfOutOfView: false,keepInMap: false,closeOnMove: false,map: null,initialize: function(id, lonlat, contentSize, contentHTML, closeBox, closeBoxCallback) {
        if (id == null) {
            id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_");
        }
        this.id = id;
        this.lonlat = lonlat;
        this.contentSize = (contentSize != null) ? contentSize : new OpenLayers.Size(OpenLayers.Popup.WIDTH, OpenLayers.Popup.HEIGHT);
        if (contentHTML != null) {
            this.contentHTML = contentHTML;
        }
        this.backgroundColor = OpenLayers.Popup.COLOR;
        this.opacity = OpenLayers.Popup.OPACITY;
        this.border = OpenLayers.Popup.BORDER;
        this.div = OpenLayers.Util.createDiv(this.id, null, null, null, null, null, "hidden");
        this.div.className = this.displayClass;
        var groupDivId = this.id + "_GroupDiv";
        this.groupDiv = OpenLayers.Util.createDiv(groupDivId, null, null, null, "relative", null, "hidden");
        var id = this.div.id + "_contentDiv";
        this.contentDiv = OpenLayers.Util.createDiv(id, null, this.contentSize.clone(), null, "relative");
        this.contentDiv.className = this.contentDisplayClass;
        this.groupDiv.appendChild(this.contentDiv);
        this.div.appendChild(this.groupDiv);
        if (closeBox) {
            this.addCloseBox(closeBoxCallback);
        }
        this.registerEvents();
    },destroy: function() {
        this.id = null;
        this.lonlat = null;
        this.size = null;
        this.contentHTML = null;
        this.backgroundColor = null;
        this.opacity = null;
        this.border = null;
        if (this.closeOnMove && this.map) {
            this.map.events.unregister("movestart", this, this.hide);
        }
        this.events.destroy();
        this.events = null;
        if (this.closeDiv) {
            OpenLayers.Event.stopObservingElement(this.closeDiv);
            this.groupDiv.removeChild(this.closeDiv);
        }
        this.closeDiv = null;
        this.div.removeChild(this.groupDiv);
        this.groupDiv = null;
        if (this.map != null) {
            this.map.removePopup(this);
        }
        this.map = null;
        this.div = null;
        this.autoSize = null;
        this.minSize = null;
        this.maxSize = null;
        this.padding = null;
        this.panMapIfOutOfView = null;
    },draw: function(px) {
        if (px == null) {
            if ((this.lonlat != null) && (this.map != null)) {
                px = this.map.getLayerPxFromLonLat(this.lonlat);
            }
        }
        if (this.closeOnMove) {
            this.map.events.register("movestart", this, this.hide);
        }
        if (!this.disableFirefoxOverflowHack && OpenLayers.BROWSER_NAME == 'firefox') {
            this.map.events.register("movestart", this, function() {
                var style = document.defaultView.getComputedStyle(this.contentDiv, null);
                var currentOverflow = style.getPropertyValue("overflow");
                if (currentOverflow != "hidden") {
                    this.contentDiv._oldOverflow = currentOverflow;
                    this.contentDiv.style.overflow = "hidden";
                }
            });
            this.map.events.register("moveend", this, function() {
                var oldOverflow = this.contentDiv._oldOverflow;
                if (oldOverflow) {
                    this.contentDiv.style.overflow = oldOverflow;
                    this.contentDiv._oldOverflow = null;
                }
            });
        }
        this.moveTo(px);
        if (!this.autoSize && !this.size) {
            this.setSize(this.contentSize);
        }
        this.setBackgroundColor();
        this.setOpacity();
        this.setBorder();
        this.setContentHTML();
        if (this.panMapIfOutOfView) {
            this.panIntoView();
        }
        return this.div;
    },updatePosition: function() {
        if ((this.lonlat) && (this.map)) {
            var px = this.map.getLayerPxFromLonLat(this.lonlat);
            if (px) {
                this.moveTo(px);
            }
        }
    },moveTo: function(px) {
        if ((px != null) && (this.div != null)) {
            this.div.style.left = px.x + "px";
            this.div.style.top = px.y + "px";
        }
    },visible: function() {
        return OpenLayers.Element.visible(this.div);
    },toggle: function() {
        if (this.visible()) {
            this.hide();
        } else {
            this.show();
        }
    },show: function() {
        this.div.style.display = '';
        if (this.panMapIfOutOfView) {
            this.panIntoView();
        }
    },hide: function() {
        this.div.style.display = 'none';
    },setSize: function(contentSize) {
        this.size = contentSize.clone();
        var contentDivPadding = this.getContentDivPadding();
        var wPadding = contentDivPadding.left + contentDivPadding.right;
        var hPadding = contentDivPadding.top + contentDivPadding.bottom;
        this.fixPadding();
        wPadding += this.padding.left + this.padding.right;
        hPadding += this.padding.top + this.padding.bottom;
        if (this.closeDiv) {
            var closeDivWidth = parseInt(this.closeDiv.style.width);
            wPadding += closeDivWidth + contentDivPadding.right;
        }
        this.size.w += wPadding;
        this.size.h += hPadding;
        if (OpenLayers.BROWSER_NAME == "msie") {
            this.contentSize.w += contentDivPadding.left + contentDivPadding.right;
            this.contentSize.h += contentDivPadding.bottom + contentDivPadding.top;
        }
        if (this.div != null) {
            this.div.style.width = this.size.w + "px";
            this.div.style.height = this.size.h + "px";
        }
        if (this.contentDiv != null) {
            this.contentDiv.style.width = contentSize.w + "px";
            this.contentDiv.style.height = contentSize.h + "px";
        }
    },updateSize: function() {
        var preparedHTML = "<div class='" + this.contentDisplayClass + "'>" + 
        this.contentDiv.innerHTML + "</div>";
        var containerElement = (this.map) ? this.map.div : document.body;
        var realSize = OpenLayers.Util.getRenderedDimensions(preparedHTML, null, {displayClass: this.displayClass,containerElement: containerElement});
        var safeSize = this.getSafeContentSize(realSize);
        var newSize = null;
        if (safeSize.equals(realSize)) {
            newSize = realSize;
        } else {
            var fixedSize = {w: (safeSize.w < realSize.w) ? safeSize.w : null,h: (safeSize.h < realSize.h) ? safeSize.h : null};
            if (fixedSize.w && fixedSize.h) {
                newSize = safeSize;
            } else {
                var clippedSize = OpenLayers.Util.getRenderedDimensions(preparedHTML, fixedSize, {displayClass: this.contentDisplayClass,containerElement: containerElement});
                var currentOverflow = OpenLayers.Element.getStyle(this.contentDiv, "overflow");
                if ((currentOverflow != "hidden") && (clippedSize.equals(safeSize))) {
                    var scrollBar = OpenLayers.Util.getScrollbarWidth();
                    if (fixedSize.w) {
                        clippedSize.h += scrollBar;
                    } else {
                        clippedSize.w += scrollBar;
                    }
                }
                newSize = this.getSafeContentSize(clippedSize);
            }
        }
        this.setSize(newSize);
    },setBackgroundColor: function(color) {
        if (color != undefined) {
            this.backgroundColor = color;
        }
        if (this.div != null) {
            this.div.style.backgroundColor = this.backgroundColor;
        }
    },setOpacity: function(opacity) {
        if (opacity != undefined) {
            this.opacity = opacity;
        }
        if (this.div != null) {
            this.div.style.opacity = this.opacity;
            this.div.style.filter = 'alpha(opacity=' + this.opacity * 100 + ')';
        }
    },setBorder: function(border) {
        if (border != undefined) {
            this.border = border;
        }
        if (this.div != null) {
            this.div.style.border = this.border;
        }
    },setContentHTML: function(contentHTML) {
        if (contentHTML != null) {
            this.contentHTML = contentHTML;
        }
        if ((this.contentDiv != null) && (this.contentHTML != null) && (this.contentHTML != this.contentDiv.innerHTML)) {
            this.contentDiv.innerHTML = this.contentHTML;
            if (this.autoSize) {
                this.registerImageListeners();
                this.updateSize();
            }
        }
    },registerImageListeners: function() {
        var onImgLoad = function() {
            if (this.popup.id === null) {
                return;
            }
            this.popup.updateSize();
            if (this.popup.visible() && this.popup.panMapIfOutOfView) {
                this.popup.panIntoView();
            }
            OpenLayers.Event.stopObserving(this.img, "load", this.img._onImageLoad);
        };
        var images = this.contentDiv.getElementsByTagName("img");
        for (var i = 0, len = images.length; i < len; i++) {
            var img = images[i];
            if (img.width == 0 || img.height == 0) {
                var context = {'popup': this,'img': img};
                img._onImgLoad = OpenLayers.Function.bind(onImgLoad, context);
                OpenLayers.Event.observe(img, 'load', img._onImgLoad);
            }
        }
    },getSafeContentSize: function(size) {
        var safeContentSize = size.clone();
        var contentDivPadding = this.getContentDivPadding();
        var wPadding = contentDivPadding.left + contentDivPadding.right;
        var hPadding = contentDivPadding.top + contentDivPadding.bottom;
        this.fixPadding();
        wPadding += this.padding.left + this.padding.right;
        hPadding += this.padding.top + this.padding.bottom;
        if (this.closeDiv) {
            var closeDivWidth = parseInt(this.closeDiv.style.width);
            wPadding += closeDivWidth + contentDivPadding.right;
        }
        if (this.minSize) {
            safeContentSize.w = Math.max(safeContentSize.w, (this.minSize.w - wPadding));
            safeContentSize.h = Math.max(safeContentSize.h, (this.minSize.h - hPadding));
        }
        if (this.maxSize) {
            safeContentSize.w = Math.min(safeContentSize.w, (this.maxSize.w - wPadding));
            safeContentSize.h = Math.min(safeContentSize.h, (this.maxSize.h - hPadding));
        }
        if (this.map && this.map.size) {
            var extraX = 0, extraY = 0;
            if (this.keepInMap && !this.panMapIfOutOfView) {
                var px = this.map.getPixelFromLonLat(this.lonlat);
                switch (this.relativePosition) {
                    case "tr":
                        extraX = px.x;
                        extraY = this.map.size.h - px.y;
                        break;
                    case "tl":
                        extraX = this.map.size.w - px.x;
                        extraY = this.map.size.h - px.y;
                        break;
                    case "bl":
                        extraX = this.map.size.w - px.x;
                        extraY = px.y;
                        break;
                    case "br":
                        extraX = px.x;
                        extraY = px.y;
                        break;
                    default:
                        extraX = px.x;
                        extraY = this.map.size.h - px.y;
                        break;
                }
            }
            var maxY = this.map.size.h - 
            this.map.paddingForPopups.top - 
            this.map.paddingForPopups.bottom - 
            hPadding - extraY;
            var maxX = this.map.size.w - 
            this.map.paddingForPopups.left - 
            this.map.paddingForPopups.right - 
            wPadding - extraX;
            safeContentSize.w = Math.min(safeContentSize.w, maxX);
            safeContentSize.h = Math.min(safeContentSize.h, maxY);
        }
        return safeContentSize;
    },getContentDivPadding: function() {
        var contentDivPadding = this._contentDivPadding;
        if (!contentDivPadding) {
            if (this.div.parentNode == null) {
                this.div.style.display = "none";
                document.body.appendChild(this.div);
            }
            contentDivPadding = new OpenLayers.Bounds(OpenLayers.Element.getStyle(this.contentDiv, "padding-left"), OpenLayers.Element.getStyle(this.contentDiv, "padding-bottom"), OpenLayers.Element.getStyle(this.contentDiv, "padding-right"), OpenLayers.Element.getStyle(this.contentDiv, "padding-top"));
            this._contentDivPadding = contentDivPadding;
            if (this.div.parentNode == document.body) {
                document.body.removeChild(this.div);
                this.div.style.display = "";
            }
        }
        return contentDivPadding;
    },addCloseBox: function(callback) {
        this.closeDiv = OpenLayers.Util.createDiv(this.id + "_close", null, {w: 17,h: 17});
        this.closeDiv.className = "olPopupCloseBox";
        var contentDivPadding = this.getContentDivPadding();
        this.closeDiv.style.right = contentDivPadding.right + "px";
        this.closeDiv.style.top = contentDivPadding.top + "px";
        this.groupDiv.appendChild(this.closeDiv);
        var closePopup = callback || function(e) {
            this.hide();
            OpenLayers.Event.stop(e);
        };
        OpenLayers.Event.observe(this.closeDiv, "touchend", OpenLayers.Function.bindAsEventListener(closePopup, this));
        OpenLayers.Event.observe(this.closeDiv, "click", OpenLayers.Function.bindAsEventListener(closePopup, this));
    },panIntoView: function() {
        var mapSize = this.map.getSize();
        var origTL = this.map.getViewPortPxFromLayerPx(new OpenLayers.Pixel(parseInt(this.div.style.left), parseInt(this.div.style.top)));
        var newTL = origTL.clone();
        if (origTL.x < this.map.paddingForPopups.left) {
            newTL.x = this.map.paddingForPopups.left;
        } else 
        if ((origTL.x + this.size.w) > (mapSize.w - this.map.paddingForPopups.right)) {
            newTL.x = mapSize.w - this.map.paddingForPopups.right - this.size.w;
        }
        if (origTL.y < this.map.paddingForPopups.top) {
            newTL.y = this.map.paddingForPopups.top;
        } else 
        if ((origTL.y + this.size.h) > (mapSize.h - this.map.paddingForPopups.bottom)) {
            newTL.y = mapSize.h - this.map.paddingForPopups.bottom - this.size.h;
        }
        var dx = origTL.x - newTL.x;
        var dy = origTL.y - newTL.y;
        this.map.pan(dx, dy);
    },registerEvents: function() {
        this.events = new OpenLayers.Events(this, this.div, null, true);
        function onTouchstart(evt) {
            OpenLayers.Event.stop(evt, true);
        }
        this.events.on({"mousedown": this.onmousedown,"mousemove": this.onmousemove,"mouseup": this.onmouseup,"click": this.onclick,"mouseout": this.onmouseout,"dblclick": this.ondblclick,"touchstart": onTouchstart,scope: this});
    },onmousedown: function(evt) {
        this.mousedown = true;
        OpenLayers.Event.stop(evt, true);
    },onmousemove: function(evt) {
        if (this.mousedown) {
            OpenLayers.Event.stop(evt, true);
        }
    },onmouseup: function(evt) {
        if (this.mousedown) {
            this.mousedown = false;
            OpenLayers.Event.stop(evt, true);
        }
    },onclick: function(evt) {
        OpenLayers.Event.stop(evt, true);
    },onmouseout: function(evt) {
        this.mousedown = false;
    },ondblclick: function(evt) {
        OpenLayers.Event.stop(evt, true);
    },CLASS_NAME: "OpenLayers.Popup"});
OpenLayers.Popup.WIDTH = 200;
OpenLayers.Popup.HEIGHT = 200;
OpenLayers.Popup.COLOR = "white";
OpenLayers.Popup.OPACITY = 1;
OpenLayers.Popup.BORDER = "0px";
//End OL Popup Patch

function Popup(id, lonlat, size, html, closeBox) {
    var popup = new OL.Popup(id, lonlat, size, html, closeBox);
    popup.closeDiv.innerHTML = 'X';
    popup.panMapIfOutOfView = true;
    return popup;
}

function getSelectedSegmentInfo() {
    'use strict';
    var i, n, j, t, item, primaryName, primaryCity, primaryState, primaryCountry, altName, 
    altCity, altState, altCountry, segmentInfo, feature, selectedSegments = [];
    
    if (!W.selectionManager.hasSelectedItems()) {
        return;
    }
    for (i = 0, n = W.selectionManager.selectedItems.length; i < n; i++) {
        feature = W.selectionManager.selectedItems[i];
        item = feature.model;
        if (item.type === 'segment' && item.attributes.streetIDs.length > 0) {
            primaryName = W.model.streets.get(item.attributes.primaryStreetID);
            primaryCity = W.model.cities.get(primaryName.cityID);
            primaryState = W.model.states.get(primaryCity.stateID);
            primaryCountry = W.model.countries.get(primaryCity.countryID);
            segmentInfo = {
                segment: item,
                feature: feature,
                primary: {
                    name: primaryName.name,
                    city: primaryCity.name,
                    state: primaryState.name,
                    country: primaryCountry.name
                }
            };
            for (j = 0, t = item.attributes.streetIDs.length; j < t; j++) {
                altName = W.model.streets.get(item.attributes.streetIDs[0]);
                altCity = W.model.cities.get(altName.cityID);
                altState = W.model.states.get(altCity.stateID);
                altCountry = W.model.countries.get(altCity.countryID);
                segmentInfo['alt' + j] = {
                    name: altName.name,
                    city: altCity.name,
                    state: altState.name,
                    country: altCountry.name
                };
            }
            selectedSegments.push(segmentInfo);
        }
    }
    return selectedSegments;
}

function drawAltNames(segments) {
    'use strict';
    var i, n, p, html, seg, feature, popupLocation;
    if (!segments) {
        return;
    }
    for (i = 0, n = segments.length; i < n; i++) {
        html = '<div style="font-size:0.5em"><p><strong>Segment ID: </strong>' + 
        segments[i].segment.attributes.id + '</p>';
        html += '<table><thead><tr><th></th><th>Name</th><th>City</th>';
        html += '<th>State</th><th>Country</th></tr></thead><tbody>';
        html += '<tr><td><strong>Primary</strong></td>' + 
        '<td>' + segments[i].primary.name + '</td>' + 
        '<td>' + segments[i].primary.city + '</td>' + 
        '<td>' + segments[i].primary.state + '</td>' + 
        '<td>' + segments[i].primary.country + '</td></tr>';
        for (p in segments[i]) {
            if (!segments[i].hasOwnProperty(p) || p === 'primary' || p === 'segment' 
            || p === 'feature') {
                continue;
            }
            html += '<tr><td><strong>' + p + '</strong></td>' + 
            '<td>' + segments[i][p].name + '</td>' + 
            '<td>' + segments[i][p].city + '</td>' + 
            '<td>' + segments[i][p].state + '</td>' + 
            '<td>' + segments[i][p].country + '</td></tr>';
        }
        html += '</tbody></table></div>';
        console.log(html);
        popupLocation = segments[i].segment.geometry.getBounds().toGeometry().getCentroid();
        popupLocation.x += 20;
        popupLocation.y += 20;
        popupLocation = popupLocation.toLonLat();
        feature = new Popup(
        segments[i].segment.attributes.id, 
        popupLocation, 
        new OL.Size('auto', 'auto'), 
        html, 
        true
        );
        feature.autoSize = true;
        //feature.minSize = new OL.Size(100,20);
        segments[i].feature.popup = feature;
        W.map.addPopup(feature);
    }
}

function checkSelection() {
    var i, n, segments;
    if (W.selectionManager.hasSelectedItems()) {
        segments = getSelectedSegmentInfo();
        for (i = 0, n = segments.length; i < n; i++) {
            if (segments[i].feature.popup) {
                segments[i].feature.popup.show();
            } else {
                drawAltNames([segments[i]]);
            }
        }
    } else {
        for (i = 0, n = W.map.popups.length; i < n; i++) {
            W.map.popups[i].hide();
        }
    }
}

function init() {
    W.loginManager.events.register('afterloginchanged', null, init);
    W.selectionManager.events.register('selectionchanged', null, checkSelection);
}

function bootstrap() {
    var bGreasemonkeyServiceDefined = false;
    try {
        if ("object" === typeof Components.interfaces.gmIGreasemonkeyService) {
            bGreasemonkeyServiceDefined = true;
        }
    } catch (err) {
    /* Ignore. */
    }
    if (undefined === typeof unsafeWindow || !bGreasemonkeyServiceDefined) {
        unsafeWindow = (function() {
            var dummyElem = document.createElement('p');
            dummyElem.setAttribute('onclick', 'return window;');
            return dummyElem.onclick();
        })();
    }
    /* begin running the code! */
    if (undefined !== typeof W.loginManager.events.register) {
        window.setTimeout(init, 100);
    } else {
        window.setTimeout(function() {
            bootstrap();
        }, 1000);
    }
}

window.setTimeout(bootstrap, 100);