main.js 60 KB

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