scratch – Blame information for rev 117

Subversion Repositories:
Rev:
Rev Author Line No. Line
117 office 1 /*=========================
2 Controller
3 ===========================*/
4 s.controller = {
5 LinearSpline: function (x, y) {
6 var binarySearch = (function() {
7 var maxIndex, minIndex, guess;
8 return function(array, val) {
9 minIndex = -1;
10 maxIndex = array.length;
11 while (maxIndex - minIndex > 1)
12 if (array[guess = maxIndex + minIndex >> 1] <= val) {
13 minIndex = guess;
14 } else {
15 maxIndex = guess;
16 }
17 return maxIndex;
18 };
19 })();
20 this.x = x;
21 this.y = y;
22 this.lastIndex = x.length - 1;
23 // Given an x value (x2), return the expected y2 value:
24 // (x1,y1) is the known point before given value,
25 // (x3,y3) is the known point after given value.
26 var i1, i3;
27 var l = this.x.length;
28  
29 this.interpolate = function (x2) {
30 if (!x2) return 0;
31  
32 // Get the indexes of x1 and x3 (the array indexes before and after given x2):
33 i3 = binarySearch(this.x, x2);
34 i1 = i3 - 1;
35  
36 // We have our indexes i1 & i3, so we can calculate already:
37 // y2 := ((x2−x1) × (y3−y1)) ÷ (x3−x1) + y1
38 return ((x2 - this.x[i1]) * (this.y[i3] - this.y[i1])) / (this.x[i3] - this.x[i1]) + this.y[i1];
39 };
40 },
41 //xxx: for now i will just save one spline function to to
42 getInterpolateFunction: function(c){
43 if(!s.controller.spline) s.controller.spline = s.params.loop ?
44 new s.controller.LinearSpline(s.slidesGrid, c.slidesGrid) :
45 new s.controller.LinearSpline(s.snapGrid, c.snapGrid);
46 },
47 setTranslate: function (translate, byController) {
48 var controlled = s.params.control;
49 var multiplier, controlledTranslate;
50 function setControlledTranslate(c) {
51 // this will create an Interpolate function based on the snapGrids
52 // x is the Grid of the scrolled scroller and y will be the controlled scroller
53 // it makes sense to create this only once and recall it for the interpolation
54 // the function does a lot of value caching for performance
55 translate = c.rtl && c.params.direction === 'horizontal' ? -s.translate : s.translate;
56 if (s.params.controlBy === 'slide') {
57 s.controller.getInterpolateFunction(c);
58 // i am not sure why the values have to be multiplicated this way, tried to invert the snapGrid
59 // but it did not work out
60 controlledTranslate = -s.controller.spline.interpolate(-translate);
61 }
62  
63 if(!controlledTranslate || s.params.controlBy === 'container'){
64 multiplier = (c.maxTranslate() - c.minTranslate()) / (s.maxTranslate() - s.minTranslate());
65 controlledTranslate = (translate - s.minTranslate()) * multiplier + c.minTranslate();
66 }
67  
68 if (s.params.controlInverse) {
69 controlledTranslate = c.maxTranslate() - controlledTranslate;
70 }
71 c.updateProgress(controlledTranslate);
72 c.setWrapperTranslate(controlledTranslate, false, s);
73 c.updateActiveIndex();
74 }
75 if (Array.isArray(controlled)) {
76 for (var i = 0; i < controlled.length; i++) {
77 if (controlled[i] !== byController && controlled[i] instanceof Swiper) {
78 setControlledTranslate(controlled[i]);
79 }
80 }
81 }
82 else if (controlled instanceof Swiper && byController !== controlled) {
83  
84 setControlledTranslate(controlled);
85 }
86 },
87 setTransition: function (duration, byController) {
88 var controlled = s.params.control;
89 var i;
90 function setControlledTransition(c) {
91 c.setWrapperTransition(duration, s);
92 if (duration !== 0) {
93 c.onTransitionStart();
94 c.wrapper.transitionEnd(function(){
95 if (!controlled) return;
96 if (c.params.loop && s.params.controlBy === 'slide') {
97 c.fixLoop();
98 }
99 c.onTransitionEnd();
100  
101 });
102 }
103 }
104 if (Array.isArray(controlled)) {
105 for (i = 0; i < controlled.length; i++) {
106 if (controlled[i] !== byController && controlled[i] instanceof Swiper) {
107 setControlledTransition(controlled[i]);
108 }
109 }
110 }
111 else if (controlled instanceof Swiper && byController !== controlled) {
112 setControlledTransition(controlled);
113 }
114 }
115 };