main.js 56 KB

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