main.js 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. console.log('EDLP THEME MAIN.js');
  2. (function($, Drupal, drupalSettings) {
  3. EdlpTheme = function(){
  4. var _ajax_settings = drupalSettings.edlp_ajax;
  5. var _$body = $('body');
  6. var _is_front = _$body.is('.path-frontpage');
  7. var _$corpus_canvas;
  8. var _$row = $('main[role="main"]>.layout-content>.row');
  9. var _$ajaxLinks;
  10. var _audioPlayer;
  11. var _randomPlayer;
  12. var _compoPlayer;
  13. // ___ _ _
  14. // |_ _|_ _ (_) |_
  15. // | || ' \| | _|
  16. // |___|_||_|_|\__|
  17. function init(){
  18. console.log("EdlpTheme init()");
  19. initHistory();
  20. if(!drupalSettings.path.isFront)
  21. return;
  22. initEvents();
  23. _audioPlayer = new AudioPlayer();
  24. _compoPlayer = new CompoPlayer();
  25. initAjaxLinks();
  26. // if (_$body.is('.path-productions')) initProductions();
  27. // if(_$body.is('.path-frontpage')) initHome();
  28. // initScrollbars();
  29. };
  30. // ___ _
  31. // | __|_ _____ _ _| |_ ___
  32. // | _|\ V / -_) ' \ _(_-<
  33. // |___|\_/\___|_||_\__/__/
  34. function initEvents(){
  35. _$body
  36. .on('corpus-map-ready', onCorpusMapReady)
  37. .on('on-studio-chutier-updated', initAjaxLinks)
  38. .on('studio-initialized', function(e){
  39. _compoPlayer.newCompo();
  40. })
  41. .on('studio-not-active', function(e){
  42. _compoPlayer.deactivate();
  43. })
  44. .on('on-studio-compo-updated', function(e){
  45. initAjaxLinks();
  46. _compoPlayer.refresh();
  47. })
  48. .on('on-studio-compo-opened', function(e){
  49. initAjaxLinks();
  50. _compoPlayer.newCompo();
  51. })
  52. .on('search-results-loaded', initAjaxLinks)
  53. .on('open_entree', closeAllModals)
  54. .on('close_entree', backToFrontPage);
  55. }
  56. // ___ _ _ ___
  57. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  58. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  59. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  60. function initScrollbars(){
  61. // console.log("initScrollbars");
  62. // TODO: find a better js scroll than overlayScrollbars which does not handle well max-height + overflow-y:auto;
  63. // $('.os-scroll').overlayScrollbars({
  64. // overflowBehavior:{
  65. // x:'h',
  66. // y:'scroll',
  67. // clipAlways:false
  68. // }
  69. // });
  70. };
  71. // _ _
  72. // /_\ (_)__ ___ __
  73. // / _ \ | / _` \ \ /
  74. // /_/ \_\/ \__,_/_\_\
  75. // |__/
  76. function parseAjaxSysPath(sys_path, view_mode){
  77. // console.log('Theme : parseAjaxSysPath', sys_path);
  78. var ajax_path = sys_path;
  79. // convert node link to edlp_ajax_node module links
  80. var node_match = ajax_path.match(/^\/?(node\/\d+)$/g);
  81. var term_match = ajax_path.match(/^\/?(taxonomy\/term\/\d+)$/g);
  82. if(node_match){
  83. ajax_path = _ajax_settings.entityjson_path+'/'+node_match[0];
  84. // check for viewmode attribute
  85. if(view_mode){
  86. ajax_path += '/'+view_mode;
  87. }
  88. }else if(term_match){
  89. ajax_path = _ajax_settings.entityjson_path+'/'+term_match[0];
  90. ajax_path = ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  91. // check for viewmode attribute
  92. if(view_mode){
  93. ajax_path += '/'+view_mode;
  94. }
  95. }else{
  96. // convert other link to ajax
  97. // TODO: we assume that other links (no node, no term) are all from own modules (e.g. productions) !! may not be true !!
  98. ajax_path += '/ajax'
  99. }
  100. return ajax_path;
  101. };
  102. function ajaxLoadContent(url, sys_path, ajax_path, selector){
  103. console.log('ajaxLoadContent : url', url);
  104. _$body.addClass('ajax-loading');
  105. var path = window.location.origin + Drupal.url(ajax_path);
  106. $.getJSON(path, {})
  107. .done(function(data){
  108. onAjaxLoaded(data, url, ajax_path, sys_path, selector);
  109. })
  110. .fail(function(jqxhr, textStatus, error){
  111. onAjaxLoadError(jqxhr, textStatus, error, sys_path);
  112. });
  113. };
  114. function onAjaxLoadError(jqxhr, textStatus, error, sys_path){
  115. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  116. $('.ajax-loading').removeClass('ajax-loading');
  117. _$body.removeClass('ajax-loading');
  118. };
  119. function onAjaxLoaded(data, url, ajax_path, sys_path, selector){
  120. console.log('ajax loaded url:'+url+' ajax_path:'+ajax_path+' sys_path:'+sys_path);
  121. // reset all style may been added by other pages (like masonry for productions)
  122. // and replace all content with newly loaded
  123. // TODO: build a system to replace or append contents (like studio + search)
  124. _$row.removeAttr('style').html(data.rendered);
  125. // add close btn
  126. if(sys_path != 'productions'){
  127. addCloseBtnToCols();
  128. }
  129. // add body class for currently loaded content
  130. var body_classes = [
  131. 'path-'+sys_path.replace(/\//g, '-'),
  132. 'entity-type-'+data.entity_type,
  133. 'bundle-'+data.bundle,
  134. 'view-mode-'+data.view_mode
  135. ];
  136. _$body.removeClass().addClass(body_classes.join(' '));
  137. // id node add a generic path-node class to body
  138. m = sys_path.match(/^\/?(node\/\d+)$/g);
  139. if(m)
  140. _$body.addClass('path-edlp-node');
  141. // handle clicked link classes
  142. $('.ajax-loading').removeClass('ajax-loading');
  143. $('.is-active').removeClass('is-active');
  144. $('.is-active-trail').removeClass('is-active-trail');
  145. if(typeof selector != 'undefined'){
  146. $('a[selector="'+selector+'"]').addClass('is-active');
  147. }else{
  148. $('a[data-drupal-link-system-path="'+sys_path+'"]').addClass('is-active');
  149. }
  150. // if bundle page (productions) activate production links
  151. if (typeof data.bundle != 'undefined' && data.bundle == "page") {
  152. $('a[data-drupal-link-system-path="productions"]').addClass('is-active-trail');
  153. }
  154. // if node is in production menu tree, set first level of tree active, e.g. pieces sonores
  155. if (typeof data.menu_parents != 'undefined') {
  156. for (var i = 0; i < data.menu_parents.length; i++) {
  157. var menu_sys_path = data.menu_parents[i];
  158. $('a[data-drupal-link-system-path="'+menu_sys_path+'"]').addClass('is-active-trail');
  159. }
  160. }
  161. // if block attached (eg : from edlp_productions module)
  162. // not used anymore as production block is always present (but not visible)
  163. if(typeof data.block != 'undefined'){
  164. // if block not already added
  165. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  166. $('.region-'+data.block.region).append(data.block.rendered);
  167. }
  168. }
  169. // initScrollbars();
  170. if(sys_path == "productions")
  171. initProductions();
  172. initAjaxLinks();
  173. // trigger other modules behaviours
  174. _$body.trigger({'type':'new-content-ajax-loaded'});
  175. // and call druapl behaviours
  176. Drupal.attachBehaviors(_$row[0]);
  177. _$body.attr('booted', 'booted');
  178. _$body.removeClass('ajax-loading');
  179. var state = {
  180. ajax_path:ajax_path,
  181. sys_path:sys_path,
  182. };
  183. // url is null means that we are loading content on popState event
  184. // so we don't record the state again
  185. if(url){
  186. // console.log('url:'+url+' ; state',state);
  187. // we can not pushestate with absolute url
  188. history.pushState(state, null, url);
  189. }
  190. };
  191. function addCloseBtnToCols(){
  192. $('.col', _$row).each(function(index, el) {
  193. if($('span.close-col-btn', this).length)
  194. return true;
  195. $(this).children('.wrapper').append($('<span>')
  196. .addClass('close-col-btn')
  197. .on('click', function(e){
  198. // check for theme attribute and emmit event
  199. var $col = $(this).parents('.col');
  200. var theme = $col.attr('theme');
  201. if(theme != ''){
  202. _$body.trigger({'type':theme+'-col-closed'});
  203. }
  204. // remove the col
  205. $col.remove();
  206. // if row is empty call closeAllModals()
  207. if(!$('.col', _$row).length){
  208. backToFrontPage();
  209. }
  210. })
  211. );
  212. });
  213. };
  214. // _ _ _ _
  215. // | || (_)__| |_ ___ _ _ _ _
  216. // | __ | (_-< _/ _ \ '_| || |
  217. // |_||_|_/__/\__\___/_| \_, |
  218. // |__/
  219. function initHistory(){
  220. initFirstLoad();
  221. window.addEventListener('popstate', onHistoryPopState);
  222. };
  223. function initFirstLoad(){
  224. console.log('theme : initFirstLoad()', window.location);
  225. console.log(document.cookie);
  226. var url = window.location.pathname;
  227. // var origin_path = getCookie('edlp_origin_path');
  228. // console.log('origin_path', origin_path);
  229. var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
  230. console.log('origin_sys_path', origin_sys_path);
  231. var origin_url = window.localStorage.getItem('edlp_origin_url');
  232. console.log('origin_url', origin_url);
  233. if(origin_sys_path){
  234. // create history state
  235. var state = {
  236. ajax_path: parseAjaxSysPath(origin_sys_path),
  237. sys_path: origin_sys_path,
  238. };
  239. console.log('state', state);
  240. // record history state
  241. history.replaceState(state, null, origin_url);
  242. // load content through ajax
  243. ajaxLoadContent(null, state.sys_path, state.ajax_path);
  244. // reset the storage
  245. window.localStorage.removeItem("edlp_origin_path");
  246. window.localStorage.removeItem("edlp_origin_url");
  247. }else{
  248. history.replaceState({home:true}, null, url);
  249. _$body.attr('booted', 'booted');
  250. }
  251. };
  252. function onHistoryPopState(e){
  253. //console.log('onPopState',e);
  254. if(e.state.home){
  255. backToFrontPage();
  256. }else{
  257. ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  258. }
  259. };
  260. // _ _ _ _ _
  261. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  262. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  263. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  264. // |__/
  265. function initAjaxLinks(){
  266. // console.log('initAjaxLinks');
  267. $('a', '#block-mainnavigation')
  268. .add('a', '#block-footer.menu--footer')
  269. .add('a', '#block-productions')
  270. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  271. .add('a', '.productions-subtree')
  272. .add('a', '.productions-parent')
  273. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  274. .addClass('ajax-link');
  275. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  276. .each(function(i,e){
  277. var $this = $(this);
  278. // avoid already ajaxified links
  279. if($this.is('.ajax-enable')) return;
  280. if($this.attr('data-drupal-link-system-path')){
  281. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  282. }
  283. });
  284. };
  285. function onClickAjaxLink(e){
  286. e.preventDefault();
  287. var $link = $(this);
  288. if($link.is('.is-active'))
  289. return false;
  290. // Audio links
  291. // launch audio player and stop here
  292. if($link.is('.audio-link')){
  293. _audioPlayer
  294. .emmit('stop-shuffle')
  295. .openDocument({
  296. nid:$link.attr('nid'),
  297. audio_url:$link.attr('audio_url')
  298. });
  299. return false;
  300. }
  301. // other links
  302. var url = $(this).attr('href');
  303. var sys_path = $(this).attr('data-drupal-link-system-path');
  304. // front page
  305. // just remove contents and stop here
  306. if(sys_path == '<front>'){
  307. backToFrontPage();
  308. return false;
  309. }
  310. var view_mode = $link.attr('viewmode');
  311. var ajax_path = parseAjaxSysPath(sys_path, view_mode);
  312. $link.addClass('ajax-loading');
  313. if($link.is('[selector]')){
  314. var selector = $link.attr('selector');
  315. }
  316. ajaxLoadContent(url, sys_path, ajax_path, selector);
  317. return false;
  318. };
  319. // ___
  320. // / __|___ _ _ _ __ _ _ ___
  321. // | (__/ _ \ '_| '_ \ || (_-<
  322. // \___\___/_| | .__/\_,_/__/
  323. // |_|
  324. function onCorpusMapReady(e){
  325. //console.log('theme : onCorpusReady', e);
  326. _$corpus_canvas = $('canvas#corpus-map');
  327. _$corpus_canvas
  328. .on('corpus-cliked-on-map', function(e) {
  329. //console.log('theme : corpus-cliked-on-map');
  330. backToFrontPage();
  331. })
  332. .on('corpus-cliked-on-node', function(e) {
  333. //console.log('theme : corpus-cliked-on-node', e);
  334. _audioPlayer
  335. .emmit('stop-shuffle')
  336. .openDocument(e.target_node);
  337. });
  338. _randomPlayer = new RandomPlayer(e.playlist);
  339. _$body.attr('corpus-map', 'ready');
  340. }
  341. // _ _ _
  342. // /_\ _ _ __| (_)___
  343. // / _ \ || / _` | / _ \
  344. // /_/ \_\_,_\__,_|_\___/
  345. //
  346. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  347. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  348. // https://www.binarytides.com/using-html5-audio-element-javascript/
  349. function AudioPlayer(){
  350. var that = this;
  351. this.fid;
  352. this.audio = new Audio();
  353. // audio events
  354. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended"];
  355. // UI dom objects
  356. this.$container = $('<div id="audio-player">');
  357. // btns
  358. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  359. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  360. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  361. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  362. // timeline
  363. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  364. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  365. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  366. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  367. // time
  368. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  369. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  370. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  371. // favoris
  372. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  373. // cartel
  374. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  375. // hiding
  376. this.hideTimer = false;
  377. this.hideTimeMS = 10000;
  378. // history
  379. this.currentHistoricIndex = null;
  380. this.historic = [];
  381. this.shuffle_is_active = false;
  382. // object events
  383. this.event_handlers = {
  384. 'audio-open-document':[],
  385. 'audio-play':[],
  386. 'audio-pause':[],
  387. 'audio-play-next':[],
  388. 'audio-ended':[],
  389. 'stop-shuffle':[]
  390. };
  391. this.init();
  392. };
  393. AudioPlayer.prototype = {
  394. init(){
  395. // append ui to document
  396. this.$container.appendTo('header[role="banner"] .region-header');
  397. // record timeline width
  398. this.timeline_w = parseInt(this.$timeline.width());
  399. // init audio events
  400. var fn = '';
  401. for (var i = 0; i < this.audio_events.length; i++) {
  402. fn = this.audio_events[i];
  403. // capitalize first letter of event (only cosmetic :p )
  404. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  405. this.audio.addEventListener(
  406. this.audio_events[i],
  407. this[fn].bind(this),
  408. true);
  409. }
  410. // init btns events
  411. this.$previous.on('click', this.playPrevious.bind(this));
  412. this.$playpause.on('click', this.togglePlayPause.bind(this));
  413. this.$next.on('click', this.playNext.bind(this));
  414. },
  415. openDocument(node, caller){
  416. // console.log('AudioPlayer openDocument', node);
  417. if(typeof node == 'undefined'
  418. || typeof node.nid == 'undefined'
  419. || typeof node.audio_url == 'undfined'){
  420. console.warn('AudioPlayer openDocument() node is malformed', node);
  421. return false;
  422. }
  423. this.historic.push(node);
  424. this.currentHistoricIndex = this.historic.length-1;
  425. // this.shuffle_mode = shuffle_mode || false;
  426. // TODO: add an hash tag to be able to share and play audio from any where
  427. this.emmit('audio-open-document', {caller:caller});
  428. this.launch();
  429. },
  430. launch(){
  431. this.clearTimeOutToHide();
  432. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  433. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  434. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  435. try {
  436. _$corpus_canvas.trigger({
  437. 'type':'audio-node-opened',
  438. 'nid':this.historic[this.currentHistoricIndex].nid
  439. });
  440. } catch (e) {
  441. console.info('AudioPlayer : _$corpus_canvas does not exists');
  442. }
  443. this.showHidePreviousBtn();
  444. this.showHideNextBtn();
  445. this.show();
  446. },
  447. // audio functions
  448. setSRC(url){
  449. // console.log('AudioPlayer setSRC : url', url);
  450. this.audio.src = url;
  451. },
  452. onLoadedmetadata(){
  453. var rem = parseInt(this.audio.duration, 10),
  454. mins = Math.floor(rem/60,10),
  455. secs = rem - mins*60;
  456. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  457. this.updateLoadingBar();
  458. },
  459. updateLoadingBar(){
  460. this.$loader.css({
  461. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  462. });
  463. if( this.audio.buffered.end(0) < this.audio.duration ){
  464. // loop through this function until file is fully loaded
  465. var that = this;
  466. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  467. }else{
  468. //console.log('Audio fully loaded');
  469. }
  470. },
  471. onCanplay(){
  472. this.play();
  473. },
  474. play(){
  475. this.clearTimeOutToHide();
  476. this.audio.play();
  477. },
  478. playPrevious(){
  479. if(this.currentHistoricIndex > 0){
  480. this.currentHistoricIndex -= 1;
  481. this.launch();
  482. }
  483. },
  484. playNext(){
  485. if(this.currentHistoricIndex < this.historic.length-1){
  486. this.currentHistoricIndex += 1;
  487. this.launch();
  488. }else{
  489. this.emmit('audio-play-next');
  490. }
  491. },
  492. togglePlayPause(e){
  493. if(this.audio.paused){
  494. this.audio.play();
  495. }else{
  496. this.audio.pause();
  497. }
  498. },
  499. stop(){
  500. // console.log('AudioPlayer stop()');
  501. this.audio.pause();
  502. this.timeOutToHide();
  503. },
  504. // audio events
  505. onPlaying(){
  506. this.$btns.addClass('is-playing');
  507. this.emmit('audio-play');
  508. },
  509. onPause(){
  510. this.$btns.removeClass('is-playing');
  511. this.emmit('audio-pause');
  512. },
  513. onTimeupdate(){
  514. // move cursor
  515. this.$cursor.css({
  516. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  517. });
  518. // update time text display
  519. var rem = parseInt(this.audio.currentTime, 10),
  520. mins = Math.floor(rem/60,10),
  521. secs = rem - mins*60;
  522. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  523. },
  524. onEnded(){
  525. this.emmit('audio-ended');
  526. this.stop();
  527. },
  528. // cartel functions
  529. loadNode(nid){
  530. this.$cartel.addClass('loading');
  531. var vm = 'player_cartel';
  532. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  533. var path = window.location.origin + Drupal.url(ajax_path);
  534. $.getJSON(path, {})
  535. .done(this.onNodeLoaded.bind(this))
  536. .fail(this.onNodeLoadFail.bind(this));
  537. },
  538. onNodeLoaded(data){
  539. // console.log('AudioPlayer node loaded');
  540. this.$cartel.html(data.rendered).removeClass('loading');
  541. _$body.trigger({'type':'new-audio-cartel-loaded'});
  542. initAjaxLinks();
  543. },
  544. onNodeLoadFail(jqxhr, textStatus, error){
  545. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  546. this.$cartel.removeClass('loading').html('');
  547. },
  548. // global
  549. show(){
  550. this.$container.addClass('visible');
  551. },
  552. showHidePreviousBtn(){
  553. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  554. this.$previous.addClass('is-active');
  555. }else{
  556. this.$previous.removeClass('is-active');
  557. }
  558. },
  559. showHideNextBtn(){
  560. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  561. this.$next.addClass('is-active');
  562. }else{
  563. this.$next.removeClass('is-active');
  564. }
  565. },
  566. timeOutToHide(){
  567. // console.log('AudioPlayer timeOutToHide()');
  568. this.clearTimeOutToHide();
  569. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  570. },
  571. clearTimeOutToHide(){
  572. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  573. if(this.hideTimer){
  574. clearTimeout(this.hideTimer);
  575. this.hideTimer = false;
  576. }
  577. },
  578. hide(){
  579. // console.log('AudioPlayer hide()');
  580. this.$container.removeClass('visible');
  581. // trigger highlighted node remove on corpus map
  582. try {
  583. _$corpus_canvas.trigger('audio-node-closed');
  584. } catch (e) {
  585. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  586. }
  587. },
  588. // object events
  589. on(event_name, handler){
  590. if(typeof this.event_handlers[event_name] == 'undefined'){
  591. console.warn('AudioPlayer : event '+event_name+' does not exists');
  592. }
  593. this.event_handlers[event_name].push(handler);
  594. return this;
  595. },
  596. emmit(event_name, args){
  597. // console.log('AudioPlayer emmit() event_name', event_name);
  598. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  599. var handler;
  600. var args = args || {};
  601. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  602. handler = this.event_handlers[event_name][i];
  603. // console.log('AudioPlayer emmit() loop handler', handler);
  604. setTimeout(function(){
  605. // console.log('AudioPlayer emmit() timeout handler', handler);
  606. handler(args);
  607. }, 0);
  608. }
  609. return this;
  610. },
  611. }
  612. // ___ _ ___ _
  613. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  614. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  615. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  616. // |__/
  617. function RandomPlayer(playlist){
  618. this.active = false;
  619. this.playlist = playlist;
  620. this.$btn = $('<a>').html('Shuffle').addClass('random-player-btn');
  621. this.init();
  622. };
  623. RandomPlayer.prototype = {
  624. init(){
  625. // this.shuffle();
  626. $('<div>')
  627. .addClass('block random-player')
  628. .append(this.$btn)
  629. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  630. .prependTo('.region-footer-right');
  631. // events
  632. this.$btn.on('click', this.toggleActive.bind(this));
  633. // attach an event on AudioPlayer
  634. _audioPlayer
  635. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  636. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  637. .on('stop-shuffle', this.stop.bind(this));
  638. },
  639. shuffle(){
  640. var tempPLaylist = [];
  641. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  642. tempPLaylist.push(this.playlist[i]);
  643. }
  644. this.shuffledPlaylist = [];
  645. while(tempPLaylist.length > 0){
  646. var r = Math.floor(Math.random() * tempPLaylist.length);
  647. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  648. }
  649. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  650. },
  651. toggleActive(e){
  652. if (this.active) {
  653. this.stop();
  654. }else{
  655. this.start();
  656. }
  657. },
  658. start(){
  659. this.active = _audioPlayer.shuffle_is_active = true;
  660. this.$btn.addClass('is-active');
  661. this.shuffle();
  662. this.next();
  663. },
  664. stop(){
  665. this.active = _audioPlayer.shuffle_is_active = false;
  666. this.$btn.removeClass('is-active');
  667. // stop audio player
  668. // _audioPlayer.stop();
  669. },
  670. next(){
  671. if(this.active && this.shuffledPlaylist.length > 0)
  672. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0]);
  673. },
  674. onAudioPlayNext(){
  675. //console.log('RandomPlayer : onAudioPlayNext()');
  676. this.next();
  677. },
  678. onAudioPlayerEnded(){
  679. //console.log('RandomPlayer : onAudioPlayerEnded()');
  680. this.next();
  681. }
  682. };
  683. // ___ ___ _
  684. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  685. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  686. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  687. // |_| |__/
  688. function CompoPlayer(){
  689. this.active = false;
  690. this.playing = false;
  691. this.paused = false;
  692. this.playlist = [];
  693. this.current_index = 0;
  694. this.$composer = null;
  695. this.$compo = null;
  696. this.$controls = null;
  697. this.init();
  698. };
  699. CompoPlayer.prototype = {
  700. init(){
  701. // console.log('CompoPlayer init()');
  702. // attach an event on AudioPlayer
  703. _audioPlayer
  704. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  705. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  706. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  707. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  708. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  709. // this.newCompo();
  710. },
  711. newCompo(){
  712. //console.log('CompoPlayer newCompo()');
  713. // this.$compo = $('.composition_ui .composer .composition');
  714. this.initControls();
  715. },
  716. initControls(){
  717. //console.log('CompoPlayer initControls()');
  718. this.$composer = $('.composition_ui .composer');
  719. this.$compo = $('.composition_ui .composer .composition');
  720. this.$controls = $('.composition_ui .composer .compo-player-controls');
  721. if(!this.$controls.is('.ready') && this.$compo){
  722. this.$previous = $('<div>').addClass('previous')
  723. .on('click', this.prev.bind(this))
  724. .appendTo(this.$controls);
  725. this.$playpause = $('<div>').addClass('play-pause')
  726. .on('click', this.togglePlayPause.bind(this))
  727. .appendTo(this.$controls);
  728. this.$next = $('<div>').addClass('next')
  729. .on('click', this.next.bind(this))
  730. .appendTo(this.$controls);
  731. this.$controls.addClass('ready');
  732. this.refresh();
  733. this.active = true;
  734. // this.newCompo();
  735. }
  736. },
  737. refresh(){
  738. // console.log('CompoPlayer refresh(), this', this);
  739. this.stop();
  740. // load new playlist
  741. this.playlist = [];
  742. var that = this;
  743. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  744. var $link = $('a.audio-link',this);
  745. that.playlist.push({
  746. item:$(this),
  747. audio_url:$link.attr("audio_url"),
  748. nid:$link.attr("nid"),
  749. });
  750. });
  751. this.showHideControls();
  752. },
  753. togglePlayPause(){
  754. // console.log('CompoPlayer togglePlayPause');
  755. if (this.playing && !this.paused) {
  756. this.pause();
  757. }else{
  758. if(this.playing && this.paused){
  759. this.play();
  760. }else{
  761. this.start();
  762. }
  763. }
  764. },
  765. start(){
  766. //console.log('start');
  767. // console.log('CompoPlayer start()');
  768. this.playing = true;
  769. this.play();
  770. },
  771. play(){
  772. // console.log('play');
  773. if(this.paused){
  774. this.paused = false;
  775. _audioPlayer.play();
  776. }else{
  777. _audioPlayer.openDocument(this.playlist[this.current_index], this);
  778. }
  779. this.setActiveItem().showHideControls();
  780. },
  781. pause(){
  782. //console.log('pause');
  783. this.paused = true;
  784. this.showHideControls();
  785. _audioPlayer.stop();
  786. },
  787. next(){
  788. // console.log('CompoPlayer next()');
  789. if(this.playing){
  790. this.current_index += 1;
  791. if(this.current_index < this.playlist.length){
  792. this.play();
  793. }else{
  794. this.stop();
  795. }
  796. }
  797. },
  798. prev(){
  799. // console.log('CompoPlayer prev()');
  800. if(this.playing){
  801. this.current_index -= 1;
  802. if(this.current_index >= 0){
  803. this.play();
  804. }else{
  805. this.stop();
  806. }
  807. }
  808. },
  809. stop(){
  810. _audioPlayer.stop();
  811. this.reset();
  812. },
  813. reset(){
  814. this.playing = false;
  815. this.paused = false;
  816. this.resetIndex();
  817. },
  818. resetIndex(){
  819. this.current_index = 0;
  820. this.showHideControls().resetActiveItems();
  821. },
  822. setActiveItem(){
  823. this.resetActiveItems();
  824. if(this.playing && this.current_index >= 0){
  825. this.playlist[this.current_index].item.addClass('is-active');
  826. }
  827. // this call shoud not be here
  828. this.showHideControls();
  829. return this;
  830. },
  831. resetActiveItems(){
  832. for (var n = 0; n < this.playlist.length; n++) {
  833. // console.log('node',node);
  834. this.playlist[n].item.removeClass('is-active');
  835. }
  836. return this;
  837. },
  838. showHideControls(){
  839. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  840. // global playing
  841. if(this.$controls){
  842. if(this.playing && !this.paused){
  843. this.$controls.addClass('is-playing');
  844. }else{
  845. this.$controls.removeClass('is-playing');
  846. }
  847. }
  848. // playpause
  849. if(this.$playpause){
  850. if(this.playlist.length > 0){
  851. this.$playpause.addClass('is-active');
  852. }else{
  853. this.$playpause.removeClass('is-active');
  854. }
  855. }
  856. // next
  857. if(this.$next){
  858. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  859. this.$next.addClass('is-active');
  860. }else{
  861. this.$next.removeClass('is-active');
  862. }
  863. }
  864. // previous
  865. if(this.$previous){
  866. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  867. this.$previous.addClass('is-active');
  868. }else{
  869. this.$previous.removeClass('is-active');
  870. }
  871. }
  872. return this;
  873. },
  874. deactivate(){
  875. this.stop();
  876. this.active = false;
  877. },
  878. // _audioPlayer events
  879. onAudioOpenDocument(args){
  880. if(args.caller !== this){
  881. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  882. this.reset();
  883. }
  884. // else{
  885. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  886. // }
  887. },
  888. onAudioPlayerPlay(){
  889. if(this.playing && this.paused){
  890. this.paused = false;
  891. this.showHideControls();
  892. }
  893. },
  894. onAudioPlayerPause(){
  895. if(this.playing && !this.paused){
  896. this.paused = true;
  897. this.showHideControls();
  898. }
  899. },
  900. onAudioPlayerEnded(){
  901. this.next();
  902. },
  903. // onAudioPlayNext(){
  904. // this.next();
  905. // }
  906. };
  907. // ___ _ ___
  908. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  909. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  910. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  911. // |___/
  912. function backToFrontPage(){
  913. closeAllModals();
  914. // assume we are going back to front page
  915. $('body').removeClass().addClass('path-frontpage');
  916. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  917. }
  918. function initHome(){
  919. addCloseBtnToCols();
  920. // console.log('theme : initHome');
  921. // console.log('theme : initProductions');
  922. var $grid = $('.grid',_$row).masonry({
  923. itemSelector:'.col',
  924. columnWidth:'.col-2',
  925. horizontalOrder: true,
  926. containerStyle: null,
  927. // disable initial layout
  928. // initLayout: false,
  929. });
  930. // bind event
  931. // $grid.masonry( 'on', 'layoutComplete', function() {
  932. // console.log('layout is complete');
  933. // });
  934. // layout Masonry after each image loads
  935. $grid.imagesLoaded().progress( function() {
  936. $grid.masonry('layout');
  937. });
  938. $grid.imagesLoaded(function(){
  939. $grid.masonry('layout');
  940. });
  941. }
  942. // ___ _ _ _
  943. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  944. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  945. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  946. function initProductions(){
  947. // console.log('theme : initProductions');
  948. var $grid = $('.grid',_$row).masonry({
  949. itemSelector:'.col',
  950. columnWidth:'.col-2',
  951. horizontalOrder: true,
  952. containerStyle: null,
  953. // disable initial layout
  954. // initLayout: false,
  955. });
  956. // bind event
  957. // $grid.masonry( 'on', 'layoutComplete', function() {
  958. // console.log('layout is complete');
  959. // });
  960. // layout Masonry after each image loads
  961. $grid.imagesLoaded().progress( function() {
  962. $grid.masonry('layout');
  963. });
  964. $grid.imagesLoaded(function(){
  965. $grid.masonry('layout');
  966. });
  967. };
  968. // __ __ _ _
  969. // | \/ |___ __| |__ _| |___
  970. // | |\/| / _ \/ _` / _` | (_-<
  971. // |_| |_\___/\__,_\__,_|_/__/
  972. function closeAllModals(){
  973. //console.log('theme : closeAllModals');
  974. // TODO: animate the remove
  975. _$row.html('');
  976. _$ajaxLinks.removeClass('is-active');
  977. _$body.trigger({'type':'all-modal-closed'});
  978. };
  979. // _ _ _
  980. // | || |___| |_ __ ___ _ _ ___
  981. // | __ / -_) | '_ \/ -_) '_(_-<
  982. // |_||_\___|_| .__/\___|_| /__/
  983. // |_|
  984. // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
  985. function getCookie(name) {
  986. var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  987. return v ? v[2] : null;
  988. }
  989. function setCookie(name, value, days) {
  990. var d = new Date;
  991. d.setTime(d.getTime() + 24*60*60*1000*days);
  992. document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
  993. }
  994. function deleteCookie(name) { setCookie(name, '', -1); }
  995. init();
  996. } // end EdlpTheme()
  997. $(document).ready(function($) {
  998. var edlptheme = new EdlpTheme();
  999. });
  1000. })(jQuery, Drupal, drupalSettings);