Debian Codegraph @

dojox/mobile/pageTurningUtils.js

raw symbols SCIP
1 define([
2 "dojo/_base/kernel",
3 "dojo/_base/array",
4 "dojo/_base/connect",
5 "dojo/_base/event",
6 "dojo/dom-class",
7 "dojo/dom-construct",
8 "dojo/dom-style",
9 "./_css3"
10 ], function(kernel, array, connect, event, domClass, domConstruct, domStyle, css3){
11 // module:
12 // dojox/mobile/pageTurningUtils
13
14 kernel.experimental("dojox.mobile.pageTurningUtils");
15
16 return function(){
17 // summary:
18 // Utilities to provide page turning effects just like turning a real book.
19 // example:
20 // | require([
21 // | "dojo/ready",
22 // | "dojox/mobile/pageTurningUtils"
23 // | ], function(ready, pageTurningUtils){
24 // | var utils = new pageTurningUtils();
25 // | ready(function(){
26 // | utils.init(300, 400); // Specify width and height by pixels
27 // | utils.initCatalog(document.getElementById("catalog"));
28 // | });
29 // | );
30 // | <div id="catalog">
31 // | <div id="page1">
32 // | <div id="front1"><img src="img1.png"></div>
33 // | <div id="back1"><img src="img2.png"></div>
34 // | </div>
35 // | <div id="page2">
36 // | <div id="front2"><img src="img3.png"></div>
37 // | <div id="back2"><img src="img4.png"></div>
38 // | </div>
39 // | <div id="page3">
40 // | <div id="front3"><img src="img5.png"></div>
41 // | <div id="back3"></div>
42 // | </div>
43 // | </div>
44
45 this.w = 0;
46 this.h = 0;
47 this.turnfrom = "top";
48 this.page = 1;
49 this.dogear = 1.0;
50 this.duration = 2;
51 this.alwaysDogeared = false;
52
53 /* Internal properties */
54 this._styleParams = {};
55 this._catalogNode = null;
56 this._currentPageNode = null;
57 this._transitionEndHandle = null;
58
59 this.init = function(/*int*/w, /*int*/h, /*String?*/turnfrom,
60 /*int?*/page, /*Number?*/dogear, /*Number?*/duration,
61 /*Boolean?*/alwaysDogeared){
62 // summary:
63 // Sets property values necessary for calculating the style parameters
64 // for page positioning and page turning effects.
65 // w: int
66 // The width of each page by pixels. You cannot specify it by percentage.
67 // h: int
68 // The height of each page by pixels. You cannot specify it by percentage.
69 // turnfrom: String?
70 // Specifies from which side/corner the page turning starts.
71 // You can choose from "top", "bottom" or "left". Defaults to "top".
72 // If "top", each page is turned from top-right corner of the page.
73 // If "bottom", each page is turned from bottom-right corner of the page.
74 // And if "left", each page is turned from top-left corner of the page.
75 // The page is shown as dog-eared except the case of "bottom".
76 // page: int?
77 // The number of pages shown in the screen at a time.
78 // This parameter should be either of 1 or 2. Defaults to 1.
79 // If 1, the only one side of two facing pages are shown.
80 // If 2, the two facing pages are shown at a time.
81 // dogear: Number?
82 // The ratio of actual dog-ear width to the maximum dog-ear width which is
83 // 11 percent of the page width (= 0.11 * w).
84 // This parameter should be a float number between 0 and 1. Defaults to 1.
85 // The actual dog-ear width is calculated by the following formula:
86 // | 0.11 * w * dogear.
87 // This parameter is ignored if "bottom" is specified to turnfrom parameter.
88 // duration: Number?
89 // The duration of page turning animations by seconds. (ex. 1.5, 3, etc)
90 // Defaults to 2.
91 // alwaysDogeared: Boolean?
92 // Specifies whether all pages are always dog-eared or not.
93 // If true, all pages are always dog-eared.
94 // If false, only the current page is dog-eared while the others are not.
95 // This parameter is ignored if "bottom" is specified to turnfrom parameter.
96
97 // Set property values
98 this.w = w;
99 this.h = h;
100 this.turnfrom = turnfrom ? turnfrom : this.turnfrom;
101 this.page = page ? page : this.page;
102 this.dogear = typeof dogear !== 'undefined' ? dogear : this.dogear;
103 this.duration = typeof duration !== 'undefined' ? duration : this.duration;
104 this.alwaysDogeared = typeof alwaysDogeared !== 'undefined' ? alwaysDogeared : this.alwaysDogeared;
105
106 if(this.turnfrom === "bottom"){ // dog-ear is not supported if using "bottom"
107 this.alwaysDogeared = true;
108 }
109 // Calculate style parameters for page positioning and page turning effects
110 this._calcStyleParams();
111 };
112
113 this._calcStyleParams = function(){
114 // tags:
115 // private
116 var tan58 = Math.tan(58 * Math.PI/180),
117 cos32 = Math.cos(32 * Math.PI/180),
118 sin32 = Math.sin(32 * Math.PI/180),
119 tan32 = Math.tan(32 * Math.PI/180),
120 w = this.w,
121 h = this.h,
122 page = this.page,
123 turnfrom = this.turnfrom,
124 params = this._styleParams;
125
126 // Calculate each div size and position based on the page turning algorithm
127 // fw: frontWidth, fh: frontHeight, dw: dogear, cx: posX, cy: posY,
128 // dx: dogearX, dy: dogearY, fy:actualPagePos
129 var fold = w * tan58,
130 Q = fold,
131 fw = Q * sin32 + Q * cos32 * tan58,
132 fh = fold + w + w/tan58,
133 dw = w * 0.11 * this.dogear,
134 pw = w - dw,
135 base = pw * cos32,
136 cx, cy, dx, dy, fy; // These params depend on the types of "turnfrom" parameter
137
138 switch(this.turnfrom){ // TODO Should separate each case as a plugin?
139 case "top":
140 cx = fw - base;
141 cy = base * tan58;
142 dx = fw - dw;
143 dy = cy + pw/tan58 - 7;
144 fy = cy/cos32;
145
146 params.init = {
147 page: css3.add({
148 top: -fy + "px",
149 left: (-fw + (page === 2 ? w : 0)) + "px",
150 width: fw + "px",
151 height: fh + "px"
152 }, {
153 transformOrigin: "100% 0%"
154 }),
155 front: css3.add({
156 width: w + "px",
157 height: h + "px"
158 }, {
159 boxShadow: "0 0"
160 }),
161 back: css3.add({
162 width: w + "px",
163 height: h + "px"
164 }, {
165 boxShadow: "0 0"
166 }),
167 shadow: {
168 display: "",
169 left: fw + "px",
170 height: h * 1.5 + "px"
171 }
172 };
173 params.turnForward = {
174 page: css3.add({}, {
175 transform: "rotate(0deg)"
176 }),
177 front: css3.add({}, {
178 transform: "translate("+ fw + "px," + fy +"px) rotate(0deg)",
179 transformOrigin: "-110px -18px"
180 }),
181 back: css3.add({}, {
182 transform: "translate("+ (fw - w) + "px," + fy +"px) rotate(0deg)",
183 transformOrigin: "0px 0px"
184 })
185 };
186 params.turnBackward = {
187 page: css3.add({}, {
188 transform: "rotate(-32deg)"
189 }),
190 front: css3.add({}, {
191 transform: "translate("+ cx + "px," + cy +"px) rotate(32deg)",
192 transformOrigin: "0px 0px"
193 }),
194 back: css3.add({}, {
195 transform: "translate("+ dx + "px," + dy +"px) rotate(-32deg)",
196 transformOrigin: "0px 0px"
197 })
198 };
199 break;
200
201 case "bottom":
202 cx = fw - (h * sin32 + w * cos32) - 2;
203 cy = fh - (h + w/tan32) * cos32;
204 dx = fw;
205 dy = fh - w/sin32 - h;
206 fy = fh - w/tan32 - h;
207
208 params.init = {
209 page: css3.add({
210 top: (-fy + 50) + "px",
211 left: (-fw + (page === 2 ? w : 0)) + "px",
212 width: fw + "px",
213 height: fh + "px"
214 }, {
215 transformOrigin: "100% 100%"
216 }),
217 front: css3.add({
218 width: w + "px",
219 height: h + "px"
220 }, {
221 boxShadow: "0 0"
222 }),
223 back: css3.add({
224 width: w + "px",
225 height: h + "px"
226 }, {
227 boxShadow: "0 0"
228 }),
229 shadow: {
230 display: "none"
231 }
232 };
233 params.turnForward = {
234 page: css3.add({}, {
235 transform: "rotate(0deg)"
236 }),
237 front: css3.add({}, {
238 transform: "translate("+ fw + "px," + fy +"px) rotate(0deg)",
239 transformOrigin: "-220px 35px"
240 }),
241 back: css3.add({}, {
242 transform: "translate("+ (w * 2) + "px," + fy +"px) rotate(0deg)",
243 transformOrigin: "0px 0px"
244 })
245 };
246 params.turnBackward = {
247 page: css3.add({}, {
248 transform: "rotate(32deg)"
249 }),
250 front: css3.add({}, {
251 transform: "translate("+ cx + "px," + cy +"px) rotate(-32deg)",
252 transformOrigin: "0px 0px"
253 }),
254 back: css3.add({}, {
255 transform: "translate("+ dx + "px," + dy +"px) rotate(0deg)",
256 transformOrigin: "0px 0px"
257 })
258 };
259 break;
260
261 case "left":
262 cx = -w;
263 cy = pw/tan32 - 2;
264 dx = -pw;
265 dy = fy = pw/sin32 + dw * sin32;
266
267 params.init = {
268 page: css3.add({
269 top: -cy + "px",
270 left: w + "px",
271 width: fw + "px",
272 height: fh + "px"
273 }, {
274 transformOrigin: "0% 0%"
275 }),
276 front: css3.add({
277 width: w + "px",
278 height: h + "px"
279 }, {
280 boxShadow: "0 0"
281 }),
282 back: css3.add({
283 width: w + "px",
284 height: h + "px"
285 }, {
286 boxShadow: "0 0"
287 }),
288 shadow: {
289 display: "",
290 left: "-4px",
291 height: ((page === 2 ? h * 1.5 : h) + 50) + "px"
292 }
293 };
294 params.turnForward = {
295 page: css3.add({}, {
296 transform: "rotate(0deg)"
297 }),
298 front: css3.add({}, {
299 transform: "translate("+ cx + "px," + cy +"px) rotate(0deg)",
300 transformOrigin: "160px 68px"
301 }),
302 back: css3.add({}, {
303 transform: "translate(0px," + cy +"px) rotate(0deg)",
304 transformOrigin: "0px 0px"
305 })
306 };
307 params.turnBackward = {
308 page: css3.add({}, {
309 transform: "rotate(32deg)"
310 }),
311 front: css3.add({}, {
312 transform: "translate("+ (-dw) + "px," + dy +"px) rotate(-32deg)",
313 transformOrigin: "0px 0px"
314 }),
315 back: css3.add({}, {
316 transform: "translate("+ dx + "px," + dy +"px) rotate(32deg)",
317 transformOrigin: "top right"
318 })
319 };
320 break;
321 }
322
323 params.init.catalog = { // catalogNode
324 width: (page === 2 ? w * 2 : w) + "px",
325 height: ((page === 2 ? h * 1.5 : h) + (turnfrom == "top" ? 0 : 50)) + "px"
326 };
327 };
328
329 this.getChildren = function(/*DomNode*/node){
330 return array.filter(node.childNodes, function(n){ return n.nodeType === 1; });
331 };
332
333 this.getPages = function(){
334 // summary:
335 // Get the array of all page nodes.
336 return this._catalogNode ? this.getChildren(this._catalogNode) : null;
337 };
338
339 this.getCurrentPage = function(){
340 // summary:
341 // Get the current page node.
342 return this._currentPageNode;
343 };
344
345 this.getIndexOfPage = function(/*DomNode*/pageNode, /*DomNode[]?*/pages){
346 if(!pages){
347 pages = this.getPages();
348 }
349 for(var i=0; i<pages.length; i++){
350 if(pageNode === pages[i]){ return i; }
351 }
352 return -1;
353 };
354
355 this.getNextPage = function(/*DomNode*/pageNode){
356 for(var n = pageNode.nextSibling; n; n = n.nextSibling){
357 if(n.nodeType === 1){ return n; }
358 }
359 return null;
360 };
361
362 this.getPreviousPage = function(/*DomNode*/pageNode){
363 for(var n = pageNode.previousSibling; n; n = n.previousSibling){
364 if(n.nodeType === 1){ return n; }
365 }
366 return null;
367 };
368
369 this.isPageTurned = function(/*DomNode*/pageNode){
370 return pageNode.style[css3.name("transform")] == "rotate(0deg)";
371 };
372
373 this._onPageTurned = function(e){
374 event.stop(e);
375 if(domClass.contains(e.target, "mblPageTurningPage")){
376 this.onPageTurned(e.target);
377 }
378 };
379
380 this.onPageTurned = function(/*DomNode*/ /*===== pageNode =====*/){
381 // summary:
382 // Stub function to which your application connects
383 // to handle the event when each page is tured
384 // description:
385 // Called just after each page is turned.
386 };
387
388 this.initCatalog = function(/*DomNode*/catalogNode){
389 // summary:
390 // Initializes the specified catalog/book.
391 // description:
392 // Apply style class and other various styles to the specified catalog node
393 // for initialization.
394 // This function must be called every time when property values
395 // are changed by calling this.init(...) function.
396 // catalogNode: DOMNode
397 // The catalog node to be initialized.
398
399 if(this._catalogNode != catalogNode){
400 if(this._transitionEndHandle){
401 connect.disconnect(this._transitionEndHandle);
402 }
403 this._transitionEndHandle = connect.connect(catalogNode, css3.name("transitionEnd"), this, "_onPageTurned");
404 this._catalogNode = catalogNode;
405 }
406
407 // Initialize catalog node
408 domClass.add(catalogNode, "mblPageTurningCatalog");
409 domStyle.set(catalogNode, this._styleParams.init.catalog);
410
411 // Initialize child pages
412 var pages = this.getPages();
413 array.forEach(pages, function(pageNode){
414 this.initPage(pageNode);
415 }, this);
416
417 this.resetCatalog();
418 };
419
420 this._getBaseZIndex = function(){
421 // Check z-index of catalogNode
422 return this._catalogNode.style.zIndex || 0;
423 };
424
425 this.resetCatalog = function(){
426 // summary:
427 // Resets the catalog by adjust the state of child pages.
428 // description:
429 // Adjust z-index and dog-ear show/hide state of each child page.
430 // This function must be called when you add a new page
431 // or remove some page after initialization.
432
433 // Adjust z-index and dog-ear of child pages
434 var pages = this.getPages(),
435 len = pages.length,
436 base = this._getBaseZIndex();
437 for(var i = len - 1; i>=0; i--){
438 var pageNode = pages[i];
439 this.showDogear(pageNode);
440 if(this.isPageTurned(pageNode)){
441 pageNode.style.zIndex = base + len + 1;
442 }else{
443 pageNode.style.zIndex = base + len - i;
444 !this.alwaysDogeared && this.hideDogear(pageNode);
445 this._currentPageNode = pageNode;
446 }
447 }
448 if(!this.alwaysDogeared && this._currentPageNode != pages[len - 1]){
449 this.showDogear(this._currentPageNode);
450 }
451 };
452
453 this.initPage = function(/*DomNode*/pageNode, /*int?*/dir){
454 // summary:
455 // Initializes the specified page.
456 // description:
457 // Apply style class and other various styles to the specified page node
458 // for initialization.
459 // This function must be called when you add a new page after initialization.
460 // pageNode: DOMNode
461 // The page node to be initialized.
462 // dir: int?
463 // Specified whether the page is turned or not at initialization.
464 // This parameter should be either of 1 or -1.
465 // If 1, the page is turned at initialization. If -1, it is not turned.
466
467 // Create shadow node if not exist
468 var childNodes = this.getChildren(pageNode);
469 while(childNodes.length < 3){
470 pageNode.appendChild(domConstruct.create("div", null));
471 childNodes = this.getChildren(pageNode);
472 }
473
474 // Check if it is the first time to initialize this page node or not
475 var isFirst = !domClass.contains(pageNode, "mblPageTurningPage");
476
477 // Apply style class
478 domClass.add(pageNode, "mblPageTurningPage");
479 domClass.add(childNodes[0], "mblPageTurningFront"); // frontNode
480 domClass.add(childNodes[1], "mblPageTurningBack"); // backNode
481 domClass.add(childNodes[2], "mblPageTurningShadow"); // shadowNode
482
483 // Apply styles
484 var p = this._styleParams.init;
485 domStyle.set(pageNode, p.page);
486 domStyle.set(childNodes[0], p.front); // frontNode
487 domStyle.set(childNodes[1], p.back); // backNode
488 p.shadow && domStyle.set(childNodes[2], p.shadow); // shadowNode
489
490 if(!dir){
491 // Determine whether to turn this page or not
492 if(isFirst && this._currentPageNode){
493 var pages = this.getPages();
494 dir = this.getIndexOfPage(pageNode) < this.getIndexOfPage(this._currentPageNode) ? 1 : -1;
495 }else{
496 dir = this.isPageTurned(pageNode) ? 1 : -1;
497 }
498 }
499 this._turnPage(pageNode, dir, 0);
500 };
501
502 this.turnToNext = function(/*Number?*/duration){
503 // summary:
504 // Turns a page forward.
505 // description:
506 // The current page is turned forward if it is not the last page of the book.
507 // duration: Number?
508 // The duration of page turning animations by seconds. (ex. 1.5, 3, etc)
509 // If this parameter is omitted, this.duration property value is used.
510 var nextPage = this.getNextPage(this._currentPageNode);
511 if(nextPage){
512 this._turnPage(this._currentPageNode, 1, duration);
513 this._currentPageNode = nextPage;
514 }
515 };
516
517 this.turnToPrev = function(/*Number?*/duration){
518 // summary:
519 // Turns a page backward.
520 // description:
521 // The current page is turned backward if it is not the first page of the book.
522 // duration: Number?
523 // The duration of page turning animations by seconds. (ex. 1.5, 3, etc)
524 // If this parameter is omitted, this.duration property value is used.
525 var prevPage = this.getPreviousPage(this._currentPageNode);
526 if(prevPage){
527 this._turnPage(prevPage, -1, duration);
528 this._currentPageNode = prevPage;
529 }
530 };
531
532 this.goTo = function(/*int*/index){
533 // summary:
534 // Jumps to the specified page without page turning animations.
535 // index: int
536 // The index of the page you want to jump to.
537 var pages = this.getPages();
538 if(this._currentPageNode === pages[index] || pages.length <= index) { return; }
539
540 var goBackward = index < this.getIndexOfPage(this._currentPageNode, pages);
541 while(this._currentPageNode !== pages[index]) {
542 goBackward ? this.turnToPrev(0) : this.turnToNext(0);
543 }
544 };
545
546 this._turnPage = function(/*DomNode*/pageNode, /*int*/dir, /*Number?*/duration){
547 // tags:
548 // private
549 var childNodes = this.getChildren(pageNode),
550 d = ((typeof duration !== 'undefined') ? duration : this.duration) + "s",
551 p = (dir === 1) ? this._styleParams.turnForward : this._styleParams.turnBackward;
552
553 // Apply styles for page turning animations
554 p.page[css3.name("transitionDuration")] = d;
555 domStyle.set(pageNode, p.page);
556
557 p.front[css3.name("transitionDuration")] = d;
558 domStyle.set(childNodes[0], p.front); // frontNode
559
560 p.back[css3.name("transitionDuration")] = d;
561 domStyle.set(childNodes[1], p.back); // backNode
562
563 // Adjust z-index and dog-ear
564 var pages = this.getPages(),
565 nextPage = this.getNextPage(pageNode),
566 len = pages.length,
567 base = this._getBaseZIndex();
568 if(dir === 1){
569 pageNode.style.zIndex = base + len + 1;
570 if(!this.alwaysDogeared && nextPage && this.getNextPage(nextPage)){
571 this.showDogear(nextPage);
572 }
573 }else{
574 if(nextPage){
575 nextPage.style.zIndex = base + len - this.getIndexOfPage(nextPage, pages);
576 !this.alwaysDogeared && this.hideDogear(nextPage);
577 }
578 }
579 };
580
581 this.showDogear = function(/*DomNode*/pageNode){
582 // summary:
583 // Shows the dog-ear.
584 var childNodes = this.getChildren(pageNode);;
585 domStyle.set(pageNode, "overflow", "");
586 childNodes[1] && domStyle.set(childNodes[1], "display", ""); // backNode
587 childNodes[2] && domStyle.set(childNodes[2], "display", this.turnfrom === "bottom" ? "none" : ""); // shadowNode
588 };
589
590 this.hideDogear = function(/*DomNode*/pageNode){
591 // summary:
592 // Hides the dog-ear.
593 if(this.turnfrom === "bottom"){ return; }
594
595 var childNodes = this.getChildren(pageNode);
596 domStyle.set(pageNode, "overflow", "visible");
597 childNodes[1] && domStyle.set(childNodes[1], "display", "none"); // backNode
598 childNodes[2] && domStyle.set(childNodes[2], "display", "none"); // shadowNode
599 };
600 };
601 });