main.js 34 KB

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