main.js 72 KB

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