main.js 51 KB

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