main.js 49 KB

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