source : PitRoad.js

  1. 'use strict';
  2. /**
  3. * This widget displays pit road information, like current speed, speed limit, and time to the stall.
  4. * It also displays yellow as you are approaching pit road speed, green when you are near it and red when you are over.
  5. * It will blink if you exceed the speed limit by more than 15 mph (25 kph).
  6. * <p>
  7. * Example:
  8. * <p><b>
  9. * <sra-pit-road></sra-pit-road><br />
  10. * </b>
  11. * <img src="../widgets/PitRoad/icon.png" />
  12. * @ngdoc directive
  13. * @name sra-pit-road
  14. * @param {integer} data-sra-args-pit-countdown The amount of time you must be from your pit before showing the count down timer. Default is -10.
  15. * @param {integer} PITCOUNTDOWN Can be set as a URL parameter, &PITCOUNTDOWN=-10.
  16. * @param {integer} data-sra-args-interval The interval, in milliseconds, that this widget will update from the server. Default is 50.
  17. * @author Jeffrey Gilliam
  18. * @since 1.0
  19. * @copyright Copyright (C) 2015 - 2024 Jeffrey Gilliam
  20. * @license Apache License 2.0
  21. */
  22. define(['SIMRacingApps','css!widgets/PitRoad/PitRoad','widgets/DataTable/DataTable'],
  23. function(SIMRacingApps) {
  24. var self = {
  25. name: "sraPitRoad",
  26. url: 'PitRoad',
  27. template: 'PitRoad.html',
  28. defaultWidth: 800,
  29. defaultHeight: 480,
  30. defaultInterval: 50 //initialize with the default interval
  31. };
  32. self.module = angular.module('SIMRacingApps'); //get the main module
  33. self.module.directive(self.name,
  34. ['sraDispatcher', '$filter', '$rootScope', '$interval',
  35. function(sraDispatcher, $filter, $rootScope, $interval) {
  36. return {
  37. restrict: 'EA',
  38. scope: true,
  39. templateUrl: sraDispatcher.getWidgetUrl(self.url) + '/' + self.template,
  40. controller: [ '$scope', function($scope) {
  41. $scope.directiveName = self.name;
  42. $scope.defaultWidth = self.defaultWidth;
  43. $scope.defaultHeight = self.defaultHeight;
  44. $scope.defaultInterval = self.defaultInterval;
  45. //load translations, if you have any un-comment this
  46. sraDispatcher.loadTranslations(sraDispatcher.getWidgetUrl(self.url),'text',function(path) {
  47. $scope.translations = sraDispatcher.getTranslation(path);
  48. });
  49. /** your code goes here **/
  50. $scope.sraPitCountDown = 10;
  51. $scope.blinkState = "OFF";
  52. $scope._blink = null;
  53. $scope.countDownClass = "SIMRacingApps-Widget-PitRoad-countdown-OFF";
  54. $scope.countDown_blink = null;
  55. $scope.updateBlink = function() {
  56. if ($scope.data.Car.REFERENCE.Status.Value.match(/PIT/)) {
  57. //var speedlimit = $scope.data.Car.REFERENCE.PitSpeedLimit.Value;
  58. //var speed = $scope.data.Car.REFERENCE.Gauge.Speedometer.ValueCurrent.Value;
  59. //var overlimit = $scope.data.Car.REFERENCE.PitSpeedLimit.UOM == "mph" ? 15.0 : 25.0;
  60. //if (speed > speedlimit + overlimit) {
  61. if ($scope.data.Car.REFERENCE.Gauge.Speedometer.ValueCurrent.State == "WAYOVERLIMIT") {
  62. if (!$scope._blink) {
  63. $scope._blink = $interval(function() {
  64. if ($scope.blinkState == "ON") {
  65. $scope.blinkState = "OFF";
  66. }
  67. else {
  68. $scope.blinkState = "ON";
  69. }
  70. }, 200);
  71. }
  72. }
  73. else {
  74. if ($scope._blink) {
  75. $interval.cancel($scope._blink);
  76. $scope._blink = null;
  77. $scope.blinkState = "OFF";
  78. }
  79. }
  80. }
  81. else {
  82. if ($scope._blink) {
  83. $interval.cancel($scope._blink);
  84. $scope._blink = null;
  85. $scope.blinkState = "OFF";
  86. }
  87. }
  88. };
  89. $scope.updateCountDown = function() {
  90. var timeToPitStall = Math.round($scope.data.Session.DiffCars.REFERENCE.PITSTALL.Value);
  91. if (timeToPitStall <= $scope.sraPitCountDown && timeToPitStall > 0) {
  92. if (!$scope.countDown_blink) {
  93. $scope.countDown_blink = $interval(function() {
  94. if ($scope.countDownClass == "SIMRacingApps-Widget-PitRoad-countdown-ON") {
  95. $scope.countDownClass = "SIMRacingApps-Widget-PitRoad-countdown-OFF";
  96. }
  97. else {
  98. $scope.countDownClass = "SIMRacingApps-Widget-PitRoad-countdown-ON";
  99. }
  100. }, 500);
  101. }
  102. }
  103. else {
  104. if ($scope.countDown_blink) {
  105. $interval.cancel($scope.countDown_blink);
  106. $scope.countDown_blink = null;
  107. $scope.countDownClass = "SIMRacingApps-Widget-PitRoad-countdown-OFF";
  108. }
  109. }
  110. };
  111. }]
  112. , link: function($scope,$element,$attrs) {
  113. //copy arguments to our scope. First if using attribute, second tag, else default to something.
  114. $attrs.sraArgsData = $attrs.sraArgsData || "";
  115. $scope.value =
  116. $scope[self.name] = sraDispatcher.getTruthy($scope.sraArgsVALUE, $attrs[self.name], $attrs.sraArgsValue, "DefaultValue");
  117. /** your code goes here **/
  118. $scope.sraPitCountdown = Math.abs(sraDispatcher.getTruthy($scope.sraPITCOUNTDOWN, $attrs.sraPITCOUNTDOWN, $attrs.sraArgsPitCountdown, $scope.sraPitCountDown) * 1);
  119. /**standard code that should be in every directive **/
  120. $rootScope.$on('sraResize', sraDispatcher.resize($scope,$element,self.defaultWidth,self.defaultHeight));
  121. //register with the dispatcher
  122. $scope.names = sraDispatcher.subscribe($scope,$attrs,self.defaultInterval); //register subscriptions and options to the dispatcher
  123. $scope.$watch("data.Car.REFERENCE.Gauge.Speedometer.ValueCurrent.Value",$scope.updateBlink);
  124. $scope.$watch("data.Session.DiffCars.REFERENCE.PITSTALL.Value", $scope.updateCountDown);
  125. }
  126. };
  127. }]);
  128. return self;
  129. });