corpus.js 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. /**
  2. * @Author: Bachir Soussi Chiadmi <bach>
  3. * @Date: 18-12-2017
  4. * @Email: bachir@figureslibres.io
  5. * @Filename: corpus.js
  6. * @Last modified by: bach
  7. * @Last modified time: 20-12-2017
  8. * @License: GPL-V3
  9. */
  10. // JS performance improvement http://archive.oreilly.com/pub/a/server-administration/excerpts/even-faster-websites/writing-efficient-javascript.html
  11. /**
  12. * depends on :
  13. * physics.js : http://jonobr1.com/Physics/#
  14. */
  15. (function($, Drupal, drupalSettings) {
  16. EdlpCorpus = function(){
  17. var _activated = true;
  18. var _$body = $('body');
  19. var _$container = _$body;
  20. var _$canvas = $('<canvas id="corpus-map">').appendTo(_$container);
  21. var _canvas = _$canvas[0];
  22. var _ctx = _canvas.getContext('2d');
  23. var _dpi = window.devicePixelRatio;
  24. var _scene_props = {
  25. width:0,
  26. height:0,
  27. // 'margin_top':90, // with red border on head
  28. // 'margin_top':75, // without red border on head
  29. 'margin_top':0,
  30. 'margin_right':0,
  31. // 'margin_bottom':65,
  32. 'margin_bottom':0,
  33. 'margin_left':0
  34. };
  35. var _physics = new Physics();
  36. var _nodes = [];
  37. var _articles_nodes = [];
  38. var _no_articles_nodes = [];
  39. var _articles_filter_on = false;
  40. var _nodes_by_entries = {};
  41. var _nodes_by_nid = {};
  42. var _nodes_Nid_Id = {};
  43. var _nodes_centered = [];
  44. var _nodes_centered_attractions = [];
  45. var _playlist = [];
  46. var _p_velocity_factor = 0.5;
  47. var _mouse_in = true;
  48. var _m_pos = {x:0, y:0};
  49. var _node_hover_id = -1;
  50. var _node_opened_id = -1;
  51. var _$entrees_block = $('#block-edlpentreesblock');
  52. var _$entrees_block_termlinks = $('a.term-link', _$entrees_block);
  53. var _$articles_link;
  54. var _node_pop_up;
  55. // Colors depend on drupalSettings loaded by edlp_corpus.module
  56. var _settings = drupalSettings.edlp_corpus;
  57. var _ecolors = _settings.colors;
  58. // Physics
  59. var _attracter;
  60. // _repulser_top,
  61. // _repulser_center,
  62. // _repulser_bottom,
  63. // _scrambler_TL,
  64. // _scrambler_TR,
  65. // _scrambler_BR,
  66. // _scrambler_BL,
  67. // _scrambler_CL,
  68. // _scrambler_CR;
  69. // ____ _ __
  70. // / _/___ (_) /______
  71. // / // __ \/ / __/ ___/
  72. // _/ // / / / / /_(__ )
  73. // /___/_/ /_/_/\__/____/
  74. function init(){
  75. initCanvas();
  76. if(_activated){
  77. loadCorpus();
  78. }else{
  79. _$canvas.addClass('de-activated');
  80. }
  81. };
  82. // ___
  83. // / __|__ _ _ ___ ____ _ ___
  84. // | (__/ _` | ' \ V / _` (_-<
  85. // \___\__,_|_||_\_/\__,_/__/
  86. function initCanvas(){
  87. // resize the canvas to fill browser window dynamically
  88. window.addEventListener('resize', onResizeCanvas, false);
  89. onResizeCanvas();
  90. };
  91. function onResizeCanvas() {
  92. // _dpi = 2;
  93. _scene_props.width = window.innerWidth;
  94. _scene_props.height = window.innerHeight;
  95. _canvas.style.width = _scene_props.width+'px';
  96. _canvas.style.height = _scene_props.height+'px';
  97. _canvas.width = _scene_props.width*_dpi;
  98. _canvas.height = _scene_props.height*_dpi;
  99. _ctx.scale(_dpi, _dpi);
  100. if(_nodes.length){
  101. for (var i = 0; i < _nodes.length; i++) {
  102. _nodes[i].onResizeCanvas();
  103. }
  104. // move _attracter and _repulser to the center again
  105. resizePhysics();
  106. }
  107. };
  108. // ___
  109. // / __|___ _ _ _ __ _ _ ___
  110. // | (__/ _ \ '_| '_ \ || (_-<
  111. // \___\___/_| | .__/\_,_/__/
  112. // |_|
  113. function loadCorpus(){
  114. // console.log('drupalSettings',drupalSettings);
  115. // console.log('window.location', window.location);
  116. // var path = window.location.origin + drupalSettings.basepath + "edlp/corpus";
  117. var ajax_path = _settings.load_corpus_ajax_url;
  118. var path = window.location.origin + Drupal.url(ajax_path);
  119. $.getJSON(path, {})
  120. .done(function(data){
  121. onCorpusLoaded(data);
  122. })
  123. .fail(function(jqxhr, textStatus, error){
  124. onCorpusLoadError(jqxhr, textStatus, error);
  125. });
  126. };
  127. function onCorpusLoadError(jqxhr, textStatus, error){
  128. console.warn('corpus load failed', jqxhr.responseText);
  129. };
  130. function onCorpusLoaded(data){
  131. //console.log('corpus loaded : data', data);
  132. // console.log('first node', data.nodes[0]);
  133. // buildParticles(data.nodes);
  134. initPhysics();
  135. buildNodes(data.nodes);
  136. initArtilesLink();
  137. initNodePopup();
  138. initEvents();
  139. checkPreOpenedEntry();
  140. startAnime();
  141. };
  142. // ___ _ _
  143. // | _ \ |_ _ _ __(_)__ ___
  144. // | _/ ' \ || (_-< / _(_-<
  145. // |_| |_||_\_, /__/_\__/__/
  146. // |__/
  147. function initPhysics(){
  148. _attracter = _physics.makeParticle(1000);
  149. _attracter.fixed = true;
  150. // move _attracter on window resize
  151. resizePhysics();
  152. };
  153. function resizePhysics(){
  154. // attracters
  155. _attracter.position = {x:_scene_props.width/2, y:_scene_props.height/2};
  156. };
  157. // _ _ _
  158. // | \| |___ __| |___ ___
  159. // | .` / _ \/ _` / -_|_-<
  160. // |_|\_\___/\__,_\___/__/
  161. function buildNodes(nodes){
  162. // console.log("buildNodes", nodes);
  163. var d;
  164. for (var i in nodes) {
  165. d = i < 1 ? true : false;
  166. // _nodes.push(new Node(i,nodes[i],d));
  167. new Node(i,nodes[i],d);
  168. _playlist.push({
  169. 'id':i,
  170. 'nid':nodes[i].nid,
  171. 'audio_url':nodes[i].audio_url
  172. });
  173. }
  174. };
  175. function Node(i,node,d){
  176. this.id = i;
  177. for(key in node)
  178. this[key] = node[key];
  179. // record the node in different lists
  180. _nodes.push(this);
  181. _nodes_by_nid[this.nid] = this;
  182. _nodes_Nid_Id[this.nid] = this.id;
  183. if(this.has_article == 1){
  184. _articles_nodes.push(this);
  185. }else{
  186. _no_articles_nodes.push(this);
  187. }
  188. for (var j = 0; j < this.entrees.length; j++) {
  189. if(typeof _nodes_by_entries[this.entrees[j]] == 'undefined')
  190. _nodes_by_entries[this.entrees[j]] = [];
  191. _nodes_by_entries[this.entrees[j]].push(this);
  192. }
  193. this.debug = d;
  194. this.mass = Math.max(1, this.entrees.length); //1;
  195. this.velocity_threshold = 0.01;
  196. // define radius regarding entries length
  197. switch(true){
  198. case this.entrees.length > 3:
  199. this.r = 8;//10;
  200. break;
  201. case this.entrees.length > 1 && this.entrees.length <= 3:
  202. this.r = 5;//7;
  203. break;
  204. default:
  205. this.r = 3;//4;
  206. break;
  207. }
  208. this.d = this.r*2;
  209. this.e_color = 'e_col_'+this.entrees[Math.floor(Math.random(this.entrees.length))];
  210. this.canvas = document.createElement('canvas');
  211. this.canvas_ctx = this.canvas.getContext('2d');
  212. this.line_width = 1.5;
  213. this.canvas_w = this.r*2+this.line_width*2;
  214. this.canvas.width = this.canvas_w;
  215. this.canvas.height = this.canvas_w;
  216. this.canvas_ctx.fillStyle = 'rgb(255,255,255)';
  217. this.canvas_ctx.lineWidth = this.line_width;
  218. this.wall_bouncing_elasticity = 0.75;
  219. // node states
  220. this.hover = false;
  221. this.opened = false;
  222. this.faded = false;
  223. this.center = false;
  224. this.aside = false;
  225. this.scrambling = false;
  226. // this.n_repulses = {};
  227. // prototypes
  228. if (typeof Node.initialized == "undefined") {
  229. Node.prototype.init = function(){
  230. this.calcWallLimits();
  231. this.x = this.wall_limits.left + Math.random()*(this.wall_limits.right - this.wall_limits.left);
  232. this.y = this.wall_limits.top + Math.random()*(this.wall_limits.bottom - this.wall_limits.top);
  233. this.ori_pos = {x:this.x,y:this.y};
  234. // TODO: don't forget to move ori_pos on window resize
  235. this.draw();
  236. this.initPhysics();
  237. };
  238. Node.prototype.draw = function(){
  239. _ctx.clearRect(0, 0, this.canvas_w, this.canvas_w);
  240. this.canvas_ctx.beginPath();
  241. // white background
  242. this.canvas_ctx.fillRect(0,0,this.canvas_w,this.canvas_w);
  243. this.canvas_ctx.globalAlpha = this.faded ? 0.15 : 1;
  244. if(this.opened){
  245. // carre plein
  246. // this.canvas_ctx.lineWidth = '1px';
  247. // this.canvas_ctx.strokeStyle = 'rgb(255,0,0)';
  248. // this.canvas_ctx.strokeRect(this.x - this.r-3,this.y - this.r-3,this.r*2+6,this.r*2+6);
  249. // ou carre contour
  250. this.canvas_ctx.save(); // save the normal context state (fill white)
  251. this.canvas_ctx.fillStyle = _ecolors[this.e_color];
  252. this.canvas_ctx.fillRect(this.line_width,this.line_width,this.d,this.d);
  253. this.canvas_ctx.restore(); // restore the normal context state (fill white)
  254. }
  255. // else{
  256. // // carre plein
  257. // // this.canvas_ctx.fillStyle = _ecolors[this.e_color];
  258. // // this.canvas_ctx.fillRect(this.x - this.r,this.y - this.r,this.d,this.d);
  259. // // ou carre contour
  260. // this.canvas_ctx.fillRect(this.line_width,this.line_width,this.d,this.d);
  261. // }
  262. this.canvas_ctx.strokeStyle = _ecolors[this.e_color];
  263. this.canvas_ctx.strokeRect(this.line_width,this.line_width,this.d,this.d);
  264. // this.canvas_ctx.globalAlpha = 1;
  265. this.canvas_ctx.closePath();
  266. };
  267. Node.prototype.initPhysics = function(){
  268. // particule
  269. this.p = _physics.makeParticle(this.mass, this.x, this.y);
  270. this.p.velocity = new Physics.Vector((Math.random()-0.5)*_p_velocity_factor, (Math.random()-0.5)*_p_velocity_factor);
  271. // origin position particule
  272. this.ori_p = _physics.makeParticle(1000, this.ori_pos.x, this.ori_pos.y);
  273. this.ori_p.fixed = true;
  274. // TODO: don't forget to move ori_p on window resize
  275. this.ori_p_attract = _physics.makeAttraction(this.ori_p, this.p, 1000, _scene_props.width*2);
  276. this.ori_p_attract.on = false;
  277. // attracter
  278. this.attract = _physics.makeAttraction(_attracter, this.p, 1000, _scene_props.width*2);
  279. this.attract.on = false;
  280. };
  281. Node.prototype.calcWallLimits = function(){
  282. this.wall_limits = {
  283. top: _scene_props.margin_top +this.r,
  284. right: _scene_props.width -_scene_props.margin_right -this.r,
  285. bottom: _scene_props.height -_scene_props.margin_bottom -this.r,
  286. left: _scene_props.margin_left +this.r
  287. }
  288. };
  289. Node.prototype.onResizeCanvas = function(){
  290. this.calcWallLimits();
  291. this.shuffleOriP();
  292. // TODO: when canvas is smaller what about nodes hors champs, they are coming back alone but it's too long
  293. };
  294. Node.prototype.shuffleOriP = function(){
  295. this.ori_pos.x = this.wall_limits.left + Math.random()*(this.wall_limits.right - this.wall_limits.left);
  296. this.ori_pos.y = this.wall_limits.top + Math.random()*(this.wall_limits.bottom - this.wall_limits.top);
  297. this.scramble();
  298. };
  299. Node.prototype.onUpdate = function(){
  300. // this.p.velocity.multiplyScalar(0.99);
  301. if(this.hover || this.opened){
  302. this.p.fixed = true;
  303. }else{
  304. this.p.fixed = false;
  305. }
  306. if(this.center)
  307. this.limitEvolutionZone();
  308. if(!this.p.resting()){
  309. // this.checkVelocityThreshold();
  310. this.checkWallBouncing();
  311. this.updatePos();
  312. if(this.scrambling){
  313. this.checkOriPDist();
  314. }
  315. }
  316. if(_mouse_in && !this.aside && !this.faded)
  317. this.checkMouse();
  318. // if(this.debug)
  319. // console.log("Node pos: ", {x:this.x, y:this.y});
  320. this.move();
  321. };
  322. Node.prototype.checkVelocityThreshold = function(){
  323. if(!this.centered || !this.aside){
  324. if (Math.abs(this.p.velocity.x) < this.velocity_threshold
  325. && Math.abs(this.p.velocity.y) < this.velocity_threshold){
  326. this.p.velocity.multiplyScalar(0);
  327. // this.p.makeFixed();
  328. }
  329. }
  330. };
  331. Node.prototype.checkWallBouncing = function(){
  332. switch(true){
  333. case this.x < this.wall_limits.left && this.p.velocity.x < 0:
  334. this.p.position.x = this.wall_limits.left;
  335. this.p.velocity
  336. .multiplyScalarXY(-1, 1)
  337. .multiplyScalar(this.wall_bouncing_elasticity);
  338. break;
  339. case this.x > this.wall_limits.right && this.p.velocity.x > 0:
  340. this.p.position.x = this.wall_limits.right;
  341. this.p.velocity
  342. .multiplyScalarXY(-1, 1)
  343. .multiplyScalar(this.wall_bouncing_elasticity);
  344. break;
  345. case this.y < this.wall_limits.top && this.p.velocity.y < 0 :
  346. this.p.position.y = this.wall_limits.top;
  347. this.p.velocity
  348. .multiplyScalarXY(1,-1)
  349. .multiplyScalar(this.wall_bouncing_elasticity);
  350. break;
  351. case this.y > this.wall_limits.bottom && this.p.velocity.y > 0 :
  352. this.p.position.y = this.wall_limits.bottom;
  353. this.p.velocity
  354. .multiplyScalarXY(1,-1)
  355. .multiplyScalar(this.wall_bouncing_elasticity);
  356. break;
  357. }
  358. };
  359. Node.prototype.updatePos = function(){
  360. this.x = this.p.position.x;
  361. this.y = this.p.position.y;
  362. };
  363. Node.prototype.checkMouse = function(){
  364. if( _m_pos.x > this.x - this.r
  365. && _m_pos.x < this.x + this.r
  366. && _m_pos.y > this.y - this.r
  367. && _m_pos.y < this.y + this.r){
  368. if(_node_hover_id == -1 || _node_hover_id !== this.id){
  369. // console.log("Node hover", this.id);
  370. this.setHover();
  371. }
  372. }else{
  373. this.unsetHover();
  374. }
  375. };
  376. Node.prototype.setHover = function(){
  377. this.hover = true;
  378. _node_hover_id = this.id;
  379. _node_pop_up.setNode(this);
  380. };
  381. Node.prototype.unsetHover = function(){
  382. this.hover = false;
  383. if (_node_hover_id == this.id) {
  384. _node_hover_id = -1;
  385. _node_pop_up.removeNode();
  386. }
  387. };
  388. Node.prototype.open = function(){
  389. this.opened = true;
  390. _node_opened_id = this.id;
  391. this.draw();
  392. };
  393. Node.prototype.close = function(){
  394. this.opened = false;
  395. this.draw();
  396. };
  397. Node.prototype.fade = function(){
  398. this.faded = true;
  399. this.draw();
  400. };
  401. Node.prototype.unFade = function(){
  402. this.faded = false;
  403. this.draw();
  404. };
  405. Node.prototype.setCenteredOnEntree = function(tid){
  406. this.e_color = 'e_col_'+tid;
  407. this.setCentered();
  408. };
  409. Node.prototype.setCentered = function(){
  410. this.center = true;
  411. this.stopScrambling();
  412. this.unsetAside();
  413. this.attract.on = true;
  414. this.draw();
  415. }
  416. Node.prototype.unsetCentered = function(){
  417. this.center = false;
  418. this.attract.on = false;
  419. }
  420. Node.prototype.limitEvolutionZone = function(){
  421. // smouthly slow down node near center
  422. // if(this.id == 1)
  423. // console.log('this.p.velocity', this.p.velocity);
  424. this.dist_to_attracter = this.p.distanceTo(_attracter);
  425. if( this.dist_to_attracter < _scene_props.width/4){
  426. if( this.p.velocity.length() > 0.4 ){
  427. this.p.velocity.multiplyScalar(0.995);
  428. }
  429. }
  430. if( Math.abs(_attracter.position.x - this.x) > _scene_props.width/3
  431. || Math.abs(_attracter.position.y - this.y) > _scene_props.height/3){
  432. this.attract.on = true;
  433. }else{
  434. this.attract.on = false;
  435. }
  436. }
  437. Node.prototype.setAside = function(){
  438. this.aside = true;
  439. // this.fade();
  440. this.stopScrambling();
  441. this.unsetCentered();
  442. this.ori_p.position.x = this.x < _scene_props.width /2 ? this.wall_limits.left : this.wall_limits.right;
  443. this.ori_p_attract.on = true;
  444. this.wall_bouncing_elasticity = 0.15;
  445. }
  446. Node.prototype.unsetAside = function(){
  447. console.log('unsetAside');
  448. this.aside = false;
  449. this.ori_p_attract.on = false;
  450. this.ori_p.position.x = this.ori_pos.x;
  451. this.wall_bouncing_elasticity = 0.75;
  452. // this.unFade();
  453. }
  454. Node.prototype.scramble = function(){
  455. this.scrambling = true;
  456. this.unsetCentered();
  457. this.unsetAside();
  458. this.ori_p.position.x = this.ori_pos.x;
  459. this.ori_p.position.y = this.ori_pos.y;
  460. this.ori_p_attract.on = true;
  461. }
  462. Node.prototype.checkOriPDist = function(){
  463. if(this.p.distanceTo(this.ori_p) < 50){
  464. this.stopScrambling();
  465. }
  466. };
  467. Node.prototype.stopScrambling = function(){
  468. this.scrambling = false;
  469. this.ori_p_attract.on = false;
  470. // this.scramble_TL.on = false;
  471. // this.scramble_TR.on = false;
  472. // this.scramble_BR.on = false;
  473. // this.scramble_BL.on = false;
  474. // this.scramble_CR.on = false;
  475. // this.scramble_CL.on = false;
  476. }
  477. Node.prototype.move = function(){
  478. // just draw the virtual node canvas into the main scene canvas at the right position
  479. _ctx.drawImage(this.canvas, this.x-this.r, this.y-this.r);
  480. };
  481. Node.initialized = true;
  482. }
  483. this.init();
  484. };
  485. // TODO: we may convert a lot of computation into a web worker
  486. // https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers
  487. function checkParticulesCollisions(){
  488. // pre create vars to save memory;
  489. var na,nb,
  490. ma,mb,
  491. w,h,dx,dy,
  492. makeup,
  493. newVelX1, newVelY1, newVelX2, newVelY2,
  494. Smamb;
  495. // , angle;
  496. // colisions between _particules
  497. for (var n = 0, l = _nodes.length; n < l; n++) {
  498. na = _nodes[n];
  499. // avoid colliding for centered nodes
  500. // if(_nodes[n].center) continue;
  501. // avoid colliding for scrambling nodes
  502. if(na.scrambling) continue;
  503. ma = na.p.mass;
  504. for (var q = n+1; q < l; q++) {
  505. nb = _nodes[q];
  506. // avoid impact between center and aside particules
  507. // if((na.center && nb.aside) || (na.aside && nb.center))
  508. // and between aside particules
  509. if(na.aside || nb.aside)
  510. continue;
  511. // avoid impact between two centered particulses that comes to the center
  512. if(na.center && nb.center){
  513. if(Math.min(na.p.distanceTo(_attracter), nb.p.distanceTo(_attracter)) > 300){
  514. if( Math.random()>0.3 ) continue;
  515. }
  516. }
  517. w = h = (na.r+nb.r);
  518. // if(!na.aside && !nb.aside){
  519. w = h += 4; // add a saftey zone around squares eccept for aside squares
  520. // }
  521. // if both dx and dy are inferior to w & h so squares are colliding (overlapping)
  522. // if( Math.abs(dx) <= w && Math.abs(dy) <= h){ console.log('colliding'); }
  523. // else not so skip
  524. // if( Math.abs(dx) > w || Math.abs(dy) > h) continue;
  525. dx = na.p.position.x - nb.p.position.x;
  526. if( Math.abs(dx) > w ) continue;
  527. dy = na.p.position.y - nb.p.position.y;
  528. if(Math.abs(dy) > h) continue
  529. mb = nb.p.mass;
  530. Smamb = ma+mb;
  531. if(Math.abs(dx) < Math.abs(dy)){ // vertical collision
  532. makeup = (h - Math.abs(dy))/2;
  533. if(dy > 0){ // a is upper than b
  534. na.p.position.y += makeup;
  535. nb.p.position.y -= makeup;
  536. }else{ // b is upper than a
  537. na.p.position.y -= makeup;
  538. nb.p.position.y += makeup;
  539. }
  540. // bounce
  541. // https://en.wikipedia.org/wiki/Elastic_collision#One-dimensional_Newtonian
  542. newVelY1 = (ma-mb)/Smamb*na.p.velocity.y+2*mb/Smamb*nb.p.velocity.y;
  543. newVelY2 = (mb-ma)/Smamb*nb.p.velocity.y+2*ma/Smamb*na.p.velocity.y;
  544. na.p.velocity.y = newVelY1;
  545. nb.p.velocity.y = newVelY2;
  546. }else{ // horizontal collision
  547. makeup = (w - Math.abs(dx))/2;
  548. if(dx > 0){ // a is at left of b
  549. na.p.position.x += makeup;
  550. nb.p.position.x -= makeup;
  551. }else{ // b is at left of a
  552. na.p.position.x -= makeup;
  553. nb.p.position.x += makeup;
  554. }
  555. // bounce
  556. // https://en.wikipedia.org/wiki/Elastic_collision#One-dimensional_Newtonian
  557. newVelX1 = (ma-mb)/Smamb*na.p.velocity.x+2*mb/Smamb*nb.p.velocity.x;
  558. newVelX2 = (mb-ma)/Smamb*nb.p.velocity.x+2*ma/Smamb*na.p.velocity.x;
  559. na.p.velocity.x = newVelX1;
  560. nb.p.velocity.x = newVelX2;
  561. }
  562. // slow down particule on impact
  563. // na.p.velocity.multiplyScalar(na.center && na.p.velocity.length() < 1 ? 1.1 : 0.90);
  564. // nb.p.velocity.multiplyScalar(nb.center && nb.p.velocity.length() < 1 ? 1.1 : 0.90);
  565. na.p.velocity.multiplyScalar(0.90);
  566. nb.p.velocity.multiplyScalar(0.90);
  567. }
  568. }
  569. };
  570. // show opened audio node
  571. function openNodeByNid(nid){
  572. // console.log('Corpus openNode()', nid);
  573. closeNode();
  574. if(typeof _nodes_Nid_Id[nid] != 'undefined'){
  575. // console.log('node exists');
  576. _node_opened_id = _nodes_Nid_Id[nid];
  577. _nodes[_nodes_Nid_Id[nid]].open();
  578. }
  579. }
  580. function closeNode(){
  581. if(_node_opened_id != -1){
  582. // if(typeof _nodes[_node_opened_id] !== 'undefined'){
  583. _nodes[_node_opened_id].close();
  584. // }
  585. _node_opened_id = -1;
  586. }
  587. }
  588. function createNodesRepulsions(){
  589. // how to delete all these attractions before creates new once
  590. //console.log('_nodes_centered', _nodes_centered);
  591. purgeNodesRepulsions();
  592. for (var n = 0; n < _nodes_centered.length; n++) {
  593. for (var q = n+1; q < _nodes_centered.length; q++) {
  594. _physics.makeAttraction(_nodes_centered[n].p, _nodes_centered[q].p, -0.4, 10);
  595. }
  596. }
  597. };
  598. function purgeNodesRepulsions(){
  599. // console.log('_physics.attractions.length', _physics.attractions.length);
  600. for (var a = _physics.attractions.length-1; a >= 0; a--) {
  601. // assuming that only nodes repulsions have constant < 1
  602. // and are all at the end of array
  603. if(Math.abs(_physics.attractions[a].constant) < 1){
  604. _physics.attractions.splice(a,1);
  605. }else{
  606. break;
  607. }
  608. }
  609. // console.log('_physics.attractions.length', _physics.attractions.length);
  610. };
  611. // ___ _ ___ _ _ _ _
  612. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _| (_)__| |_
  613. // | / _` | ' \/ _` / _ \ ' \ | _/ / _` | || | | (_-< _|
  614. // |_|_\__,_|_||_\__,_\___/_|_|_| |_| |_\__,_|\_, |_|_/__/\__|
  615. // |__/
  616. function updateRandomPlaylist(p){
  617. _$canvas.trigger({
  618. type:'update-random-playlist',
  619. playlist:p
  620. })
  621. };
  622. // ___ _
  623. // | __|_ _| |_ _ _ ___ ___ ___
  624. // | _|| ' \ _| '_/ -_) -_|_-<
  625. // |___|_||_\__|_| \___\___/__/
  626. function toggleEntree(tid, bubbling){
  627. var $link = _$entrees_block_termlinks.filter('[tid="'+tid+'"]');
  628. var $li = $link.parents('li');
  629. var sys_path = $link.attr('data-drupal-link-system-path');
  630. var url = $link.attr('href');
  631. var title = $link.text();
  632. if(!$li.is('.opened')){
  633. _$entrees_block_termlinks.parents('li').removeClass('opened');
  634. $li.addClass('opened');
  635. $li.parents('.item-list').addClass('opened')
  636. filterEntree(tid);
  637. if(bubbling){
  638. _$body.trigger({
  639. 'type':'open_entree',
  640. 'tid':tid,
  641. 'url':url,
  642. 'sys_path':sys_path,
  643. 'title':title
  644. });
  645. }
  646. // bubbling is true only when event is a real click on entree link
  647. }else if(bubbling){
  648. $li.removeClass('opened');
  649. $li.parents('.item-list').removeClass('opened')
  650. scrambleCollection();
  651. _$body.trigger({'type':'close_entree', 'tid':tid});
  652. }
  653. };
  654. function filterEntree(t){
  655. shutDownArticles();
  656. _nodes_centered = [];
  657. for (var n = 0; n < _nodes.length; n++) {
  658. if(_nodes[n].entrees.indexOf(t) == -1){
  659. _nodes[n].setAside();
  660. }else{
  661. _nodes[n].setCenteredOnEntree(t);
  662. // record centered nodes for inter node repulsions
  663. _nodes_centered.push(_nodes[n]);
  664. }
  665. }
  666. updateRandomPlaylist(_nodes_centered);
  667. createNodesRepulsions();
  668. };
  669. function highlightEntries(){
  670. _$entrees_block_termlinks
  671. .parents('li').removeClass('highlighted')
  672. .parents('.item-list').removeClass('highlighted');
  673. var id = -1;
  674. if(_node_hover_id != -1){
  675. id = _node_hover_id;
  676. }else if(_node_opened_id != -1){
  677. id = _node_opened_id;
  678. }
  679. var entree;
  680. if(id != -1){
  681. for (var i = 0; i < _nodes[id].entrees.length; i++) {
  682. entree = _nodes[id].entrees[i];
  683. _$entrees_block_termlinks.filter(function(){
  684. return $(this).attr('tid') == entree;
  685. }).parents('li').addClass('highlighted')
  686. .parents('.item-list').addClass('highlighted');
  687. }
  688. }
  689. };
  690. // ___ _
  691. // / __| ___ __ _ _ _ __| |_
  692. // \__ \/ -_) _` | '_/ _| ' \
  693. // |___/\___\__,_|_| \__|_||_|
  694. function filterSearchResults(nids){
  695. shutDownArticles();
  696. _nodes_centered = [];
  697. for (var n = 0; n < _nodes.length; n++) {
  698. if(nids.indexOf(_nodes[n].nid) == -1){
  699. _nodes[n].setAside();
  700. }else{
  701. _nodes[n].setCentered();
  702. // record centered nodes for inter node repulsions
  703. _nodes_centered.push(_nodes[n]);
  704. }
  705. }
  706. updateRandomPlaylist(_nodes_centered);
  707. createNodesRepulsions();
  708. };
  709. // ___ _ _ _
  710. // / __| __ _ _ __ _ _ __ | |__| (_)_ _ __ _
  711. // \__ \/ _| '_/ _` | ' \| '_ \ | | ' \/ _` |
  712. // |___/\__|_| \__,_|_|_|_|_.__/_|_|_||_\__, |
  713. // |___/
  714. function scrambleCollection(){
  715. // console.log('scrambleCollection');
  716. for (var i = 0; i < _nodes.length; i++) {
  717. _nodes[i].scramble();
  718. }
  719. updateRandomPlaylist(_playlist);
  720. // setTimeout(stopScrambling, 2000);
  721. // stopScrambling();
  722. };
  723. function closeAllEntries(){
  724. _$entrees_block_termlinks.each(function(index, el) {
  725. if($(this).parents('li').is('.opened')){
  726. $(this).trigger('click');
  727. return false;
  728. }
  729. });
  730. };
  731. // _ _ _ _
  732. // /_\ _ _| |_(_)__| |___ ___
  733. // / _ \| '_| _| / _| / -_|_-<
  734. // /_/ \_\_| \__|_\__|_\___/__/
  735. function initArtilesLink(){
  736. // add "articles link to blockentrees"
  737. _$articles_link = $('<a>')
  738. .html('Articles')
  739. .attr("href", "#articles")
  740. .addClass('articles-link')
  741. .on('click', onCLickedOnArticles);
  742. $('.item-list ul',_$entrees_block).append(
  743. $('<li>').append(
  744. $('<span class="oblique-wrapper">').append(_$articles_link)
  745. )
  746. );
  747. };
  748. function onCLickedOnArticles(e){
  749. e.preventDefault();
  750. $(this).toggleClass('is-active');
  751. if($(this).is('.is-active')){
  752. activateArticlesFilter();
  753. closeAllEntries();
  754. }else{
  755. deactivateArticlesFilter();
  756. }
  757. // _$canvas.trigger({
  758. // 'type':'corpus-cliqued-on-articles'
  759. // });
  760. return false;
  761. };
  762. function activateArticlesFilter(){
  763. //console.log('activateArticlesFilter');
  764. _articles_filter_on = true;
  765. for (var i = 0; i < _no_articles_nodes.length; i++) {
  766. _no_articles_nodes[i].fade();
  767. }
  768. updateRandomPlaylist(_articles_nodes);
  769. if(typeof _paq !== 'undefined'){
  770. // trackEvent(category, action, [name], [value])
  771. _paq.push(['trackEvent', 'Corpus Articles', 'on']);
  772. }
  773. };
  774. function deactivateArticlesFilter(){
  775. //console.log('deactivateArticlesFilter');
  776. _articles_filter_on = false;
  777. for (var i = 0; i < _no_articles_nodes.length; i++) {
  778. _no_articles_nodes[i].unFade();
  779. }
  780. updateRandomPlaylist(_playlist);
  781. if(typeof _paq !== 'undefined'){
  782. // trackEvent(category, action, [name], [value])
  783. _paq.push(['trackEvent', 'Corpus Articles', 'off']);
  784. }
  785. };
  786. function shutDownArticles(){
  787. // shutdown articles if active
  788. if(_$articles_link.is('.is-active'))
  789. _$articles_link.trigger('click');
  790. };
  791. // ___ _
  792. // | __|_ _____ _ _| |_ ___
  793. // | _|\ V / -_) ' \ _(_-<
  794. // |___|\_/\___|_||_\__/__/
  795. function initEvents(){
  796. console.log('Corpus, initEvents');
  797. _$canvas
  798. .on('mousemove', function(event) {
  799. event.preventDefault();
  800. _m_pos.x = event.originalEvent.clientX;
  801. _m_pos.y = event.originalEvent.clientY;
  802. })
  803. .on('mouseenter', function(event){
  804. // console.log('onMouseIN');
  805. _mouse_in = true;
  806. })
  807. .on('mouseout', function(event){
  808. // console.log('onMouseOUT');
  809. _mouse_in = false;
  810. _node_pop_up.removeNode();
  811. })
  812. .on('click', function(event) {
  813. if(event.target.tagName != "A" && event.target.tagName != "INPUT"){
  814. // console.log("Corpus : click");
  815. event.preventDefault();
  816. if(_node_hover_id != -1){
  817. // console.log("corpus : click on node", _nodes[_node_hover_id]);
  818. var event = {
  819. 'type':'corpus-cliked-on-node',
  820. 'target_node':_nodes[_node_hover_id],
  821. 'article':_articles_filter_on
  822. };
  823. _$canvas.trigger(event);
  824. // instead of directly opening the doc, create an event listener (e.g. : audio played from random)
  825. // openNode(_node_hover_id);
  826. }else{
  827. // console.log('corpus : click on map');
  828. _$canvas.trigger('corpus-cliked-on-map');
  829. shutDownArticles();
  830. }
  831. }
  832. })
  833. .on('mouseover-audio-link', function(e){
  834. console.log("Corpus on mouseover-audio-link", e);
  835. // _node_hover_id = _nodes_Nid_Id[e.nid];
  836. // _node_pop_up.setNode(_nodes_by_nid[e.nid]);
  837. _nodes_by_nid[e.nid].setHover();
  838. })
  839. .on('mouseout-audio-link', function(e){
  840. console.log("Corpus on mouseover-audio-link", e);
  841. // _node_hover_id = -1;
  842. // _node_pop_up.removeNode();
  843. _nodes_by_nid[e.nid].unsetHover();
  844. })
  845. .on('audio-node-opened', function(e){
  846. // console.log('Corpus audio-node-opened', e);
  847. openNodeByNid(e.nid);
  848. })
  849. .on('audio-node-closed', function(e){
  850. closeNode();
  851. })
  852. .on('open-entree', function(e){
  853. toggleEntree(e.tid);
  854. })
  855. .on('close-all-entree', function(e){
  856. closeAllEntries();
  857. });
  858. _$entrees_block_termlinks.on('click', function(event) {
  859. // console.log('_$entrees_block_termlinks click', this);
  860. event.preventDefault();
  861. toggleEntree($(this).attr('tid'), true);
  862. // var $link = $(this);
  863. // var tid = $link.attr('tid');
  864. // var $li = $link.parents('li');
  865. // var sys_path = $link.attr('data-drupal-link-system-path');
  866. // var url = $link.attr('href');
  867. // if(!$li.is('.opened')){
  868. // _$entrees_block_termlinks.parents('li').removeClass('opened');
  869. // $li.addClass('opened');
  870. // toggleEntree(tid);
  871. // _$body.trigger({'type':'open_entree', 'tid':tid, 'url':url, 'sys_path':sys_path});
  872. // }else{
  873. // $li.removeClass('opened');
  874. // scrambleCollection();
  875. // _$body.trigger({'type':'close_entree', 'tid':tid});
  876. // }
  877. return false;
  878. });
  879. _$body
  880. .on('chutier-action-done', function(e) {
  881. _nodes_by_nid[e.target_id].chutier_action = e.new_action;
  882. })
  883. .on('search-results-loaded', function(e){
  884. //console.log("Edlp Corpus, search-results-loaded",e.results);
  885. // TODO: filter map's nodes
  886. filterSearchResults(e.results);
  887. })
  888. .on('search-closed', function(e){
  889. scrambleCollection();
  890. })
  891. .on('new-content-not-entree-ajax-loaded', function(e){
  892. // close all entries only if entry already opened
  893. // TODO: user the function close all entries ?
  894. // closeAllEntries(); not working, is closing also the ajax loaded content
  895. if(_$entrees_block_termlinks.parents('li.opened').length){
  896. _$entrees_block_termlinks
  897. .parents('li').removeClass('opened')
  898. .parents('.item-list').removeClass('opened');
  899. scrambleCollection();
  900. }
  901. });
  902. };
  903. function checkPreOpenedEntry(){
  904. _$entrees_block.find('li.entree').each(function(index, el) {
  905. var $li = $(this);
  906. if($('a.is-active', $li).length){
  907. $li.addClass('opened')
  908. .parents('.item-list').addClass('opened');
  909. filterEntree($li.attr('tid'));
  910. return false;
  911. }
  912. });
  913. };
  914. // _ _ _ ___ _ _
  915. // | \| |___ __| |___| _ \___ _ __| | | |_ __
  916. // | .` / _ \/ _` / -_) _/ _ \ '_ \ |_| | '_ \
  917. // |_|\_\___/\__,_\___|_| \___/ .__/\___/| .__/
  918. // |_| |_|
  919. function initNodePopup(){
  920. _node_pop_up = new NodePopUp();
  921. };
  922. function NodePopUp(){
  923. this.visible = false;
  924. this.node;
  925. this.$dom = $('<div>')
  926. .addClass('node-popup')
  927. .attr('pos', 'top-right')
  928. .appendTo('body');
  929. this.$content = $('<div>').addClass('inner').appendTo(this.$dom);
  930. if (typeof NodePopUp.initialized == "undefined") {
  931. NodePopUp.prototype.setNode = function(n){
  932. // console.log('NodePopUp setNode()');
  933. this.node = n;
  934. // positioning NodePopUp regarding node position
  935. this.setPositioning();
  936. // update NodePopUp content
  937. this.setContent();
  938. };
  939. NodePopUp.prototype.setPositioning = function(){
  940. switch(true){
  941. case this.node.x > this.node.wall_limits.right-350 && this.node.y < this.node.wall_limits.top+200:
  942. this.$dom.attr('pos', 'bottom-left');
  943. break;
  944. case this.node.x > this.node.wall_limits.right-350:
  945. this.$dom.attr('pos', 'top-left');
  946. break;
  947. case this.node.y < this.node.wall_limits.top+200:
  948. this.$dom.attr('pos', 'bottom-right');
  949. break;
  950. default:
  951. this.$dom.attr('pos', 'top-right');
  952. }
  953. };
  954. NodePopUp.prototype.setContent = function(){
  955. // console.log(this.node);
  956. this.$content.html('');
  957. var $entrees = $('<div>').addClass('entrees');
  958. for (var i = 0; i < this.node.entrees.length; i++) {
  959. var tid = this.node.entrees[i];
  960. $entrees.append($('<span>').addClass('entree').attr('tid', tid));
  961. }
  962. var $chutier_action = $('<span>').addClass('chutier-icon').attr('action', this.node.chutier_action);
  963. this.$content
  964. .append($entrees)
  965. .append('<h2 class="title">'+this.node.title+'</h2>')
  966. .append('<section class="description">'+this.node.description+'</section>')
  967. .append($chutier_action);
  968. };
  969. NodePopUp.prototype.removeNode = function(){
  970. // console.log('NodePopUp removeNode()');
  971. this.node = false;
  972. };
  973. NodePopUp.prototype.draw = function(){
  974. if(this.node){
  975. this.$dom.css({
  976. 'display':"block",
  977. 'left':this.node.x+"px",
  978. 'top':this.node.y+"px",
  979. });
  980. }else{
  981. this.$dom.css({
  982. 'display':"none",
  983. });
  984. }
  985. };
  986. NodePopUp.initialized = true;
  987. }
  988. }
  989. // ___ _
  990. // | _ \___ _ _ __| |___ _ _
  991. // | / -_) ' \/ _` / -_) '_|
  992. // |_|_\___|_||_\__,_\___|_|
  993. function render(){
  994. _ctx.clearRect(0, 0, _scene_props.width, _scene_props.height);
  995. checkParticulesCollisions();
  996. for (var i = 0; i < _nodes.length; i++) {
  997. _nodes[i].onUpdate();
  998. }
  999. _node_pop_up.draw();
  1000. if(_node_hover_id != -1){
  1001. _canvas.style.cursor = 'pointer';
  1002. }else{
  1003. _canvas.style.cursor = 'auto';
  1004. }
  1005. // check for highlighted entries
  1006. highlightEntries();
  1007. };
  1008. // ___ _ _ _ _
  1009. // / __| |_ __ _ _ _| |_ /_\ _ _ (_)_ __ ___
  1010. // \__ \ _/ _` | '_| _|/ _ \| ' \| | ' \/ -_)
  1011. // |___/\__\__,_|_| \__/_/ \_\_||_|_|_|_|_\___|
  1012. function startAnime(){
  1013. _physics.onUpdate(render);
  1014. _physics.play()
  1015. $('body')
  1016. .attr('corpus', 'map-ready')
  1017. .trigger({
  1018. 'type':'corpus-map-ready',
  1019. 'playlist':_playlist
  1020. });
  1021. };
  1022. init();
  1023. }
  1024. $(document).ready(function($) {
  1025. var edlpcorpus = new EdlpCorpus();
  1026. });
  1027. })(jQuery, Drupal, drupalSettings);