main.js 55 KB

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