dashboardCtrl.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var dashboardCtrl = angular.module('dashboardCtrl', ['resultsFactory', 'menuService']);
  2. dashboardCtrl.controller('DashboardCtrl', ['$scope', '$rootScope', '$routeParams', '$location', 'Results', 'API', 'Menu', function($scope, $rootScope, $routeParams, $location, Results, API, Menu) {
  3. $scope.runId = $routeParams.runId;
  4. $scope.Menu = Menu.setCurrentPage('dashboard', $scope.runId);
  5. $scope.fromSocialShare = $location.search().share;
  6. function loadResults() {
  7. // Load result if needed
  8. if (!$rootScope.loadedResult || $rootScope.loadedResult.runId !== $routeParams.runId) {
  9. Results.get({runId: $routeParams.runId}, function(result) {
  10. $rootScope.loadedResult = result;
  11. $scope.result = result;
  12. init();
  13. }, function(err) {
  14. $scope.error = true;
  15. });
  16. } else {
  17. $scope.result = $rootScope.loadedResult;
  18. init();
  19. }
  20. }
  21. function init() {
  22. // By default, Angular sorts object's attributes alphabetically. Countering this problem by retrieving the keys order here.
  23. $scope.categoriesOrder = Object.keys($scope.result.scoreProfiles.generic.categories);
  24. $scope.globalScore = Math.max($scope.result.scoreProfiles.generic.globalScore, 0);
  25. $scope.tweetText = 'My website\'s score is ' + $scope.globalScore + '/100 on #YellowLabTools!';
  26. }
  27. $scope.showRulePage = function(ruleName) {
  28. $location.path('/result/' + $scope.runId + '/rule/' + ruleName);
  29. };
  30. $scope.testAgain = function() {
  31. API.launchTest($scope.result.params.url);
  32. };
  33. /// When comming from a social shared link, the user needs to click on "See full report" button to display the full dashboard.
  34. $scope.seeFullReport = function() {
  35. $scope.fromSocialShare = false;
  36. $location.search({});
  37. };
  38. $scope.shareOnTwitter = function(message) {
  39. openSocialPopup('https://twitter.com/intent/tweet?url=' + document.URL + '%3Fshare&text=' + encodeURIComponent(message));
  40. };
  41. $scope.shareOnLinkedin = function(message) {
  42. openSocialPopup('https://www.linkedin.com/shareArticle?mini=true&url=' + document.URL + '%3Fshare&title=' + encodeURIComponent(message) + '&summary=' + encodeURIComponent('YellowLabTools is a free online tool that analyzes performance and front-end quality of a webpage.'));
  43. };
  44. function openSocialPopup(url) {
  45. var winHeight = 400;
  46. var winWidth = 600;
  47. var winTop = (screen.height / 2) - (winHeight / 2);
  48. var winLeft = (screen.width / 2) - (winWidth / 2);
  49. window.open(url, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width=' + winWidth + ',height=' + winHeight);
  50. }
  51. loadResults();
  52. }]);