jee.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. Drupal.behaviors.init_theme = function (context) {
  2. // Growl-style system messages
  3. $('#messages-and-help > div.messages:not(.processed)')
  4. .addClass('processed')
  5. .each(function() {
  6. // If a message meets these criteria, we don't autoclose
  7. // - contains a link
  8. // - is an error or warning
  9. // - contains a lenghthy amount of text
  10. if ($('a', this).size() || $(this).is('.error') || $(this).is('.warning') || $(this).text().length > 100) {
  11. $(this).prepend("<span class='close'>X</span>");
  12. $('span.close', this).click(function() {
  13. $(this).parent().slideUp('fast');
  14. });
  15. }
  16. else {
  17. // This essentially adds a 3 second pause before hiding the message.
  18. $(this).animate({opacity:1}, 5000, 'linear', function() {
  19. $(this).slideUp('fast');
  20. });
  21. }
  22. });
  23. };
  24. jQuery(document).ready(function($) {
  25. console.log('Hello Jee');
  26. var $header = $("#header"),
  27. $chapitres = $('.node-chapitre', '#main'),
  28. chapitres_len = $chapitres.length,
  29. $container = $('#main'),
  30. centerX = $container.width()/2,
  31. centerY = $container.height()/2,
  32. // radius = Math.min($container.width(), $container.height())/2.5,
  33. // angle = Math.random() * 360,
  34. // is_dragging = false,
  35. nav_pos = {x:0, y:0},
  36. nav_timer;
  37. function init(){
  38. placeChapters();
  39. launchNav();
  40. };
  41. function placeChapters(){
  42. // Place each chapters on the ellipse contained on the screen
  43. var w2, h2, angle, c, s, radius, x, y, a = Math.random() *360;
  44. $chapitres.each(function(i, e) {
  45. w2 = $(e).outerWidth(true)/2;
  46. h2 = $(e).outerHeight(true)/2;
  47. angle = (360/chapitres_len*i+a)*Math.PI/180;
  48. // angle = randB(angle*0.95, angle*1.05);
  49. c = Math.abs(Math.cos(angle));
  50. s = Math.abs(Math.sin(angle));
  51. if (c * $container.height() > s * $container.width()) { // It crosses left or right side
  52. radius = (($container.width()/2) / c)*0.5;
  53. }else { // Top or bottom side
  54. radius = (($container.height()/2) / s)*0.5;
  55. }
  56. // change randomly radius
  57. radius = randB(radius, radius*2);
  58. x = Math.round(centerX+radius * Math.cos(angle));
  59. y = Math.round(centerY+radius * -Math.sin(angle));
  60. $(e).css({
  61. left:x-w2,
  62. top:y-h2
  63. });
  64. });
  65. };
  66. function launchNav(){
  67. var last_clientX,
  68. last_clientY,
  69. td;
  70. $(document)
  71. // DESKTOP EVENTS
  72. .bind('mousedown', function(e){
  73. last_clientX = e.clientX;
  74. last_clientY = e.clientY;
  75. $chapitres.each(function(i, e) {
  76. td = randB(0.1, 0.3);//Math.random() * (0.3 - 0.1) + 0.1;
  77. $(e).css({"transitionDuration":td+"s"});
  78. });
  79. nav_timer = setInterval(moveNav, 1000/24);
  80. $(this).bind('mousemove', function(e){
  81. nav_pos.x += e.clientX - last_clientX;
  82. nav_pos.y += e.clientY - last_clientY;
  83. last_clientX = e.clientX;
  84. last_clientY = e.clientY;
  85. });
  86. })
  87. .bind('mouseup', function(e){
  88. clearInterval(nav_timer);
  89. $(this).unbind('mousemove');
  90. })
  91. //
  92. // TOUCH EVENTS - - - - - - - - - - - - - -
  93. //
  94. .bind('touchstart', function(e){
  95. // console.log('touchstart', e);
  96. last_clientX = e.originalEvent.touches[0].clientX;
  97. last_clientY = e.originalEvent.touches[0].clientY;
  98. $chapitres.each(function(i, e) {
  99. td = randB(0.1, 0.3); //Math.random() * (0.3 - 0.1) + 0.1;
  100. $(e).css({"transitionDuration":td+"s"});
  101. });
  102. nav_timer = setInterval(moveNav, 1000/12);
  103. })
  104. .bind('touchmove', function(e){
  105. // console.log('touchmove', e);
  106. nav_pos.x += e.originalEvent.touches[0].clientX - last_clientX;
  107. nav_pos.y += e.originalEvent.touches[0].clientY - last_clientY;
  108. last_clientX = e.originalEvent.touches[0].clientX;
  109. last_clientY = e.originalEvent.touches[0].clientY;
  110. })
  111. .bind('touchend', function(e){
  112. // console.log("touchend", e);
  113. clearInterval(nav_timer);
  114. });
  115. // click to preview chapter
  116. $('h2.node-title, .field-name-field-partie:first>.field-name-field-vignette', $chapitres).bind('click', previewChapter);
  117. $('.links a', $chapitres).on('click', openChapter);
  118. };
  119. function moveNav(){
  120. // console.log("moveNav");
  121. $chapitres.each(function(i, e){
  122. $(e).stop(true, false).css({
  123. translate:[nav_pos.x, nav_pos.y]
  124. });
  125. // $(e).transition({translate:[nav_pos.x, nav_pos.y]}, 200, 'out');
  126. });
  127. $header.stop(true, false).css({
  128. translate:[nav_pos.x*0.2, nav_pos.y*0.2]
  129. // WebkitTransform:'translate('+nav_pos.x*0.2+', '+nav_pos.y*0.2+')'
  130. });
  131. };
  132. var $chapt, $parties, px, py;
  133. function previewChapter(e){
  134. e.preventDefault();
  135. // console.log('hello', this);
  136. $chapt = $(this).parents('.node-chapitre');
  137. closePreview($chapt);
  138. $parties = $('.field-name-field-partie', $chapt);
  139. $parties.each(function(i, e) {
  140. switch(i){
  141. case 0:
  142. px=randB(-40, 40); py=randB(40, 60);
  143. break;
  144. case 1:
  145. px=randB(100, 200); py=randB(120,220);
  146. break;
  147. case 2:
  148. px=randB(-200, -100); py=randB(250,320);
  149. break;
  150. case 3:
  151. px=randB(-100, 100); py=randB(320,350)+100;
  152. break;
  153. }
  154. setTimeout(
  155. (function($chapt, $part, px, py){
  156. $part
  157. .css({
  158. translate:[px, py],
  159. // opacity:1
  160. });
  161. // console.log('part', $part);
  162. // console.log('part left', $part.height());
  163. })($chapt, $(e), px, py),10);
  164. }); // each $parties
  165. $chapt.addClass('previewed');
  166. return false;
  167. }; // previewChapter
  168. function closePreview($active){
  169. $chapitres
  170. .filter('.previewed').not($active)
  171. .removeClass('previewed')
  172. .find('.field-name-field-partie')
  173. .css({transform:"none"});
  174. }; // closePreview
  175. var nid;
  176. function openChapter(e){
  177. e.preventDefault();
  178. nid = $(this).attr("href").match(/^\/node\/(\d+)/);
  179. console.log(nid);
  180. $.getJSON(
  181. '/jee/chapter/'+nid[1],
  182. {},
  183. displayChapter
  184. );
  185. return false;
  186. }; // openChapter
  187. var $chapterwrapper;
  188. function displayChapter(json, textStatus) {
  189. $chapterwrapper = $('<div>').addClass('chapter-wrapper').append(json.node).appendTo($container);
  190. // $chapterwrapper.append(json.node);
  191. console.log('displayChapter :: json', json);
  192. console.log('displayChapter :: chapterwrapper', $chapterwrapper);
  193. }; //displayChapter
  194. /* HELPERS */
  195. function randB(min, max){
  196. return Math.random() * (max - min) + min;
  197. }
  198. init();
  199. });