main.min.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. (function($, Drupal, drupalSettings) {
  2. EdlpTheme = function(){
  3. var _$body = $('body');
  4. var _is_front = _$body.is('.path-frontpage');
  5. var _$corpus_canvas;
  6. var _$content_container = $('main[role="main"]>.layout-content');
  7. var _$ajaxLinks;
  8. var _audio_player;
  9. var _randomPlayer;
  10. // ___ _ _
  11. // |_ _|_ _ (_) |_
  12. // | || ' \| | _|
  13. // |___|_||_|_|\__|
  14. function init(){
  15. console.log("EdlpTheme init()");
  16. // TODO: redirect all no-front pages to front with write hash
  17. _$body.on('corpus-map-ready', onCorpusMapReady);
  18. _audio_player = new AudioPlayer();
  19. initAjaxLinks();
  20. if (_$body.is('.path-productions')) {
  21. initProductions();
  22. }
  23. // initScrollbars();
  24. initEvents();
  25. };
  26. // ___ _
  27. // | __|_ _____ _ _| |_ ___
  28. // | _|\ V / -_) ' \ _(_-<
  29. // |___|\_/\___|_||_\__/__/
  30. function initEvents(){
  31. $('body')
  32. .on('on-studio-chutier-updated', function(e){
  33. initAjaxLinks();
  34. })
  35. .on('open_entree', function(e){
  36. // e.tid available
  37. closeAllModals();
  38. })
  39. .on('close_entree', function(e){
  40. // e.tid available
  41. closeAllModals();
  42. });
  43. }
  44. // _ _ _
  45. // /_\ _ _ __| (_)___
  46. // / _ \ || / _` | / _ \
  47. // /_/ \_\_,_\__,_|_\___/
  48. //
  49. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  50. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  51. // https://www.binarytides.com/using-html5-audio-element-javascript/
  52. function AudioPlayer(){
  53. var that = this;
  54. this.fid;
  55. this.audio = new Audio();
  56. // audio events
  57. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended"];
  58. // UI dom objects
  59. this.$container = $('<div id="audio-player">');
  60. // btns
  61. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  62. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  63. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  64. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  65. // timeline
  66. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  67. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  68. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  69. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  70. // time
  71. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  72. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  73. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  74. // favoris
  75. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  76. // cartel
  77. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  78. // hiding
  79. this.hideTimer = false;
  80. this.hideTimeMS = 10000;
  81. // history
  82. this.currentHistoricIndex = null;
  83. this.historic = [];
  84. this.shuffle_is_active = false;
  85. // object events
  86. this.event_handlers = {
  87. 'audio-play-next':[],
  88. 'audio-ended':[]
  89. };
  90. this.init();
  91. };
  92. AudioPlayer.prototype = {
  93. init(){
  94. // append ui to document
  95. this.$container.appendTo('header[role="banner"] .region-header');
  96. // record timeline width
  97. this.timeline_w = parseInt(this.$timeline.width());
  98. // init audio events
  99. var fn = '';
  100. for (var i = 0; i < this.audio_events.length; i++) {
  101. fn = this.audio_events[i];
  102. // capitalize first letter of event (only cosmetic :p )
  103. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  104. this.audio.addEventListener(
  105. this.audio_events[i],
  106. this[fn].bind(this),
  107. true);
  108. }
  109. // init btns events
  110. this.$previous.on('click', this.playPrevious.bind(this));
  111. this.$playpause.on('click', this.togglePlayPause.bind(this));
  112. this.$next.on('click', this.playNext.bind(this));
  113. // TODO: previous and next btns
  114. },
  115. openDocument(node){
  116. console.log('AudioPlayer openDocument', node);
  117. this.historic.push(node);
  118. this.currentHistoricIndex = this.historic.length-1;
  119. // this.shuffle_mode = shuffle_mode || false;
  120. this.launch();
  121. },
  122. launch(){
  123. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  124. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  125. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  126. _$corpus_canvas.trigger({
  127. 'type':'audio-node-opened',
  128. 'id':this.historic[this.currentHistoricIndex].id
  129. });
  130. this.showHidePreviousBtn();
  131. this.showHideNextBtn();
  132. this.show();
  133. },
  134. // audio functions
  135. setSRC(url){
  136. // console.log('AudioPlayer setSRC : url', url);
  137. this.clearTimeOutToHide();
  138. this.audio.src = url;
  139. },
  140. onLoadedmetadata(){
  141. var rem = parseInt(this.audio.duration, 10),
  142. mins = Math.floor(rem/60,10),
  143. secs = rem - mins*60;
  144. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  145. this.updateLoadingBar();
  146. },
  147. updateLoadingBar(){
  148. this.$loader.css({
  149. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  150. });
  151. if( this.audio.buffered.end(0) < this.audio.duration ){
  152. // loop through this function until file is fully loaded
  153. var that = this;
  154. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  155. }else{
  156. console.log('Audio fully loaded');
  157. }
  158. },
  159. onCanplay(){
  160. this.play();
  161. },
  162. play(){
  163. this.audio.play();
  164. },
  165. playPrevious(){
  166. if(this.currentHistoricIndex > 0){
  167. this.currentHistoricIndex -= 1;
  168. this.launch();
  169. }
  170. },
  171. playNext(){
  172. if(this.currentHistoricIndex < this.historic.length-1){
  173. this.currentHistoricIndex += 1;
  174. this.launch();
  175. }else{
  176. this.emmit('audio-play-next');
  177. }
  178. },
  179. togglePlayPause(e){
  180. if(this.audio.paused){
  181. this.audio.play();
  182. }else{
  183. this.audio.pause();
  184. }
  185. },
  186. stop(){
  187. this.audio.pause();
  188. this.timeOutToHide();
  189. },
  190. // audio events
  191. onPlaying(){
  192. this.$container.addClass('is-playing');
  193. },
  194. onPause(){
  195. this.$container.removeClass('is-playing');
  196. },
  197. onTimeupdate(){
  198. // move cursor
  199. this.$cursor.css({
  200. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  201. });
  202. // update time text display
  203. var rem = parseInt(this.audio.currentTime, 10),
  204. mins = Math.floor(rem/60,10),
  205. secs = rem - mins*60;
  206. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  207. },
  208. onEnded(){
  209. this.emmit('audio-ended');
  210. this.stop();
  211. },
  212. // cartel functions
  213. loadNode(nid){
  214. this.$cartel.addClass('loading');
  215. $.getJSON('/edlp/ajax/json/node/'+nid+'/player_cartel', {})
  216. .done(this.onNodeLoaded.bind(this))
  217. .fail(this.onNodeLoadFail.bind(this));
  218. },
  219. onNodeLoaded(data){
  220. console.log('AudioPlayer node loaded', data);
  221. this.$cartel.html(data.rendered).removeClass('loading');
  222. _$body.trigger({'type':'new-audio-cartel-loaded'});
  223. initAjaxLinks();
  224. },
  225. onNodeLoadFail(jqxhr, textStatus, error){
  226. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  227. this.$cartel.removeClass('loading').html('');
  228. },
  229. // global
  230. show(){
  231. this.$container.addClass('visible');
  232. },
  233. showHidePreviousBtn(){
  234. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  235. this.$previous.addClass('is-active');
  236. }else{
  237. this.$previous.removeClass('is-active');
  238. }
  239. },
  240. showHideNextBtn(){
  241. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  242. this.$next.addClass('is-active');
  243. }else{
  244. this.$next.removeClass('is-active');
  245. }
  246. },
  247. timeOutToHide(){
  248. this.clearTimeOutToHide();
  249. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  250. },
  251. clearTimeOutToHide(){
  252. if(this.hideTimer){
  253. clearTimeout(this.hideTimer);
  254. }
  255. },
  256. hide(){
  257. this.$container.removeClass('visible');
  258. // trigger highlighted node remove on corpus map
  259. _$corpus_canvas.trigger('audio-node-closed');
  260. },
  261. // object events
  262. on(event_name, handler){
  263. if(typeof this.event_handlers[event_name] == 'undefined'){
  264. console.warn('AudioPlayer : event '+event_name+' does not exists');
  265. }
  266. this.event_handlers[event_name].push(handler);
  267. return this;
  268. },
  269. emmit(event_name){
  270. var handler;
  271. for (var i = this.event_handlers[event_name].length; i >= 0 ; i--) {
  272. handler = this.event_handlers[event_name][i];
  273. setTimeout(handler, 0);
  274. }
  275. },
  276. }
  277. // ___ _ _ ___
  278. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  279. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  280. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  281. function initScrollbars(){
  282. // console.log("initScrollbars");
  283. // TODO: find a better js scroll than overlayScrollbars which does not handle well max-height + overflow-y:auto;
  284. // $('.os-scroll').overlayScrollbars({
  285. // overflowBehavior:{
  286. // x:'h',
  287. // y:'scroll',
  288. // clipAlways:false
  289. // }
  290. // });
  291. };
  292. // _ _
  293. // /_\ (_)__ ___ __
  294. // / _ \ | / _` \ \ /
  295. // /_/ \_\/ \__,_/_\_\
  296. // |__/
  297. // TODO: add url hash nav
  298. // TODO: implement history.js
  299. function initAjaxLinks(){
  300. console.log('initAjaxLinks');
  301. $('a', '#block-mainnavigation')
  302. .add('a', '#block-footer.menu--footer')
  303. .add('a', '#block-productions')
  304. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  305. .add('a', '.productions-subtree')
  306. .add('a', '.productions-parent')
  307. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  308. .addClass('ajax-link');
  309. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  310. .each(function(i,e){
  311. var $this = $(this);
  312. // avoid already ajaxified links
  313. if($this.is('.ajax-enable')) return;
  314. if($this.attr('data-drupal-link-system-path')){
  315. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  316. }
  317. });
  318. };
  319. function onClickAjaxLink(e){
  320. e.preventDefault();
  321. var $link = $(this);
  322. if($link.is('.is-active'))
  323. return false;
  324. // Audio links
  325. if($link.is('.audio-link')){
  326. _audio_player.openDocument({
  327. nid:$link.attr('nid'),
  328. audio_url:$link.attr('audio_url')
  329. });
  330. return false;
  331. }
  332. // other links
  333. var sys_path = $(this).attr('data-drupal-link-system-path');
  334. var ajax_path = sys_path;
  335. if(sys_path == '<front>'){
  336. backToFrontPage();
  337. return false;
  338. }
  339. // convert node link to edlp_ajax_node module links
  340. var node_match = ajax_path.match(/^\/?(node\/\d+)$/g);
  341. var term_match = ajax_path.match(/^\/?(taxonomy\/term\/\d+)$/g);
  342. if(node_match){
  343. ajax_path = 'edlp/ajax/json/'+node_match[0];
  344. // check for viewmode attribute
  345. if($link.attr('viewmode')){
  346. ajax_path += '/'+$link.attr('viewmode');
  347. }
  348. }else if(term_match){
  349. ajax_path = 'edlp/ajax/json/'+term_match[0];
  350. ajax_path = ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  351. // check for viewmode attribute
  352. if($link.attr('viewmode')){
  353. ajax_path += '/'+$link.attr('viewmode');
  354. }
  355. }else{
  356. // convert other link to ajax
  357. ajax_path += '/ajax'
  358. }
  359. _$body.addClass('ajax-loading');
  360. $link.addClass('ajax-loading');
  361. // TODO: use Drupal.url()
  362. // Drupal.url = function (path) {
  363. // return drupalSettings.path.baseUrl + drupalSettings.path.pathPrefix + path;
  364. // };
  365. var path = window.location.origin + drupalSettings.path.baseUrl + ajax_path;
  366. $.getJSON(path, {})
  367. .done(function(data){
  368. onAjaxLinkLoaded(data, $link, sys_path);
  369. })
  370. .fail(function(jqxhr, textStatus, error){
  371. onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path);
  372. });
  373. return false;
  374. };
  375. function onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path){
  376. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  377. $link.removeClass('ajax-loading');
  378. _$body.removeClass('ajax-loading');
  379. };
  380. function onAjaxLinkLoaded(data, $link, sys_path){
  381. console.log('ajax link loaded : data', data);
  382. _$body.removeClass('ajax-loading');
  383. // replace all content with newly loaded
  384. _$content_container.html(data.rendered);
  385. // add body class for currently loaded content
  386. var body_classes = [
  387. 'path-'+sys_path.replace(/\//g, '-'),
  388. 'entity-type-'+data.entity_type,
  389. 'bundle-'+data.bundle,
  390. 'view-mode-'+data.view_mode
  391. ];
  392. _$body.removeClass().addClass(body_classes.join(' '));
  393. // id node add a generic path-node class to body
  394. m = sys_path.match(/^\/?(node\/\d+)$/g);
  395. if(m)
  396. _$body.addClass('path-edlp-node');
  397. // handle clicked link classes
  398. _$ajaxLinks.removeClass('is-active');
  399. $link.removeClass('ajax-loading').addClass('is-active');
  400. // if block attached (eg : from edlp_productions module)
  401. if(typeof data.block != 'undefined'){
  402. // if block not already added
  403. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  404. $('.region-'+data.block.region).append(data.block.rendered);
  405. }
  406. }
  407. initScrollbars();
  408. if(sys_path == "productions")
  409. initProductions();
  410. initAjaxLinks();
  411. _$body.trigger({'type':'new-content-ajax-loaded'});
  412. // TODO: call behaviours
  413. Drupal.attachBehaviors(_$content_container);
  414. };
  415. // ___
  416. // / __|___ _ _ _ __ _ _ ___
  417. // | (__/ _ \ '_| '_ \ || (_-<
  418. // \___\___/_| | .__/\_,_/__/
  419. // |_|
  420. function onCorpusMapReady(e){
  421. console.log('theme : onCorpusReady', e);
  422. _$corpus_canvas = $('canvas#corpus-map');
  423. _$corpus_canvas
  424. .on('corpus-cliked-on-map', function(e) {
  425. console.log('theme : corpus-cliked-on-map');
  426. backToFrontPage();
  427. })
  428. .on('corpus-cliked-on-node', function(e) {
  429. console.log('theme : corpus-cliked-on-node', e);
  430. _audio_player.openDocument(e.target_node);
  431. });
  432. _randomPlayer = new RandomPlayer(e.playlist);
  433. _$body.attr('corpus-map', 'ready');
  434. }
  435. // ___ _ ___ _
  436. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  437. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  438. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  439. // |__/
  440. function RandomPlayer(playlist){
  441. this.active = false;
  442. this.playlist = playlist;
  443. this.$btn = $('<a>').html('Shuffle').addClass('random-player-btn');
  444. this.init()
  445. };
  446. RandomPlayer.prototype = {
  447. init(){
  448. // this.shuffle();
  449. $('<div>')
  450. .addClass('block random-player')
  451. .append(this.$btn)
  452. .insertAfter('#block-userlogin, #block-studiolinkblock');
  453. // events
  454. this.$btn.on('click', this.toggleActive.bind(this));
  455. // attach an event on AudioPlayer
  456. _audio_player
  457. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  458. .on('audio-play-next', this.onAudioPlayNext.bind(this));
  459. },
  460. shuffle(){
  461. var tempPLaylist = [];
  462. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  463. tempPLaylist.push(this.playlist[i]);
  464. }
  465. this.shuffledPlaylist = [];
  466. while(tempPLaylist.length > 0){
  467. var r = Math.floor(Math.random() * tempPLaylist.length);
  468. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  469. }
  470. console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  471. },
  472. toggleActive(e){
  473. if (this.active) {
  474. this.$btn.removeClass('is-active');
  475. this.stop();
  476. }else{
  477. this.$btn.addClass('is-active');
  478. this.shuffle();
  479. this.start();
  480. }
  481. },
  482. start(){
  483. this.active = _audio_player.shuffle_is_active = true;
  484. this.next();
  485. },
  486. stop(){
  487. this.active = _audio_player.shuffle_is_active = false;
  488. // stop audio player
  489. _audio_player.stop();
  490. },
  491. next(){
  492. if(this.active && this.shuffledPlaylist.length > 0)
  493. _audio_player.openDocument(this.shuffledPlaylist.splice(0,1)[0]);
  494. },
  495. onAudioPlayNext(){
  496. console.log('RandomPlayer : onAudioPlayNext()');
  497. this.next();
  498. },
  499. onAudioPlayerEnded(){
  500. console.log('RandomPlayer : onAudioPlayerEnded()');
  501. this.next();
  502. }
  503. };
  504. // ___ _ _ _
  505. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  506. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  507. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  508. function initProductions(){
  509. console.log('theme : initProductions');
  510. var $grid = $('.row', _$content_container).masonry({
  511. itemSelector:'.col',
  512. columnWidth:'.col-2'
  513. });
  514. // layout Masonry after each image loads
  515. $grid.imagesLoaded().progress( function() {
  516. $grid.masonry('layout');
  517. });
  518. // var $grid = $('.row', _$content_container).imagesLoaded( function() {
  519. // // init Masonry after all images have loaded
  520. // $grid.masonry({
  521. // itemSelector:'.col',
  522. // columnWidth:'.col-2'
  523. // });
  524. // });
  525. };
  526. // ___ _ ___
  527. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  528. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  529. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  530. // |___/
  531. function backToFrontPage(){
  532. closeAllModals();
  533. // assume we are going back to front page
  534. $('body').removeClass().addClass('path-frontpage');
  535. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  536. }
  537. // __ __ _ _
  538. // | \/ |___ __| |__ _| |___
  539. // | |\/| / _ \/ _` / _` | (_-<
  540. // |_| |_\___/\__,_\__,_|_/__/
  541. function closeAllModals(){
  542. console.log('theme : closeAllModals');
  543. // TODO: animate the remove
  544. _$content_container.html('');
  545. _$ajaxLinks.removeClass('is-active');
  546. };
  547. init();
  548. } // end EdlpTheme()
  549. $(document).ready(function($) {
  550. var edlptheme = new EdlpTheme();
  551. });
  552. })(jQuery, Drupal, drupalSettings);