blob: 700bf4b0e4f6a958605769f9cac5b1dfb0b13c10 [file] [log] [blame]
zachr@google.com1bc995e2013-07-12 13:39:53 +00001angular.module('diff_viewer', []).
2config(['$routeProvider', function($routeProvider) {
3 // Show the list of differences by default
4 $routeProvider.
5 otherwise({ templateUrl: '/diff_list.html', controller: DiffListController});
6}]).
7directive('swapImg', function() {
8 // Custom directive for showing an image that gets swapped my mouseover.
9 return {
10 restrict: 'E', // The directive can be used as an element name
11 replace: true, // The directive replaces itself with the template
12 template: '<canvas ng-mouseenter="swap()" ng-mouseleave="swap()"></canvas>',
13 scope: { // The attributes below are bound to the scope
14 leftSrc: '@',
15 rightSrc: '@',
16 side: '@'
17 },
18 link: function(scope, elm, attrs, ctrl) {
19 var leftImage = new Image();
20 var rightImage = new Image();
21 var ctx = elm[0].getContext('2d');
22
23 scope.render = function() {
24 var image;
25 if (scope.side == "left") {
26 image = leftImage;
27 } else {
28 image = rightImage;
29 }
30
31 // Make it so the maximum size of an image is 500, and the images are scaled
32 // down in halves.
33 var divisor = 1;
34 while ((image.width / divisor) > 500) {
35 divisor *= 2;
36 }
37
38 // Set canvas to correct size and draw the image into it
39 elm[0].width = image.width / divisor;
40 elm[0].height = image.height / divisor;
41 ctx.drawImage(image, 0, 0, elm[0].width, elm[0].height);
42 };
43
44 // When the leftSrc attribute changes, load the image and then rerender
45 attrs.$observe('leftSrc', function(value) {
46 leftImage.src = value;
47 leftImage.onload = function() {
48 if (scope.side == "left") {
49 scope.render();
50 }
51 };
52 });
53
54 // When the rightSrc attribute changes, load the image and then rerender
55 attrs.$observe('rightSrc', function(value) {
56 rightImage.src = value;
57 rightImage.onload = function() {
58 if (scope.side == "right") {
59 scope.render();
60 }
61 };
62 });
63
64 // Swap which side to draw onto the canvas and then rerender
65 scope.swap = function() {
66 if (scope.side == "left") {
67 scope.side = "right";
68 } else {
69 scope.side = "left";
70 }
71 scope.render();
72 };
73 }
74 };
75});
76
77function DiffListController($scope) {
78 // Label each kind of differ for the sort buttons.
zachr@google.com9432c0c2013-07-09 21:08:28 +000079 $scope.differs = [
80 {
81 "title": "Different Pixels"
82 },
83 {
84 "title": "Perceptual Difference"
85 }
86 ];
zachr@google.com1bc995e2013-07-12 13:39:53 +000087
88 // Puts the records within AngularJS scope
zachr@google.com9432c0c2013-07-09 21:08:28 +000089 $scope.records = SkPDiffRecords.records;
zachr@google.com1bc995e2013-07-12 13:39:53 +000090
91 // Indicates which diff metric is used for sorting
92 $scope.sortIndex = 1;
93
94 // Called by the sort buttons to adjust the metric used for sorting
95 $scope.setSortIndex = function(idx) {
96 $scope.sortIndex = idx;
97 };
98
99 // A predicate for pulling out the number used for sorting
100 $scope.sortingDiffer = function(record) {
101 return record.diffs[$scope.sortIndex].result;
102 };
103}