ruleCtrl.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. var ruleCtrl = angular.module('ruleCtrl', ['chart.js']);
  2. ruleCtrl.config(['ChartJsProvider', function (ChartJsProvider) {
  3. // Configure all charts
  4. ChartJsProvider.setOptions({
  5. animation: false,
  6. colours: ['#FF5252', '#FF8A80'],
  7. responsive: true
  8. });
  9. }]);
  10. ruleCtrl.controller('RuleCtrl', ['$scope', '$rootScope', '$routeParams', '$location', '$sce', 'Menu', 'Results', 'API', function($scope, $rootScope, $routeParams, $location, $sce, Menu, Results, API) {
  11. $scope.runId = $routeParams.runId;
  12. $scope.policyName = $routeParams.policy;
  13. $scope.Menu = Menu.setCurrentPage(null, $scope.runId);
  14. $scope.rule = null;
  15. function loadResults() {
  16. // Load result if needed
  17. if (!$rootScope.loadedResult || $rootScope.loadedResult.runId !== $routeParams.runId) {
  18. Results.get({runId: $routeParams.runId, exclude: 'toolsResults'}, function(result) {
  19. $rootScope.loadedResult = result;
  20. $scope.result = result;
  21. init();
  22. });
  23. } else {
  24. $scope.result = $rootScope.loadedResult;
  25. init();
  26. }
  27. }
  28. function init() {
  29. $scope.rule = $scope.result.rules[$scope.policyName];
  30. // Init "Total Weight" chart
  31. if ($scope.policyName === 'totalWeight') {
  32. $scope.weightLabels = [];
  33. $scope.weightColours = ['#7ECCCC', '#A7E846', '#FF944D', '#FFE74A', '#C2A3FF', '#5A9AED', '#FF6452', '#C1C1C1'];
  34. $scope.weightData = [];
  35. var types = ['html', 'css', 'js', 'json', 'image', 'video', 'webfont', 'other'];
  36. types.forEach(function(type) {
  37. $scope.weightLabels.push(type);
  38. $scope.weightData.push(Math.round($scope.rule.offendersObj.list.byType[type].totalWeight / 1024));
  39. });
  40. $scope.weightOptions = {
  41. tooltipTemplate: '<%=label%>: <%=value%> KB'
  42. };
  43. }
  44. // Init "Breakpoints" chart
  45. if ($scope.policyName === 'cssBreakpoints' && $scope.rule.value > 0) {
  46. // Seek for the biggest breakpoint
  47. var max = 0;
  48. $scope.rule.offendersObj.forEach(function(offender) {
  49. if (offender.pixels > max) {
  50. max = offender.pixels;
  51. }
  52. });
  53. max = Math.max(max + 100, 1400);
  54. // We group offenders 10px by 10px
  55. var GROUP_SIZE = 20;
  56. // Generate an empty array of values
  57. $scope.breakpointsLabels = [];
  58. $scope.breakpointsData = [[]];
  59. for (var i = 0; i <= max / GROUP_SIZE; i++) {
  60. $scope.breakpointsLabels[i] = '';
  61. $scope.breakpointsData[0][i] = 0;
  62. }
  63. // Fill it with results
  64. $scope.rule.offendersObj.forEach(function(offender) {
  65. var group = Math.floor((offender.pixels + 1) / GROUP_SIZE);
  66. if ($scope.breakpointsLabels[group] !== '') {
  67. $scope.breakpointsLabels[group] += '/';
  68. }
  69. $scope.breakpointsLabels[group] += offender.breakpoint;
  70. $scope.breakpointsData[0][group] += offender.count;
  71. });
  72. $scope.breakpointsColours = ['#9c4274'];
  73. $scope.breakpointsOptions = {
  74. scaleShowGridLines: false,
  75. barShowStroke: false,
  76. showTooltips: false,
  77. pointDot: false,
  78. responsive: true,
  79. maintainAspectRatio: true,
  80. strokeColor: 'rgba(20, 200, 20, 1)',
  81. scaleFontSize: 9
  82. };
  83. }
  84. }
  85. $scope.backToDashboard = function() {
  86. $location.path('/result/' + $scope.runId);
  87. };
  88. $scope.testAgain = function() {
  89. API.relaunchTest($scope.result);
  90. };
  91. loadResults();
  92. }]);