main.js 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438
  1. (function($, Drupal, drupalSettings) {
  2. EdlpTheme = function(){
  3. var _ajax_settings = drupalSettings.edlp_ajax;
  4. var _$body = $('body');
  5. // var _is_front = drupalSettings.path.isFront;
  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. _audioPlayer = new AudioPlayer();
  20. _compoPlayer = new CompoPlayer();
  21. initAjaxLinks();
  22. initHistory();
  23. if(!drupalSettings.path.isFront)
  24. return;
  25. initEvents();
  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', function(e){
  74. backToFrontPage();
  75. });
  76. }
  77. // ___ _ _ ___
  78. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  79. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  80. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  81. function initScrollbars(){
  82. // console.log("initScrollbars");
  83. // TODO: find a better js scroll than overlayScrollbars which does not handle well max-height + overflow-y:auto;
  84. // $('.os-scroll').overlayScrollbars({
  85. // overflowBehavior:{
  86. // x:'h',
  87. // y:'scroll',
  88. // clipAlways:false
  89. // }
  90. // });
  91. };
  92. // _ _
  93. // /_\ (_)__ ___ __
  94. // / _ \ | / _` \ \ /
  95. // /_/ \_\/ \__,_/_\_\
  96. // |__/
  97. function getSysPathState(sys_path, view_mode){
  98. // console.log('Theme : getSysPathState', sys_path);
  99. var state = {
  100. 'sys_path':sys_path,
  101. 'ajax_path': sys_path
  102. };
  103. // convert node link to edlp_ajax_node module links
  104. var node_match = state.ajax_path.match(/^\/?(node\/(\d+))$/i);
  105. console.log('node_match', node_match);
  106. var term_match = state.ajax_path.match(/^\/?(taxonomy\/term\/(\d+))$/i);
  107. console.log('term_match', term_match);
  108. if(node_match){
  109. // TODO: detect audio links which will open audioplayer and wont load any ajax content unless view_mode "article" or "transcript"
  110. state.ajax_path = _ajax_settings.entityjson_path+'/'+node_match[1];
  111. state.node_nid = node_match[2];
  112. // check for viewmode attribute
  113. if(view_mode){
  114. state.ajax_path += '/'+view_mode;
  115. state.view_mode = view_mode;
  116. }
  117. }else if(term_match){
  118. // terms are always entrees, there's no other vocabulary links in front
  119. state.ajax_path = _ajax_settings.entityjson_path+'/'+term_match[1];
  120. state.ajax_path = state.ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  121. state.entree_tid = term_match[2];
  122. // check for viewmode attribute
  123. if(view_mode){
  124. state.ajax_path += '/'+view_mode;
  125. state.view_mode = view_mode;
  126. }else{
  127. state.ajax_path = null;
  128. }
  129. }else{
  130. // convert other link to ajax
  131. // TODO: we assume that other links (no node, no term) are all from own modules (e.g. productions) !! may not be true !!
  132. state.ajax_path += '/ajax'
  133. }
  134. return state;
  135. };
  136. function ajaxLoadContent(state){
  137. console.log('ajaxLoadContent : state', state);
  138. _$body.addClass('ajax-loading');
  139. var path = window.location.origin + Drupal.url(state.ajax_path);
  140. $.getJSON(path, {})
  141. .done(function(data){
  142. onAjaxLoaded(data, state);
  143. })
  144. .fail(function(jqxhr, textStatus, error){
  145. onAjaxLoadError(jqxhr, textStatus, error, state.sys_path);
  146. });
  147. };
  148. function onAjaxLoadError(jqxhr, textStatus, error, sys_path){
  149. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  150. $('.ajax-loading').removeClass('ajax-loading');
  151. _$body.removeClass('ajax-loading');
  152. };
  153. function onAjaxLoaded(data, state){
  154. console.log('ajax loaded : state', state);
  155. // console.log(data);
  156. // reset all style may been added by other pages (like masonry for productions)
  157. // and replace all content with newly loaded
  158. // TODO: build a system to replace or append contents (like studio + search)
  159. if(data.entity_type == "node" && data.bundle == "evenement"){
  160. if(_$row.find('.col.event').length){
  161. _$row.find('.col.event').replaceWith(data.rendered);
  162. }else if(_$row.find('.col.aside').length){
  163. _$row.find('.col.aside').replaceWith(data.rendered);
  164. }else{
  165. _$row.append(data.rendered);
  166. }
  167. }else{
  168. _$row.removeAttr('style').html(data.rendered);
  169. }
  170. // add body class for currently loaded content
  171. var body_classes = [
  172. 'path-'+state.sys_path.replace(/\//g, '-'),
  173. 'entity-type-'+data.entity_type,
  174. 'bundle-'+data.bundle,
  175. 'view-mode-'+data.view_mode
  176. ];
  177. _$body.removeClass().addClass(body_classes.join(' '));
  178. // id node add a generic path-node class to body
  179. // m = state.sys_path.match(/^\/?(node\/\d+)$/g);
  180. // if(m)
  181. if(state.node_nid)
  182. _$body.addClass('path-edlp-node');
  183. // handle clicked link classes
  184. $('.ajax-loading').removeClass('ajax-loading');
  185. $('.ajax-link.is-active').removeClass('is-active');
  186. $('.is-active-trail').removeClass('is-active-trail');
  187. if(typeof state.selector != 'undefined'){
  188. // in case of entree link (actualy, selector is used only for entries links)
  189. // TODO: unfortunatly on initFirstLoad there is no selector property
  190. console.log('selector', state.selector);
  191. $('a[selector="'+state.selector+'"]').addClass('is-active');
  192. initAudioLinksInContent();
  193. }else{
  194. if(typeof state.view_mode != 'undefined'){
  195. $('a[viewmode="'+state.view_mode+'"][data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
  196. }else{
  197. $('a[data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
  198. }
  199. // as new content is not related to entree, we trigger close entree
  200. _$body.trigger({'type':'new-content-not-entree-ajax-loaded'});
  201. }
  202. // if bundle page (productions) activate production links
  203. if (typeof data.bundle != 'undefined' && data.bundle == "page") {
  204. $('a[data-drupal-link-system-path="productions"]').addClass('is-active-trail');
  205. }
  206. // if node is in production menu tree, set first level of tree active, e.g. pieces sonores
  207. if (typeof data.menu_parents != 'undefined') {
  208. for (var i = 0; i < data.menu_parents.length; i++) {
  209. var menu_sys_path = data.menu_parents[i];
  210. $('a[data-drupal-link-system-path="'+menu_sys_path+'"]').addClass('is-active-trail');
  211. }
  212. }
  213. // if block attached (eg : from edlp_productions module)
  214. // not used anymore as production block is always present (but not visible)
  215. if(typeof data.block != 'undefined'){
  216. // if block not already added
  217. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  218. $('.region-'+data.block.region).append(data.block.rendered);
  219. }
  220. }
  221. // initScrollbars();
  222. if(state.sys_path == "productions"){
  223. initProductions();
  224. }else{
  225. addCloseModalBtnToCols();
  226. }
  227. // enregistrement transcription
  228. if(data.entity_type == "node" && data.bundle == "enregistrement" && data.view_mode == "transcript"){
  229. // window.requestAnimationFrame(initEnregistrementTranscript);
  230. initEnregistrementTranscript();
  231. }
  232. // update the language switcher block if it comes in the response
  233. if(typeof data.translations_links != 'undefined'){
  234. console.log('state',state);
  235. var lang_code = drupalSettings.path.currentLanguage;
  236. var $links = $(data.translations_links);
  237. // set active link
  238. $links.find('li[hreflang="'+lang_code+'"]').addClass('is-active').find('a').addClass('is-active');
  239. if(state.view_mode){
  240. $links.find('a').each(function(i,e){
  241. var $a = $(this);
  242. $a.attr('href', $a.attr('href')+'#'+state.view_mode);
  243. });
  244. }
  245. // if(state.selector){
  246. // $links.find('a').attr('selector', state.selector);
  247. // }
  248. $('ul','.block.language-switcher-language-url').replaceWith($links);
  249. }
  250. initAjaxLinks();
  251. // trigger other modules behaviours
  252. _$body.trigger({'type':'new-content-ajax-loaded'});
  253. // and call druapl behaviours
  254. Drupal.attachBehaviors(_$row[0]);
  255. _$body.attr('booted', 'booted');
  256. _$body.removeClass('ajax-loading');
  257. // url is null means that we are loading content on popState event
  258. // so we don't record the state again
  259. if(state.url){
  260. // var state = {
  261. // ajax_path:ajax_path,
  262. // sys_path:sys_path,
  263. // };
  264. // console.log('url:'+url+' ; state',state);
  265. // console.log(window.location);
  266. // /!\ we can not pushestate with absolute url /!\
  267. history.pushState(state, null, state.url);
  268. // piwik
  269. if(typeof _paq !== 'undefined'){
  270. // page tracking
  271. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  272. _paq.push(['setCustomUrl', state.url]);
  273. _paq.push(['setDocumentTitle', data.title]);
  274. _paq.push(['trackPageView']);
  275. // js event
  276. // trackEvent(category, action, [name], [value])
  277. _paq.push(['trackEvent', 'AjaxNav', 'loaded', state.url]);
  278. }
  279. }
  280. };
  281. function initAudioLinksInContent(){
  282. _$row.find('a.audio-link')
  283. .on('mouseover', function(event) {
  284. event.preventDefault();
  285. if(_corpus_ready){
  286. _$corpus_canvas.trigger({
  287. type:'mouseover-audio-link',
  288. nid:$(this).attr('nid')
  289. });
  290. }
  291. })
  292. .on('mouseout', function(event) {
  293. event.preventDefault();
  294. if(_corpus_ready){
  295. _$corpus_canvas.trigger({
  296. type:'mouseout-audio-link',
  297. nid:$(this).attr('nid')
  298. });
  299. }
  300. });
  301. };
  302. function addCloseModalBtnToCols(){
  303. $('.col', _$row).each(function(index, el) {
  304. if($('span.close-col-btn', this).length)
  305. return true;
  306. $(this).children('.wrapper').prepend($('<span>')
  307. .addClass('close-col-btn')
  308. .on('click', onCloseModal)
  309. );
  310. });
  311. };
  312. function onCloseModal(e){
  313. // check for theme attribute and emmit event
  314. var $col = $(this).parents('.col');
  315. var theme = $col.attr('theme');
  316. if(theme != ''){
  317. _$body.trigger({'type':theme+'-col-closed'});
  318. }
  319. // remove the col
  320. $col.remove();
  321. // if row is empty and we are not in productions or entree notice|index call closeAllModals()
  322. if(!$('.col', _$row).length
  323. && !_$body.is('.entity-type-node.bundle-page')
  324. && !_$body.is('.entity-type-taxonomy_term.bundle-entrees')){
  325. backToFrontPage();
  326. }
  327. };
  328. // _ _ ___ _ _
  329. // /_\ (_)__ ___ __ | _ ) |___ __| |__ ___
  330. // / _ \ | / _` \ \ / | _ \ / _ \/ _| / /(_-<
  331. // /_/ \_\/ \__,_/_\_\ |___/_\___/\__|_\_\/__/
  332. // |__/
  333. // NOT USED (YET)
  334. function refreshAllBlocks(){
  335. var path = window.location.origin + Drupal.url(_ajax_settings.blocksjson_path);
  336. $.getJSON(path, {})
  337. .done(function(data){
  338. onAjaxBlockLoaded(data);
  339. })
  340. .fail(function(jqxhr, textStatus, error){
  341. onAjaxBlockLoadError(jqxhr, textStatus, error);
  342. });
  343. };
  344. function onAjaxBlockLoadError(jqxhr, textStatus, error){
  345. console.warn('ajax block load failed: '+error, jqxhr.responseText);
  346. };
  347. function onAjaxBlockLoaded(data){
  348. console.log('onAjaxBlockLoaded', data);
  349. // TODO: update each blocks (exepted language switcher)
  350. for (var blockname in data.blocks) {
  351. var block = data.blocks[blockname];
  352. console.log(blockname, block);
  353. $(block.id).replaceWith(block.rendered);
  354. }
  355. };
  356. // _ _ _ _
  357. // | || (_)__| |_ ___ _ _ _ _
  358. // | __ | (_-< _/ _ \ '_| || |
  359. // |_||_|_/__/\__\___/_| \_, |
  360. // |__/
  361. function initHistory(){
  362. initFirstLoad();
  363. window.addEventListener('popstate', onHistoryPopState);
  364. };
  365. function initFirstLoad(){
  366. console.log('theme : initFirstLoad()');
  367. // var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
  368. var edlp_origin = JSON.parse(window.localStorage.getItem('edlp_origin'));
  369. console.log('edlp_origin', edlp_origin);
  370. if(edlp_origin != null && edlp_origin.sys_path){
  371. // hash is used as viewmode for taxonomy term entrees load (index or notice)
  372. // and for audio contents (article|transcript)
  373. var view_mode = edlp_origin.hash.replace('#', '');
  374. // create history state
  375. var state = getSysPathState(edlp_origin.sys_path, view_mode);
  376. // open entree tray in case of entree index|notice
  377. // (index or notice will be opened with ajaxLoadContent)
  378. // refactorized with new infos from edlp_origin
  379. // if(view_mode){
  380. if(edlp_origin.entity_type == "taxonomy_term"
  381. && edlp_origin.entity_bundle == "entrees"
  382. && view_mode){
  383. // var $link = $('[href="'+edlp_origin.url+'"][viewmode="'+view_mode+'"]');
  384. // var selector = $link.attr('selector') || null;
  385. // record the selector in the state for actions after ajaxContentLoaded
  386. state.selector = 'entree-'+view_mode+'-link-'+edlp_origin.entity_id; // entree-index-link-125
  387. // if(selector){
  388. // in case of entree link (actualy, selector is used only for entries links)
  389. // TODO: use a promise
  390. // TODO: but what if corpus ready before onAjaxLoaded >> use a promise !!
  391. if(_corpus_ready){
  392. _$corpus_canvas.trigger({
  393. type:'open-entree',
  394. // tid:$link.attr('tid')
  395. tid:edlp_origin.entity_id
  396. });
  397. }else{
  398. // else : EdlpCorpus will check when ready if entry item (notice or index) is already .is-active
  399. // .is-active class is added by onAjaxLoaded() (when content is loaded)
  400. // $('li.entree[tid="'+$link.attr('tid')+'"] a.term-link').addClass('is-active');
  401. $('li.entree[tid="'+edlp_origin.entity_id+'"] a.term-link').addClass('is-active');
  402. }
  403. // }
  404. }
  405. // check if audio link
  406. if(edlp_origin.audio_url){
  407. var node = {
  408. nid:edlp_origin.entity_id,
  409. audio_url:edlp_origin.audio_url
  410. };
  411. _audioPlayer.openDocument(node, 'history_first_load');
  412. if(view_mode == ""){
  413. // if audio only record in state
  414. state.audio = true;
  415. state.node = node;
  416. // we don't laod ajax content so we boot the home
  417. _$body.attr('booted', 'booted');
  418. }else{
  419. // ajax load content for audio only if article or transcript
  420. ajaxLoadContent(state);
  421. }
  422. }
  423. // only if not entree path
  424. // only if not audio (without article or transcript) path
  425. else if(state.ajax_path){
  426. // load content through ajax
  427. // ajaxLoadContent(null, state.sys_path, state.ajax_path, selector);
  428. ajaxLoadContent(state);
  429. }
  430. if(state.entree_tid){
  431. openEntree(state.entree_tid);
  432. }
  433. // record history state
  434. history.replaceState(state, null, edlp_origin.url+edlp_origin.hash);
  435. // reset the storage
  436. window.localStorage.removeItem("edlp_origin");
  437. // window.localStorage.removeItem("edlp_origin_url");
  438. }else{
  439. history.replaceState({home:true}, null, window.location.pathname);
  440. _$body.attr('booted', 'booted');
  441. }
  442. };
  443. function onHistoryPopState(e){
  444. console.log('onPopState',e.state);
  445. if(e.state.home){
  446. backToFrontPage(true);
  447. }
  448. else if (e.state.audio) {
  449. _audioPlayer.openDocument(e.state.node, 'popstate', e.state.historic_index);
  450. }
  451. else{
  452. if(e.state.entree_tid){
  453. openEntree(e.state.entree_tid);
  454. }
  455. if(e.state.ajax_path){
  456. e.state.url = null;
  457. // ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  458. ajaxLoadContent(e.state);
  459. }
  460. }
  461. };
  462. // _ _ _ _ _
  463. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  464. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  465. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  466. // |__/
  467. function initAjaxLinks(){
  468. // console.log('initAjaxLinks');
  469. $('a.site-name', '#block-edlptheme-branding')
  470. .add('a', '#block-mainnavigation')
  471. // .add('a', '.block.language-switcher-language-url')
  472. .add('a', '#block-footer.menu--footer')
  473. .add('a', '#block-productions')
  474. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  475. .add('a', '.productions-subtree')
  476. .add('a', '.productions-parent')
  477. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  478. .addClass('ajax-link');
  479. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  480. .each(function(i,e){
  481. var $this = $(this);
  482. // avoid already ajaxified links
  483. if($this.is('.ajax-enable')) return;
  484. if($this.attr('data-drupal-link-system-path')){
  485. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  486. }
  487. });
  488. };
  489. function onClickAjaxLink(e){
  490. e.preventDefault();
  491. var $link = $(this);
  492. if($link.is('.is-active'))
  493. return false;
  494. // Audio links
  495. // launch audio player and stop here
  496. if($link.is('.audio-link')){
  497. _audioPlayer
  498. .emmit('stop-shuffle')
  499. .openDocument({
  500. nid:$link.attr('nid'),
  501. audio_url:$link.attr('audio_url'),
  502. title:$link.find('.field--name-title').html()
  503. });
  504. return false;
  505. }
  506. // NOT USED (YET)
  507. // if($link.is('.language-link')){
  508. // TODO: change global site current language
  509. // load all blocks translated (exepted language switcher) ... :/
  510. // refreshAllBlocks();
  511. // TODO: use a promise to initAjaxLinks again after blocks updated
  512. // then let load the content
  513. // }
  514. // other links
  515. var sys_path = $(this).attr('data-drupal-link-system-path');
  516. // front page
  517. // just remove contents and stop here
  518. if(sys_path == '<front>'){
  519. backToFrontPage();
  520. return false;
  521. }
  522. var view_mode = $link.attr('viewmode');
  523. var state = getSysPathState(sys_path, view_mode);
  524. state.url = $(this).attr('href');
  525. if(view_mode){
  526. state.url += "#"+view_mode;
  527. }
  528. if($link.is('[selector]')){
  529. state.selector = $link.attr('selector');
  530. }
  531. $link.addClass('ajax-loading');
  532. // ajaxLoadContent(url, sys_path, state.ajax_path, selector);
  533. ajaxLoadContent(state);
  534. return false;
  535. };
  536. // ___
  537. // / __|___ _ _ _ __ _ _ ___
  538. // | (__/ _ \ '_| '_ \ || (_-<
  539. // \___\___/_| | .__/\_,_/__/
  540. // |_|
  541. function onCorpusMapReady(e){
  542. //console.log('theme : onCorpusReady', e);
  543. _corpus_ready = true;
  544. _$corpus_canvas = $('canvas#corpus-map');
  545. _$corpus_canvas
  546. .on('corpus-cliked-on-map', function(e) {
  547. //console.log('theme : corpus-cliked-on-map');
  548. backToFrontPage();
  549. })
  550. .on('corpus-cliked-on-node', function(e) {
  551. //console.log('theme : corpus-cliked-on-node', e);
  552. _audioPlayer
  553. .emmit('stop-shuffle')
  554. .setAutoOpenArticle(e.article)
  555. .openDocument(e.target_node);
  556. });
  557. _randomPlayer = new RandomPlayer(e.playlist);
  558. _$body.attr('corpus-map', 'ready');
  559. }
  560. function openEntree(tid){
  561. if(tid){
  562. closeAllModals();
  563. _$body.removeClass();//.addClass(body_classes.join(' '));
  564. // open entree
  565. if(_corpus_ready){
  566. _$corpus_canvas.trigger({
  567. type:'open-entree',
  568. tid:tid
  569. });
  570. }else{
  571. // else : EdlpCorpus will check when ready if entry item (notice or index) is already .is-active
  572. $('li.entree[tid="'+tid+'"] a.term-link').addClass('is-active');
  573. }
  574. }
  575. };
  576. // _ _ _
  577. // /_\ _ _ __| (_)___
  578. // / _ \ || / _` | / _ \
  579. // /_/ \_\_,_\__,_|_\___/
  580. //
  581. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  582. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  583. // https://www.binarytides.com/using-html5-audio-element-javascript/
  584. function AudioPlayer(){
  585. var that = this;
  586. this.fid;
  587. this.audio = new Audio();
  588. // audio events
  589. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended","error"];
  590. // UI dom objects
  591. this.$container = $('<div id="audio-player">');
  592. // btns
  593. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  594. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  595. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  596. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  597. // timeline
  598. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  599. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  600. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  601. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  602. // time
  603. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  604. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  605. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  606. // favoris
  607. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  608. // cartel
  609. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  610. // hiding
  611. this.hideTimer = false;
  612. this.hideTimeMS = 10000;
  613. // history
  614. this.currentHistoricIndex = null;
  615. this.historic = [];
  616. this.shuffle_is_active = false;
  617. // artciles
  618. this.auto_open_article = false;
  619. // object events
  620. this.event_handlers = {
  621. 'audio-open-document':[],
  622. 'audio-play':[],
  623. 'audio-pause':[],
  624. 'audio-play-next':[],
  625. 'audio-ended':[],
  626. 'stop-shuffle':[]
  627. };
  628. this.init();
  629. };
  630. AudioPlayer.prototype = {
  631. init(){
  632. // append ui to document
  633. this.$container.appendTo('header[role="banner"] .region-header');
  634. // record timeline width
  635. this.timeline_w = parseInt(this.$timeline.width());
  636. // init audio events
  637. var fn = '';
  638. for (var i = 0; i < this.audio_events.length; i++) {
  639. fn = this.audio_events[i];
  640. // capitalize first letter of event (only cosmetic :p )
  641. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  642. this.audio.addEventListener(
  643. this.audio_events[i],
  644. this[fn].bind(this),
  645. true);
  646. }
  647. // init btns events
  648. this.$previous.on('click', this.playPrevious.bind(this));
  649. this.$playpause.on('click', this.togglePlayPause.bind(this));
  650. this.$next.on('click', this.playNext.bind(this));
  651. },
  652. openDocument(node, caller, historic_index){
  653. // console.log('AudioPlayer openDocument', node);
  654. if(typeof node == 'undefined'
  655. || typeof node.nid == 'undefined'
  656. || typeof node.audio_url == 'undfined'){
  657. console.warn('AudioPlayer openDocument() node is malformed', node);
  658. return false;
  659. }
  660. // if we don't come from history popstate
  661. if(typeof caller == 'undefined' || caller != 'popstate'){
  662. this.historic.push(node);
  663. this.currentHistoricIndex = this.historic.length-1;
  664. // this.shuffle_mode = shuffle_mode || false;
  665. // add the document opening to history to be able to share and play audio from any where
  666. if(caller != "history_first_load"){
  667. state = {
  668. audio:true,
  669. node:node,
  670. historic_index : this.currentHistoricIndex,
  671. };
  672. history.pushState(state, null, node.document_url);
  673. }
  674. }else{
  675. // if the call commes from popstate, we update the current position of index
  676. this.currentHistoricIndex = historic_index;
  677. }
  678. this.emmit('audio-open-document', {caller:caller});
  679. // piwik
  680. debugger;
  681. if(typeof _paq !== 'undefined'){
  682. // page tracking
  683. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  684. // _paq.push(['setCustomUrl', state.url]);
  685. // _paq.push(['setDocumentTitle', data.title]);
  686. // _paq.push(['trackPageView']);
  687. // js event
  688. // trackEvent(category, action, [name], [value])
  689. if(typeof node.title != 'undefined'){
  690. _paq.push(['trackEvent', 'Audio', 'play', node.title]);
  691. }
  692. }
  693. this.launch();
  694. },
  695. launch(){
  696. this.clearTimeOutToHide();
  697. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  698. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  699. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  700. try {
  701. _$corpus_canvas.trigger({
  702. 'type':'audio-node-opened',
  703. 'nid':this.historic[this.currentHistoricIndex].nid
  704. });
  705. } catch (e) {
  706. console.info('AudioPlayer : _$corpus_canvas does not exists');
  707. }
  708. this.showHidePreviousBtn();
  709. this.showHideNextBtn();
  710. this.show();
  711. },
  712. // audio functions
  713. setSRC(url){
  714. console.log('AudioPlayer setSRC : url', url);
  715. this.audio.src = url;
  716. },
  717. onLoadedmetadata(){
  718. var rem = parseInt(this.audio.duration, 10),
  719. mins = Math.floor(rem/60,10),
  720. secs = rem - mins*60;
  721. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  722. this.updateLoadingBar();
  723. },
  724. updateLoadingBar(){
  725. // if (track.readyState === 4){
  726. if(track.buffered.length>0){
  727. this.$loader.css({
  728. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  729. });
  730. if( this.audio.buffered.end(0) < this.audio.duration ){
  731. // loop through this function until file is fully loaded
  732. var that = this;
  733. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  734. }else{
  735. //console.log('Audio fully loaded');
  736. }
  737. }
  738. },
  739. onCanplay(){
  740. this.play();
  741. },
  742. onError(){
  743. console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message);
  744. },
  745. play(){
  746. this.clearTimeOutToHide();
  747. this.audio.play();
  748. },
  749. playPrevious(){
  750. if(this.currentHistoricIndex > 0){
  751. this.currentHistoricIndex -= 1;
  752. this.launch();
  753. }
  754. },
  755. playNext(){
  756. if(this.currentHistoricIndex < this.historic.length-1){
  757. this.currentHistoricIndex += 1;
  758. this.launch();
  759. }else{
  760. this.emmit('audio-play-next');
  761. }
  762. },
  763. togglePlayPause(e){
  764. if(this.audio.paused){
  765. this.audio.play();
  766. }else{
  767. this.audio.pause();
  768. }
  769. },
  770. stop(){
  771. // console.log('AudioPlayer stop()');
  772. // debugger;
  773. this.audio.pause();
  774. // don't close player if article or transcript is open
  775. if(!(_$body.is('.path-node-'+this.historic[this.currentHistoricIndex].nid)
  776. && (_$body.is('.view-mode-article') || _$body.is('.view-mode-transcript')))){
  777. this.timeOutToHide();
  778. }
  779. },
  780. // audio events
  781. onPlaying(){
  782. this.$btns.addClass('is-playing');
  783. this.emmit('audio-play');
  784. },
  785. onPause(){
  786. this.$btns.removeClass('is-playing');
  787. this.emmit('audio-pause');
  788. },
  789. onTimeupdate(){
  790. // move cursor
  791. this.$cursor.css({
  792. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  793. });
  794. // update time text display
  795. var rem = parseInt(this.audio.currentTime, 10),
  796. mins = Math.floor(rem/60,10),
  797. secs = rem - mins*60;
  798. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  799. },
  800. onEnded(){
  801. console.log('AudioPlayer onEnded()');
  802. this.emmit('audio-ended');
  803. this.stop();
  804. },
  805. // cartel functions
  806. loadNode(nid){
  807. this.$cartel.addClass('loading');
  808. var vm = 'player_cartel';
  809. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  810. var path = window.location.origin + Drupal.url(ajax_path);
  811. $.getJSON(path, {})
  812. .done(this.onNodeLoaded.bind(this))
  813. .fail(this.onNodeLoadFail.bind(this));
  814. },
  815. onNodeLoaded(data){
  816. // console.log('AudioPlayer node loaded');
  817. this.$cartel.html(data.rendered).removeClass('loading');
  818. _$body.trigger({'type':'new-audio-cartel-loaded'});
  819. initAjaxLinks();
  820. if(this.auto_open_article){
  821. this.$cartel.find('a.link-article').trigger('click');
  822. this.auto_open_article = false;
  823. }
  824. },
  825. onNodeLoadFail(jqxhr, textStatus, error){
  826. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  827. this.$cartel.removeClass('loading').html('');
  828. },
  829. setAutoOpenArticle(art){
  830. this.auto_open_article = art;
  831. return this;
  832. },
  833. // global
  834. show(){
  835. this.$container.addClass('visible');
  836. },
  837. showHidePreviousBtn(){
  838. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  839. this.$previous.addClass('is-active');
  840. }else{
  841. this.$previous.removeClass('is-active');
  842. }
  843. },
  844. showHideNextBtn(){
  845. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  846. this.$next.addClass('is-active');
  847. }else{
  848. this.$next.removeClass('is-active');
  849. }
  850. },
  851. timeOutToHide(){
  852. // console.log('AudioPlayer timeOutToHide()');
  853. this.clearTimeOutToHide();
  854. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  855. },
  856. clearTimeOutToHide(){
  857. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  858. if(this.hideTimer){
  859. clearTimeout(this.hideTimer);
  860. this.hideTimer = false;
  861. }
  862. },
  863. hide(){
  864. // console.log('AudioPlayer hide()');
  865. this.$container.removeClass('visible');
  866. // trigger highlighted node remove on corpus map
  867. try {
  868. _$corpus_canvas.trigger('audio-node-closed');
  869. } catch (e) {
  870. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  871. }
  872. },
  873. // object events
  874. on(event_name, handler){
  875. if(typeof this.event_handlers[event_name] == 'undefined'){
  876. console.warn('AudioPlayer : event '+event_name+' does not exists');
  877. }
  878. this.event_handlers[event_name].push(handler);
  879. return this;
  880. },
  881. emmit(event_name, args){
  882. // console.log('AudioPlayer emmit() event_name', event_name);
  883. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  884. var handler;
  885. var args = args || {};
  886. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  887. handler = this.event_handlers[event_name][i];
  888. // console.log('AudioPlayer emmit() loop handler', handler);
  889. setTimeout(function(){
  890. // console.log('AudioPlayer emmit() timeout handler', handler);
  891. handler(args);
  892. }, 0);
  893. }
  894. return this;
  895. },
  896. }
  897. // ___ _ ___ _
  898. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  899. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  900. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  901. // |__/
  902. function RandomPlayer(playlist){
  903. this.active = false;
  904. this.playlist = playlist;
  905. this.$btn = $('<a>').html('Shuffle').addClass('random-player-btn');
  906. this.init();
  907. };
  908. RandomPlayer.prototype = {
  909. init(){
  910. // this.shuffle();
  911. $('<div>')
  912. .addClass('block random-player')
  913. .append(this.$btn)
  914. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  915. .prependTo('.region-footer-right');
  916. // events
  917. this.$btn.on('click', this.toggleActive.bind(this));
  918. // attach an event on AudioPlayer
  919. _audioPlayer
  920. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  921. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  922. .on('stop-shuffle', this.stop.bind(this));
  923. _$corpus_canvas
  924. .on('update-random-playlist', this.updatePlaylist.bind(this));
  925. },
  926. updatePlaylist(e){
  927. // debugger;
  928. this.playlist = e.playlist;
  929. this.shuffle();
  930. },
  931. shuffle(){
  932. var tempPLaylist = [];
  933. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  934. tempPLaylist.push(this.playlist[i]);
  935. }
  936. this.shuffledPlaylist = [];
  937. while(tempPLaylist.length > 0){
  938. var r = Math.floor(Math.random() * tempPLaylist.length);
  939. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  940. }
  941. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  942. },
  943. toggleActive(e){
  944. if (this.active) {
  945. this.stop();
  946. }else{
  947. this.start();
  948. }
  949. },
  950. start(){
  951. this.active = _audioPlayer.shuffle_is_active = true;
  952. this.$btn.addClass('is-active');
  953. this.shuffle();
  954. this.next();
  955. },
  956. stop(){
  957. this.active = _audioPlayer.shuffle_is_active = false;
  958. this.$btn.removeClass('is-active');
  959. // stop audio player
  960. // _audioPlayer.stop();
  961. },
  962. next(){
  963. if(this.active && this.shuffledPlaylist.length > 0)
  964. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0]);
  965. },
  966. onAudioPlayNext(){
  967. //console.log('RandomPlayer : onAudioPlayNext()');
  968. this.next();
  969. },
  970. onAudioPlayerEnded(){
  971. //console.log('RandomPlayer : onAudioPlayerEnded()');
  972. this.next();
  973. }
  974. };
  975. // ___ ___ _
  976. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  977. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  978. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  979. // |_| |__/
  980. function CompoPlayer(){
  981. this.active = false;
  982. this.playing = false;
  983. this.paused = false;
  984. this.playlist = [];
  985. this.current_index = 0;
  986. this.$composer = null;
  987. this.$compo = null;
  988. this.$controls = null;
  989. this.init();
  990. };
  991. CompoPlayer.prototype = {
  992. init(){
  993. // console.log('CompoPlayer init()');
  994. // attach an event on AudioPlayer
  995. _audioPlayer
  996. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  997. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  998. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  999. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  1000. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  1001. // this.newCompo();
  1002. },
  1003. newCompo(){
  1004. //console.log('CompoPlayer newCompo()');
  1005. // this.$compo = $('.composition_ui .composer .composition');
  1006. this.initControls();
  1007. },
  1008. initControls(){
  1009. //console.log('CompoPlayer initControls()');
  1010. this.$composer = $('.composition_ui .composer');
  1011. this.$compo = $('.composition_ui .composer .composition');
  1012. this.$controls = $('.composition_ui .composer .compo-player-controls');
  1013. if(!this.$controls.is('.ready') && this.$compo){
  1014. this.$previous = $('<div>').addClass('previous')
  1015. .on('click', this.prev.bind(this))
  1016. .appendTo(this.$controls);
  1017. this.$playpause = $('<div>').addClass('play-pause')
  1018. .on('click', this.togglePlayPause.bind(this))
  1019. .appendTo(this.$controls);
  1020. this.$next = $('<div>').addClass('next')
  1021. .on('click', this.next.bind(this))
  1022. .appendTo(this.$controls);
  1023. this.$controls.addClass('ready');
  1024. this.refresh();
  1025. this.active = true;
  1026. // this.newCompo();
  1027. }
  1028. },
  1029. refresh(){
  1030. // console.log('CompoPlayer refresh(), this', this);
  1031. this.stop();
  1032. // load new playlist
  1033. this.playlist = [];
  1034. var that = this;
  1035. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  1036. var $link = $('a.audio-link',this);
  1037. that.playlist.push({
  1038. item:$(this),
  1039. audio_url:$link.attr("audio_url"),
  1040. nid:$link.attr("nid"),
  1041. });
  1042. });
  1043. this.showHideControls();
  1044. },
  1045. togglePlayPause(){
  1046. // console.log('CompoPlayer togglePlayPause');
  1047. if (this.playing && !this.paused) {
  1048. this.pause();
  1049. }else{
  1050. if(this.playing && this.paused){
  1051. this.play();
  1052. }else{
  1053. this.start();
  1054. }
  1055. }
  1056. },
  1057. start(){
  1058. //console.log('start');
  1059. // console.log('CompoPlayer start()');
  1060. this.playing = true;
  1061. this.play();
  1062. },
  1063. play(){
  1064. // console.log('play');
  1065. if(this.paused){
  1066. this.paused = false;
  1067. _audioPlayer.play();
  1068. }else{
  1069. _audioPlayer.openDocument(this.playlist[this.current_index], this);
  1070. }
  1071. this.setActiveItem().showHideControls();
  1072. },
  1073. pause(){
  1074. //console.log('pause');
  1075. this.paused = true;
  1076. this.showHideControls();
  1077. _audioPlayer.stop();
  1078. },
  1079. next(){
  1080. // console.log('CompoPlayer next()');
  1081. if(this.playing){
  1082. this.current_index += 1;
  1083. if(this.current_index < this.playlist.length){
  1084. this.play();
  1085. }else{
  1086. this.stop();
  1087. }
  1088. }
  1089. },
  1090. prev(){
  1091. // console.log('CompoPlayer prev()');
  1092. if(this.playing){
  1093. this.current_index -= 1;
  1094. if(this.current_index >= 0){
  1095. this.play();
  1096. }else{
  1097. this.stop();
  1098. }
  1099. }
  1100. },
  1101. stop(){
  1102. if(this.playing){
  1103. _audioPlayer.stop();
  1104. }
  1105. this.reset();
  1106. },
  1107. reset(){
  1108. this.playing = false;
  1109. this.paused = false;
  1110. this.resetIndex();
  1111. },
  1112. resetIndex(){
  1113. this.current_index = 0;
  1114. this.showHideControls().resetActiveItems();
  1115. },
  1116. setActiveItem(){
  1117. this.resetActiveItems();
  1118. if(this.playing && this.current_index >= 0){
  1119. this.playlist[this.current_index].item.addClass('is-active');
  1120. }
  1121. // this call shoud not be here
  1122. this.showHideControls();
  1123. return this;
  1124. },
  1125. resetActiveItems(){
  1126. for (var n = 0; n < this.playlist.length; n++) {
  1127. // console.log('node',node);
  1128. this.playlist[n].item.removeClass('is-active');
  1129. }
  1130. return this;
  1131. },
  1132. showHideControls(){
  1133. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  1134. // global playing
  1135. if(this.$controls){
  1136. if(this.playing && !this.paused){
  1137. this.$controls.addClass('is-playing');
  1138. }else{
  1139. this.$controls.removeClass('is-playing');
  1140. }
  1141. }
  1142. // playpause
  1143. if(this.$playpause){
  1144. if(this.playlist.length > 0){
  1145. this.$playpause.addClass('is-active');
  1146. }else{
  1147. this.$playpause.removeClass('is-active');
  1148. }
  1149. }
  1150. // next
  1151. if(this.$next){
  1152. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  1153. this.$next.addClass('is-active');
  1154. }else{
  1155. this.$next.removeClass('is-active');
  1156. }
  1157. }
  1158. // previous
  1159. if(this.$previous){
  1160. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  1161. this.$previous.addClass('is-active');
  1162. }else{
  1163. this.$previous.removeClass('is-active');
  1164. }
  1165. }
  1166. return this;
  1167. },
  1168. deactivate(){
  1169. this.stop();
  1170. this.active = false;
  1171. },
  1172. // _audioPlayer events
  1173. onAudioOpenDocument(args){
  1174. if(args.caller !== this){
  1175. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  1176. this.reset();
  1177. }
  1178. // else{
  1179. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  1180. // }
  1181. },
  1182. onAudioPlayerPlay(){
  1183. if(this.playing && this.paused){
  1184. this.paused = false;
  1185. this.showHideControls();
  1186. }
  1187. },
  1188. onAudioPlayerPause(){
  1189. if(this.playing && !this.paused){
  1190. this.paused = true;
  1191. this.showHideControls();
  1192. }
  1193. },
  1194. onAudioPlayerEnded(){
  1195. this.next();
  1196. },
  1197. // onAudioPlayNext(){
  1198. // this.next();
  1199. // }
  1200. };
  1201. // ___ _ ___
  1202. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  1203. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  1204. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  1205. // |___/
  1206. function backToFrontPage(pop_state){
  1207. console.log('backToFrontPage', pop_state);
  1208. closeAllModals();
  1209. // assume we are going back to front page
  1210. $('body').removeClass().addClass('path-frontpage');
  1211. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  1212. // close entrees
  1213. _$corpus_canvas.trigger({'type':'close-all-entree'});
  1214. if(typeof pop_state == "undefined" || !pop_state){
  1215. console.log('backToFrontPage push state');
  1216. history.pushState({home:true}, null, drupalSettings.path.baseUrl+drupalSettings.path.currentLanguage);
  1217. }
  1218. }
  1219. function initHome(){
  1220. addCloseModalBtnToCols();
  1221. // console.log('theme : initHome');
  1222. // console.log('theme : initProductions');
  1223. var $grid = $('.grid',_$row).masonry({
  1224. itemSelector:'.col',
  1225. columnWidth:'.col-2',
  1226. horizontalOrder: true,
  1227. containerStyle: null,
  1228. // disable initial layout
  1229. // initLayout: false,
  1230. });
  1231. // bind event
  1232. // $grid.masonry( 'on', 'layoutComplete', function() {
  1233. // console.log('layout is complete');
  1234. // });
  1235. // layout Masonry after each image loads
  1236. $grid.imagesLoaded().progress( function() {
  1237. $grid.masonry('layout');
  1238. });
  1239. $grid.imagesLoaded(function(){
  1240. $grid.masonry('layout');
  1241. });
  1242. }
  1243. // ___ _ _ _
  1244. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  1245. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  1246. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  1247. function initProductions(){
  1248. // console.log('theme : initProductions');
  1249. var $grid = $('.grid',_$row).masonry({
  1250. itemSelector:'.col',
  1251. columnWidth:'.col-2',
  1252. horizontalOrder: true,
  1253. containerStyle: null,
  1254. // disable initial layout
  1255. // initLayout: false,
  1256. });
  1257. // bind event
  1258. // $grid.masonry( 'on', 'layoutComplete', function() {
  1259. // console.log('layout is complete');
  1260. // });
  1261. // layout Masonry after each image loads
  1262. $grid.imagesLoaded().progress( function() {
  1263. $grid.masonry('layout');
  1264. });
  1265. $grid.imagesLoaded(function(){
  1266. $grid.masonry('layout');
  1267. });
  1268. };
  1269. // ___ _ _ _
  1270. // | __|_ _ _ _ ___ __ _(_)__| |_ _ _ ___ _ __ ___ _ _| |_
  1271. // | _|| ' \| '_/ -_) _` | (_-< _| '_/ -_) ' \/ -_) ' \ _|
  1272. // |___|_||_|_| \___\__, |_/__/\__|_| \___|_|_|_\___|_||_\__|
  1273. // |___/
  1274. function initEnregistrementTranscript(){
  1275. console.log('initEnregistrementTranscript');
  1276. var $node = _$row.find('article.node--type-enregistrement.node--view-mode-transcript');
  1277. var $nav = $('<nav>').prependTo($node);
  1278. $node.find('.field--name-field-transcript-vo').addClass('visible').find('.field__label')
  1279. .clone().appendTo($nav).addClass('is-active')
  1280. .attr('field_target', '.field--name-field-transcript-vo');
  1281. $node.find('.field--name-field-transcript-trad').find('.field__label')
  1282. .clone().appendTo($nav)
  1283. .attr('field_target', '.field--name-field-transcript-trad');
  1284. $nav.find('.field__label').on('click', function(){
  1285. var $this = $(this).addClass('is-active');
  1286. $this.siblings('.is-active').removeClass('is-active');
  1287. $this.parents('article.node').find('.field.visible').removeClass('visible');
  1288. $this.parents('article.node').find($this.attr('field_target')).addClass('visible');
  1289. });
  1290. };
  1291. // __ __ _ _
  1292. // | \/ |___ __| |__ _| |___
  1293. // | |\/| / _ \/ _` / _` | (_-<
  1294. // |_| |_\___/\__,_\__,_|_/__/
  1295. function closeAllModals(){
  1296. //console.log('theme : closeAllModals');
  1297. // TODO: animate the remove
  1298. _$row.html('');
  1299. _$ajaxLinks.removeClass('is-active');
  1300. _$body.trigger({'type':'all-modal-closed'});
  1301. };
  1302. // _ _ _
  1303. // | || |___| |_ __ ___ _ _ ___
  1304. // | __ / -_) | '_ \/ -_) '_(_-<
  1305. // |_||_\___|_| .__/\___|_| /__/
  1306. // |_|
  1307. // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
  1308. // function getCookie(name) {
  1309. // var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  1310. // return v ? v[2] : null;
  1311. // }
  1312. // function setCookie(name, value, days) {
  1313. // var d = new Date;
  1314. // d.setTime(d.getTime() + 24*60*60*1000*days);
  1315. // document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
  1316. // }
  1317. // function deleteCookie(name) { setCookie(name, '', -1); }
  1318. init();
  1319. } // end EdlpTheme()
  1320. $(document).ready(function($) {
  1321. if(drupalSettings.path.isFront){
  1322. var edlptheme = new EdlpTheme();
  1323. }
  1324. });
  1325. })(jQuery, Drupal, drupalSettings);