main.js 38 KB

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