main.js 49 KB

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