main.js 77 KB

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