jee.js 31 KB

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