main.js 78 KB

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