This repository has been archived by the owner on Oct 8, 2021. It is now read-only.
/
transitions_core.js
237 lines (187 loc) · 6.02 KB
/
transitions_core.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/*
* Transitions unit tests
*/
(function( $ ){
var instance, proto, $to, $from;
module( "Transition cleanFrom" );
test( "cleanFrom removes transition classes", function(){
var $from = $("<div>"),
transition = new $.mobile.Transition( "foo", false, $(), $from);
$from.addClass( "out in reverse foo" );
transition.cleanFrom();
ok( !$from.hasClass("out") );
ok( !$from.hasClass("in") );
ok( !$from.hasClass("reverse") );
ok( !$from.hasClass("foo") );
});
module( "Transition scrollPage", {
setup: function() {
instance = new $.mobile.Transition();
},
teardown: function() {
window.scrollTo( 0, 0 );
$( "body" ).height( $(window).height() );
}
});
test( "moves the page to the toScroll position", function() {
$( "body" ).height( 5000 );
instance.toScroll = 100;
instance.scrollPage();
equal( $(window).scrollTop(), 100, "page has been scrolled" );
});
test( "disables scrollstart for a short duration", function() {
instance.toScroll = 0;
instance.scrollPage();
equal( $.event.special.scrollstart.enabled, false, "scrollstart is disabled" );
});
module( "Transition doneIn", {
setup: function() {
$to = $("<div>");
instance = new $.mobile.Transition( "foo", "reverse", $to, "from");
instance.toggleViewportClass = $.noop;
}
});
test( "removes classes from the destination 'page'", function() {
$to.addClass( "out in reverse foo" );
instance.doneIn();
ok( !$to.hasClass("out") );
ok( !$to.hasClass("in") );
ok( !$to.hasClass("reverse") );
ok( !$to.hasClass("foo") );
});
test( "scrolls the page", function() {
expect( 1 );
// ensure the two values are different to trigger the method call
window.scrollTo( 0, 0 );
instance.toScroll = 100;
// stub to capture call
instance.scrollPage = function() {
ok(true, "scrollPage called" );
};
instance.doneIn();
});
test( "resolves the transition deferred with the requisite data", function() {
expect( 4 );
$.when( instance.deferred ).then(function( name, reverse, to, from ) {
equal( name, "foo" );
equal( reverse, "reverse" );
equal( to, $to );
equal( from, "from" );
});
instance.doneIn();
});
module( "Transition hideIn", {
setup: function() {
$to = $("<div>");
instance = new $.mobile.Transition( "foo", "reverse", $to, "from");
instance.toggleViewportClass = $.noop;
}
});
test( "sets the z-index on the to element to prevent flickering in phonegap", function() {
expect( 3 );
equal( $to.css( "z-index" ), "" );
instance.hideIn(function() {
equal( $to.css( "z-index" ), "-10" );
});
equal( $to.css( "z-index" ), "" );
});
module( "Transition startIn", {
setup: function() {
$to = $("<div>");
instance = new $.mobile.Transition( "foo", "reverse", $to, "from");
}
});
test( "sets active page class on the dom element", function() {
ok( !$to.hasClass($.mobile.activePageClass) );
instance.startIn();
ok( $to.hasClass($.mobile.activePageClass) );
});
test( "sets the height", function() {
$to.height( 10 );
equal( $to.height(), 10 );
instance.toScroll = 5;
instance.startIn( 10 );
equal( $to.height(), 15, "height is toScroll + screenheight" );
});
test( "adds the reverse class and the transition name", function() {
ok( !$to.hasClass("foo") );
instance.name = "bar";
instance.startIn( 0, "foo" );
ok( $to.hasClass("foo"), "has class 'foo'" );
ok( $to.hasClass("bar"), "has class 'bar'" );
});
module( "Transition transition", {
setup: function() {
$to = $from = $("<div>");
instance = new $.mobile.Transition( "foo", "reverse", $to, $from );
}
});
asyncTest( "runs in and out methods in order", function() {
expect( 4 );
var counter = 0, defaults;
defaults = $.extend({}, $.mobile.Transition.prototype);
// transition child classes generally set this up
instance.beforeStartOut = function() {
this.doneOut.apply(this, arguments);
};
// when the transition is anything but "none" the animation is waited for
// to fire the last step. stub and fire here
$to.animationComplete = function( callback ) {
callback();
};
instance.startOut = function() {
equal( counter, 0, "startOut is first" );
counter++;
defaults.startOut.apply(this, arguments);
};
instance.doneOut = function() {
equal( counter, 1, "doneOut is second" );
counter++;
defaults.doneOut.apply(this, arguments);
};
instance.startIn = function() {
equal( counter, 2, "startIn is first" );
counter++;
defaults.startIn.apply(this, arguments);
};
instance.doneIn = function() {
equal( counter, 3, "doneIn is fourth" );
counter++;
defaults.doneIn.apply(this, arguments);
start();
};
instance.transition();
});
test( "transition respects getMaxScrollForTransition", function() {
var transition, valuePassedToNone,
original = {
maxScroll: $.mobile.getMaxScrollForTransition,
startOut: $.mobile.Transition.prototype.startOut,
doneOut: $.mobile.Transition.prototype.doneOut,
defaultHomeScroll: $.mobile.defaultHomeScroll
},
startOutCalled = false,
doneOutCalled = false;
$.mobile.getMaxScrollForTransition = function() {
return -1;
};
$.mobile.Transition.prototype.startOut = function( screenHeight, reverseClass, none ) {
startOutCalled = true;
return original.startOut.apply( this, arguments );
};
$.mobile.Transition.prototype.doneOut = function( height, reverse, none, preventFocus ) {
doneOutCalled = true;
valuePassedToNone = none;
return original.doneOut.apply( this, arguments );
};
$.mobile.defaultHomeScroll = 0;
( new $.mobile.Transition( "slide", false, $([]), $([]) ) ).transition();
deepEqual( startOutCalled, false, "startOut was not called" );
deepEqual( doneOutCalled, true, "doneOut was called" );
deepEqual( valuePassedToNone, true, "The value passed to none was true" );
$.mobile.getMaxScrollForTransition = original.maxScroll;
$.mobile.Transition.prototype.startOut = original.startOut;
$.mobile.Transition.prototype.doneOut = original.doneOut;
$.mobile.defaultHomeScroll = original.defaultHomeScroll;
});
})( jQuery );