jee.js 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. _ _
  3. |_|___ _ _ ___ ___ ___ ___| |_ ___ ___ ___ ___ ___ ___ ___
  4. | | -_| | | | -_|_ -| | -_| _| | -_| | . | .'| . | -_|_ -|
  5. _| |___|___|_|_|___|___| |___|_| |___|_|_|_ |__,|_ |___|___|
  6. |___| |___| |___|
  7. Bachir Soussi Chiadmi
  8. 2015
  9. */
  10. jQuery(document).ready(function($) {
  11. // console.log('Hello Jee');
  12. // http://stackoverflow.com/questions/2890898/preventing-mouse-emulation-events-ie-click-from-touch-events-in-mobile-safari
  13. // FastClick.attach(document.body);
  14. var _debug = false,
  15. _avgDelay = 1,
  16. _lastDraw = new Date,
  17. _fps,
  18. _$nav_cursor,
  19. // _History = window.History;
  20. _isInIFrame = window.location !== window.parent.location;
  21. var _$body = $('body'),
  22. _is_mobile = $('html').is('.mobile'),
  23. _$header = $("#header"),
  24. _$chapitres = $('.node-chapitre', '#main'),
  25. _chapitres_len = _$chapitres.length,
  26. _chapters = [],
  27. _$container = $('#main'),
  28. _container = {
  29. w:_$container.width(),
  30. h:_$container.height()
  31. },
  32. _center = {x:_container.w/2,y:_container.h/2},
  33. _nav_pos = {x:0, y:0},
  34. _mouse_down_pos = {x:0,y:0},
  35. _prev_mouse_pos = {x:0,y:0},
  36. _fps = 1000/12,
  37. _dragging = false, _timeout_dragging,
  38. _previewed_chapter = false;
  39. var _$chapter_wrapper = $('<div>').attr('id', 'chapter-wrapper').appendTo(_$container),
  40. _loaded_chapter = false;
  41. var _bubbles = new Array(),
  42. _stars = new Array();
  43. var _$footer = $('#footer'),
  44. _$static_wrapper = $('<div><div class="inner"></div></div>').attr('id', 'static-wrapper').appendTo(_$container),
  45. _$close_static = $('<div>').addClass('close').appendTo(_$static_wrapper);
  46. _loaded_static = false;
  47. var _$loader = $('<div>').attr('id','loader').appendTo(_$body);
  48. var _touch_started = false;
  49. function init(){
  50. if(_debug)
  51. initDebug();
  52. initChapterWrapper();
  53. initChapters();
  54. initStaticLinks();
  55. // initHistoryNav();
  56. launchNav();
  57. // FULL SCREEN
  58. var $fullscreenBtn = $('<div>Plein écran</div>').attr('id','fullscreen-btn').appendTo("#root");
  59. $fullscreenBtn.on('click', onFullScreen);
  60. // create bubbles
  61. for (var i = 2; i > 0; i--) {
  62. _bubbles.push($('<div>').addClass('bubble bubble-'+i).prependTo(_$container));
  63. };
  64. // create stars
  65. // for (var i = 20; i > 0; i--) {
  66. // _stars.push(
  67. // $('<div>')
  68. // .addClass('star star-'+i)
  69. // .css({
  70. // left:randB(-400, _container.w+400),
  71. // top:randB(-400, _container.h+400)
  72. // })
  73. // .prependTo(_$container)
  74. // );
  75. // };
  76. initHashNav();
  77. };
  78. function initDebug(){
  79. $('<p>').attr('id', 'fps').appendTo(_$container);
  80. _fps = document.getElementById('fps');
  81. requestAnimationFrame(displayFps);
  82. _$nav_cursor = $('<div id="nav-cursor"></div>').appendTo(_$container);
  83. moveDebugCursor();
  84. };
  85. function displayFps(){
  86. requestAnimationFrame(displayFps);
  87. var now = new Date;
  88. var delay = now - _lastDraw;
  89. _avgDelay += (delay - _avgDelay) / 10;
  90. _lastDraw = now;
  91. _fps.innerHTML = (1000/_avgDelay).toFixed(1) + " fps";
  92. };
  93. function moveDebugCursor(){
  94. _$nav_cursor.css({
  95. left:_nav_pos.x+_center.x+"px",
  96. top:_nav_pos.y+_center.y+"px"
  97. });
  98. };
  99. function initChapterWrapper(){
  100. if(!_isInIFrame){
  101. $('<div id="home-btn">')
  102. .on("click", onCloseChapterWrapper)
  103. .appendTo(_$chapter_wrapper);
  104. }
  105. };
  106. function onCloseChapterWrapper(e){
  107. _loaded_chapter.stopAndClose();
  108. _loaded_chapter = false;
  109. _$chapter_wrapper.removeClass('visible');
  110. _$body.removeClass('chapter-displayed');
  111. setTimeout(function(){
  112. _$chapter_wrapper.find('.node').remove();
  113. }, 2000);
  114. updateHash('');
  115. };
  116. function initChapters(){
  117. // Place each chapters on the ellipse contained on the screen
  118. var base_a = Math.random() *360;
  119. _$chapitres.each(function(i, e) {
  120. // Lets create the chapter object and place him self
  121. _chapters.push(new Chapter(i, e, base_a));
  122. });
  123. };
  124. function launchNav(){
  125. $("#block-system-main")
  126. /*
  127. ____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
  128. | \| __| __| | |_ _| | _ | | __| | | __| | |_ _| __|
  129. | | | __|__ | -| | | | | | __| | __| | | __| | | | | | |__ |
  130. |____/|_____|_____|__|__| |_| |_____|__| |_____|\___/|_____|_|___| |_| |_____|
  131. */
  132. .on('mousemove', function(e){
  133. e.preventDefault();
  134. // prevent firing mousemove with touch events
  135. if(_touch_started)
  136. return false;
  137. console.log('document mousemove');
  138. clearTimeout(_timeout_dragging);
  139. // if(_previewed_chapter || _loaded_chapter || _loaded_static)
  140. // return false;
  141. // activate dragging if already activated
  142. if(!_dragging){
  143. _dragging = true;
  144. startMoveNav();
  145. }else{
  146. updateNavPos(e.clientX, e.clientY, false, false);
  147. }
  148. })
  149. .on('mouseup', function(e){
  150. e.preventDefault();
  151. if(e.target !== this)
  152. return false;
  153. console.log('document mouseup');
  154. if(_loaded_chapter || _loaded_static)
  155. return false;
  156. for (var i = _chapitres_len - 1; i >= 0; i--)
  157. _chapters[i].closePreview().unMitigate();
  158. })
  159. /*
  160. _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
  161. |_ _| | | | | | | | __| | | __| | |_ _| __|
  162. | | | | | | | --| | | __| | | __| | | | | | |__ |
  163. |_| |_____|_____|_____|__|__| |_____|\___/|_____|_|___| |_| |_____|
  164. */
  165. .on('touchstart', function(e){
  166. // e.preventDefault();
  167. console.log('document touchstart');
  168. _touch_started = true;
  169. clearTimeout(_timeout_dragging);
  170. if(_loaded_chapter || _loaded_static)
  171. return false;
  172. // set initial pos
  173. updateNavPos(e.originalEvent.touches[0].clientX, e.originalEvent.touches[0].clientY, true, true);
  174. // activate dragging if already activated
  175. if(!_dragging){
  176. _dragging = true;
  177. startMoveNav();
  178. }
  179. })
  180. .on('touchmove', function(e){
  181. e.preventDefault();
  182. console.log('document touchmove');
  183. if(_loaded_chapter || _loaded_static)
  184. return false;
  185. updateNavPos(e.originalEvent.touches[0].clientX, e.originalEvent.touches[0].clientY, true, false);
  186. })
  187. .on('touchend', function(e){
  188. // e.preventDefault();
  189. console.log("document touchend");
  190. if(_loaded_chapter || _loaded_static)
  191. return false;
  192. stopMoveNav();
  193. setTimeout(function(){_touch_started = false;}, 500);
  194. });
  195. // TODO : nav on scroll events
  196. };
  197. function updateNavPos(x,y,touch,init){
  198. if(touch){
  199. if(!init){
  200. _nav_pos.x += x - _prev_mouse_pos.x;
  201. _nav_pos.y += y - _prev_mouse_pos.y;
  202. // constrain nav pos on container
  203. _nav_pos.x = _nav_pos.x < -_center.x*2
  204. ? -_center.x*2
  205. : _nav_pos.x > _center.x*2
  206. ? _center.x*2
  207. : _nav_pos.x;
  208. _nav_pos.y = _nav_pos.y < -_center.y*2
  209. ? -_center.y*2
  210. : _nav_pos.y > _center.y*2
  211. ? _center.y*2
  212. : _nav_pos.y;
  213. }
  214. }else{
  215. _nav_pos.x = (_center.x -x)*0.5;
  216. _nav_pos.y = (_center.y -y)*0.5;
  217. }
  218. _prev_mouse_pos.x = x;
  219. _prev_mouse_pos.y = y;
  220. // debuging cursor
  221. if(_debug)
  222. moveDebugCursor();
  223. };
  224. function startMoveNav(){
  225. window.requestAnimationFrame(moveNav);
  226. };
  227. function moveNav(){
  228. // console.log("moveNav");
  229. if(!_dragging) return;
  230. window.requestAnimationFrame(moveNav);
  231. // move chapters
  232. for (var i = _chapitres_len - 1; i >= 0; i--)
  233. _chapters[i].move();
  234. // move header
  235. // _$header.css({
  236. // transform:"translate3d("+(_nav_pos.x)*0.2+"px, "+(_nav_pos.y)*0.2+"px,0)"
  237. // });
  238. _$header.translate3d({x:_nav_pos.x*0.2,y:_nav_pos.y*0.2});
  239. // bubbles and stars dont move so smoothly on tablette ...
  240. // should keep it only on desktop
  241. // move bubbles
  242. // for (var i = _bubbles.length - 1; i >= 0; i--) {
  243. // _bubbles[i].translate3d({x:_nav_pos.x*0.4,y:_nav_pos.y*0.4});
  244. // };
  245. // move stars
  246. // for (var i = _stars.length - 1; i >= 0; i--) {
  247. // _stars[i].translate3d({x:_nav_pos.x*-0.3,y:_nav_pos.y*-0.3});
  248. // };
  249. };
  250. function stopMoveNav(){
  251. console.log('stopMoveNav');
  252. clearTimeout(_timeout_dragging);
  253. _timeout_dragging = setTimeout(function(){
  254. console.log("dragging stoped");
  255. _dragging = false;
  256. },3000);
  257. };
  258. function onFullScreen(e){
  259. if(_$body.is('fullscreen')){
  260. exitFullscreen();
  261. _$body.removeClass('fullscreen');
  262. }else{
  263. launchIntoFullscreen(document.getElementById("root"));
  264. _$body.addClass('fullscreen');
  265. }
  266. if(_loaded_chapter)
  267. setTimeout(_loaded_chapter.buildVideos(), 100);
  268. };
  269. /*
  270. __ __
  271. / /_ ____ ______/ /_ ____ ____ __ __
  272. / __ \/ __ `/ ___/ __ \ / __ \/ __ `/ | / /
  273. / / / / /_/ (__ ) / / / / / / / /_/ /| |/ /
  274. /_/ /_/\__,_/____/_/ /_/ /_/ /_/\__,_/ |___/
  275. */
  276. function updateHash(h){
  277. window.location.hash = h;
  278. };
  279. function initHashNav(){
  280. if(window.location.hash !== ''){
  281. // console.log("hash = "+window.location.hash);
  282. var p = window.location.hash.match('^#principe-([0-9]+)$');
  283. // console.log("p :", p);
  284. if(p){
  285. // console.log($('li.node-readmore a[href="/node/'+p[1]+'"]', 'section#node-'+p[1]));
  286. setTimeout((function(nid){
  287. $('li.node-readmore a[href="/node/'+nid+'"]', 'section#node-'+nid).trigger('click').delay(1000).trigger('click');
  288. }(p[1])), 500);
  289. }else{
  290. var h = window.location.hash.match('^#(.+)$');
  291. // console.log("h :", h);
  292. setTimeout((function(href){
  293. $('a[href="'+href+'"]', '#block-menu-menu-footer-menu').trigger('click');
  294. }(h[1])), 500);
  295. }
  296. }
  297. };
  298. /*
  299. _____ _____ _____ _____ _____ _____ _____
  300. | __|_ _| _ |_ _| | | __|
  301. |__ | | | | | | | |- -| --|__ |
  302. |_____| |_| |__|__| |_| |_____|_____|_____|
  303. */
  304. function initStaticLinks(){
  305. $('ul.menu a', _$footer).on('click', onClickStaticLink);
  306. _$close_static.on('click', onCloseStatic);
  307. };
  308. function onClickStaticLink(e){
  309. e.preventDefault();
  310. _$static_wrapper.removeClass('visible');
  311. loadStaticContent($(this).attr("href"));
  312. _$body.addClass('loading');
  313. _loaded_static = true;
  314. return false;
  315. };
  316. function loadStaticContent(h){
  317. // change hash address bar
  318. updateHash(h);
  319. $.getJSON(
  320. '/jee/static',
  321. {"href":h},
  322. onStaticLoaded
  323. );
  324. };
  325. function onStaticLoaded(data, textstatus){
  326. console.log('data',data);
  327. displayStatic(data.node);
  328. };
  329. function displayStatic(node){
  330. _$static_wrapper.addClass('visible').find('.inner').html(node);
  331. _$body.removeClass('loading');
  332. };
  333. function onCloseStatic(e){
  334. updateHash('');
  335. _$static_wrapper.removeClass('visible');
  336. _loaded_static = false;
  337. };
  338. /*
  339. ________ __
  340. / ____/ /_ ____ _____ / /____ _____
  341. / / / __ \/ __ `/ __ \/ __/ _ \/ ___/
  342. / /___/ / / / /_/ / /_/ / /_/ __/ /
  343. \____/_/ /_/\__,_/ .___/\__/\___/_/
  344. /_/
  345. */
  346. function Chapter(i, e, base_a){
  347. // $e.obj = this;
  348. this.i = i;
  349. this.e = e;
  350. this.$e = $(e);
  351. this.nid = this.$e.attr("id").match(/^node-(\d+)/)[1];
  352. this.geom = {
  353. base_a:base_a,
  354. a:0,
  355. r:0,
  356. w:this.$e.outerWidth(true),
  357. h:this.$e.outerHeight(true)
  358. }
  359. this.pos = {x:0,y:0};
  360. this.trans = {x:0, y:0,z:0};
  361. this.trans_previewed = {x:0, y:0,z:0};
  362. this.ease = randB(0.05, 0.2);
  363. //drifting
  364. // this.$title = $('h2.node-title', this.$e);
  365. // this.$content = $('.content:first', this.$e);
  366. // this.title_x = 0;
  367. // this.content_x = 0;
  368. // this.drifting_direction = Math.random()-0.5 > 0 ? 1 : -1;
  369. // this.drifting_time = null;
  370. //preview
  371. this.is_previewed = false;
  372. //mitigate
  373. this.is_mitigated = false;
  374. //parties
  375. this.$parties = $('.field-name-field-partie', e);
  376. this.parts_pos = {xs:new Array(), ys:new Array()};
  377. this.lines = new Array();
  378. this.displayPreviewAnimeStartTime = 0,
  379. this.displayPreviewAnimeDuration = 4000, // milli sec
  380. this.are_lines_animated = false;
  381. // chapter
  382. this.$n = false;
  383. this.$blocks = false;
  384. this.$vids_container = false;
  385. this.$vids = false;
  386. this.texts_pos = shuffleArray([1,2,3]);
  387. this.dimvideo = {w:0, h:0};
  388. this.$slider = null;
  389. this.cur_vid_playing = 0;
  390. // prototypes
  391. if (typeof Chapter.initialized == "undefined") {
  392. Chapter.prototype.init = function(){
  393. this.setInitPos();
  394. this.drawLines();
  395. this.setEvents();
  396. // this.initDrifiting();
  397. // enable nodes after intro anime
  398. setTimeout(
  399. (function(t){
  400. return function(){
  401. t.$e.addClass('enabled');
  402. }
  403. }(this)),
  404. randB(5000, 8000));
  405. };
  406. Chapter.prototype.setInitPos = function(){
  407. // distribute elements arround the center
  408. this.geom.a = (360/_chapitres_len*this.i+this.geom.base_a)*Math.PI/180;
  409. // console.log("Chapter :: setInitPos", this.$e);
  410. this.geom.c = Math.cos(this.geom.a);
  411. this.geom.s = Math.sin(this.geom.a);
  412. this.geom.abs_c = Math.abs(this.geom.c);
  413. this.geom.abs_s = Math.abs(this.geom.s);
  414. if (this.geom.abs_c * _container.h > this.geom.abs_s * _container.w) {
  415. // It crosses left or right side
  416. this.geom.r = (_center.x / this.geom.abs_c)*0.5;
  417. }else {
  418. // Top or bottom side
  419. this.geom.r = (_center.y / this.geom.abs_s)*0.5;
  420. }
  421. // change randomly radius
  422. if(this.i%2){
  423. this.geom.r = randB(this.geom.r*1.5, this.geom.r*2);
  424. }else{
  425. this.geom.r = randB(this.geom.r*0.8, this.geom.r*1.5);
  426. }
  427. this.pos.x = Math.round(_center.x+this.geom.r * this.geom.c) - this.geom.w/2;
  428. this.pos.y = Math.round(_center.y+this.geom.r * -this.geom.s) - this.geom.h/2;
  429. // calculate the translation needed for the previewed position (center top)
  430. this.trans_previewed.x = -(this.pos.x - _center.x) - this.geom.w*0.5;
  431. this.trans_previewed.y = -this.pos.y+this.$e.find('.node-title').height()*1.3;
  432. this.$e.css({
  433. left:this.pos.x,
  434. top:this.pos.y
  435. });
  436. };
  437. Chapter.prototype.setEvents = function(){
  438. //http://technify.me/user-experience/javascript/jquery/trigger-custom-events-with-jquery/
  439. // click to preview chapter
  440. // $('h2.node-title, .field-name-field-partie:first>.field-name-field-vignette', this.$e)
  441. this.$e
  442. .on('click', this, function(e){
  443. if(_is_mobile){
  444. e.data.loadNode(e);
  445. }else{
  446. e.data.preview(e);
  447. }
  448. });
  449. // .on('click', this, function(e){
  450. // // e.stopImmediatePropagation();
  451. // console.log('click on chapter');
  452. // e.stopPropagation();
  453. // e.preventDefault();
  454. // e.data.preview(e);
  455. // return false;
  456. // });
  457. $('.links a, .field-name-field-vignette img', this.$e)
  458. .on('click', this, function(e){
  459. if(e.data.is_previewed){
  460. console.log('click, is previewed');
  461. e.stopPropagation();
  462. e.preventDefault();
  463. e.data.loadNode();
  464. return false;
  465. }
  466. });
  467. };
  468. // Chapter.prototype.initDrifiting = function(){
  469. // // an other option could be to drift the whole page with the same engine than draging
  470. // requestAnimationFrame(this.drift.bind(this));
  471. // };
  472. // Chapter.prototype.drift = function(timestamp){
  473. // requestAnimationFrame(this.drift.bind(this));
  474. // var now = new Date().getTime(),
  475. // dt = now - (this.drifting_time || now);
  476. // this.drifting_time = now;
  477. // if(!this.is_previewed){
  478. // this.title_x += (10/1000)*dt*this.drifting_direction;
  479. // this.content_x += (6/1000)*dt*this.drifting_direction;
  480. // this.drifting_direction =
  481. // this.content_x > randB(100,130)
  482. // ? -1
  483. // : this.content_x < -randB(100,130)
  484. // ? 1
  485. // : this.drifting_direction;
  486. // this.$title.translate3d({x:this.title_x});
  487. // this.$content.translate3d({x:this.content_x});
  488. // }
  489. // };
  490. Chapter.prototype.move = function(){
  491. if(this.is_previewed)
  492. return false;
  493. this.trans.x += (_nav_pos.x - this.trans.x)*this.ease;
  494. this.trans.y += (_nav_pos.y - this.trans.y)*this.ease;
  495. // this.trans.z = Math.floor(Math.sqrt(
  496. // Math.pow(
  497. // _center.x-(this.pos.x+this.trans.x)
  498. // ,2
  499. // )
  500. // +
  501. // Math.pow(
  502. // _center.y-(this.pos.y+this.trans.y)
  503. // ,2
  504. // )
  505. // ));
  506. // if(this.i === 0)
  507. // console.log(this.i+" this.trans.z", this.trans.z);
  508. this.$e.translate3d({x:this.trans.x,y:this.trans.y});
  509. };
  510. Chapter.prototype.preview = function(e){
  511. e.stopPropagation();
  512. e.preventDefault();
  513. console.log('preview : e',e);
  514. console.log('preview : this',this);
  515. // don't relaunch preview more that one time
  516. if(this.is_previewed) return false;
  517. console.log('is_previewed', this.is_previewed);
  518. this.is_previewed = true;
  519. _previewed_chapter = this;
  520. this.unMitigate();
  521. // close other chapters
  522. for (var i = _chapitres_len - 1; i >= 0; i--)
  523. if(i !== this.i)
  524. _chapters[i].closePreview().mitigate();
  525. this.displayPreview();
  526. return false;
  527. };
  528. Chapter.prototype.displayPreview = function(e){
  529. // define randomly position of parties
  530. this.resetPartsPos();
  531. this.are_lines_animated = false;
  532. requestAnimationFrame(this.animePreviewDisplay.bind(this));
  533. };
  534. Chapter.prototype.resetPartsPos = function(){
  535. this.parts_pos = {xs:new Array(), ys:new Array()};
  536. var h = _container.h * 0.6;
  537. for (var i = 0; i < 2; i++) {
  538. switch(i){
  539. case 0:
  540. this.parts_pos.xs.push(randB(180,280));
  541. this.parts_pos.ys.push(randB(h*0.55,h*0.6));
  542. break;
  543. case 1:
  544. this.parts_pos.xs.push(randB(-280,-180));
  545. this.parts_pos.ys.push(randB(h*0.65,h));
  546. break;
  547. }
  548. };
  549. this.parts_pos.xs = shuffleArray(this.parts_pos.xs);
  550. this.parts_pos.ys = shuffleArray(this.parts_pos.ys);
  551. // always place the first circle on top center
  552. this.parts_pos.xs.unshift(randB(-30,30));
  553. this.parts_pos.ys.unshift(120);
  554. };
  555. Chapter.prototype.closePreview = function(){
  556. if(!this.is_previewed) return this;
  557. this.$e.removeClass('previewed')
  558. .find('.field-name-field-partie')
  559. .css({transform:"none"});
  560. this.is_previewed = false;
  561. _previewed_chapter = false;
  562. requestAnimationFrame(this.animePreviewDisplay.bind(this));
  563. return this;
  564. };
  565. Chapter.prototype.mitigate = function(){
  566. if(this.is_mitigated) return this;
  567. this.$e.addClass('mitigated');
  568. return this;
  569. };
  570. Chapter.prototype.unMitigate = function(){
  571. if(this.is_mitigated) return this;
  572. this.$e.removeClass('mitigated');
  573. return this;
  574. };
  575. Chapter.prototype.drawLines = function(){
  576. for (var i = 0; i < 2; i++) {
  577. this.lines.push({
  578. $line:$("<div>").addClass('line', 'line-'+i).prependTo(this.$parties[i])
  579. });
  580. };
  581. };
  582. Chapter.prototype.animePreviewDisplay = function(timestamp){
  583. // console.log("anime line "+this.nid);
  584. // get the time on first anime launch
  585. if(this.displayPreviewAnimeStartTime === 0)
  586. this.displayPreviewAnimeStartTime = timestamp;
  587. // limit the animation time
  588. if(timestamp - this.displayPreviewAnimeStartTime < this.displayPreviewAnimeDuration){
  589. requestAnimationFrame(this.animePreviewDisplay.bind(this));
  590. }else{
  591. this.displayPreviewAnimeStartTime = 0;
  592. }
  593. // first move chapter to previewed pos (center top screen)
  594. if(this.is_previewed && !this.are_lines_animated){
  595. this.trans.x = this.trans.x + (this.trans_previewed.x - this.trans.x)*0.4;
  596. this.trans.y = this.trans.y + (this.trans_previewed.y - this.trans.y)*0.4;
  597. this.$e.translate3d({x:this.trans.x,y:this.trans.y});
  598. // append just once when positioning is done
  599. if(Math.abs(this.trans_previewed.x - this.trans.x) < 5){
  600. // apply new position to parties
  601. var that = this;
  602. this.$parties.each(function(i, e) {
  603. setTimeout(
  604. (function(i, e, xs, ys){
  605. return function(){
  606. $(e).translate3d({x:xs[i],y:ys[i]});
  607. }
  608. }(i, e, that.parts_pos.xs, that.parts_pos.ys)),
  609. 10);
  610. }); // each $parties
  611. //activate lines anime for next frame
  612. this.are_lines_animated = true;
  613. this.$e.addClass('previewed');
  614. }
  615. }
  616. // after positiong juste anime lines
  617. if(this.are_lines_animated){
  618. // get the lines length
  619. var l, a, pos1, pos2;
  620. for (var i = 0; i < this.lines.length; i++) {
  621. pos1 = this.$parties.eq(i).position();
  622. pos2 = this.$parties.eq(i+1).position();
  623. l = Math.sqrt(
  624. Math.pow(
  625. pos2.left - pos1.left
  626. ,2
  627. )
  628. +
  629. Math.pow(
  630. pos2.top - pos1.top
  631. ,2
  632. )
  633. );
  634. // get the rotation
  635. a = 180 / 3.14 * Math.acos((pos2.top - pos1.top) / l);
  636. if(pos2.left > pos1.left)
  637. a *= -1;
  638. // console.log("a = "+a);
  639. this.lines[i].$line.css({
  640. 'height':l
  641. }).rotate3d({z:1,a:a});
  642. };
  643. }
  644. };
  645. // _____ _____ _____ _____ _____ _____ _____ _____ _____ _____ _____
  646. // | | _ | __| | | | | | | _ | _ |_ _| __| __ |
  647. // | | | __| __| | | | | --| | | __| | | | __| -|
  648. // |_____|__| |_____|_|___| |_____|__|__|__|__|__| |_| |_____|__|__|
  649. Chapter.prototype.loadNode = function(e){
  650. console.log("Chapter :: open : nid", this.nid);
  651. // change hash address bar
  652. updateHash("principe-"+this.nid);
  653. _$body.addClass('loading');
  654. $.getJSON(
  655. '/jee/chapter/'+this.nid,
  656. {},
  657. this.nodeLoaded.bind(this)
  658. );
  659. _$body.addClass('chapter-displayed');
  660. };
  661. Chapter.prototype.nodeLoaded = function(json, textstatus){
  662. console.log('Chapter :: nodeLoaded '+this.nid+' : json', json);
  663. // remove previous loaded nodes
  664. _$chapter_wrapper.find('.node').remove();
  665. // insert ajax loaded into dom
  666. _$chapter_wrapper.append(json.node);
  667. // record some usefull data
  668. this.$n = $('.node-chapitre', _$chapter_wrapper);
  669. this.$blocks = $('.field-type-text-long, .field-type-text', this.$n);
  670. this.$vids_container = $('.field-name-field-partie', this.$n);
  671. this.$vids = $('iframe', this.$vids_container);
  672. // record the current loaded chapter
  673. // this will stop first interface to run
  674. _loaded_chapter = this;
  675. // wait to build correct display of chapter
  676. setTimeout(this.displayNode.bind(this), 100);
  677. };
  678. Chapter.prototype.displayNode = function(){
  679. console.log('Chapter :: displayNode '+this.nid);
  680. // place text blocks
  681. if(!_is_mobile){
  682. this.texts_pos = shuffleArray([1,2,3,4]);
  683. this.$blocks
  684. .each(this.placeText.bind(this))
  685. .pep({
  686. allowDragEventPropagation:false,
  687. disableSelect:true,
  688. velocityMultiplier:1
  689. });
  690. }
  691. // build video player
  692. this.buildVideos();
  693. // show the whole thing
  694. _$chapter_wrapper.addClass('visible');
  695. // this.$vids.eq(this.cur_vid_playing).vimeo('play');
  696. _$body.removeClass('loading');
  697. };
  698. Chapter.prototype.placeText = function(i, e){
  699. // console.log("Chapter :: placeText", e);
  700. switch(this.texts_pos[i]){
  701. case 1: // top left
  702. $(e)
  703. .css({
  704. top:randB(_container.h*0.05-$(e).height(),_container.h*0.15-$(e).height()),
  705. left:randB(_container.w*0.1,_container.w*0.2)
  706. })
  707. // .pep({debug:true, startPos:{bottom:randB(80,90),right:randB(80,90)}})
  708. .find('.field-label')
  709. .appendTo(e);
  710. break;
  711. case 2: // top right
  712. $(e)
  713. .css({
  714. top:randB(_container.h*0.05-$(e).height(),_container.h*0.15-$(e).height()),
  715. left:randB(_container.w*0.8-$(e).width(),_container.w*0.9-$(e).width())
  716. })
  717. // .pep({debug:true, startPos:{bottom:randB(80,90),right:randB(80,90)}})
  718. .find('.field-label')
  719. .appendTo(e);
  720. break;
  721. case 3: // bottom left
  722. $(e).css({
  723. top:randB(_container.h*0.8,_container.h*0.9),
  724. left:randB(_container.w*0.1,_container.w*0.2)
  725. });
  726. // $(e).pep({debug:true, startPos:{top:randB(80,90),left:randB(10,30)}});
  727. break;
  728. case 4: // bottom right
  729. $(e).css({
  730. top:randB(_container.h*0.8,_container.h*0.9),
  731. left:randB(_container.w*0.7,_container.w*0.9-$(e).width())
  732. });
  733. // $(e).pep({debug:true, startPos:{top:randB(80,90),right:randB(10,30)}});
  734. break;
  735. }
  736. };
  737. Chapter.prototype.buildVideos = function(){
  738. console.log('Chapter :: buildVideos');
  739. var $first_vid = this.$vids.eq(0);
  740. // compute proportional video width regarding the parent height
  741. this.dimvideo.h = this.$vids_container.height();
  742. this.dimvideo.w = (this.dimvideo.h * parseInt($first_vid.attr("width")))/parseInt($first_vid.attr("height"));
  743. // if resulted width if larger than container
  744. // first get good width and then calculate proportional height
  745. if(this.dimvideo.w > _container.w){
  746. this.dimvideo.w = _container.w *0.8;
  747. this.dimvideo.h = (this.dimvideo.w * parseInt($first_vid.attr("height")))/parseInt($first_vid.attr("width"));
  748. }
  749. // redim each iframe to fit
  750. this.$vids
  751. .each(this.redimVideo.bind(this))
  752. .each(this.setVidsEvents.bind(this));
  753. var marge = (_container.w-this.dimvideo.w)/4;
  754. this.$vids_container.css({
  755. width:this.dimvideo.w+marge,//*1.2,
  756. // height:this.dimvideo.h,
  757. marginLeft:marge
  758. });
  759. if(!_is_mobile){
  760. this.$vids_container.css({
  761. height:this.dimvideo.h
  762. });
  763. // add a mask on top of each iframe to avoid bad interaction with vimeo
  764. this.$vids.after('<div class="mask"></div>');
  765. // create the slider with peppermint
  766. this.$slider = $('.field-items', this.$vids_container).Peppermint({
  767. onSlideChange:this.onSlideChange.bind(this)
  768. });
  769. }
  770. };
  771. Chapter.prototype.redimVideo = function(i,e){
  772. var $e = $(e);
  773. // apply thees sizes
  774. $e
  775. .css({ width:this.dimvideo.w, height:this.dimvideo.h })
  776. .attr({ width:this.dimvideo.w, height:this.dimvideo.h });
  777. // add some padding to parent for slider display
  778. $e.parent()
  779. .css({
  780. paddingLeft:(_container.w-this.dimvideo.w)/4,
  781. });
  782. };
  783. Chapter.prototype.setVidsEvents = function(i,e){
  784. $(e).on("finish", this.onVidFinished.bind(this));
  785. };
  786. Chapter.prototype.onVidFinished = function(){
  787. if( this.cur_vid_playing < 2 ){
  788. this.$slider.data('Peppermint').next();
  789. }
  790. };
  791. // not used
  792. Chapter.prototype.onClickVid = function(e){
  793. e.stopPropagation();
  794. e.preventDefault();
  795. console.log('Chapter :: onClickVid '+this.nid, e);
  796. var $vid = $('iframe', e.currentTarget);
  797. $vid.vimeo('paused', function(data){
  798. console.log('paused : ', data)
  799. if(data){
  800. $vid.vimeo('play');
  801. }else{
  802. $vid.vimeo('pause');
  803. }
  804. });
  805. return false;
  806. };
  807. Chapter.prototype.onSlideChange = function(){
  808. console.log('onSlideChange nid :'+this.nid+'| current : ', this.$slider.data('Peppermint').getCurrentPos());
  809. //stop current video playing
  810. this.$vids.eq(this.cur_vid_playing).vimeo('pause');
  811. // start new current video
  812. this.cur_vid_playing = this.$slider.data('Peppermint').getCurrentPos();
  813. this.$vids.eq(this.cur_vid_playing).vimeo('play');
  814. };
  815. Chapter.prototype.stopAndClose = function(){
  816. console.log('stopAndClose nid :'+this.nid+'| current : '+this.cur_vid_playing);
  817. this.$vids.eq(this.cur_vid_playing).vimeo('unload');
  818. };
  819. Chapter.initialized = true;
  820. };
  821. this.init();
  822. };//Chapter
  823. /*
  824. __ __________ ____ __________ _____
  825. / / / / ____/ / / __ \/ ____/ __ \/ ___/
  826. / /_/ / __/ / / / /_/ / __/ / /_/ /\__ \
  827. / __ / /___/ /___/ ____/ /___/ _, _/___/ /
  828. /_/ /_/_____/_____/_/ /_____/_/ |_|/____/
  829. */
  830. function randB(min, max){
  831. return Math.random() * (max - min) + min;
  832. };
  833. //+ Jonas Raoni Soares Silva
  834. //@ http://jsfromhell.com/array/shuffle [v1.0]
  835. function shuffleArray(o){ //v1.0
  836. for(var j, x, i = o.length; i; j = Math.floor(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
  837. return o;
  838. };
  839. if (!Date.now) {
  840. Date.now = function now() {
  841. return new Date().getTime();
  842. };
  843. }
  844. // http://davidwalsh.name/fullscreen
  845. // http://www.sitepoint.com/html5-full-screen-api/
  846. function launchIntoFullscreen(element) {
  847. // Find the right method, call on correct element
  848. if(element.requestFullscreen) {
  849. element.requestFullscreen();
  850. } else if(element.mozRequestFullScreen) {
  851. element.mozRequestFullScreen();
  852. } else if(element.webkitRequestFullscreen) {
  853. element.webkitRequestFullscreen();
  854. } else if(element.msRequestFullscreen) {
  855. element.msRequestFullscreen();
  856. }
  857. }
  858. // Whack fullscreen
  859. function exitFullscreen() {
  860. if(document.exitFullscreen) {
  861. document.exitFullscreen();
  862. } else if(document.mozCancelFullScreen) {
  863. document.mozCancelFullScreen();
  864. } else if(document.webkitExitFullscreen) {
  865. document.webkitExitFullscreen();
  866. }
  867. }
  868. /*
  869. _ _ __
  870. (_)___ (_) /_
  871. / / __ \/ / __/
  872. / / / / / / /_
  873. /_/_/ /_/_/\__/
  874. */
  875. init();
  876. });
  877. /*
  878. ____ __________ __ ___________________ ___ _ ________ ______ ______________ _ __ __________ ___ __ _________
  879. / __ \/ ____/ __ \/ / / / ____/ ___/_ __/ / | / | / / _/ |/ / |/_ __/ _/ __ \/ | / / / ____/ __ \/ | / |/ / ____/
  880. / /_/ / __/ / / / / / / / __/ \__ \ / / / /| | / |/ // // /|_/ / /| | / / / // / / / |/ / / /_ / /_/ / /| | / /|_/ / __/
  881. / _, _/ /___/ /_/ / /_/ / /___ ___/ // / / ___ |/ /| // // / / / ___ |/ / _/ // /_/ / /| / / __/ / _, _/ ___ |/ / / / /___
  882. /_/ |_/_____/\___\_\____/_____//____//_/ /_/ |_/_/ |_/___/_/ /_/_/ |_/_/ /___/\____/_/ |_/ /_/ /_/ |_/_/ |_/_/ /_/_____/
  883. */
  884. (function() {
  885. var lastTime = 0;
  886. var vendors = ['ms', 'moz', 'webkit', 'o'];
  887. for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x) {
  888. window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
  889. window.cancelAnimationFrame = window[vendors[x]+'CancelAnimationFrame']
  890. || window[vendors[x]+'CancelRequestAnimationFrame'];
  891. }
  892. if (!window.requestAnimationFrame)
  893. window.requestAnimationFrame = function(callback, element) {
  894. var currTime = new Date().getTime();
  895. var timeToCall = Math.max(0, 16 - (currTime - lastTime));
  896. var id = window.setTimeout(function() { callback(currTime + timeToCall); },
  897. timeToCall);
  898. lastTime = currTime + timeToCall;
  899. return id;
  900. };
  901. if (!window.cancelAnimationFrame)
  902. window.cancelAnimationFrame = function(id) {
  903. clearTimeout(id);
  904. };
  905. }());
  906. /*
  907. ____ __ __ _____________ _______
  908. / __ \/ / / / / / ____/ _/ | / / ___/
  909. / /_/ / / / / / / / __ / // |/ /\__ \
  910. / ____/ /___/ /_/ / /_/ // // /| /___/ /
  911. /_/ /_____/\____/\____/___/_/ |_//____/
  912. */
  913. // translate3d
  914. (function($) {
  915. $.fn.translate3d = function(opt) {
  916. opt = $.extend({x:0,y:0,z:0}, opt);
  917. var t = "translate3d("+opt.x+"px, "+opt.y+"px,"+opt.z+"px)";
  918. this.css({
  919. "-webkit-transform": t,
  920. "-moz-transform": t,
  921. "-ms-transform": t,
  922. "-o-transform": t,
  923. "transform": t
  924. });
  925. };
  926. $.fn.rotate3d = function(opt) {
  927. opt = $.extend({x:0,y:0,z:0,a:0}, opt);
  928. // transform:"rotate3d(0,0,1,"+a+"deg)"
  929. var t = "rotate3d("+opt.x+", "+opt.y+","+opt.z+","+opt.a+"deg)";
  930. this.css({
  931. "-webkit-transform": t,
  932. "-moz-transform": t,
  933. "-ms-transform": t,
  934. "-o-transform": t,
  935. "transform": t
  936. });
  937. };
  938. })(jQuery);