main.js 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841
  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 druapl 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. if(this.auto_open_article){
  1033. this.$cartel.find('a.link-article').trigger('click');
  1034. this.auto_open_article = false;
  1035. }
  1036. },
  1037. onNodeLoadFail(jqxhr, textStatus, error){
  1038. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  1039. this.$cartel.removeClass('loading').html('');
  1040. },
  1041. setAutoOpenArticle(art){
  1042. this.auto_open_article = art;
  1043. return this;
  1044. },
  1045. // global
  1046. show(){
  1047. this.$container_parent.addClass('audio-player-visible');
  1048. this.$container.addClass('visible');
  1049. },
  1050. showHidePreviousBtn(){
  1051. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  1052. this.$previous.addClass('is-active');
  1053. }else{
  1054. this.$previous.removeClass('is-active');
  1055. }
  1056. },
  1057. showHideNextBtn(){
  1058. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  1059. this.$next.addClass('is-active');
  1060. }else{
  1061. this.$next.removeClass('is-active');
  1062. }
  1063. },
  1064. timeOutToHide(){
  1065. // console.log('AudioPlayer timeOutToHide()');
  1066. this.clearTimeOutToHide();
  1067. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  1068. },
  1069. clearTimeOutToHide(){
  1070. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  1071. if(this.hideTimer){
  1072. clearTimeout(this.hideTimer);
  1073. this.hideTimer = false;
  1074. }
  1075. },
  1076. hide(){
  1077. // console.log('AudioPlayer hide()');
  1078. this.$container_parent.removeClass('audio-player-visible');
  1079. this.$container.removeClass('visible');
  1080. // trigger highlighted node remove on corpus map
  1081. try {
  1082. _$corpus_canvas.trigger('audio-node-closed');
  1083. } catch (e) {
  1084. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  1085. }
  1086. },
  1087. deActivateRandom(){
  1088. this.shuffle_is_active = false;
  1089. this.showHideNextBtn();
  1090. },
  1091. // object events
  1092. on(event_name, handler){
  1093. if(typeof this.event_handlers[event_name] == 'undefined'){
  1094. console.warn('AudioPlayer : event '+event_name+' does not exists');
  1095. }
  1096. this.event_handlers[event_name].push(handler);
  1097. return this;
  1098. },
  1099. emmit(event_name, args){
  1100. console.log('AudioPlayer emmit()', event_name, args, this.event_handlers[event_name]);
  1101. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  1102. var handler;
  1103. var args = args || {};
  1104. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  1105. handler = this.event_handlers[event_name][i];
  1106. // console.log('AudioPlayer emmit() loop handler', handler);
  1107. (function(handler, args){
  1108. setTimeout(function(){
  1109. // console.log('AudioPlayer emmit() timeout handler', handler);
  1110. handler(args);
  1111. }, 0);
  1112. }(handler, args));
  1113. }
  1114. return this;
  1115. },
  1116. }
  1117. // ___ _ ___ _
  1118. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  1119. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  1120. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  1121. // |__/
  1122. function RandomPlayer(playlist){
  1123. this.active = false;
  1124. this.playlist = playlist;
  1125. this.path = drupalSettings.path.baseUrl+drupalSettings.path.pathPrefix+'#random'
  1126. this.$btn = $('<a>').html('Shuffle').attr('href',this.path).addClass('random-player-btn');
  1127. this.init();
  1128. };
  1129. RandomPlayer.prototype = {
  1130. init(){
  1131. // this.shuffle();
  1132. $('<div>')
  1133. .addClass('block random-player')
  1134. .append(this.$btn)
  1135. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  1136. .prependTo('.region-footer-right');
  1137. // events
  1138. this.$btn.on('click', this.toggleActive.bind(this));
  1139. // attach an event on AudioPlayer
  1140. _audioPlayer
  1141. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  1142. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  1143. // .on('audio-ended', function(e){
  1144. // console.log('RandomPlayer, audio-ended', e);
  1145. // })
  1146. .on('stop-shuffle', this.stop.bind(this));
  1147. _$corpus_canvas
  1148. .on('update-random-playlist', this.updatePlaylist.bind(this));
  1149. // provide a link to auto start random player
  1150. if(window.location.hash == '#random'){
  1151. this.start();
  1152. // window.location.hash = '';
  1153. }
  1154. },
  1155. updatePlaylist(e){
  1156. // debugger;
  1157. this.playlist = e.playlist;
  1158. this.shuffle();
  1159. },
  1160. shuffle(){
  1161. var tempPLaylist = [];
  1162. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  1163. tempPLaylist.push(this.playlist[i]);
  1164. }
  1165. this.shuffledPlaylist = [];
  1166. while(tempPLaylist.length > 0){
  1167. var r = Math.floor(Math.random() * tempPLaylist.length);
  1168. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  1169. }
  1170. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  1171. },
  1172. toggleActive(e){
  1173. e.preventDefault();
  1174. if (this.active) {
  1175. this.stop();
  1176. }else{
  1177. this.start();
  1178. }
  1179. return false;
  1180. },
  1181. start(){
  1182. this.active = _audioPlayer.shuffle_is_active = true;
  1183. this.$btn.addClass('is-active');
  1184. this.shuffle();
  1185. this.next();
  1186. // piwik
  1187. if(typeof _paq !== 'undefined'){
  1188. // trackEvent(category, action, [name], [value])
  1189. _paq.push(['trackEvent', 'RandomPlayer', 'start']);
  1190. }
  1191. },
  1192. stop(){
  1193. this.active = false;
  1194. this.$btn.removeClass('is-active');
  1195. // stop audio player
  1196. // _audioPlayer.stop();
  1197. // remove hash
  1198. history.replaceState(history.state, null, window.location.pathname);
  1199. // tell to AudioPlayer to deactivate netx btn
  1200. _audioPlayer.deActivateRandom();
  1201. if(typeof _paq !== 'undefined'){
  1202. // trackEvent(category, action, [name], [value])
  1203. _paq.push(['trackEvent', 'RandomPlayer', 'stop']);
  1204. }
  1205. },
  1206. next(){
  1207. console.log('RandomPlayer next', this.active, this.shuffledPlaylist.length);
  1208. if(this.active && this.shuffledPlaylist.length > 0)
  1209. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0], 'random');
  1210. },
  1211. onAudioPlayNext(){
  1212. console.log('RandomPlayer : onAudioPlayNext()');
  1213. if(this.active){
  1214. this.next();
  1215. }
  1216. },
  1217. onAudioPlayerEnded(){
  1218. console.log('RandomPlayer : onAudioPlayerEnded()');
  1219. if(this.active){
  1220. this.next();
  1221. }
  1222. }
  1223. };
  1224. // ___ ___ _
  1225. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  1226. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  1227. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  1228. // |_| |__/
  1229. function CompoPlayer(){
  1230. this.active = false;
  1231. this.playing = false;
  1232. this.paused = false;
  1233. this.playlist = [];
  1234. this.current_index = 0;
  1235. this.$composer = null;
  1236. this.$compo = null;
  1237. this.$controls = null;
  1238. this.init();
  1239. };
  1240. CompoPlayer.prototype = {
  1241. init(){
  1242. // console.log('CompoPlayer init()');
  1243. // attach an event on AudioPlayer
  1244. _audioPlayer
  1245. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  1246. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  1247. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  1248. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  1249. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  1250. // this.newCompo();
  1251. },
  1252. newCompo(){
  1253. //console.log('CompoPlayer newCompo()');
  1254. // this.$compo = $('.composition_ui .composer .composition');
  1255. this.initControls();
  1256. },
  1257. initControls(){
  1258. //console.log('CompoPlayer initControls()');
  1259. this.$composer = $('.composition_ui .composer');
  1260. this.$compo = $('.composition_ui .composer .composition');
  1261. this.$controls = $('.composition_ui .composer .compo-player-controls');
  1262. if(!this.$controls.is('.ready') && this.$compo){
  1263. this.$previous = $('<div>').addClass('previous')
  1264. .on('click', this.prev.bind(this))
  1265. .appendTo(this.$controls);
  1266. this.$playpause = $('<div>').addClass('play-pause')
  1267. .on('click', this.togglePlayPause.bind(this))
  1268. .appendTo(this.$controls);
  1269. this.$next = $('<div>').addClass('next')
  1270. .on('click', this.next.bind(this))
  1271. .appendTo(this.$controls);
  1272. this.$controls.addClass('ready');
  1273. this.refresh();
  1274. this.active = true;
  1275. // this.newCompo();
  1276. }
  1277. },
  1278. refresh(){
  1279. // console.log('CompoPlayer refresh(), this', this);
  1280. this.stop();
  1281. // load new playlist
  1282. this.playlist = [];
  1283. var that = this;
  1284. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  1285. var $link = $('a.audio-link',this);
  1286. that.playlist.push({
  1287. item:$(this),
  1288. audio_url:$link.attr("audio_url"),
  1289. nid:$link.attr("nid"),
  1290. });
  1291. });
  1292. this.showHideControls();
  1293. },
  1294. togglePlayPause(){
  1295. // console.log('CompoPlayer togglePlayPause');
  1296. if (this.playing && !this.paused) {
  1297. this.pause();
  1298. }else{
  1299. if(this.playing && this.paused){
  1300. this.play();
  1301. }else{
  1302. this.start();
  1303. }
  1304. }
  1305. },
  1306. start(){
  1307. //console.log('start');
  1308. // console.log('CompoPlayer start()');
  1309. this.playing = true;
  1310. this.play();
  1311. },
  1312. play(){
  1313. // console.log('play');
  1314. if(this.paused){
  1315. this.paused = false;
  1316. _audioPlayer.play();
  1317. }else{
  1318. // _audioPlayer;
  1319. _audioPlayer.emmit('stop-shuffle').openDocument(this.playlist[this.current_index], this);
  1320. // TODO: stop random player
  1321. }
  1322. this.setActiveItem().showHideControls();
  1323. },
  1324. pause(){
  1325. //console.log('pause');
  1326. this.paused = true;
  1327. this.showHideControls();
  1328. _audioPlayer.stop();
  1329. },
  1330. next(){
  1331. // console.log('CompoPlayer next()');
  1332. if(this.playing){
  1333. this.current_index += 1;
  1334. if(this.current_index < this.playlist.length){
  1335. this.play();
  1336. }else{
  1337. this.stop();
  1338. }
  1339. }
  1340. },
  1341. prev(){
  1342. // console.log('CompoPlayer prev()');
  1343. if(this.playing){
  1344. this.current_index -= 1;
  1345. if(this.current_index >= 0){
  1346. this.play();
  1347. }else{
  1348. this.stop();
  1349. }
  1350. }
  1351. },
  1352. stop(){
  1353. if(this.playing){
  1354. _audioPlayer.stop();
  1355. }
  1356. this.reset();
  1357. },
  1358. reset(){
  1359. this.playing = false;
  1360. this.paused = false;
  1361. this.resetIndex();
  1362. },
  1363. resetIndex(){
  1364. this.current_index = 0;
  1365. this.showHideControls().resetActiveItems();
  1366. },
  1367. setActiveItem(){
  1368. this.resetActiveItems();
  1369. if(this.playing && this.current_index >= 0){
  1370. this.playlist[this.current_index].item.addClass('is-active');
  1371. }
  1372. // this call shoud not be here
  1373. this.showHideControls();
  1374. return this;
  1375. },
  1376. resetActiveItems(){
  1377. for (var n = 0; n < this.playlist.length; n++) {
  1378. // console.log('node',node);
  1379. this.playlist[n].item.removeClass('is-active');
  1380. }
  1381. return this;
  1382. },
  1383. showHideControls(){
  1384. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  1385. // global playing
  1386. if(this.$controls){
  1387. if(this.playing && !this.paused){
  1388. this.$controls.addClass('is-playing');
  1389. }else{
  1390. this.$controls.removeClass('is-playing');
  1391. }
  1392. }
  1393. // playpause
  1394. if(this.$playpause){
  1395. if(this.playlist.length > 0){
  1396. this.$playpause.addClass('is-active');
  1397. }else{
  1398. this.$playpause.removeClass('is-active');
  1399. }
  1400. }
  1401. // next
  1402. if(this.$next){
  1403. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  1404. this.$next.addClass('is-active');
  1405. }else{
  1406. this.$next.removeClass('is-active');
  1407. }
  1408. }
  1409. // previous
  1410. if(this.$previous){
  1411. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  1412. this.$previous.addClass('is-active');
  1413. }else{
  1414. this.$previous.removeClass('is-active');
  1415. }
  1416. }
  1417. return this;
  1418. },
  1419. deactivate(){
  1420. this.stop();
  1421. this.active = false;
  1422. },
  1423. // _audioPlayer events
  1424. onAudioOpenDocument(args){
  1425. if(args.caller !== this){
  1426. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  1427. this.reset();
  1428. }
  1429. // else{
  1430. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  1431. // }
  1432. },
  1433. onAudioPlayerPlay(){
  1434. if(this.playing && this.paused){
  1435. this.paused = false;
  1436. this.showHideControls();
  1437. }
  1438. },
  1439. onAudioPlayerPause(){
  1440. if(this.playing && !this.paused){
  1441. this.paused = true;
  1442. this.showHideControls();
  1443. }
  1444. },
  1445. onAudioPlayerEnded(){
  1446. if(this.playing){
  1447. this.next();
  1448. }
  1449. },
  1450. // onAudioPlayNext(){
  1451. // this.next();
  1452. // }
  1453. };
  1454. // ___ _ _
  1455. // / __|_ _(_)__| |
  1456. // | (_ | '_| / _` |
  1457. // \___|_| |_\__,_|
  1458. function initGrid(){
  1459. console.log('theme : initGrid');
  1460. checkGridBlockHeight();
  1461. _$row.find('.col').addClass('offfield');
  1462. if(false){
  1463. var $grid = $('.grid',_$row).masonry({
  1464. itemSelector:'.col',
  1465. columnWidth:'.col-2',
  1466. containerStyle: null,
  1467. resizeContainer:false,
  1468. // horizontalOrder: true,
  1469. transitionDuration:0,//'0.2s',
  1470. // stagger:30,
  1471. // disable initial layout
  1472. // initLayout: false,
  1473. });
  1474. // layout Masonry after each image loads
  1475. $grid.imagesLoaded().progress( function() {
  1476. $grid.masonry('layout');
  1477. });
  1478. $grid.imagesLoaded(function(){
  1479. $grid.masonry('layout');
  1480. // checkProductionBlockVisible();
  1481. });
  1482. // bind event
  1483. // $grid.masonry( 'on', 'layoutComplete', function() {
  1484. // console.log('layout is complete');
  1485. // });
  1486. $grid.on('layoutComplete', checkGridBlockVisible);
  1487. }else{
  1488. // setTimeout(checkGridBlockVisible, 100);
  1489. checkGridBlockVisible();
  1490. }
  1491. };
  1492. function checkGridBlockHeight(){
  1493. // console.log('checkGridBlockHeight');
  1494. var $r_h = _$row.height();
  1495. var $this;
  1496. // console.log($r_h);
  1497. $('.grid .col', _$row).each(function(i,e){
  1498. $this = $(this);
  1499. if(!$this.is('[init-height]')){
  1500. $this.attr('init-height', $this.outerHeight())
  1501. }
  1502. // console.log($(this).height(), $(this).innerHeight(), $(this).outerHeight());
  1503. // if($this.height() > $r_h){
  1504. // $this.height($r_h);
  1505. // }else{
  1506. // $this.height('auto');
  1507. // }
  1508. $this.height(Math.min($this.attr('init-height'), _$row.height()));
  1509. });
  1510. };
  1511. function checkGridBlockVisible(){
  1512. // console.log('checkGridBlockVisible');
  1513. var $r_h = _$row.height();
  1514. var $this,pos;
  1515. $('.grid .col', _$row).each(function(i,e){
  1516. // $(this).on('load',function(event){
  1517. $this = $(this);
  1518. pos = $this.position();
  1519. // console.log(pos.top, $this.height(), pos.top+$this.height(), $r_h, $this);
  1520. if(pos.top+$this.height() <= $r_h){
  1521. $this.removeClass('offfield');
  1522. }else{
  1523. $this.addClass('offfield');
  1524. }
  1525. // });
  1526. });
  1527. // for (var i in items) {
  1528. // if(!items[i].isTransitioning){
  1529. // console.log('y+outerHeight', items[i].position.y+items[i].size.outerHeight);
  1530. // console.log('_$row.height()', _$row.height());
  1531. // if(items[i].position.y+items[i].size.outerHeight < _$row.height()){
  1532. // $(items[i].element).removeClass('offfield');
  1533. // }else{
  1534. // // $(items[i].element).addClass('offfield');
  1535. // }
  1536. // }
  1537. // }
  1538. }
  1539. // ___ _ _ _
  1540. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  1541. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  1542. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  1543. // function initProductions(){
  1544. // console.log('theme : initProductions');
  1545. //
  1546. // // _$row.find('.col').addClass('offfield');
  1547. // var $grid = $('.grid',_$row).masonry({
  1548. // itemSelector:'.col',
  1549. // columnWidth:'.col-2',
  1550. // containerStyle: null,
  1551. // resizeContainer:false,
  1552. // // horizontalOrder: true,
  1553. // transitionDuration:0,//'0.2s',
  1554. // // stagger:30,
  1555. // // disable initial layout
  1556. // // initLayout: false,
  1557. // });
  1558. //
  1559. // // layout Masonry after each image loads
  1560. // $grid.imagesLoaded().progress( function() {
  1561. // $grid.masonry('layout');
  1562. // });
  1563. //
  1564. // $grid.imagesLoaded(function(){
  1565. // $grid.masonry('layout');
  1566. // // checkProductionBlockVisible();
  1567. // });
  1568. //
  1569. // // bind event
  1570. // // $grid.masonry( 'on', 'layoutComplete', function() {
  1571. // // console.log('layout is complete');
  1572. // // });
  1573. // // $grid.on('layoutComplete', checkProductionBlockVisible);
  1574. // };
  1575. // ___ _
  1576. // / __| ___ __ _ _ _ __| |_
  1577. // \__ \/ -_) _` | '_/ _| ' \
  1578. // |___/\___\__,_|_| \__|_||_|
  1579. function initSearch(){
  1580. // if($('#edit-entries input:checked', '#edlp-search-form').length){
  1581. // $('#edit-entries--wrapper', '#edlp-search-form').toggleClass('opened');
  1582. // }
  1583. $('#edit-entries--wrapper legend', '#edlp-search-form').on('click', function(){
  1584. $(this).parent().toggleClass('opened');
  1585. });
  1586. };
  1587. // ___ _ _ _
  1588. // | __|_ _ _ _ ___ __ _(_)__| |_ _ _ ___ _ __ ___ _ _| |_
  1589. // | _|| ' \| '_/ -_) _` | (_-< _| '_/ -_) ' \/ -_) ' \ _|
  1590. // |___|_||_|_| \___\__, |_/__/\__|_| \___|_|_|_\___|_||_\__|
  1591. // |___/
  1592. function initEnregistrementTranscript(){
  1593. console.log('initEnregistrementTranscript');
  1594. var $node = _$row.find('article.node--type-enregistrement.node--view-mode-transcript');
  1595. var $nav = $('<nav>').prependTo($node);
  1596. $node.find('.field--name-field-transcript-vo').addClass('visible').find('.field__label')
  1597. .clone().appendTo($nav).addClass('is-active')
  1598. .attr('field_target', '.field--name-field-transcript-vo');
  1599. $node.find('.field--name-field-transcript-trad').find('.field__label')
  1600. .clone().appendTo($nav)
  1601. .attr('field_target', '.field--name-field-transcript-trad');
  1602. $nav.find('.field__label').on('click', function(){
  1603. var $this = $(this).addClass('is-active');
  1604. $this.siblings('.is-active').removeClass('is-active');
  1605. $this.parents('article.node').find('.field.visible').removeClass('visible');
  1606. $this.parents('article.node').find($this.attr('field_target')).addClass('visible');
  1607. });
  1608. };
  1609. // ___ _ ___
  1610. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  1611. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  1612. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  1613. // |___/
  1614. function backToFrontPage(pop_state){
  1615. console.log('backToFrontPage', pop_state);
  1616. closeAllModals();
  1617. // assume we are going back to front page
  1618. $('body').removeClass().addClass('path-frontpage');
  1619. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  1620. // close entrees
  1621. _$corpus_canvas.trigger({'type':'close-all-entree'});
  1622. _$corpus_canvas.trigger({'type':'scramble-collection'});
  1623. if(typeof pop_state == "undefined" || !pop_state){
  1624. console.log('backToFrontPage push state');
  1625. history.pushState({home:true}, null, drupalSettings.path.baseUrl+drupalSettings.path.currentLanguage);
  1626. }
  1627. }
  1628. function initHome(){
  1629. addCloseModalBtnToCols();
  1630. return;
  1631. console.log('theme : initHome');
  1632. var $grid = $('.grid',_$row).masonry({
  1633. itemSelector:'.col',
  1634. columnWidth:'.col-2',
  1635. horizontalOrder: true,
  1636. containerStyle: null,
  1637. // disable initial layout
  1638. // initLayout: false,
  1639. });
  1640. // bind event
  1641. // $grid.masonry( 'on', 'layoutComplete', function() {
  1642. // console.log('layout is complete');
  1643. // });
  1644. // layout Masonry after each image loads
  1645. $grid.imagesLoaded().progress( function() {
  1646. $grid.masonry('layout');
  1647. });
  1648. $grid.imagesLoaded(function(){
  1649. $grid.masonry('layout');
  1650. });
  1651. }
  1652. // __ __ _ _
  1653. // | \/ |___ __| |__ _| |___
  1654. // | |\/| / _ \/ _` / _` | (_-<
  1655. // |_| |_\___/\__,_\__,_|_/__/
  1656. function closeAllModals(){
  1657. //console.log('theme : closeAllModals');
  1658. // TODO: animate the remove
  1659. _$row.html('');
  1660. _$ajaxLinks.removeClass('is-active');
  1661. _$body.trigger({'type':'all-modal-closed'});
  1662. // checkRowEmpty();
  1663. };
  1664. function checkRowEmpty(){
  1665. // if row is empty and we are not in productions or entree notice|index call closeAllModals()
  1666. if(!$('.col', _$row).length){
  1667. if(!_$body.is('.entity-type-taxonomy_term.bundle-entrees')){
  1668. if(!_$body.is('.entity-type-node.bundle-page')){
  1669. // we weren't on production or entree, so go back to front page
  1670. backToFrontPage();
  1671. }else{
  1672. // if we were on production page just scramble collection in case of map was filtered
  1673. _$corpus_canvas.trigger({'type':'scramble-collection'});
  1674. // reload production home
  1675. $('a[data-drupal-link-system-path="productions"]', '#block-mainnavigation')
  1676. .removeClass('is-active').trigger('click');
  1677. }
  1678. }else{
  1679. // remove is-active class from index or notice entree links
  1680. $('.entree-content a.is-active').removeClass('is-active');
  1681. // TODO: remove the hash index or notice
  1682. }
  1683. }
  1684. };
  1685. init();
  1686. } // end EdlpTheme()
  1687. $(document).ready(function($) {
  1688. if(drupalSettings.path.isFront){
  1689. var edlptheme = new EdlpTheme();
  1690. }else{
  1691. $('body').attr('booted', 'booted');
  1692. }
  1693. });
  1694. })(jQuery, Drupal, drupalSettings);