dashboardCtrl.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. var dashboardCtrl = angular.module('dashboardCtrl', ['resultsFactory', 'menuService']);
  2. dashboardCtrl.controller('DashboardCtrl', ['$scope', '$rootScope', '$routeParams', '$location', 'Results', 'Runs', 'Menu', function($scope, $rootScope, $routeParams, $location, Results, Runs, 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. });
  14. } else {
  15. $scope.result = $rootScope.loadedResult;
  16. init();
  17. }
  18. }
  19. function init() {
  20. // By default, Angular sorts object's attributes alphabetically. Countering this problem by retrieving the keys order here.
  21. $scope.categoriesOrder = Object.keys($scope.result.scoreProfiles.generic.categories);
  22. $scope.globalScore = Math.max($scope.result.scoreProfiles.generic.globalScore, 0);
  23. $scope.tweetText = 'My website\'s score is ' + $scope.globalScore + '/100 on #YellowLabTools!';
  24. }
  25. $scope.showRulePage = function(ruleName) {
  26. $location.path('/result/' + $scope.runId + '/rule/' + ruleName);
  27. };
  28. $scope.testAgain = function() {
  29. Runs.save({
  30. url: $scope.result.params.url,
  31. waitForResponse: false
  32. }, function(data) {
  33. $location.path('/queue/' + data.runId);
  34. });
  35. };
  36. /// When comming from a social shared link, the user needs to click on "See full report" button to display the full dashboard.
  37. $scope.seeFullReport = function() {
  38. $scope.fromSocialShare = false;
  39. $location.search({});
  40. };
  41. $scope.shareOnTwitter = function(message) {
  42. openSocialPopup('https://twitter.com/intent/tweet?url=' + document.URL + '%3Fshare&text=' + encodeURIComponent(message));
  43. };
  44. $scope.shareOnLinkedin = function(message) {
  45. 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.'));
  46. };
  47. function openSocialPopup(url) {
  48. var winHeight = 400;
  49. var winWidth = 600;
  50. var winTop = (screen.height / 2) - (winHeight / 2);
  51. var winLeft = (screen.width / 2) - (winWidth / 2);
  52. window.open(url, 'sharer', 'top=' + winTop + ',left=' + winLeft + ',toolbar=0,status=0,width=' + winWidth + ',height=' + winHeight);
  53. }
  54. // Returns the URL of the JSON result
  55. $scope.getAPIUrl = function() {
  56. return '/api/results/' + $scope.runId;
  57. };
  58. loadResults();
  59. }]);