|
| 1 | +(function (window, $, undefined) { |
| 2 | + 'use strict'; |
| 3 | + var TopDownCompass = function(cockpit) { |
| 4 | + |
| 5 | + var context = this; |
| 6 | + |
| 7 | + // Event bindings |
| 8 | + setInterval(this.handleResize, 100); |
| 9 | + |
| 10 | + // Instance variables |
| 11 | + this.cockpit = cockpit; |
| 12 | + |
| 13 | + this.compassPanel = $("<div></div>").addClass('topdowncompasspanel'); |
| 14 | + this.compass = $("<div></div>").addClass("topdowncompass"); |
| 15 | + this.compass.append($("<div></div>").addClass("compassNeedle")); |
| 16 | + |
| 17 | + this.compassPanel.append(this.compass); |
| 18 | + |
| 19 | + // Add required UI elements |
| 20 | + $('#footercontent').append(this.compass); |
| 21 | + |
| 22 | + this.cockpit.socket.on('navdata', function (data) { |
| 23 | + if (!jQuery.isEmptyObject(data)) { |
| 24 | + |
| 25 | + context.setCompassHeading(parseFloat(data.hdgd)); |
| 26 | + |
| 27 | + } |
| 28 | + }); |
| 29 | + |
| 30 | + this.setCompassHeading(0); |
| 31 | + // test |
| 32 | + // setInterval(this.setCompassHeading(this.heading+10),250); |
| 33 | + |
| 34 | + }; |
| 35 | + /** |
| 36 | + * Set the compass heading |
| 37 | + * @param degrees |
| 38 | + */ |
| 39 | + var el, rot; |
| 40 | + TopDownCompass.prototype.setCompassHeading = function(degrees) { |
| 41 | + |
| 42 | + var aR; |
| 43 | + this.heading = this.heading || 0; // if rot undefined or 0, make 0, else rot |
| 44 | + aR = this.heading % 360; |
| 45 | + if ( aR < 0 ) { aR += 360; } |
| 46 | + if ( aR < 180 && (degrees > (aR + 180)) ) { this.heading -= 360; } |
| 47 | + if ( aR >= 180 && (degrees <= (aR - 180)) ) { this.heading += 360; } |
| 48 | + this.heading += (degrees - aR); |
| 49 | + |
| 50 | + |
| 51 | + var cp = $(".compassNeedle"); |
| 52 | + // lb = $(".compassDegValue"); |
| 53 | + |
| 54 | + //lb.text(Math.round(degrees)); |
| 55 | + |
| 56 | + cp.css({ |
| 57 | + "transform" : "rotate(" + (this.heading) + "deg)", |
| 58 | + "-ms-transform" : "rotate(" + (this.heading) + "deg)", |
| 59 | + "-webkit-transform" : "rotate(" + (this.heading) + "deg)" |
| 60 | + }) |
| 61 | + }; |
| 62 | + |
| 63 | + /** |
| 64 | + * Resize the UI |
| 65 | + */ |
| 66 | + TopDownCompass.prototype.handleResize = function() { |
| 67 | + var wn = $(window), |
| 68 | + w = wn.width(), |
| 69 | + h = wn.height(); |
| 70 | + |
| 71 | + var nd = $(".topdowncompasspanel"); |
| 72 | + if (nd.length > 0) { |
| 73 | + var pn = nd[0].parentNode; |
| 74 | + nd.css({ |
| 75 | + width: $(pn).width(), |
| 76 | + height: $(pn).height() |
| 77 | + }); |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + // Add the panel to the plugin list |
| 82 | + window.Cockpit.plugins.push(TopDownCompass); |
| 83 | +}(window, jQuery)); |
0 commit comments