main.js 62 KB

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