~jstys-z/helioviewer.org/timeline

« back to all changes in this revision

Viewing changes to lib/helioviewer/LEGACY/SunCoordsDisplay.js

  • Committer: Keith Hughitt
  • Date: 2010-02-17 22:00:59 UTC
  • mfrom: (402.1.68 hv)
  • Revision ID: keith.hughitt@nasa.gov-20100217220059-wmdq7kgokj4seryx
Merged with Keith's branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * @author Patrick Schmiedel patrick.schmiedel@gmx.net
3
 
 */
4
 
 
5
 
/**
6
 
 * @classDescription A user interface element that shows the latitude/longitude value of the location under the mouse cursor.
7
 
 */
8
 
 /*global Class, $, $H, Event, Element */
9
 
var SunCoordsDisplay = Class.create();
10
 
 
11
 
SunCoordsDisplay.prototype = Object.extend({
12
 
        defaultOptions: $H({
13
 
                decimals: 0
14
 
        }),
15
 
        
16
 
        /**
17
 
         * @constructor
18
 
         * @param {Sring} elementId                                     The ID of the SunCoordsDisplay HTML element.
19
 
         * @param {TileLayerProvider} layerProvider     The corresponding TileLayerProvider.
20
 
         * @param {Hash} options                                        Available options: decimals
21
 
         */
22
 
        initialize: function (elementId, layerProvider, options) {
23
 
                this.domNode = $(elementId);
24
 
                this.layerProvider = layerProvider;
25
 
                
26
 
                Object.extend(this, this.defaultOptions.toObject());
27
 
                Object.extend(this, options);
28
 
                
29
 
                this.createElements();
30
 
                
31
 
                Event.observe(this.layerProvider.tileContainer.viewport.domNode, 'mousemove', this.updateCoords.bindAsEventListener(this));
32
 
        },
33
 
        
34
 
        /**
35
 
         * @method createElements       Creates the user interface HTML elements.
36
 
         */
37
 
        createElements: function () {
38
 
                var table = new Element('table', {'class': 'sunCoordsDisplay'});
39
 
                var tr = new Element('tr');
40
 
                this.latText = new Element('td', {style: 'width: 15px'});       
41
 
        this.latValue = new Element('td', {style: 'width: 25px'});      
42
 
                this.lonText = new Element('td', {style: 'width: 15px'});       
43
 
        this.lonValue = new Element('td', {style: 'width: 25px'});
44
 
                tr.appendChild(this.latText);
45
 
                tr.appendChild(this.latValue);
46
 
                tr.appendChild(this.lonText);
47
 
                tr.appendChild(this.lonValue);
48
 
                table.appendChild(tr);
49
 
                this.domNode.appendChild(table);
50
 
        },
51
 
        
52
 
        /**
53
 
         * TODO: Move to LatLon.fromXY()
54
 
         * @method XYToRad              Converts (x,y) pixel coordinates in the viewport to latitude/longitude sun coordinates.
55
 
         * @param {Number} x    The x coordinate in the viewport in pixels.
56
 
         * @param {Number} y    The y coordinate in the viewport in pixels.
57
 
         * @return {Hash}               The latitude/longitude coordinates or null if out of bounds.
58
 
         */
59
 
        XYToRad: function (x, y) {
60
 
                if (!this.layerProvider.sunImage) {
61
 
                    return null;
62
 
                }
63
 
                var sunRadius = this.layerProvider.sunImage.sunRadius * this.layerProvider.getFullSize();
64
 
                var viewerPosition = this.layerProvider.tileContainer.viewport.position;
65
 
                var y1 = (y - viewerPosition.y) / sunRadius;
66
 
                if (-1 <= y1 && y1 <= 1) {
67
 
                        var latitudeRad = Math.asin(y1);
68
 
 
69
 
                        var x1 = (x - viewerPosition.x) / sunRadius;
70
 
                        var x2 = x1 / Math.cos(latitudeRad);
71
 
                        if (-1 <= x2 && x2 <= 1) {
72
 
                                var longitudeRad = Math.asin(x2);
73
 
 
74
 
                                return { latitude: latitudeRad, longitude: longitudeRad };
75
 
                        }
76
 
                }
77
 
                return null;
78
 
        },
79
 
        
80
 
        /**
81
 
         * @method displayCoords                Shows the latitude/longitude values in the user interface element.
82
 
         * @param {Number} latitude             The latitude value.
83
 
         * @param {Number} longitude    The longitude value.
84
 
         */
85
 
        displayCoords: function (latitude, longitude) {
86
 
                var showCoords = (latitude !== null && longitude !== null);
87
 
                this.latValue.innerHTML = showCoords ? Math.abs(latitude.toFixed(this.decimals)) : '';
88
 
                this.latText.innerHTML = showCoords ? (latitude < 0 ? 'N' : 'S') : '';
89
 
                this.lonValue.innerHTML = showCoords ? Math.abs(longitude.toFixed(this.decimals)) : '';
90
 
                this.lonText.innerHTML = showCoords ? (longitude < 0 ? 'E' : 'W') : '';
91
 
        },
92
 
        
93
 
        /**
94
 
         * @method updateCoords                 Calculates the sun coordinates and shows them in the user interface.
95
 
         * @param {DOM MouseEvent} e    The DOM MouseEvent.
96
 
         */
97
 
        updateCoords: function (e) {
98
 
                if (this.layerProvider.tileContainer.viewport.surface.moving) {
99
 
                    return;
100
 
                }
101
 
                var viewportOffset = this.layerProvider.tileContainer.viewport.offset;
102
 
                var screenCoords = {
103
 
                        x: Event.pointerX(e) - viewportOffset[0],
104
 
                        y: Event.pointerY(e) - viewportOffset[1]
105
 
                };
106
 
                var screenSunCoords = this.XYToRad(screenCoords.x, screenCoords.y);
107
 
                if (screenSunCoords === null) {
108
 
                        this.displayCoords();
109
 
                } else {
110
 
                        var realSunCoordsDeg = {
111
 
                                latitude: screenSunCoords.latitude + this.layerProvider.sunImage.date.getObliquity(),
112
 
                                longitude: screenSunCoords.longitude
113
 
                        };
114
 
                        this.displayCoords(realSunCoordsDeg.latitude.toDeg(), realSunCoordsDeg.longitude.toDeg());
115
 
                }
116
 
        }
117
 
});
 
 
b'\\ No newline at end of file'