main.js 47 KB

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