main.js 76 KB

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