main.js 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296
  1. (function($, Drupal, drupalSettings) {
  2. EdlpTheme = function(){
  3. var _origin = window.location.origin;
  4. var _base_url = drupalSettings.path.baseUrl;
  5. var _ajax_settings = drupalSettings.edlp_ajax;
  6. var _$body = $('body');
  7. // var _is_front = drupalSettings.path.isFront;
  8. var _corpus_ready = false;
  9. var _$corpus_canvas;
  10. var _$row = $('main[role="main"]>.layout-content>.row');
  11. var _$ajaxLinks;
  12. var _audioPlayer;
  13. var _randomPlayer;
  14. var _compoPlayer;
  15. var _ajax_timing = {
  16. start:0,
  17. end:0
  18. };
  19. var _corpus_promise;
  20. var _is_mobile = edlp_mobile.device_is_mobile;
  21. var _is_loggedin = drupalSettings.user.uid === 0 ? false : true;
  22. var _$log_form;
  23. var _user_tokens;
  24. var _states_history = [];
  25. var _is_expo = $('body').hasClass('domain-expo-encyclopediedelaparole-org');
  26. // ___ _ _
  27. // |_ _|_ _ (_) |_
  28. // | || ' \| | _|
  29. // |___|_||_|_|\__|
  30. function init(){
  31. console.log("EdlpTheme init()");
  32. if (!_is_loggedin) {
  33. initLogForm();
  34. }
  35. if(_is_mobile){
  36. initMobile();
  37. }
  38. initEvents();
  39. _audioPlayer = new AudioPlayer();
  40. _compoPlayer = new CompoPlayer();
  41. initAjaxLinks();
  42. initHistory();
  43. if(!_is_mobile){
  44. checkLayout();
  45. initAudioLinksHover();
  46. }
  47. if(_is_mobile){
  48. if(drupalSettings.path.isFront){
  49. initHomeMobile();
  50. }
  51. _$body.attr('booted', 'booted');
  52. }
  53. };
  54. // _ ___
  55. // | | ___ __ _ | __|__ _ _ _ __
  56. // | |__/ _ \/ _` | | _/ _ \ '_| ' \
  57. // |____\___/\__, | |_|\___/_| |_|_|_|
  58. // |___/
  59. function initLogForm(){
  60. console.log('initLogForm');
  61. _$log_form = $('#user-login-form:not(ajax-enebled)')
  62. .on('submit', onSubmitLogForm)
  63. .addClass('ajax-enabled');
  64. };
  65. function onSubmitLogForm(e){
  66. e.preventDefault();
  67. // console.log('onSubmitLogForm', e);
  68. var args = {
  69. name : $('input#edit-name[type="text"]', this).val(),
  70. pass : $('input#edit-pass[type="password"]', this).val()
  71. };
  72. // console.log(args);
  73. logIn(args).then(getStudioLinkBlock);
  74. return false;
  75. };
  76. function logIn(args){
  77. console.log('logIn', args);
  78. _$log_form.addClass('ajax-loading');
  79. _$body.addClass('ajax-loading');
  80. return $.ajax({
  81. type: 'POST',
  82. headers: {'Content-Type': 'application/json'},
  83. xhrFields: {
  84. withCredentials: true
  85. },
  86. // accessToken : 'tokenvaluehere',
  87. url: _origin+_base_url+"user/login?_format=json",
  88. data: JSON.stringify(args),
  89. // name: args.mail,
  90. // pass: args.pass,
  91. success: function(response){
  92. // console.log('sucess', response);
  93. _user_tokens = response;
  94. },
  95. error: function (xhr, ajaxOptions, thrownError){
  96. console.log(xhr.status);
  97. console.log(thrownError);
  98. }
  99. });
  100. };
  101. function getStudioLinkBlock(){
  102. var path = _origin+_base_url+'edlp/ajax/blocks/json/';
  103. $.getJSON(path+"studiolinkblock", {})
  104. .done(onStudioLinkBlockLoaded)
  105. .fail(function(jqxhr, textStatus, error){
  106. console.warn('Studio ink block load failed', jqxhr.responseText);
  107. });
  108. };
  109. function onStudioLinkBlockLoaded(data){
  110. console.log('onStudioLinkBlockLoaded', data);
  111. $('#block-studiouserlogin').replaceWith(data.blocks.studiolinkblock.rendered);
  112. initAjaxLinks();
  113. refreshFavoritesLinks();
  114. _$log_form.removeClass('ajax-loading');
  115. _$body.removeClass('ajax-loading');
  116. }
  117. // __ __ _ _ _
  118. // | \/ |___| |__(_) |___
  119. // | |\/| / _ \ '_ \ | / -_)
  120. // |_| |_\___/_.__/_|_\___|
  121. function initMobile(){
  122. // $('[data-drupal-link-system-path="<front>"]','#block-mainnavigation')
  123. // .removeClass('is-active')
  124. // .attr('href', '#collection');
  125. // edlp_mobile.mobile_home_path.replace(/^\//, '')
  126. // TODO: remove collection from mobile home
  127. // TODO: replace ajax link to only collection for mobile
  128. $('h2, a', '#block-mainnavigation')
  129. .add('h2, a', '#block-mainnavigation-2')
  130. .on('click', onclickHomeMobileMenu);
  131. }
  132. function onclickHomeMobileMenu(e){
  133. // $('#block-mainnavigation').toggleClass('opened');
  134. $('#block-mainnavigation-2').toggleClass('opened');
  135. };
  136. function initHomeMobile(){
  137. // taxonomy-term.vocabulary-entrees.home_mobile
  138. // $('.field--name-field-notice, .index', '.entrees .taxonomy-term.vocabulary-entrees')
  139. // .addClass('closed');
  140. // $('.field--name-field-notice>.field__label', '.entrees .taxonomy-term.vocabulary-entrees')
  141. // .on('click', onClickHomeMobileNotice);
  142. // $('.index>.field__label', '.entrees .taxonomy-term.vocabulary-entrees')
  143. // .on('click', onClickHomeMobileIndex);
  144. };
  145. // function onClickHomeMobileNotice(e){
  146. // // console.log('onClickHomeMobileNotice');
  147. // // var $part = $(this).parent();//parents('.taxonomy-term');
  148. // toggleEntreeOpening($(this).parent(), 'notice');
  149. // };
  150. // function onClickHomeMobileIndex(e){
  151. // // console.log('onClickHomeMobileIndex');
  152. // // var $part = $(this).parent();//parents('.taxonomy-term');
  153. // toggleEntreeOpening($(this).parent(), 'index');
  154. // };
  155. // function toggleEntreeOpening($e, part){
  156. // $e.toggleClass('closed')
  157. // .parents('.taxonomy-term.vocabulary-entrees.home_mobile').toggleClass(part+'-opened');
  158. // }
  159. // ___ _
  160. // | __|_ _____ _ _| |_ ___
  161. // | _|\ V / -_) ' \ _(_-<
  162. // |___|\_/\___|_||_\__/__/
  163. function initEvents(){
  164. // https://www.html5rocks.com/en/tutorials/async/deferred/
  165. var $corpus_df = $.Deferred();
  166. _corpus_promise = $corpus_df.promise();
  167. _$body
  168. .on('corpus-map-ready', function(e){
  169. onCorpusMapReady(e);
  170. $corpus_df.resolve();
  171. })
  172. .on('on-studio-chutier-updated', initAjaxLinks)
  173. .on('studio-initialized', function(e){
  174. _compoPlayer.newCompo();
  175. })
  176. .on('studio-not-active', function(e){
  177. _compoPlayer.deactivate();
  178. })
  179. .on('on-studio-compo-updated', function(e){
  180. initAjaxLinks();
  181. _compoPlayer.refresh();
  182. })
  183. .on('on-studio-compo-opened', function(e){
  184. initAjaxLinks();
  185. _compoPlayer.newCompo();
  186. })
  187. .on('search-results-loaded', function(e){
  188. initAjaxLinks();
  189. initAudioLinksHover();
  190. checkVisibleCorpusMapSpace();
  191. })
  192. // do not close index or notice modale on entree click
  193. .on('open_entree', function(e){
  194. console.log('on_open_entree : e', e);
  195. closeAllModals();
  196. checkLayout();
  197. // add body class for currently loaded content
  198. // var body_classes = [
  199. // 'path-'+sys_path.replace(/\//g, '-'),
  200. // 'entity-type-'+data.entity_type,
  201. // 'bundle-'+data.bundle,
  202. // 'view-mode-'+data.view_mode
  203. // ];
  204. _$body.removeClass();//.addClass(body_classes.join(' '));
  205. // record new history state
  206. if(typeof e.url != 'undefined'){
  207. var state = getSysPathState(e.sys_path);
  208. history.pushState(state, null, e.url);
  209. // piwik
  210. if(typeof _paq !== 'undefined'){
  211. // page tracking
  212. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  213. _paq.push(['setCustomUrl', e.url]);
  214. _paq.push(['setDocumentTitle', e.title]);
  215. _paq.push(['trackPageView']);
  216. }
  217. }
  218. })
  219. .on('close_entree', function(e){
  220. backToFrontPage();
  221. checkLayout();
  222. });
  223. window.addEventListener('resize', checkLayout, false);
  224. }
  225. // __ ___ _ ___ _
  226. // \ \ / (_)_ _ __| |_____ __ __ | _ \___ __(_)______
  227. // \ \/\/ /| | ' \/ _` / _ \ V V / | / -_|_-< |_ / -_)
  228. // \_/\_/ |_|_||_\__,_\___/\_/\_/ |_|_\___/__/_/__\___|
  229. function checkLayout(){
  230. var $audioplayer = $("#audio-player");
  231. if($audioplayer.length){
  232. var navpos = $('#block-mainnavigation').position();
  233. // console.log('navpos', navpos);
  234. if(typeof navpos != 'undefined'){
  235. $audioplayer.css({
  236. 'width':navpos.left+'px'
  237. });
  238. }
  239. }
  240. checkGridBlockHeight();
  241. checkGridBlockVisible();
  242. // entrees
  243. // TODO: center the entrees menu
  244. };
  245. // ___ _ _ ___
  246. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  247. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  248. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  249. function initScrollbars(){
  250. // console.log("initScrollbars");
  251. // TODO: find a better js scroll than overlayScrollbars which does not handle well max-height + overflow-y:auto;
  252. // $('.os-scroll').overlayScrollbars({
  253. // overflowBehavior:{
  254. // x:'h',
  255. // y:'scroll',
  256. // clipAlways:false
  257. // }
  258. // });
  259. };
  260. // _ _
  261. // /_\ (_)__ ___ __
  262. // / _ \ | / _` \ \ /
  263. // /_/ \_\/ \__,_/_\_\
  264. // |__/
  265. function getSysPathState(sys_path, view_mode){
  266. // console.log('Theme : getSysPathState', sys_path);
  267. var state = {
  268. 'sys_path':sys_path,
  269. 'ajax_path': sys_path
  270. };
  271. // convert node link to edlp_ajax_node module links
  272. var node_match = state.ajax_path.match(/^\/?(node\/(\d+))$/i);
  273. console.log('node_match', node_match);
  274. var term_match = state.ajax_path.match(/^\/?(taxonomy\/term\/(\d+))$/i);
  275. console.log('term_match', term_match);
  276. if(node_match){
  277. // TODO: detect audio links which will open audioplayer and wont load any ajax content unless view_mode "article" or "transcript"
  278. state.ajax_path = _ajax_settings.entityjson_path+'/'+node_match[1];
  279. state.node_nid = node_match[2];
  280. // check for viewmode attribute
  281. if(view_mode){
  282. state.ajax_path += '/'+view_mode;
  283. state.view_mode = view_mode;
  284. }
  285. }else if(term_match){
  286. // terms are always entrees, there's no other vocabulary links in front
  287. state.ajax_path = _ajax_settings.entityjson_path+'/'+term_match[1];
  288. state.ajax_path = state.ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  289. state.entree_tid = term_match[2];
  290. // check for viewmode attribute
  291. if(view_mode){
  292. state.ajax_path += '/'+view_mode;
  293. state.view_mode = view_mode;
  294. }else{
  295. state.ajax_path = null;
  296. }
  297. }else{
  298. // convert other link to ajax
  299. // TODO: we assume that other links (no node, no term) are all from own modules (e.g. productions) !! may not be true !!
  300. state.ajax_path += '/ajax'
  301. }
  302. return state;
  303. };
  304. function ajaxLoadContent(state){
  305. console.log('ajaxLoadContent : state', state);
  306. _$body.addClass('ajax-loading');
  307. _ajax_timing.start = performance.now();
  308. var path = _origin + Drupal.url(state.ajax_path);
  309. $.getJSON(path, {})
  310. .done(function(data){
  311. onAjaxLoaded(data, state);
  312. })
  313. .fail(function(jqxhr, textStatus, error){
  314. onAjaxLoadError(jqxhr, textStatus, error, state.sys_path);
  315. });
  316. };
  317. function onAjaxLoadError(jqxhr, textStatus, error, sys_path){
  318. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  319. $('.ajax-loading').removeClass('ajax-loading');
  320. _$body.removeClass('ajax-loading');
  321. };
  322. function onAjaxLoaded(data, state){
  323. console.log('ajax loaded', state, data);
  324. var $rendered = $(data.rendered);
  325. $rendered.attr({
  326. 'sys_path':state.sys_path,
  327. 'view_mode': state.view_mode
  328. });
  329. // console.log(data);
  330. // reset all style may been added by other pages (like masonry for productions)
  331. // and replace all content with newly loaded
  332. // TODO: build a system to replace or append contents (like studio + search)
  333. if(data.entity_type == "node" && data.bundle == "evenement"){
  334. if(_$row.find('.col.event').length){
  335. _$row.find('.col.event').replaceWith($rendered);
  336. }else if(_$row.find('.col.aside').length){
  337. _$row.find('.col.aside').replaceWith($rendered);
  338. }else{
  339. _$row.append($rendered);
  340. }
  341. }
  342. else if(
  343. (state.context == 'document-transcript' || state.context == 'document-artcile')
  344. && state.caller == "search"
  345. ){
  346. _$row.append($rendered);
  347. }
  348. else{
  349. _$row.removeAttr('style').html($rendered);
  350. }
  351. // remove article is-active class is static content loaded
  352. if ((data.entity_type == "node" && data.bundle == "static")
  353. || state.sys_path == "docsindex") {
  354. $('.ajax-link.is-active.articles-link').removeClass('is-active');
  355. }
  356. // add body class for currently loaded content
  357. var body_classes = [
  358. 'path-'+state.sys_path.replace(/\//g, '-'),
  359. 'entity-type-'+data.entity_type,
  360. 'bundle-'+data.bundle,
  361. 'view-mode-'+data.view_mode
  362. ];
  363. _$body.removeClass().addClass(body_classes.join(' '));
  364. // id node add a generic path-node class to body
  365. // m = state.sys_path.match(/^\/?(node\/\d+)$/g);
  366. // if(m)
  367. if(state.node_nid)
  368. _$body.addClass('path-edlp-node');
  369. // handle clicked link classes
  370. $('.ajax-loading').removeClass('ajax-loading');
  371. $('.ajax-link.is-active:not(.articles-link)').removeClass('is-active');
  372. $('.is-active-trail').removeClass('is-active-trail');
  373. if(typeof state.selector != 'undefined'){
  374. // in case of entree link (actualy, selector is used only for entries links)
  375. // TODO: unfortunatly on initFirstLoad there is no selector property
  376. console.log('selector', state.selector);
  377. $('a[selector="'+state.selector+'"]').addClass('is-active');
  378. initAudioLinksHover();
  379. }else{
  380. if(typeof state.view_mode != 'undefined'){
  381. $('a[viewmode="'+state.view_mode+'"][data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
  382. }else{
  383. // wait for corpus map beeing ready (e.g. for articles link)
  384. _corpus_promise.done(function(){
  385. $('a[data-drupal-link-system-path="'+state.sys_path+'"]').addClass('is-active');
  386. });
  387. }
  388. // as new content is not related to entree, we trigger close entree
  389. _$body.trigger({'type':'new-content-not-entree-ajax-loaded'});
  390. }
  391. // if bundle page (productions) activate production links
  392. if (typeof data.bundle != 'undefined' && data.bundle == "page") {
  393. $('a[data-drupal-link-system-path="productions"]').addClass('is-active-trail');
  394. }
  395. // if node is in production menu tree, set first level of tree active, e.g. pieces sonores
  396. if (typeof data.menu_parents != 'undefined') {
  397. for (var i = 0; i < data.menu_parents.length; i++) {
  398. var menu_sys_path = data.menu_parents[i];
  399. $('a[data-drupal-link-system-path="'+menu_sys_path+'"]').addClass('is-active-trail');
  400. }
  401. }
  402. // if entity has corpus-map's linked document call a filter to corpus map
  403. if(typeof data.documents_lies != 'undefined'){
  404. if(_corpus_ready){
  405. _$body.trigger({
  406. type:'ajax-node-loaded-linked-documents',
  407. nids:data.documents_lies
  408. });
  409. }else{
  410. _corpus_promise.done(function(){
  411. _$body.trigger({
  412. type:'ajax-node-loaded-linked-documents',
  413. nids:data.documents_lies
  414. });
  415. });
  416. }
  417. }
  418. // if block attached (eg : from edlp_productions module)
  419. // not used anymore as production block is always present (but not visible)
  420. if(typeof data.block != 'undefined'){
  421. // if block not already added
  422. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  423. $('.region-'+data.block.region).append(data.block.rendered);
  424. }
  425. }
  426. // initScrollbars();
  427. if(state.sys_path == "productions"){
  428. // initProductions();
  429. initGrid();
  430. }else{
  431. addCloseModalBtnToCols();
  432. }
  433. if(state.sys_path == "collection"){
  434. // only for mobile version of collection
  435. initCollectionNav();
  436. }
  437. // enregistrement transcription
  438. if(data.entity_type == "node" && data.bundle == "enregistrement" && data.view_mode == "transcript"){
  439. // window.requestAnimationFrame(initEnregistrementTranscript);
  440. initEnregistrementTranscript();
  441. }
  442. if(state.sys_path == "search"){
  443. initSearch();
  444. }
  445. // update the language switcher block if it comes in the response
  446. if(typeof data.translations_links != 'undefined'){
  447. console.log('state',state);
  448. var lang_code = drupalSettings.path.currentLanguage;
  449. var $links = $(data.translations_links);
  450. // set active link
  451. $links.find('li[hreflang="'+lang_code+'"]').addClass('is-active').find('a').addClass('is-active');
  452. if(state.view_mode){
  453. $links.find('a').each(function(i,e){
  454. var $a = $(this);
  455. $a.attr('href', $a.attr('href')+'#'+state.view_mode);
  456. });
  457. }
  458. // if(state.selector){
  459. // $links.find('a').attr('selector', state.selector);
  460. // }
  461. $('ul','.block.language-switcher-language-url').replaceWith($links);
  462. }
  463. initAjaxLinks();
  464. initAudioLinksHover();
  465. checkVisibleCorpusMapSpace();
  466. // trigger other modules behaviours
  467. _$body.trigger({'type':'new-content-ajax-loaded'});
  468. // and call drupal behaviours
  469. Drupal.attachBehaviors(_$row[0]);
  470. _$body.attr('booted', 'booted');
  471. _$body.removeClass('ajax-loading');
  472. // record the states history as we can't get them from history api
  473. _states_history.unshift(state);
  474. // url is null means that we are loading content on popState event
  475. // so we don't record the state again
  476. if(state.url){
  477. // /!\ we can not pushestate with absolute url /!\
  478. history.pushState(state, null, state.url);
  479. // piwik
  480. if(typeof _paq !== 'undefined'){
  481. // page tracking
  482. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  483. _paq.push(['setCustomUrl', state.url]);
  484. _paq.push(['setDocumentTitle', data.title]);
  485. // TODO: piwik track load time
  486. _ajax_timing.end = performance.now();
  487. _paq.push(['setGenerationTimeMs', _ajax_timing.end-_ajax_timing.start]);
  488. _paq.push(['trackPageView']);
  489. // js event
  490. // trackEvent(category, action, [name], [value])
  491. // _paq.push(['trackEvent', 'AjaxNav', 'loaded', state.url]);
  492. }
  493. }
  494. };
  495. function initAudioLinksHover(){
  496. console.log("initAudioLinksHover()");
  497. _$row.find('a.audio-link')
  498. .on('mouseover', function(event) {
  499. event.preventDefault();
  500. if(_corpus_ready){
  501. _$corpus_canvas.trigger({
  502. type:'mouseover-audio-link',
  503. nid:$(this).attr('nid')
  504. });
  505. }
  506. })
  507. .on('mouseout', function(event) {
  508. event.preventDefault();
  509. if(_corpus_ready){
  510. _$corpus_canvas.trigger({
  511. type:'mouseout-audio-link',
  512. nid:$(this).attr('nid')
  513. });
  514. }
  515. });
  516. };
  517. function addCloseModalBtnToCols(){
  518. if(_is_mobile) return;
  519. $('.col', _$row).each(function(index, el) {
  520. if($('span.close-col-btn', this).length)
  521. return true;
  522. $(this).children('.wrapper').prepend($('<span>')
  523. .addClass('close-col-btn')
  524. .on('click', onCloseModal)
  525. );
  526. });
  527. };
  528. function onCloseModal(e){
  529. console.log("onCloseModal");
  530. // check from production to audio text back to production
  531. console.log("onCloseModal _states_history",_states_history);
  532. if(['document-transcript', 'document-article'].indexOf(_states_history[0].context) != -1)
  533. {
  534. if (_states_history[0].caller == "production") {
  535. for (var i = 0; i < _states_history.length; i++) {
  536. if (_states_history[i].context == "production") {
  537. // history.go(i*-1-1);
  538. console.log("onCloseModal return to production", _states_history[i]);
  539. ajaxLoadContent(_states_history[i]);
  540. return;
  541. }
  542. }
  543. }
  544. if (_states_history[0].caller == "entree-index") {
  545. for (var i = 0; i < _states_history.length; i++) {
  546. if (_states_history[i].context == "entree-index") {
  547. console.log("onCloseModal return to entree-index", _states_history[i]);
  548. ajaxLoadContent(_states_history[i]);
  549. return;
  550. }
  551. }
  552. }
  553. if (_states_history[0].caller == "entree-notice") {
  554. for (var i = 0; i < _states_history.length; i++) {
  555. if (_states_history[i].context == "entree-notice") {
  556. console.log("onCloseModal return to entree-notice", _states_history[i]);
  557. ajaxLoadContent(_states_history[i]);
  558. return;
  559. }
  560. }
  561. }
  562. }
  563. // return to productions home when closing a prod
  564. // if(['production'].indexOf(_states_history[0].context) != -1)
  565. // {
  566. // var i = 0;
  567. // while (_states_history[i].caller == "production-home") {
  568. // if (_states_history[i].context == "production-home") {
  569. // // history.go(i*-1-1);
  570. // console.log("onCloseModal return to productions home", _states_history[i]);
  571. // ajaxLoadContent(_states_history[i]);
  572. // return;
  573. // }
  574. // i++;
  575. // }
  576. // }
  577. // check for theme attribute and emmit event
  578. var $col = $(this).parents('.col');
  579. var theme = $col.attr('theme');
  580. if(theme != ''){
  581. _$body.trigger({'type':theme+'-col-closed'});
  582. }
  583. if(_$body.is('.entity-type-node.bundle-page') && $(this).next().is('.node--type-page')){
  584. // remove all the col (prod page and agenda in case) only if closing the main page col
  585. // if closing the aside col (agenda) leave the main col in place
  586. $col.add($col.siblings('.col')).remove();
  587. }else{
  588. // remove the col
  589. $col.remove();
  590. }
  591. checkRowEmpty();
  592. checkVisibleCorpusMapSpace();
  593. // set links to this modal to not active (tested on articles from audio cartel)
  594. if( $col.attr('view_mode') && $col.attr('sys_path')){
  595. $('a[data-drupal-link-system-path="'+$col.attr('sys_path')+'"][viewmode="'+$col.attr('view_mode')+'"]').removeClass('is-active');
  596. }
  597. };
  598. // _ _ ___ _ _
  599. // /_\ (_)__ ___ __ | _ ) |___ __| |__ ___
  600. // / _ \ | / _` \ \ / | _ \ / _ \/ _| / /(_-<
  601. // /_/ \_\/ \__,_/_\_\ |___/_\___/\__|_\_\/__/
  602. // |__/
  603. // NOT USED (YET)
  604. // function refreshAllBlocks(){
  605. // var path = _origin + Drupal.url(_ajax_settings.blocksjson_path);
  606. // $.getJSON(path, {})
  607. // .done(function(data){
  608. // onAjaxBlockLoaded(data);
  609. // })
  610. // .fail(function(jqxhr, textStatus, error){
  611. // onAjaxBlockLoadError(jqxhr, textStatus, error);
  612. // });
  613. // };
  614. // function onAjaxBlockLoadError(jqxhr, textStatus, error){
  615. // console.warn('ajax block load failed: '+error, jqxhr.responseText);
  616. // };
  617. // function onAjaxBlockLoaded(data){
  618. // console.log('onAjaxBlockLoaded', data);
  619. // // TODO: update each blocks (exepted language switcher)
  620. // for (var blockname in data.blocks) {
  621. // var block = data.blocks[blockname];
  622. // console.log(blockname, block);
  623. // $(block.id).replaceWith(block.rendered);
  624. // }
  625. // };
  626. // _ _ _ _
  627. // | || (_)__| |_ ___ _ _ _ _
  628. // | __ | (_-< _/ _ \ '_| || |
  629. // |_||_|_/__/\__\___/_| \_, |
  630. // |__/
  631. function initHistory(){
  632. initFirstLoad();
  633. window.addEventListener('popstate', onHistoryPopState);
  634. };
  635. function initFirstLoad(){
  636. console.log('theme : initFirstLoad()');
  637. console.log('window.location', window.location);
  638. // var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
  639. var edlp_origin = JSON.parse(window.localStorage.getItem('edlp_origin'));
  640. console.log('edlp_origin', edlp_origin);
  641. if(edlp_origin != null && edlp_origin.sys_path){
  642. // hash is used as viewmode for taxonomy term entrees load (index or notice)
  643. // and for audio contents (article|transcript)
  644. var hash = edlp_origin.hash.replace('#', '');
  645. // create history state
  646. var state = getSysPathState(edlp_origin.sys_path, hash);
  647. // open entree tray in case of entree index|notice
  648. // (index or notice will be opened with ajaxLoadContent)
  649. // refactorized with new infos from edlp_origin
  650. // if(hash){
  651. if(edlp_origin.entity_type == "taxonomy_term"
  652. && edlp_origin.entity_bundle == "entrees"
  653. && hash){
  654. // var $link = $('[href="'+edlp_origin.url+'"][viewmode="'+hash+'"]');
  655. // var selector = $link.attr('selector') || null;
  656. // record the selector in the state for actions after ajaxContentLoaded
  657. state.selector = 'entree-'+hash+'-link-'+edlp_origin.entity_id; // entree-index-link-125
  658. // if(selector){
  659. // in case of entree link (actualy, selector is used only for entries links)
  660. // TODO: use a promise
  661. // TODO: but what if corpus ready before onAjaxLoaded >> use a promise !!
  662. if(_corpus_ready){
  663. _$corpus_canvas.trigger({
  664. type:'open-entree',
  665. // tid:$link.attr('tid')
  666. tid:edlp_origin.entity_id
  667. });
  668. }else{
  669. // else : EdlpCorpus will check when ready if entry item (notice or index) is already .is-active
  670. // .is-active class is added by onAjaxLoaded() (when content is loaded)
  671. // $('li.entree[tid="'+$link.attr('tid')+'"] a.term-link').addClass('is-active');
  672. $('li.entree[tid="'+edlp_origin.entity_id+'"] a.term-link').addClass('is-active');
  673. }
  674. // }
  675. }
  676. // check if audio link
  677. // only if not random link
  678. if(edlp_origin.audio_url){
  679. var node = {
  680. nid:edlp_origin.entity_id,
  681. audio_url:edlp_origin.audio_url
  682. };
  683. _audioPlayer.openDocument(node, 'history_first_load');
  684. // close home
  685. closeAllModals();
  686. if(hash == "" || hash == 'random'){
  687. // if hash is random, RandomPlayer will be started when playlist sent by corpus.js
  688. // if audio only record in state
  689. state.audio = true;
  690. state.node = node;
  691. // we don't laod ajax content so we boot the home
  692. _$body.attr('booted', 'booted');
  693. }else{
  694. // ajax load content for audio only if article or transcript
  695. ajaxLoadContent(state);
  696. }
  697. }
  698. // only if not entree path
  699. // only if not audio (without article or transcript) path
  700. else if(state.ajax_path){
  701. // load content through ajax
  702. // ajaxLoadContent(null, state.sys_path, state.ajax_path, selector);
  703. ajaxLoadContent(state);
  704. }
  705. if(state.entree_tid){
  706. openEntree(state.entree_tid);
  707. }
  708. // record history state
  709. history.replaceState(state, null, edlp_origin.url+edlp_origin.hash);
  710. // reset the storage
  711. window.localStorage.removeItem("edlp_origin");
  712. // window.localStorage.removeItem("edlp_origin_url");
  713. }else{
  714. // if(window.location.hash == "#random"){
  715. //
  716. // }
  717. history.replaceState({home:true}, null, window.location.pathname+window.location.hash);
  718. // initHome();
  719. initGrid();
  720. _$body.attr('booted', 'booted');
  721. }
  722. };
  723. function onHistoryPopState(e){
  724. console.log('onPopState',e.state);
  725. if(e.state.home){
  726. backToFrontPage(true);
  727. }
  728. else if (e.state.audio) {
  729. _audioPlayer.openDocument(e.state.node, 'popstate', e.state.historic_index);
  730. }
  731. else{
  732. if(e.state.entree_tid){
  733. openEntree(e.state.entree_tid);
  734. }
  735. if(e.state.ajax_path){
  736. e.state.url = null;
  737. // ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  738. ajaxLoadContent(e.state);
  739. }
  740. }
  741. };
  742. // _ _ _ _ _
  743. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  744. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  745. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  746. // |__/
  747. function initAjaxLinks(){
  748. // console.log('initAjaxLinks');
  749. $('a', '#block-mainnavigation')
  750. .add('a', '#block-mainnavigation-2')
  751. // .add('a', '.block.language-switcher-language-url')
  752. .add('a', '#block-footer.menu--footer')
  753. .add('a', '#block-productions')
  754. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  755. // .add('a', 'article.node--type-evenement.node--view-mode-teaser .field--name-field-page-liee')
  756. .add('a', '.productions-subtree')
  757. .add('a', '.productions-parent')
  758. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  759. .add('a', '.field--name-field-son')
  760. .addClass('ajax-link');
  761. if(!_is_mobile){
  762. $('a.site-name', '#block-edlptheme-branding').addClass('ajax-link');
  763. }
  764. _$ajaxLinks = $('.ajax-link');
  765. activateAjaxLinks();
  766. };
  767. function activateAjaxLinks(){
  768. // $('.ajax-link:not(.ajax-enabled)')
  769. _$ajaxLinks.each(function(i,e){
  770. var $this = $(this);
  771. // avoid already ajaxified links
  772. if($this.is('.ajax-enable')) return;
  773. if($this.attr('data-drupal-link-system-path') || $this.is('[type^="audio"]')){
  774. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  775. }
  776. });
  777. };
  778. function onClickAjaxLink(e){
  779. e.preventDefault();
  780. var $link = $(this);
  781. // let the site-name link going through as it will shuffle the map if already active
  782. if($link.is('.is-active') && !$link.is('.site-name'))
  783. return false;
  784. // Audio links
  785. // launch audio player and stop here
  786. if($link.is('.audio-link')){
  787. // check if caller is lastdocs block
  788. caller = $link.parents('.lastdocs, .lastdocs-home').length ? 'lastdocs' : null;
  789. // check if caller is studio composition
  790. if(!caller){
  791. caller = $link.parents('.composition_ui').length ? 'studio-compo' : null;
  792. }
  793. // open audio player
  794. if(caller == 'studio-compo'){
  795. var index = $link.parents('.field__item').index();
  796. console.log('audio link caller', caller, index);
  797. // _compoPlayer.setIndex(index);
  798. _compoPlayer.start(index);
  799. }else{
  800. _audioPlayer
  801. .emmit('stop-shuffle')
  802. .openDocument({
  803. nid:$link.attr('nid'),
  804. audio_url:$link.attr('audio_url'),
  805. title:$link.find('.field--name-title').html()
  806. },
  807. caller);
  808. }
  809. return false;
  810. }
  811. // other sounds playing
  812. if($link.is('[type^="audio"]')){
  813. _audioPlayer
  814. .emmit('stop-shuffle')
  815. .openSound($link.attr('href'), $link.html());
  816. return false;
  817. }
  818. // NOT USED ( YET ? )
  819. // if($link.is('.language-link')){
  820. // TODO: change global site current language
  821. // load all blocks translated (exepted language switcher) ... :/
  822. // refreshAllBlocks();
  823. // TODO: use a promise to initAjaxLinks again after blocks updated
  824. // then let load the content
  825. // }
  826. // other links
  827. var sys_path = $(this).attr('data-drupal-link-system-path');
  828. // front page
  829. // just remove contents and stop here
  830. if(sys_path == '<front>'){
  831. if(!_is_mobile){
  832. if($link.is('.is-active') && _corpus_ready){
  833. _$corpus_canvas.trigger({'type':'shuffle-collection'});
  834. }else{
  835. backToFrontPage();
  836. }
  837. return false;
  838. }
  839. // else{
  840. // sys_path = edlp_mobile.mobile_home_path.replace(/^\//, '');
  841. // }
  842. }
  843. var view_mode = $link.attr('viewmode');
  844. var state = getSysPathState(sys_path, view_mode);
  845. state.url = $(this).attr('href');
  846. if(view_mode){
  847. state.url += "#"+view_mode;
  848. }
  849. // get context for navigation purpose
  850. // var contexts = state.url.match(/^.*\/(productions)\/.*/g);
  851. var contexts = /^.*\/(productions|documents|entrees|search)(\/.*)?/g.exec(state.url);
  852. console.log("onClickAjaxLink contexts", contexts);
  853. if(contexts){
  854. switch (contexts[1]) {
  855. case 'productions':
  856. // console.log("onClickAjaxLink is production");
  857. // if (typeof contexts[2] !== 'undefined') {
  858. state.context = "production"
  859. // }else{
  860. // state.context = "production-home"
  861. // }
  862. break;
  863. case 'entrees':
  864. switch(view_mode) {
  865. case 'index':
  866. state.context = "entree-index";
  867. break;
  868. case 'notice':
  869. state.context = "entree-notice";
  870. break;
  871. }
  872. break;
  873. case 'search':
  874. state.context = "search";
  875. break;
  876. case 'documents':
  877. // console.log("onClickAjaxLink is production");
  878. switch (view_mode) {
  879. case 'transcript':
  880. state.context = "document-transcript";
  881. break;
  882. case 'article':
  883. state.context = "document-article";
  884. break;
  885. default:
  886. state.context = "document";
  887. }
  888. // get the caller from pecedent context
  889. if(typeof _states_history[0] != "undefined"){
  890. if(_states_history[0].caller){
  891. state.caller = _states_history[0].caller
  892. }else{
  893. state.caller = _states_history[0].context
  894. }
  895. }else{
  896. state.caller = null;
  897. }
  898. break;
  899. }
  900. }else{
  901. state.context = null;
  902. // state.context = caller;
  903. }
  904. if($link.is('[selector]')){
  905. state.selector = $link.attr('selector');
  906. }
  907. $link.addClass('ajax-loading');
  908. // ajaxLoadContent(url, sys_path, state.ajax_path, selector);
  909. ajaxLoadContent(state);
  910. return false;
  911. };
  912. function refreshFavoritesLinks(){
  913. console.log("refreshFavoritesLinks", _states_history);
  914. // reload the active audioPlayer cartel
  915. _audioPlayer.reloadNode();
  916. // current content (index)
  917. if(_states_history[0].view_mode == 'index' || _states_history[0].sys_path == "docsindex"){
  918. ajaxLoadContent(_states_history[0]);
  919. }
  920. }
  921. // ___
  922. // / __|___ _ _ _ __ _ _ ___
  923. // | (__/ _ \ '_| '_ \ || (_-<
  924. // \___\___/_| | .__/\_,_/__/
  925. // |_|
  926. function onCorpusMapReady(e){
  927. //console.log('theme : onCorpusReady', e);
  928. _corpus_ready = true;
  929. _$corpus_canvas = $('canvas#corpus-map');
  930. _$corpus_canvas
  931. .on('corpus-cliked-on-map', function(e) {
  932. //console.log('theme : corpus-cliked-on-map');
  933. backToFrontPage();
  934. })
  935. .on('corpus-cliked-on-node', function(e) {
  936. //console.log('theme : corpus-cliked-on-node', e);
  937. _audioPlayer
  938. .emmit('stop-shuffle')
  939. // .setAutoOpenArticle(e.article)
  940. .openDocument(e.target_node);
  941. });
  942. _randomPlayer = new RandomPlayer(e.playlist);
  943. if (_is_expo) {
  944. initExpo();
  945. }
  946. // mainly for articles link
  947. initAjaxLinks();
  948. _$body.attr('corpus-map', 'ready');
  949. }
  950. function openEntree(tid){
  951. if(tid){
  952. closeAllModals();
  953. _$body.removeClass();//.addClass(body_classes.join(' '));
  954. // open entree
  955. if(_corpus_ready){
  956. _$corpus_canvas.trigger({
  957. type:'open-entree',
  958. tid:tid
  959. });
  960. }else{
  961. // else : EdlpCorpus will check when ready if entry item (notice or index) is already .is-active
  962. $('li.entree[tid="'+tid+'"] a.term-link').addClass('is-active');
  963. }
  964. }
  965. };
  966. function checkVisibleCorpusMapSpace(){
  967. var left_limit = 0, right_limit = 0;
  968. _$row.find('.col').each(function(i,e){
  969. var $col = $(this);
  970. var offset = $col.offset();
  971. console.log('$col', $col);
  972. switch(true){
  973. case $col.is('.float-right'):
  974. right_limit = Math.max(right_limit, Math.abs(offset.left-15-window.innerWidth));
  975. break;
  976. default:
  977. left_limit = Math.max(left_limit, offset.left+$col.width()+15);
  978. break;
  979. }
  980. });
  981. console.log('checkVisibleCorpusMapSpace left_limit:'+left_limit+" right_limit:"+right_limit);
  982. if(_corpus_ready){
  983. _$body.trigger({
  984. type:'visible-space-changed',
  985. left_limit:left_limit,
  986. right_limit:right_limit
  987. });
  988. }else{
  989. _corpus_promise.done(function(){
  990. _$body.trigger({
  991. type:'visible-space-changed',
  992. left_limit:left_limit,
  993. right_limit:right_limit
  994. });
  995. });
  996. }
  997. };
  998. // _ _ _
  999. // /_\ _ _ __| (_)___
  1000. // / _ \ || / _` | / _ \
  1001. // /_/ \_\_,_\__,_|_\___/
  1002. //
  1003. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  1004. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  1005. // https://www.binarytides.com/using-html5-audio-element-javascript/
  1006. function AudioPlayer(){
  1007. var that = this;
  1008. this.fid;
  1009. this.audio = new Audio();
  1010. // audio events
  1011. this.audio_events = ["loadedmetadata","playing","pause","timeupdate","ended","error"]; // ,"canplay"
  1012. // UI dom objects
  1013. this.$container = $('<div id="audio-player">');
  1014. // btns
  1015. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  1016. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  1017. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  1018. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  1019. // timeline
  1020. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  1021. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  1022. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  1023. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  1024. // time
  1025. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  1026. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  1027. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  1028. // favoris
  1029. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  1030. // cartel
  1031. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  1032. this.scndCartel_visible = 0;
  1033. this.cartelSwitchIntervalMS = 7000;
  1034. this.cartelSwitchInterval = false;
  1035. // hiding
  1036. this.visible = false;
  1037. this.hideTimer = false;
  1038. this.hideTimeMS = 15000;
  1039. // history
  1040. this.currentHistoricIndex = null;
  1041. this.historic = [];
  1042. this.shuffle_is_active = false;
  1043. // artciles
  1044. this.auto_open_article = false;
  1045. // object events
  1046. this.event_handlers = {
  1047. 'audio-open-document':[],
  1048. 'audio-play':[],
  1049. 'audio-pause':[],
  1050. 'audio-play-next':[],
  1051. 'audio-ended':[],
  1052. 'stop-shuffle':[]
  1053. };
  1054. this.init();
  1055. };
  1056. AudioPlayer.prototype = {
  1057. init(){
  1058. if(!_is_expo){
  1059. this.$container_parent = $('header[role="banner"] .region-header');
  1060. }else{
  1061. this.$container_parent = $('main[role="main"]');
  1062. }
  1063. // append ui to document
  1064. this.$container.appendTo(this.$container_parent);
  1065. // record timeline width
  1066. this.timeline_w = parseInt(this.$timeline.width());
  1067. // init seeking
  1068. this.$loader.on('click', this.seek.bind(this));
  1069. // init audio events
  1070. var fn = '';
  1071. for (var i = 0; i < this.audio_events.length; i++) {
  1072. fn = this.audio_events[i];
  1073. // capitalize first letter of event (only cosmetic :p )
  1074. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  1075. this.audio.addEventListener(
  1076. this.audio_events[i],
  1077. this[fn].bind(this),
  1078. true);
  1079. }
  1080. // init btns events
  1081. this.$previous.on('click', this.playPrevious.bind(this));
  1082. this.$playpause.on('click', this.togglePlayPause.bind(this));
  1083. this.$next.on('click', this.playNext.bind(this));
  1084. },
  1085. openDocument(node, caller, historic_index){
  1086. console.log('AudioPlayer openDocument', node, caller);
  1087. if(typeof node == 'undefined'
  1088. || typeof node.nid == 'undefined'
  1089. || typeof node.audio_url == 'undefined'){
  1090. console.warn('AudioPlayer openDocument() node is malformed', node);
  1091. return false;
  1092. }
  1093. // if we don't come from history popstate
  1094. if(typeof caller == 'undefined' || caller != 'popstate'){
  1095. console.log('');
  1096. this.historic.push(node);
  1097. this.currentHistoricIndex = this.historic.length-1;
  1098. // this.shuffle_mode = shuffle_mode || false;
  1099. // add the document opening to history to be able to share and play audio from any where
  1100. if(caller != "history_first_load"){
  1101. if(typeof node.document_url == 'undefined'){
  1102. console.warn("AudioPlayer openDocument missing document_url, can't push state to history", caller, node);
  1103. }else{
  1104. var state = {
  1105. audio:true,
  1106. node:{
  1107. nid:node.nid,
  1108. audio_url:node.audio_url,
  1109. document_url:node.document_url,
  1110. title:node.title || null,
  1111. },
  1112. historic_index : this.currentHistoricIndex,
  1113. };
  1114. // console.log('AudioPlayer pushstate', state);
  1115. var url = node.document_url + (caller == 'random' ? '#random' : '');
  1116. history.pushState(state, null, url);
  1117. }
  1118. }
  1119. }else{
  1120. // if the call commes from popstate, we update the current position of index
  1121. this.currentHistoricIndex = historic_index;
  1122. }
  1123. if(_$body.is('.path-frontpage') && caller !== 'lastdocs' && !_is_mobile){
  1124. closeAllModals();
  1125. }
  1126. // TODO: update language switcher for document url
  1127. // TODO: stop random player if playing from studio
  1128. this.emmit('audio-open-document', {caller:caller});
  1129. // piwik
  1130. // debugger;
  1131. if(typeof _paq !== 'undefined'){
  1132. // page tracking
  1133. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  1134. // _paq.push(['setCustomUrl', state.url]);
  1135. // _paq.push(['setDocumentTitle', data.title]);
  1136. // _paq.push(['trackPageView']);
  1137. // js event
  1138. // trackEvent(category, action, [name], [value])
  1139. if(typeof node.title != 'undefined'){
  1140. _paq.push(['trackEvent', 'Audio', 'play', node.title]);
  1141. }
  1142. }
  1143. this.launch();
  1144. },
  1145. launch(){
  1146. this.clearTimeOutToHide();
  1147. this.clearIntervalAutoCartelSwitch();
  1148. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  1149. if(!_is_mobile){
  1150. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  1151. }
  1152. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  1153. try {
  1154. _$corpus_canvas.trigger({
  1155. 'type':'audio-node-opened',
  1156. 'nid':this.historic[this.currentHistoricIndex].nid
  1157. });
  1158. } catch (e) {
  1159. console.info('AudioPlayer : _$corpus_canvas does not exists, testing promise');
  1160. var that = this;
  1161. _corpus_promise.done(function(){
  1162. _$corpus_canvas.trigger({
  1163. 'type':'audio-node-opened',
  1164. 'nid':that.historic[that.currentHistoricIndex].nid
  1165. });
  1166. });
  1167. }
  1168. this.showHidePreviousBtn();
  1169. this.showHideNextBtn();
  1170. this.show();
  1171. },
  1172. openSound(url, title){
  1173. this.hide();
  1174. this.clearTimeOutToHide();
  1175. this.$cartel.html("");
  1176. this.setSRC(url);
  1177. this.show();
  1178. if(typeof _paq !== 'undefined'){
  1179. _paq.push(['trackEvent', 'Audio', 'play', url]);
  1180. }
  1181. },
  1182. // audio functions
  1183. setSRC(url){
  1184. console.log('AudioPlayer setSRC : url', url);
  1185. this.audio.src = url;
  1186. // need to trigger play right now to prevent safari blocking auto-play
  1187. this.play();
  1188. },
  1189. // audio is ready to play
  1190. onLoadedmetadata(){
  1191. var rem = parseInt(this.audio.duration, 10),
  1192. mins = Math.floor(rem/60,10),
  1193. secs = rem - mins*60;
  1194. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  1195. this.updateLoadingBar();
  1196. },
  1197. updateLoadingBar(){
  1198. console.log("updateLoadingBar");
  1199. // var that = this;
  1200. // if (this.audio.readyState === 4){
  1201. if(this.audio.buffered.length>0){
  1202. // debugger;
  1203. this.$loader.css({
  1204. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  1205. });
  1206. if( this.audio.buffered.end(0) < this.audio.duration ){
  1207. // loop through this function until file is fully loaded
  1208. window.requestAnimationFrame(this.updateLoadingBar.bind(this));
  1209. }else{
  1210. console.log('Audio fully loaded');
  1211. }
  1212. }else{
  1213. // as audio buffer is not ready, recall updateLoadingBar in next frame
  1214. window.requestAnimationFrame(this.updateLoadingBar.bind(this));
  1215. }
  1216. },
  1217. // onCanplay(){
  1218. // // this.play();
  1219. // },
  1220. onError(){
  1221. console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message);
  1222. },
  1223. play(){
  1224. this.clearTimeOutToHide();
  1225. // this.audio.play();
  1226. // making sound shorter for debuging purpose
  1227. // if(_is_expo){
  1228. // setTimeout(this.seekToEnd.bind(this), 20000);
  1229. // }
  1230. // 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
  1231. var promise = this.audio.play();
  1232. // console.log('promise', promise);
  1233. if (promise !== undefined) {
  1234. promise.catch(function(error){
  1235. // Auto-play was prevented
  1236. // Show a UI element to let the user manually start playback
  1237. console.warn('autoplay failed, ask for playing', error);
  1238. }).then(function(){
  1239. // Auto-play started
  1240. console.log('autoplay succeed');
  1241. });
  1242. }
  1243. },
  1244. playPrevious(){
  1245. if(this.currentHistoricIndex > 0){
  1246. this.currentHistoricIndex -= 1;
  1247. this.launch();
  1248. // TODO: update history state
  1249. }
  1250. },
  1251. playNext(){
  1252. if(this.currentHistoricIndex < this.historic.length-1){
  1253. this.currentHistoricIndex += 1;
  1254. this.launch();
  1255. // TODO: update history state
  1256. }else{
  1257. this.emmit('audio-play-next');
  1258. }
  1259. },
  1260. togglePlayPause(e){
  1261. if(this.audio.paused){
  1262. // this.audio.play();
  1263. this.play();
  1264. }else{
  1265. // this.audio.pause();
  1266. this.stop();
  1267. }
  1268. },
  1269. stop(){
  1270. // console.log('AudioPlayer stop()');
  1271. // debugger;
  1272. this.audio.pause();
  1273. // don't close player if article or transcript is open
  1274. if(!(_$body.is('.path-node-'+this.historic[this.currentHistoricIndex].nid)
  1275. && (_$body.is('.view-mode-article') || _$body.is('.view-mode-transcript')))){
  1276. this.timeOutToHide();
  1277. }
  1278. },
  1279. seek(e){
  1280. var seek = e.originalEvent.layerX / this.timeline_w * this.audio.duration
  1281. console.log("Audio seeking : seek", seek);
  1282. this.audio.currentTime = seek;
  1283. },
  1284. seekToEnd(){
  1285. if (this.audio.duration > 40) {
  1286. console.log("seekToEnd", this.audio.duration);
  1287. this.audio.currentTime = this.audio.duration - 20;
  1288. }
  1289. },
  1290. // audio events
  1291. onPlaying(){
  1292. this.$btns.addClass('is-playing');
  1293. this.emmit('audio-play');
  1294. },
  1295. onPause(){
  1296. this.$btns.removeClass('is-playing');
  1297. this.emmit('audio-pause');
  1298. },
  1299. onTimeupdate(){
  1300. // move cursor
  1301. this.$cursor.css({
  1302. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  1303. });
  1304. // update time text display
  1305. var rem = parseInt(this.audio.currentTime, 10),
  1306. mins = Math.floor(rem/60,10),
  1307. secs = rem - mins*60;
  1308. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  1309. },
  1310. onEnded(){
  1311. console.log('AudioPlayer onEnded()');
  1312. this.stop();
  1313. this.emmit('audio-ended');
  1314. },
  1315. // cartel functions
  1316. reloadNode(){
  1317. if(this.visible){
  1318. if(!_is_mobile){
  1319. console.log('reloadNode (cartel)');
  1320. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  1321. }
  1322. }
  1323. },
  1324. loadNode(nid){
  1325. this.$cartel.addClass('loading');
  1326. var vm = 'player_cartel';
  1327. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  1328. var path = _origin + Drupal.url(ajax_path);
  1329. $.getJSON(path, {})
  1330. .done(this.onNodeLoaded.bind(this))
  1331. .fail(this.onNodeLoadFail.bind(this));
  1332. },
  1333. onNodeLoaded(data){
  1334. console.log('AudioPlayer node loaded');//, data);
  1335. this.$cartel.html(data.rendered).removeClass('loading');
  1336. _$body.trigger({'type':'new-audio-cartel-loaded'});
  1337. initAjaxLinks();
  1338. // cartel autoswitch
  1339. this.scndCartel_visible = 0;
  1340. this.$cartel.removeClass('second-visible');
  1341. if(!_is_expo){
  1342. // if second cartel has special info ?
  1343. if (this.$cartel.find('.second-cartel .col-left').children().length > 1
  1344. || this.$cartel.find('.second-cartel').children('.col-right').length){
  1345. // init cartel auto switch
  1346. this.cartelSwitchInterval = setInterval(this.switchCartel.bind(this), this.cartelSwitchIntervalMS);
  1347. }
  1348. // call drupal behaviours (for addtoany)
  1349. Drupal.attachBehaviors(this.$cartel);
  1350. }
  1351. // open automaticly tha article if needed
  1352. this.setAutoOpenArticle();
  1353. if(this.auto_open_article){
  1354. this.$cartel.find('a.link-article').trigger('click');
  1355. this.auto_open_article = false;
  1356. }
  1357. },
  1358. onNodeLoadFail(jqxhr, textStatus, error){
  1359. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  1360. this.$cartel.removeClass('loading').html('');
  1361. },
  1362. setAutoOpenArticle(art){
  1363. this.auto_open_article = $('a.articles-link').is('.is-active');
  1364. return this;
  1365. },
  1366. // global
  1367. show(){
  1368. this.visible = true;
  1369. this.$container_parent.addClass('audio-player-visible');
  1370. this.$container.addClass('visible');
  1371. },
  1372. showHidePreviousBtn(){
  1373. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  1374. this.$previous.addClass('is-active');
  1375. }else{
  1376. this.$previous.removeClass('is-active');
  1377. }
  1378. },
  1379. showHideNextBtn(){
  1380. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  1381. this.$next.addClass('is-active');
  1382. }else{
  1383. this.$next.removeClass('is-active');
  1384. }
  1385. },
  1386. timeOutToHide(){
  1387. // console.log('AudioPlayer timeOutToHide()');
  1388. this.clearTimeOutToHide();
  1389. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  1390. },
  1391. clearTimeOutToHide(){
  1392. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  1393. if(this.hideTimer){
  1394. clearTimeout(this.hideTimer);
  1395. this.hideTimer = false;
  1396. }
  1397. },
  1398. // cartel
  1399. switchCartel(){
  1400. // console.log("switchCartel", this.scndCartel_visible);
  1401. if (this.scndCartel_visible) {
  1402. // return to first cartel
  1403. this.$cartel.removeClass('second-visible');
  1404. this.scndCartel_visible = 0;
  1405. // kill the auto switch after one rotation
  1406. this.clearIntervalAutoCartelSwitch();
  1407. }else{
  1408. // switch to 2nd cartel
  1409. this.$cartel.addClass('second-visible');
  1410. this.scndCartel_visible = 1;
  1411. }
  1412. },
  1413. clearIntervalAutoCartelSwitch(){
  1414. if(this.cartelSwitchInterval){
  1415. clearInterval(this.cartelSwitchInterval);
  1416. this.cartelSwitchInterval = false;
  1417. }
  1418. },
  1419. hide(){
  1420. // console.log('AudioPlayer hide()');
  1421. this.visible = false;
  1422. this.$container_parent.removeClass('audio-player-visible');
  1423. this.$container.removeClass('visible');
  1424. // trigger highlighted node remove on corpus map
  1425. try {
  1426. _$corpus_canvas.trigger('audio-node-closed');
  1427. } catch (e) {
  1428. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  1429. }
  1430. },
  1431. deActivateRandom(){
  1432. this.shuffle_is_active = false;
  1433. this.showHideNextBtn();
  1434. },
  1435. // object events
  1436. on(event_name, handler){
  1437. if(typeof this.event_handlers[event_name] == 'undefined'){
  1438. console.warn('AudioPlayer : event '+event_name+' does not exists');
  1439. }
  1440. this.event_handlers[event_name].push(handler);
  1441. return this;
  1442. },
  1443. emmit(event_name, args){
  1444. console.log('AudioPlayer emmit()', event_name, args, this.event_handlers[event_name]);
  1445. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  1446. var handler;
  1447. var args = args || {};
  1448. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  1449. handler = this.event_handlers[event_name][i];
  1450. // console.log('AudioPlayer emmit() loop handler', handler);
  1451. (function(handler, args){
  1452. setTimeout(function(){
  1453. // console.log('AudioPlayer emmit() timeout handler', handler);
  1454. handler(args);
  1455. }, 0);
  1456. }(handler, args));
  1457. }
  1458. return this;
  1459. },
  1460. }
  1461. // ___ _ ___ _
  1462. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  1463. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  1464. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  1465. // |__/
  1466. function RandomPlayer(playlist){
  1467. this.active = false;
  1468. this.playlist = playlist;
  1469. this.path = _base_url+drupalSettings.path.pathPrefix+'#random'
  1470. this.$btn = $('<a>')
  1471. .html('Shuffle')
  1472. .attr('href',this.path)
  1473. .attr('alt', drupalSettings.edlp_corpus.random_link_title)
  1474. .addClass('random-player-btn');
  1475. this.init();
  1476. };
  1477. RandomPlayer.prototype = {
  1478. init(){
  1479. // this.shuffle();
  1480. $('<div>')
  1481. .addClass('block random-player')
  1482. .append(this.$btn)
  1483. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  1484. .prependTo('.region-footer-right');
  1485. // events
  1486. this.$btn.on('click', this.toggleActive.bind(this));
  1487. // attach an event on AudioPlayer
  1488. _audioPlayer
  1489. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  1490. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  1491. .on('stop-shuffle', this.stop.bind(this));
  1492. _$corpus_canvas
  1493. .on('update-random-playlist', this.updatePlaylist.bind(this));
  1494. // provide a link to auto start random player
  1495. if(window.location.hash == '#random'){
  1496. this.start();
  1497. }
  1498. },
  1499. updatePlaylist(e){
  1500. if (!_is_expo) {
  1501. this.playlist = e.playlist;
  1502. this.shuffle();
  1503. }
  1504. },
  1505. shuffle(){
  1506. var tempPLaylist = [];
  1507. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  1508. tempPLaylist.push(this.playlist[i]);
  1509. }
  1510. this.shuffledPlaylist = [];
  1511. while(tempPLaylist.length > 0){
  1512. var r = Math.floor(Math.random() * tempPLaylist.length);
  1513. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  1514. }
  1515. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  1516. },
  1517. toggleActive(e){
  1518. e.preventDefault();
  1519. if (this.active) {
  1520. this.stop();
  1521. }else{
  1522. this.start();
  1523. }
  1524. return false;
  1525. },
  1526. start(){
  1527. this.active = _audioPlayer.shuffle_is_active = true;
  1528. this.$btn.addClass('is-active');
  1529. this.shuffle();
  1530. this.next();
  1531. // piwik
  1532. if(typeof _paq !== 'undefined'){
  1533. // trackEvent(category, action, [name], [value])
  1534. _paq.push(['trackEvent', 'RandomPlayer', 'start']);
  1535. }
  1536. },
  1537. stop(){
  1538. this.active = false;
  1539. this.$btn.removeClass('is-active');
  1540. // stop audio player
  1541. // _audioPlayer.stop();
  1542. // remove hash
  1543. history.replaceState(history.state, null, window.location.pathname);
  1544. // tell to AudioPlayer to deactivate netx btn
  1545. _audioPlayer.deActivateRandom();
  1546. if(typeof _paq !== 'undefined'){
  1547. // trackEvent(category, action, [name], [value])
  1548. _paq.push(['trackEvent', 'RandomPlayer', 'stop']);
  1549. }
  1550. },
  1551. next(){
  1552. console.log('RandomPlayer next', this.active, this.shuffledPlaylist.length);
  1553. if(this.active){
  1554. if (this.shuffledPlaylist.length == 0) {
  1555. this.shuffle();
  1556. }
  1557. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0], 'random');
  1558. }
  1559. },
  1560. onAudioPlayNext(){
  1561. console.log('RandomPlayer : onAudioPlayNext()');
  1562. if(this.active){
  1563. this.next();
  1564. }
  1565. },
  1566. onAudioPlayerEnded(){
  1567. console.log('RandomPlayer : onAudioPlayerEnded()');
  1568. if(this.active){
  1569. this.next();
  1570. if(_is_expo){
  1571. _$corpus_canvas.trigger({'type':'scramble-collection'}); //shuffle-collection scramble-collection
  1572. }
  1573. }
  1574. }
  1575. };
  1576. // ___ ___ _
  1577. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  1578. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  1579. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  1580. // |_| |__/
  1581. function CompoPlayer(){
  1582. this.active = false;
  1583. this.playing = false;
  1584. this.paused = false;
  1585. this.playlist = [];
  1586. this.current_index = 0;
  1587. this.$composer = null;
  1588. this.$compo = null;
  1589. this.$controls = null;
  1590. this.init();
  1591. };
  1592. CompoPlayer.prototype = {
  1593. init(){
  1594. // console.log('CompoPlayer init()');
  1595. // attach an event on AudioPlayer
  1596. _audioPlayer
  1597. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  1598. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  1599. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  1600. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  1601. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  1602. // this.newCompo();
  1603. },
  1604. newCompo(){
  1605. console.log('CompoPlayer newCompo()');
  1606. // this.$compo = $('.composition_ui .composer .composition');
  1607. this.initControls();
  1608. },
  1609. initControls(){
  1610. //console.log('CompoPlayer initControls()');
  1611. this.$composer = $('.composition_ui .composer');
  1612. this.$compo = $('.composition_ui .composer .composition');
  1613. this.$controls = $('.composition_ui .composer .compo-player-controls');
  1614. if(!this.$controls.is('.ready') && this.$compo){
  1615. this.$previous = $('<div>').addClass('previous')
  1616. .on('click', this.prev.bind(this))
  1617. .appendTo(this.$controls);
  1618. this.$playpause = $('<div>').addClass('play-pause')
  1619. .on('click', this.togglePlayPause.bind(this))
  1620. .appendTo(this.$controls);
  1621. this.$next = $('<div>').addClass('next')
  1622. .on('click', this.next.bind(this))
  1623. .appendTo(this.$controls);
  1624. this.$controls.addClass('ready');
  1625. this.active = true;
  1626. // this.newCompo();
  1627. }
  1628. this.refresh();
  1629. },
  1630. refresh(){
  1631. // console.log('CompoPlayer refresh(), this', this);
  1632. this.stop();
  1633. // load new playlist
  1634. this.playlist = [];
  1635. var that = this;
  1636. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  1637. var $link = $('a.audio-link',this);
  1638. that.playlist.push({
  1639. item:$(this),
  1640. audio_url:$link.attr("audio_url"),
  1641. nid:$link.attr("nid"),
  1642. });
  1643. });
  1644. this.showHideControls();
  1645. },
  1646. togglePlayPause(){
  1647. // console.log('CompoPlayer togglePlayPause');
  1648. if (this.playing && !this.paused) {
  1649. this.pause();
  1650. }else{
  1651. if(this.playing && this.paused){
  1652. this.play();
  1653. }else{
  1654. this.start();
  1655. }
  1656. }
  1657. },
  1658. start(i){
  1659. //console.log('start');
  1660. // console.log('CompoPlayer start()');
  1661. this.current_index = i || 0;
  1662. this.playing = true;
  1663. this.play();
  1664. },
  1665. play(){
  1666. console.log('CompoPlayer play()', this.playlist[this.current_index]);
  1667. if(this.paused){
  1668. this.paused = false;
  1669. _audioPlayer.play();
  1670. }else{
  1671. console.log('CompoPlayer opendoc with audio player');
  1672. // _audioPlayer;
  1673. _audioPlayer.emmit('stop-shuffle').openDocument(this.playlist[this.current_index], this);
  1674. }
  1675. this.setActiveItem().showHideControls();
  1676. },
  1677. pause(){
  1678. //console.log('pause');
  1679. this.paused = true;
  1680. this.showHideControls();
  1681. _audioPlayer.stop();
  1682. },
  1683. next(){
  1684. console.log('CompoPlayer next()',this.playing);
  1685. if(this.playing){
  1686. this.current_index += 1;
  1687. if(this.current_index < this.playlist.length){
  1688. this.play();
  1689. }else{
  1690. this.stop();
  1691. }
  1692. }
  1693. },
  1694. prev(){
  1695. // console.log('CompoPlayer prev()');
  1696. if(this.playing){
  1697. this.current_index -= 1;
  1698. if(this.current_index >= 0){
  1699. this.play();
  1700. }else{
  1701. this.stop();
  1702. }
  1703. }
  1704. },
  1705. stop(){
  1706. if(this.playing){
  1707. _audioPlayer.stop();
  1708. }
  1709. this.reset();
  1710. },
  1711. reset(){
  1712. this.playing = false;
  1713. this.paused = false;
  1714. this.resetIndex();
  1715. },
  1716. resetIndex(){
  1717. this.current_index = 0;
  1718. this.showHideControls().resetActiveItems();
  1719. },
  1720. setActiveItem(){
  1721. this.resetActiveItems();
  1722. if(this.playing && this.current_index >= 0){
  1723. this.playlist[this.current_index].item.addClass('is-active');
  1724. }
  1725. // this call shoud not be here
  1726. this.showHideControls();
  1727. return this;
  1728. },
  1729. resetActiveItems(){
  1730. for (var n = 0; n < this.playlist.length; n++) {
  1731. // console.log('node',node);
  1732. this.playlist[n].item.removeClass('is-active');
  1733. }
  1734. return this;
  1735. },
  1736. showHideControls(){
  1737. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  1738. // global playing
  1739. if(this.$controls){
  1740. if(this.playing && !this.paused){
  1741. this.$controls.addClass('is-playing');
  1742. }else{
  1743. this.$controls.removeClass('is-playing');
  1744. }
  1745. }
  1746. // playpause
  1747. if(this.$playpause){
  1748. if(this.playlist.length > 0){
  1749. this.$playpause.addClass('is-active');
  1750. }else{
  1751. this.$playpause.removeClass('is-active');
  1752. }
  1753. }
  1754. // next
  1755. if(this.$next){
  1756. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  1757. this.$next.addClass('is-active');
  1758. }else{
  1759. this.$next.removeClass('is-active');
  1760. }
  1761. }
  1762. // previous
  1763. if(this.$previous){
  1764. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  1765. this.$previous.addClass('is-active');
  1766. }else{
  1767. this.$previous.removeClass('is-active');
  1768. }
  1769. }
  1770. return this;
  1771. },
  1772. deactivate(){
  1773. this.stop();
  1774. this.active = false;
  1775. },
  1776. // _audioPlayer events
  1777. onAudioOpenDocument(args){
  1778. console.log('Compo player onAudioOpenDocument', args);
  1779. if(args.caller !== this){
  1780. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  1781. this.reset();
  1782. }
  1783. // else{
  1784. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  1785. // }
  1786. },
  1787. onAudioPlayerPlay(){
  1788. if(this.playing && this.paused){
  1789. this.paused = false;
  1790. this.showHideControls();
  1791. }
  1792. },
  1793. onAudioPlayerPause(){
  1794. if(this.playing && !this.paused){
  1795. this.paused = true;
  1796. this.showHideControls();
  1797. }
  1798. },
  1799. onAudioPlayerEnded(){
  1800. console.log('onAudioPlayerEnded');
  1801. if(this.playing){
  1802. // this paused shoudn't be true but i'm lazzy to find why it is ...
  1803. this.paused = false;
  1804. this.next();
  1805. }
  1806. },
  1807. // onAudioPlayNext(){
  1808. // this.next();
  1809. // }
  1810. };
  1811. // ___
  1812. // | __|_ ___ __ ___
  1813. // | _|\ \ / '_ \/ _ \
  1814. // |___/_\_\ .__/\___/
  1815. // |_|
  1816. function initExpo(){
  1817. console.log('initExpo', window.location.hash);
  1818. if(window.location.hash == '#black'){
  1819. $('body').addClass('expo-black');
  1820. }
  1821. _randomPlayer.start();
  1822. }
  1823. // ___ _ _
  1824. // / __|_ _(_)__| |
  1825. // | (_ | '_| / _` |
  1826. // \___|_| |_\__,_|
  1827. function initGrid(){
  1828. console.log('theme : initGrid');
  1829. if(!_is_mobile){
  1830. checkGridBlockHeight();
  1831. _$row.find('.col').addClass('offfield');
  1832. }
  1833. if(false){
  1834. var $grid = $('.grid',_$row).masonry({
  1835. itemSelector:'.col',
  1836. columnWidth:'.col-2',
  1837. containerStyle: null,
  1838. resizeContainer:false,
  1839. // horizontalOrder: true,
  1840. transitionDuration:0,//'0.2s',
  1841. // stagger:30,
  1842. // disable initial layout
  1843. // initLayout: false,
  1844. });
  1845. // layout Masonry after each image loads
  1846. $grid.imagesLoaded().progress( function() {
  1847. $grid.masonry('layout');
  1848. });
  1849. $grid.imagesLoaded(function(){
  1850. $grid.masonry('layout');
  1851. // checkProductionBlockVisible();
  1852. });
  1853. // bind event
  1854. // $grid.masonry( 'on', 'layoutComplete', function() {
  1855. // console.log('layout is complete');
  1856. // });
  1857. $grid.on('layoutComplete', checkGridBlockVisible);
  1858. }else{
  1859. // setTimeout(checkGridBlockVisible, 100);
  1860. if(!_is_mobile){
  1861. checkGridBlockVisible();
  1862. }
  1863. }
  1864. };
  1865. function checkGridBlockHeight(){
  1866. // console.log('checkGridBlockHeight');
  1867. // if(_is_mobile) return;
  1868. var $r_h = _$row.height();
  1869. var $this;
  1870. // console.log($r_h);
  1871. $('.grid .col', _$row).each(function(i,e){
  1872. $this = $(this);
  1873. if(!$this.is('[init-height]')){
  1874. $this.attr('init-height', $this.outerHeight())
  1875. }
  1876. // console.log($(this).height(), $(this).innerHeight(), $(this).outerHeight());
  1877. // if($this.height() > $r_h){
  1878. // $this.height($r_h);
  1879. // }else{
  1880. // $this.height('auto');
  1881. // }
  1882. $this.height(Math.min($this.attr('init-height'), _$row.height()));
  1883. });
  1884. };
  1885. function checkGridBlockVisible(){
  1886. // console.log('checkGridBlockVisible');
  1887. // if(_is_mobile) return;
  1888. var $r_h = _$row.height();
  1889. var $this,pos;
  1890. $('.grid .col', _$row).each(function(i,e){
  1891. // $(this).on('load',function(event){
  1892. $this = $(this);
  1893. pos = $this.position();
  1894. // console.log(pos.top, $this.height(), pos.top+$this.height(), $r_h, $this);
  1895. if(pos.top+$this.height() <= $r_h){
  1896. $this.removeClass('offfield');
  1897. }else{
  1898. $this.addClass('offfield');
  1899. }
  1900. // });
  1901. });
  1902. // for (var i in items) {
  1903. // if(!items[i].isTransitioning){
  1904. // console.log('y+outerHeight', items[i].position.y+items[i].size.outerHeight);
  1905. // console.log('_$row.height()', _$row.height());
  1906. // if(items[i].position.y+items[i].size.outerHeight < _$row.height()){
  1907. // $(items[i].element).removeClass('offfield');
  1908. // }else{
  1909. // // $(items[i].element).addClass('offfield');
  1910. // }
  1911. // }
  1912. // }
  1913. }
  1914. // ___ _ _ _
  1915. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  1916. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  1917. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  1918. // function initProductions(){
  1919. // console.log('theme : initProductions');
  1920. //
  1921. // // _$row.find('.col').addClass('offfield');
  1922. // var $grid = $('.grid',_$row).masonry({
  1923. // itemSelector:'.col',
  1924. // columnWidth:'.col-2',
  1925. // containerStyle: null,
  1926. // resizeContainer:false,
  1927. // // horizontalOrder: true,
  1928. // transitionDuration:0,//'0.2s',
  1929. // // stagger:30,
  1930. // // disable initial layout
  1931. // // initLayout: false,
  1932. // });
  1933. //
  1934. // // layout Masonry after each image loads
  1935. // $grid.imagesLoaded().progress( function() {
  1936. // $grid.masonry('layout');
  1937. // });
  1938. //
  1939. // $grid.imagesLoaded(function(){
  1940. // $grid.masonry('layout');
  1941. // // checkProductionBlockVisible();
  1942. // });
  1943. //
  1944. // // bind event
  1945. // // $grid.masonry( 'on', 'layoutComplete', function() {
  1946. // // console.log('layout is complete');
  1947. // // });
  1948. // // $grid.on('layoutComplete', checkProductionBlockVisible);
  1949. // };
  1950. // ___ _ _ _ _
  1951. // / __|___| | |___ __| |_(_)___ _ _
  1952. // | (__/ _ \ | / -_) _| _| / _ \ ' \
  1953. // \___\___/_|_\___\__|\__|_\___/_||_|
  1954. // mobile version of collection
  1955. function initCollectionNav(){
  1956. console.log('initCollectionNav');
  1957. // taxonomy-term.vocabulary-entrees.home_mobile
  1958. $('.field--name-field-notice, .index', '.taxonomy-term.vocabulary-entrees.home_mobile')
  1959. .addClass('closed');
  1960. $('.field--name-field-notice>.field__label', '.taxonomy-term.vocabulary-entrees.home_mobile')
  1961. .on('click', onClickCollectionNotice);
  1962. $('.index>.field__label', '.taxonomy-term.vocabulary-entrees.home_mobile')
  1963. .on('click', onClickCollectionIndex);
  1964. };
  1965. function onClickCollectionNotice(e){
  1966. // console.log('onClickCollectionNotice');
  1967. // var $part = $(this).parent();//parents('.taxonomy-term');
  1968. toggleEntreeOpening($(this).parent(), 'notice');
  1969. };
  1970. function onClickCollectionIndex(e){
  1971. // console.log('onClickCollectionIndex');
  1972. // var $part = $(this).parent();//parents('.taxonomy-term');
  1973. toggleEntreeOpening($(this).parent(), 'index');
  1974. };
  1975. function toggleEntreeOpening($e, part){
  1976. $e.toggleClass('closed')
  1977. .parents('.taxonomy-term.vocabulary-entrees.home_mobile').toggleClass(part+'-opened');
  1978. }
  1979. // ___ _
  1980. // / __| ___ __ _ _ _ __| |_
  1981. // \__ \/ -_) _` | '_/ _| ' \
  1982. // |___/\___\__,_|_| \__|_||_|
  1983. function initSearch(){
  1984. // if($('#edit-entries input:checked', '#edlp-search-form').length){
  1985. // $('#edit-entries--wrapper', '#edlp-search-form').toggleClass('opened');
  1986. // }
  1987. $('#edit-entries--wrapper legend', '#edlp-search-form').on('click', function(){
  1988. $(this).parent().toggleClass('opened');
  1989. });
  1990. };
  1991. // ___ _ _ _
  1992. // | __|_ _ _ _ ___ __ _(_)__| |_ _ _ ___ _ __ ___ _ _| |_
  1993. // | _|| ' \| '_/ -_) _` | (_-< _| '_/ -_) ' \/ -_) ' \ _|
  1994. // |___|_||_|_| \___\__, |_/__/\__|_| \___|_|_|_\___|_||_\__|
  1995. // |___/
  1996. function initEnregistrementTranscript(){
  1997. console.log('initEnregistrementTranscript');
  1998. var $node = _$row.find('article.node--type-enregistrement.node--view-mode-transcript');
  1999. var $nav = $('<nav>').prependTo($node);
  2000. $node.find('.field--name-field-transcript-vo').addClass('visible').find('.field__label')
  2001. .clone().appendTo($nav).addClass('is-active')
  2002. .attr('field_target', '.field--name-field-transcript-vo');
  2003. $node.find('.field--name-field-transcript-trad').find('.field__label')
  2004. .clone().appendTo($nav)
  2005. .attr('field_target', '.field--name-field-transcript-trad');
  2006. $nav.find('.field__label').on('click', function(){
  2007. var $this = $(this).addClass('is-active');
  2008. $this.siblings('.is-active').removeClass('is-active');
  2009. $this.parents('article.node').find('.field.visible').removeClass('visible');
  2010. $this.parents('article.node').find($this.attr('field_target')).addClass('visible');
  2011. });
  2012. };
  2013. // ___ _ ___
  2014. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  2015. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  2016. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  2017. // |___/
  2018. function backToFrontPage(pop_state){
  2019. console.log('backToFrontPage', pop_state);
  2020. closeAllModals();
  2021. // assume we are going back to front page
  2022. $('body').removeClass().addClass('path-frontpage');
  2023. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  2024. // close entrees
  2025. if(_corpus_ready){
  2026. _$corpus_canvas.trigger({'type':'close-all-entree'});
  2027. _$corpus_canvas.trigger({'type':'scramble-collection'});
  2028. }
  2029. if(typeof pop_state == "undefined" || !pop_state){
  2030. console.log('backToFrontPage push state');
  2031. state = {
  2032. home:true,
  2033. context:"home",
  2034. caller:null
  2035. }
  2036. _states_history.unshift(state);
  2037. history.pushState(state, null, _base_url+drupalSettings.path.currentLanguage);
  2038. }
  2039. }
  2040. // function initHome(){
  2041. // addCloseModalBtnToCols();
  2042. // return;
  2043. // console.log('theme : initHome');
  2044. // var $grid = $('.grid',_$row).masonry({
  2045. // itemSelector:'.col',
  2046. // columnWidth:'.col-2',
  2047. // horizontalOrder: true,
  2048. // containerStyle: null,
  2049. // // disable initial layout
  2050. // // initLayout: false,
  2051. // });
  2052. // // bind event
  2053. // // $grid.masonry( 'on', 'layoutComplete', function() {
  2054. // // console.log('layout is complete');
  2055. // // });
  2056. //
  2057. // // layout Masonry after each image loads
  2058. // $grid.imagesLoaded().progress( function() {
  2059. // $grid.masonry('layout');
  2060. // });
  2061. //
  2062. // $grid.imagesLoaded(function(){
  2063. // $grid.masonry('layout');
  2064. // });
  2065. // }
  2066. // __ __ _ _
  2067. // | \/ |___ __| |__ _| |___
  2068. // | |\/| / _ \/ _` / _` | (_-<
  2069. // |_| |_\___/\__,_\__,_|_/__/
  2070. function closeAllModals(){
  2071. //console.log('theme : closeAllModals');
  2072. // TODO: animate the remove
  2073. _$row.html('');
  2074. _$ajaxLinks.removeClass('is-active');
  2075. _$body.trigger({'type':'all-modal-closed'});
  2076. // checkRowEmpty();
  2077. checkVisibleCorpusMapSpace();
  2078. };
  2079. function checkRowEmpty(){
  2080. console.log('checkRowEmpty');
  2081. // if row is empty and we are not in productions or entree notice|index call closeAllModals()
  2082. if(!$('.col', _$row).length){
  2083. if(!_$body.is('.entity-type-taxonomy_term.bundle-entrees')){
  2084. if(!_$body.is('.entity-type-node.bundle-page')){
  2085. // we weren't on production or entree, so go back to front page
  2086. // debugger;
  2087. if(_$body.is('.bundle-enregistrement.view-mode-article')
  2088. && $('a.articles-link').is('.is-active')){
  2089. console.log('Closing article while article index is active');
  2090. // if node article and articles is active, reload index
  2091. $('a.articles-link').removeClass('is-active').trigger('click');
  2092. }else{
  2093. backToFrontPage();
  2094. }
  2095. // if(!$('a.articles-link').is('.is-active')){
  2096. // // don't go back to front page if articles filter is on
  2097. // backToFrontPage();
  2098. // }else{
  2099. // }
  2100. }else{
  2101. // if we were on production page just scramble collection in case of map was filtered
  2102. if(_corpus_ready){
  2103. _$corpus_canvas.trigger({'type':'scramble-collection'});
  2104. }
  2105. // reload production home
  2106. $('a[data-drupal-link-system-path="productions"]', '#block-mainnavigation')
  2107. .removeClass('is-active').trigger('click');
  2108. }
  2109. }else{
  2110. // remove is-active class from index or notice entree links
  2111. $('.entree-content a.is-active').removeClass('is-active');
  2112. // TODO: remove the hash index or notice
  2113. }
  2114. }
  2115. };
  2116. init();
  2117. } // end EdlpTheme()
  2118. $(document).ready(function($) {
  2119. if(drupalSettings.path.isFront){
  2120. var edlptheme = new EdlpTheme();
  2121. }else{
  2122. $('body').attr('booted', 'booted');
  2123. }
  2124. });
  2125. })(jQuery, Drupal, drupalSettings);