main.js 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. this.$container_parent = $('header[role="banner"] .region-header');
  652. // append ui to document
  653. this.$container.appendTo(this.$container_parent);
  654. // record timeline width
  655. this.timeline_w = parseInt(this.$timeline.width());
  656. // init audio events
  657. var fn = '';
  658. for (var i = 0; i < this.audio_events.length; i++) {
  659. fn = this.audio_events[i];
  660. // capitalize first letter of event (only cosmetic :p )
  661. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  662. this.audio.addEventListener(
  663. this.audio_events[i],
  664. this[fn].bind(this),
  665. true);
  666. }
  667. // init btns events
  668. this.$previous.on('click', this.playPrevious.bind(this));
  669. this.$playpause.on('click', this.togglePlayPause.bind(this));
  670. this.$next.on('click', this.playNext.bind(this));
  671. },
  672. openDocument(node, caller, historic_index){
  673. // console.log('AudioPlayer openDocument', node);
  674. if(typeof node == 'undefined'
  675. || typeof node.nid == 'undefined'
  676. || typeof node.audio_url == 'undfined'
  677. || typeof node.document_url == 'undfined'){
  678. console.warn('AudioPlayer openDocument() node is malformed', node);
  679. return false;
  680. }
  681. // if we don't come from history popstate
  682. if(typeof caller == 'undefined' || caller != 'popstate'){
  683. this.historic.push(node);
  684. this.currentHistoricIndex = this.historic.length-1;
  685. // this.shuffle_mode = shuffle_mode || false;
  686. // add the document opening to history to be able to share and play audio from any where
  687. if(caller != "history_first_load"){
  688. var state = {
  689. audio:true,
  690. node:{
  691. nid:node.nid,
  692. audio_url:node.audio_url,
  693. document_url:node.document_url,
  694. title:node.title || null,
  695. },
  696. historic_index : this.currentHistoricIndex,
  697. };
  698. history.pushState(state, null, node.document_url);
  699. }
  700. }else{
  701. // if the call commes from popstate, we update the current position of index
  702. this.currentHistoricIndex = historic_index;
  703. }
  704. this.emmit('audio-open-document', {caller:caller});
  705. // piwik
  706. // debugger;
  707. if(typeof _paq !== 'undefined'){
  708. // page tracking
  709. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  710. // _paq.push(['setCustomUrl', state.url]);
  711. // _paq.push(['setDocumentTitle', data.title]);
  712. // _paq.push(['trackPageView']);
  713. // js event
  714. // trackEvent(category, action, [name], [value])
  715. if(typeof node.title != 'undefined'){
  716. _paq.push(['trackEvent', 'Audio', 'play', node.title]);
  717. }
  718. }
  719. this.launch();
  720. },
  721. launch(){
  722. this.clearTimeOutToHide();
  723. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  724. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  725. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  726. try {
  727. _$corpus_canvas.trigger({
  728. 'type':'audio-node-opened',
  729. 'nid':this.historic[this.currentHistoricIndex].nid
  730. });
  731. } catch (e) {
  732. console.info('AudioPlayer : _$corpus_canvas does not exists');
  733. }
  734. this.showHidePreviousBtn();
  735. this.showHideNextBtn();
  736. this.show();
  737. },
  738. // audio functions
  739. setSRC(url){
  740. console.log('AudioPlayer setSRC : url', url);
  741. this.audio.src = url;
  742. },
  743. onLoadedmetadata(){
  744. var rem = parseInt(this.audio.duration, 10),
  745. mins = Math.floor(rem/60,10),
  746. secs = rem - mins*60;
  747. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  748. this.updateLoadingBar();
  749. },
  750. updateLoadingBar(){
  751. // if (this.audio.readyState === 4){
  752. if(this.audio.buffered.length>0){
  753. this.$loader.css({
  754. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  755. });
  756. if( this.audio.buffered.end(0) < this.audio.duration ){
  757. // loop through this function until file is fully loaded
  758. var that = this;
  759. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  760. }else{
  761. //console.log('Audio fully loaded');
  762. }
  763. }
  764. },
  765. onCanplay(){
  766. this.play();
  767. },
  768. onError(){
  769. console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message);
  770. },
  771. play(){
  772. this.clearTimeOutToHide();
  773. this.audio.play();
  774. },
  775. playPrevious(){
  776. if(this.currentHistoricIndex > 0){
  777. this.currentHistoricIndex -= 1;
  778. this.launch();
  779. }
  780. },
  781. playNext(){
  782. if(this.currentHistoricIndex < this.historic.length-1){
  783. this.currentHistoricIndex += 1;
  784. this.launch();
  785. }else{
  786. this.emmit('audio-play-next');
  787. }
  788. },
  789. togglePlayPause(e){
  790. if(this.audio.paused){
  791. this.audio.play();
  792. }else{
  793. this.audio.pause();
  794. }
  795. },
  796. stop(){
  797. // console.log('AudioPlayer stop()');
  798. // debugger;
  799. this.audio.pause();
  800. // don't close player if article or transcript is open
  801. if(!(_$body.is('.path-node-'+this.historic[this.currentHistoricIndex].nid)
  802. && (_$body.is('.view-mode-article') || _$body.is('.view-mode-transcript')))){
  803. this.timeOutToHide();
  804. }
  805. },
  806. // audio events
  807. onPlaying(){
  808. this.$btns.addClass('is-playing');
  809. this.emmit('audio-play');
  810. },
  811. onPause(){
  812. this.$btns.removeClass('is-playing');
  813. this.emmit('audio-pause');
  814. },
  815. onTimeupdate(){
  816. // move cursor
  817. this.$cursor.css({
  818. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  819. });
  820. // update time text display
  821. var rem = parseInt(this.audio.currentTime, 10),
  822. mins = Math.floor(rem/60,10),
  823. secs = rem - mins*60;
  824. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  825. },
  826. onEnded(){
  827. console.log('AudioPlayer onEnded()');
  828. this.emmit('audio-ended');
  829. this.stop();
  830. },
  831. // cartel functions
  832. loadNode(nid){
  833. this.$cartel.addClass('loading');
  834. var vm = 'player_cartel';
  835. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  836. var path = window.location.origin + Drupal.url(ajax_path);
  837. $.getJSON(path, {})
  838. .done(this.onNodeLoaded.bind(this))
  839. .fail(this.onNodeLoadFail.bind(this));
  840. },
  841. onNodeLoaded(data){
  842. // console.log('AudioPlayer node loaded');
  843. this.$cartel.html(data.rendered).removeClass('loading');
  844. _$body.trigger({'type':'new-audio-cartel-loaded'});
  845. initAjaxLinks();
  846. if(this.auto_open_article){
  847. this.$cartel.find('a.link-article').trigger('click');
  848. this.auto_open_article = false;
  849. }
  850. },
  851. onNodeLoadFail(jqxhr, textStatus, error){
  852. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  853. this.$cartel.removeClass('loading').html('');
  854. },
  855. setAutoOpenArticle(art){
  856. this.auto_open_article = art;
  857. return this;
  858. },
  859. // global
  860. show(){
  861. this.$container_parent.addClass('audio-player-visible');
  862. this.$container.addClass('visible');
  863. },
  864. showHidePreviousBtn(){
  865. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  866. this.$previous.addClass('is-active');
  867. }else{
  868. this.$previous.removeClass('is-active');
  869. }
  870. },
  871. showHideNextBtn(){
  872. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  873. this.$next.addClass('is-active');
  874. }else{
  875. this.$next.removeClass('is-active');
  876. }
  877. },
  878. timeOutToHide(){
  879. // console.log('AudioPlayer timeOutToHide()');
  880. this.clearTimeOutToHide();
  881. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  882. },
  883. clearTimeOutToHide(){
  884. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  885. if(this.hideTimer){
  886. clearTimeout(this.hideTimer);
  887. this.hideTimer = false;
  888. }
  889. },
  890. hide(){
  891. // console.log('AudioPlayer hide()');
  892. this.$container_parent.removeClass('audio-player-visible');
  893. this.$container.removeClass('visible');
  894. // trigger highlighted node remove on corpus map
  895. try {
  896. _$corpus_canvas.trigger('audio-node-closed');
  897. } catch (e) {
  898. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  899. }
  900. },
  901. // object events
  902. on(event_name, handler){
  903. if(typeof this.event_handlers[event_name] == 'undefined'){
  904. console.warn('AudioPlayer : event '+event_name+' does not exists');
  905. }
  906. this.event_handlers[event_name].push(handler);
  907. return this;
  908. },
  909. emmit(event_name, args){
  910. // console.log('AudioPlayer emmit() event_name', event_name);
  911. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  912. var handler;
  913. var args = args || {};
  914. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  915. handler = this.event_handlers[event_name][i];
  916. // console.log('AudioPlayer emmit() loop handler', handler);
  917. setTimeout(function(){
  918. // console.log('AudioPlayer emmit() timeout handler', handler);
  919. handler(args);
  920. }, 0);
  921. }
  922. return this;
  923. },
  924. }
  925. // ___ _ ___ _
  926. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  927. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  928. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  929. // |__/
  930. function RandomPlayer(playlist){
  931. this.active = false;
  932. this.playlist = playlist;
  933. this.$btn = $('<a>').html('Shuffle').addClass('random-player-btn');
  934. this.init();
  935. };
  936. RandomPlayer.prototype = {
  937. init(){
  938. // this.shuffle();
  939. $('<div>')
  940. .addClass('block random-player')
  941. .append(this.$btn)
  942. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  943. .prependTo('.region-footer-right');
  944. // events
  945. this.$btn.on('click', this.toggleActive.bind(this));
  946. // attach an event on AudioPlayer
  947. _audioPlayer
  948. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  949. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  950. .on('stop-shuffle', this.stop.bind(this));
  951. _$corpus_canvas
  952. .on('update-random-playlist', this.updatePlaylist.bind(this));
  953. },
  954. updatePlaylist(e){
  955. // debugger;
  956. this.playlist = e.playlist;
  957. this.shuffle();
  958. },
  959. shuffle(){
  960. var tempPLaylist = [];
  961. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  962. tempPLaylist.push(this.playlist[i]);
  963. }
  964. this.shuffledPlaylist = [];
  965. while(tempPLaylist.length > 0){
  966. var r = Math.floor(Math.random() * tempPLaylist.length);
  967. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  968. }
  969. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  970. },
  971. toggleActive(e){
  972. if (this.active) {
  973. this.stop();
  974. }else{
  975. this.start();
  976. }
  977. },
  978. start(){
  979. this.active = _audioPlayer.shuffle_is_active = true;
  980. this.$btn.addClass('is-active');
  981. this.shuffle();
  982. this.next();
  983. // piwik
  984. if(typeof _paq !== 'undefined'){
  985. // trackEvent(category, action, [name], [value])
  986. _paq.push(['trackEvent', 'RandomPlayer', 'start']);
  987. }
  988. },
  989. stop(){
  990. this.active = _audioPlayer.shuffle_is_active = false;
  991. this.$btn.removeClass('is-active');
  992. // stop audio player
  993. // _audioPlayer.stop();
  994. if(typeof _paq !== 'undefined'){
  995. // trackEvent(category, action, [name], [value])
  996. _paq.push(['trackEvent', 'RandomPlayer', 'stop']);
  997. }
  998. },
  999. next(){
  1000. if(this.active && this.shuffledPlaylist.length > 0)
  1001. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0]);
  1002. },
  1003. onAudioPlayNext(){
  1004. //console.log('RandomPlayer : onAudioPlayNext()');
  1005. this.next();
  1006. },
  1007. onAudioPlayerEnded(){
  1008. //console.log('RandomPlayer : onAudioPlayerEnded()');
  1009. this.next();
  1010. }
  1011. };
  1012. // ___ ___ _
  1013. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  1014. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  1015. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  1016. // |_| |__/
  1017. function CompoPlayer(){
  1018. this.active = false;
  1019. this.playing = false;
  1020. this.paused = false;
  1021. this.playlist = [];
  1022. this.current_index = 0;
  1023. this.$composer = null;
  1024. this.$compo = null;
  1025. this.$controls = null;
  1026. this.init();
  1027. };
  1028. CompoPlayer.prototype = {
  1029. init(){
  1030. // console.log('CompoPlayer init()');
  1031. // attach an event on AudioPlayer
  1032. _audioPlayer
  1033. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  1034. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  1035. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  1036. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  1037. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  1038. // this.newCompo();
  1039. },
  1040. newCompo(){
  1041. //console.log('CompoPlayer newCompo()');
  1042. // this.$compo = $('.composition_ui .composer .composition');
  1043. this.initControls();
  1044. },
  1045. initControls(){
  1046. //console.log('CompoPlayer initControls()');
  1047. this.$composer = $('.composition_ui .composer');
  1048. this.$compo = $('.composition_ui .composer .composition');
  1049. this.$controls = $('.composition_ui .composer .compo-player-controls');
  1050. if(!this.$controls.is('.ready') && this.$compo){
  1051. this.$previous = $('<div>').addClass('previous')
  1052. .on('click', this.prev.bind(this))
  1053. .appendTo(this.$controls);
  1054. this.$playpause = $('<div>').addClass('play-pause')
  1055. .on('click', this.togglePlayPause.bind(this))
  1056. .appendTo(this.$controls);
  1057. this.$next = $('<div>').addClass('next')
  1058. .on('click', this.next.bind(this))
  1059. .appendTo(this.$controls);
  1060. this.$controls.addClass('ready');
  1061. this.refresh();
  1062. this.active = true;
  1063. // this.newCompo();
  1064. }
  1065. },
  1066. refresh(){
  1067. // console.log('CompoPlayer refresh(), this', this);
  1068. this.stop();
  1069. // load new playlist
  1070. this.playlist = [];
  1071. var that = this;
  1072. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  1073. var $link = $('a.audio-link',this);
  1074. that.playlist.push({
  1075. item:$(this),
  1076. audio_url:$link.attr("audio_url"),
  1077. nid:$link.attr("nid"),
  1078. });
  1079. });
  1080. this.showHideControls();
  1081. },
  1082. togglePlayPause(){
  1083. // console.log('CompoPlayer togglePlayPause');
  1084. if (this.playing && !this.paused) {
  1085. this.pause();
  1086. }else{
  1087. if(this.playing && this.paused){
  1088. this.play();
  1089. }else{
  1090. this.start();
  1091. }
  1092. }
  1093. },
  1094. start(){
  1095. //console.log('start');
  1096. // console.log('CompoPlayer start()');
  1097. this.playing = true;
  1098. this.play();
  1099. },
  1100. play(){
  1101. // console.log('play');
  1102. if(this.paused){
  1103. this.paused = false;
  1104. _audioPlayer.play();
  1105. }else{
  1106. _audioPlayer.openDocument(this.playlist[this.current_index], this);
  1107. }
  1108. this.setActiveItem().showHideControls();
  1109. },
  1110. pause(){
  1111. //console.log('pause');
  1112. this.paused = true;
  1113. this.showHideControls();
  1114. _audioPlayer.stop();
  1115. },
  1116. next(){
  1117. // console.log('CompoPlayer next()');
  1118. if(this.playing){
  1119. this.current_index += 1;
  1120. if(this.current_index < this.playlist.length){
  1121. this.play();
  1122. }else{
  1123. this.stop();
  1124. }
  1125. }
  1126. },
  1127. prev(){
  1128. // console.log('CompoPlayer prev()');
  1129. if(this.playing){
  1130. this.current_index -= 1;
  1131. if(this.current_index >= 0){
  1132. this.play();
  1133. }else{
  1134. this.stop();
  1135. }
  1136. }
  1137. },
  1138. stop(){
  1139. if(this.playing){
  1140. _audioPlayer.stop();
  1141. }
  1142. this.reset();
  1143. },
  1144. reset(){
  1145. this.playing = false;
  1146. this.paused = false;
  1147. this.resetIndex();
  1148. },
  1149. resetIndex(){
  1150. this.current_index = 0;
  1151. this.showHideControls().resetActiveItems();
  1152. },
  1153. setActiveItem(){
  1154. this.resetActiveItems();
  1155. if(this.playing && this.current_index >= 0){
  1156. this.playlist[this.current_index].item.addClass('is-active');
  1157. }
  1158. // this call shoud not be here
  1159. this.showHideControls();
  1160. return this;
  1161. },
  1162. resetActiveItems(){
  1163. for (var n = 0; n < this.playlist.length; n++) {
  1164. // console.log('node',node);
  1165. this.playlist[n].item.removeClass('is-active');
  1166. }
  1167. return this;
  1168. },
  1169. showHideControls(){
  1170. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  1171. // global playing
  1172. if(this.$controls){
  1173. if(this.playing && !this.paused){
  1174. this.$controls.addClass('is-playing');
  1175. }else{
  1176. this.$controls.removeClass('is-playing');
  1177. }
  1178. }
  1179. // playpause
  1180. if(this.$playpause){
  1181. if(this.playlist.length > 0){
  1182. this.$playpause.addClass('is-active');
  1183. }else{
  1184. this.$playpause.removeClass('is-active');
  1185. }
  1186. }
  1187. // next
  1188. if(this.$next){
  1189. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  1190. this.$next.addClass('is-active');
  1191. }else{
  1192. this.$next.removeClass('is-active');
  1193. }
  1194. }
  1195. // previous
  1196. if(this.$previous){
  1197. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  1198. this.$previous.addClass('is-active');
  1199. }else{
  1200. this.$previous.removeClass('is-active');
  1201. }
  1202. }
  1203. return this;
  1204. },
  1205. deactivate(){
  1206. this.stop();
  1207. this.active = false;
  1208. },
  1209. // _audioPlayer events
  1210. onAudioOpenDocument(args){
  1211. if(args.caller !== this){
  1212. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  1213. this.reset();
  1214. }
  1215. // else{
  1216. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  1217. // }
  1218. },
  1219. onAudioPlayerPlay(){
  1220. if(this.playing && this.paused){
  1221. this.paused = false;
  1222. this.showHideControls();
  1223. }
  1224. },
  1225. onAudioPlayerPause(){
  1226. if(this.playing && !this.paused){
  1227. this.paused = true;
  1228. this.showHideControls();
  1229. }
  1230. },
  1231. onAudioPlayerEnded(){
  1232. this.next();
  1233. },
  1234. // onAudioPlayNext(){
  1235. // this.next();
  1236. // }
  1237. };
  1238. // ___ _ ___
  1239. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  1240. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  1241. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  1242. // |___/
  1243. function backToFrontPage(pop_state){
  1244. console.log('backToFrontPage', pop_state);
  1245. closeAllModals();
  1246. // assume we are going back to front page
  1247. $('body').removeClass().addClass('path-frontpage');
  1248. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  1249. // close entrees
  1250. _$corpus_canvas.trigger({'type':'close-all-entree'});
  1251. if(typeof pop_state == "undefined" || !pop_state){
  1252. console.log('backToFrontPage push state');
  1253. history.pushState({home:true}, null, drupalSettings.path.baseUrl+drupalSettings.path.currentLanguage);
  1254. }
  1255. }
  1256. function initHome(){
  1257. addCloseModalBtnToCols();
  1258. // console.log('theme : initHome');
  1259. // console.log('theme : initProductions');
  1260. var $grid = $('.grid',_$row).masonry({
  1261. itemSelector:'.col',
  1262. columnWidth:'.col-2',
  1263. horizontalOrder: true,
  1264. containerStyle: null,
  1265. // disable initial layout
  1266. // initLayout: false,
  1267. });
  1268. // bind event
  1269. // $grid.masonry( 'on', 'layoutComplete', function() {
  1270. // console.log('layout is complete');
  1271. // });
  1272. // layout Masonry after each image loads
  1273. $grid.imagesLoaded().progress( function() {
  1274. $grid.masonry('layout');
  1275. });
  1276. $grid.imagesLoaded(function(){
  1277. $grid.masonry('layout');
  1278. });
  1279. }
  1280. // ___ _ _ _
  1281. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  1282. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  1283. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  1284. function initProductions(){
  1285. // console.log('theme : initProductions');
  1286. var $grid = $('.grid',_$row).masonry({
  1287. itemSelector:'.col',
  1288. columnWidth:'.col-2',
  1289. horizontalOrder: true,
  1290. containerStyle: null,
  1291. // disable initial layout
  1292. // initLayout: false,
  1293. });
  1294. // bind event
  1295. // $grid.masonry( 'on', 'layoutComplete', function() {
  1296. // console.log('layout is complete');
  1297. // });
  1298. // layout Masonry after each image loads
  1299. $grid.imagesLoaded().progress( function() {
  1300. $grid.masonry('layout');
  1301. });
  1302. $grid.imagesLoaded(function(){
  1303. $grid.masonry('layout');
  1304. });
  1305. };
  1306. // ___ _ _ _
  1307. // | __|_ _ _ _ ___ __ _(_)__| |_ _ _ ___ _ __ ___ _ _| |_
  1308. // | _|| ' \| '_/ -_) _` | (_-< _| '_/ -_) ' \/ -_) ' \ _|
  1309. // |___|_||_|_| \___\__, |_/__/\__|_| \___|_|_|_\___|_||_\__|
  1310. // |___/
  1311. function initEnregistrementTranscript(){
  1312. console.log('initEnregistrementTranscript');
  1313. var $node = _$row.find('article.node--type-enregistrement.node--view-mode-transcript');
  1314. var $nav = $('<nav>').prependTo($node);
  1315. $node.find('.field--name-field-transcript-vo').addClass('visible').find('.field__label')
  1316. .clone().appendTo($nav).addClass('is-active')
  1317. .attr('field_target', '.field--name-field-transcript-vo');
  1318. $node.find('.field--name-field-transcript-trad').find('.field__label')
  1319. .clone().appendTo($nav)
  1320. .attr('field_target', '.field--name-field-transcript-trad');
  1321. $nav.find('.field__label').on('click', function(){
  1322. var $this = $(this).addClass('is-active');
  1323. $this.siblings('.is-active').removeClass('is-active');
  1324. $this.parents('article.node').find('.field.visible').removeClass('visible');
  1325. $this.parents('article.node').find($this.attr('field_target')).addClass('visible');
  1326. });
  1327. };
  1328. // __ __ _ _
  1329. // | \/ |___ __| |__ _| |___
  1330. // | |\/| / _ \/ _` / _` | (_-<
  1331. // |_| |_\___/\__,_\__,_|_/__/
  1332. function closeAllModals(){
  1333. //console.log('theme : closeAllModals');
  1334. // TODO: animate the remove
  1335. _$row.html('');
  1336. _$ajaxLinks.removeClass('is-active');
  1337. _$body.trigger({'type':'all-modal-closed'});
  1338. };
  1339. // _ _ _
  1340. // | || |___| |_ __ ___ _ _ ___
  1341. // | __ / -_) | '_ \/ -_) '_(_-<
  1342. // |_||_\___|_| .__/\___|_| /__/
  1343. // |_|
  1344. // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
  1345. // function getCookie(name) {
  1346. // var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  1347. // return v ? v[2] : null;
  1348. // }
  1349. // function setCookie(name, value, days) {
  1350. // var d = new Date;
  1351. // d.setTime(d.getTime() + 24*60*60*1000*days);
  1352. // document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
  1353. // }
  1354. // function deleteCookie(name) { setCookie(name, '', -1); }
  1355. init();
  1356. } // end EdlpTheme()
  1357. $(document).ready(function($) {
  1358. if(drupalSettings.path.isFront){
  1359. var edlptheme = new EdlpTheme();
  1360. }
  1361. });
  1362. })(jQuery, Drupal, drupalSettings);