main.js 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103
  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. // check for theme attribute and emmit event
  524. var $col = $(this).parents('.col');
  525. var theme = $col.attr('theme');
  526. if(theme != ''){
  527. _$body.trigger({'type':theme+'-col-closed'});
  528. }
  529. if(_$body.is('.entity-type-node.bundle-page') && $(this).next().is('.node--type-page')){
  530. // remove all the col (prod page and agenda in case) only if closing the main page col
  531. // if closing the aside col (agenda) leave the main col in place
  532. $col.add($col.siblings('.col')).remove();
  533. }else{
  534. // remove the col
  535. $col.remove();
  536. }
  537. checkRowEmpty();
  538. checkVisibleCorpusMapSpace();
  539. // set links to this modal to not active (tested on articles from audio cartel)
  540. if( $col.attr('view_mode') && $col.attr('sys_path')){
  541. $('a[data-drupal-link-system-path="'+$col.attr('sys_path')+'"][viewmode="'+$col.attr('view_mode')+'"]').removeClass('is-active');
  542. }
  543. };
  544. // _ _ ___ _ _
  545. // /_\ (_)__ ___ __ | _ ) |___ __| |__ ___
  546. // / _ \ | / _` \ \ / | _ \ / _ \/ _| / /(_-<
  547. // /_/ \_\/ \__,_/_\_\ |___/_\___/\__|_\_\/__/
  548. // |__/
  549. // NOT USED (YET)
  550. // function refreshAllBlocks(){
  551. // var path = _origin + Drupal.url(_ajax_settings.blocksjson_path);
  552. // $.getJSON(path, {})
  553. // .done(function(data){
  554. // onAjaxBlockLoaded(data);
  555. // })
  556. // .fail(function(jqxhr, textStatus, error){
  557. // onAjaxBlockLoadError(jqxhr, textStatus, error);
  558. // });
  559. // };
  560. // function onAjaxBlockLoadError(jqxhr, textStatus, error){
  561. // console.warn('ajax block load failed: '+error, jqxhr.responseText);
  562. // };
  563. // function onAjaxBlockLoaded(data){
  564. // console.log('onAjaxBlockLoaded', data);
  565. // // TODO: update each blocks (exepted language switcher)
  566. // for (var blockname in data.blocks) {
  567. // var block = data.blocks[blockname];
  568. // console.log(blockname, block);
  569. // $(block.id).replaceWith(block.rendered);
  570. // }
  571. // };
  572. // _ _ _ _
  573. // | || (_)__| |_ ___ _ _ _ _
  574. // | __ | (_-< _/ _ \ '_| || |
  575. // |_||_|_/__/\__\___/_| \_, |
  576. // |__/
  577. function initHistory(){
  578. initFirstLoad();
  579. window.addEventListener('popstate', onHistoryPopState);
  580. };
  581. function initFirstLoad(){
  582. console.log('theme : initFirstLoad()');
  583. console.log('window.location', window.location);
  584. // var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
  585. var edlp_origin = JSON.parse(window.localStorage.getItem('edlp_origin'));
  586. console.log('edlp_origin', edlp_origin);
  587. if(edlp_origin != null && edlp_origin.sys_path){
  588. // hash is used as viewmode for taxonomy term entrees load (index or notice)
  589. // and for audio contents (article|transcript)
  590. var hash = edlp_origin.hash.replace('#', '');
  591. // create history state
  592. var state = getSysPathState(edlp_origin.sys_path, hash);
  593. // open entree tray in case of entree index|notice
  594. // (index or notice will be opened with ajaxLoadContent)
  595. // refactorized with new infos from edlp_origin
  596. // if(hash){
  597. if(edlp_origin.entity_type == "taxonomy_term"
  598. && edlp_origin.entity_bundle == "entrees"
  599. && hash){
  600. // var $link = $('[href="'+edlp_origin.url+'"][viewmode="'+hash+'"]');
  601. // var selector = $link.attr('selector') || null;
  602. // record the selector in the state for actions after ajaxContentLoaded
  603. state.selector = 'entree-'+hash+'-link-'+edlp_origin.entity_id; // entree-index-link-125
  604. // if(selector){
  605. // in case of entree link (actualy, selector is used only for entries links)
  606. // TODO: use a promise
  607. // TODO: but what if corpus ready before onAjaxLoaded >> use a promise !!
  608. if(_corpus_ready){
  609. _$corpus_canvas.trigger({
  610. type:'open-entree',
  611. // tid:$link.attr('tid')
  612. tid:edlp_origin.entity_id
  613. });
  614. }else{
  615. // else : EdlpCorpus will check when ready if entry item (notice or index) is already .is-active
  616. // .is-active class is added by onAjaxLoaded() (when content is loaded)
  617. // $('li.entree[tid="'+$link.attr('tid')+'"] a.term-link').addClass('is-active');
  618. $('li.entree[tid="'+edlp_origin.entity_id+'"] a.term-link').addClass('is-active');
  619. }
  620. // }
  621. }
  622. // check if audio link
  623. // only if not random link
  624. if(edlp_origin.audio_url){
  625. var node = {
  626. nid:edlp_origin.entity_id,
  627. audio_url:edlp_origin.audio_url
  628. };
  629. _audioPlayer.openDocument(node, 'history_first_load');
  630. // close home
  631. closeAllModals();
  632. if(hash == "" || hash == 'random'){
  633. // if hash is random, RandomPlayer will be started when playlist sent by corpus.js
  634. // if audio only record in state
  635. state.audio = true;
  636. state.node = node;
  637. // we don't laod ajax content so we boot the home
  638. _$body.attr('booted', 'booted');
  639. }else{
  640. // ajax load content for audio only if article or transcript
  641. ajaxLoadContent(state);
  642. }
  643. }
  644. // only if not entree path
  645. // only if not audio (without article or transcript) path
  646. else if(state.ajax_path){
  647. // load content through ajax
  648. // ajaxLoadContent(null, state.sys_path, state.ajax_path, selector);
  649. ajaxLoadContent(state);
  650. }
  651. if(state.entree_tid){
  652. openEntree(state.entree_tid);
  653. }
  654. // record history state
  655. history.replaceState(state, null, edlp_origin.url+edlp_origin.hash);
  656. // reset the storage
  657. window.localStorage.removeItem("edlp_origin");
  658. // window.localStorage.removeItem("edlp_origin_url");
  659. }else{
  660. // if(window.location.hash == "#random"){
  661. //
  662. // }
  663. history.replaceState({home:true}, null, window.location.pathname+window.location.hash);
  664. // initHome();
  665. initGrid();
  666. _$body.attr('booted', 'booted');
  667. }
  668. };
  669. function onHistoryPopState(e){
  670. console.log('onPopState',e.state);
  671. if(e.state.home){
  672. backToFrontPage(true);
  673. }
  674. else if (e.state.audio) {
  675. _audioPlayer.openDocument(e.state.node, 'popstate', e.state.historic_index);
  676. }
  677. else{
  678. if(e.state.entree_tid){
  679. openEntree(e.state.entree_tid);
  680. }
  681. if(e.state.ajax_path){
  682. e.state.url = null;
  683. // ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  684. ajaxLoadContent(e.state);
  685. }
  686. }
  687. };
  688. // _ _ _ _ _
  689. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  690. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  691. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  692. // |__/
  693. function initAjaxLinks(){
  694. // console.log('initAjaxLinks');
  695. $('a', '#block-mainnavigation')
  696. .add('a', '#block-mainnavigation-2')
  697. // .add('a', '.block.language-switcher-language-url')
  698. .add('a', '#block-footer.menu--footer')
  699. .add('a', '#block-productions')
  700. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  701. // .add('a', 'article.node--type-evenement.node--view-mode-teaser .field--name-field-page-liee')
  702. .add('a', '.productions-subtree')
  703. .add('a', '.productions-parent')
  704. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  705. .add('a', '.field--name-field-son')
  706. .addClass('ajax-link');
  707. if(!_is_mobile){
  708. $('a.site-name', '#block-edlptheme-branding').addClass('ajax-link');
  709. }
  710. _$ajaxLinks = $('.ajax-link');
  711. activateAjaxLinks();
  712. };
  713. function activateAjaxLinks(){
  714. // $('.ajax-link:not(.ajax-enabled)')
  715. _$ajaxLinks.each(function(i,e){
  716. var $this = $(this);
  717. // avoid already ajaxified links
  718. if($this.is('.ajax-enable')) return;
  719. if($this.attr('data-drupal-link-system-path') || $this.is('[type^="audio"]')){
  720. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  721. }
  722. });
  723. };
  724. function onClickAjaxLink(e){
  725. e.preventDefault();
  726. var $link = $(this);
  727. // let the site-name link going through as it will shuffle the map if already active
  728. if($link.is('.is-active') && !$link.is('.site-name'))
  729. return false;
  730. // Audio links
  731. // launch audio player and stop here
  732. if($link.is('.audio-link')){
  733. // check if caller is lastdocs block
  734. caller = $link.parents('.lastdocs, .lastdocs-home').length ? 'lastdocs' : null;
  735. // open audio player
  736. _audioPlayer
  737. .emmit('stop-shuffle')
  738. .openDocument({
  739. nid:$link.attr('nid'),
  740. audio_url:$link.attr('audio_url'),
  741. title:$link.find('.field--name-title').html()
  742. },
  743. caller);
  744. return false;
  745. }
  746. // other sounds playing
  747. if($link.is('[type^="audio"]')){
  748. _audioPlayer
  749. .emmit('stop-shuffle')
  750. .openSound($link.attr('href'), $link.html());
  751. return false;
  752. }
  753. // NOT USED ( YET ? )
  754. // if($link.is('.language-link')){
  755. // TODO: change global site current language
  756. // load all blocks translated (exepted language switcher) ... :/
  757. // refreshAllBlocks();
  758. // TODO: use a promise to initAjaxLinks again after blocks updated
  759. // then let load the content
  760. // }
  761. // other links
  762. var sys_path = $(this).attr('data-drupal-link-system-path');
  763. // front page
  764. // just remove contents and stop here
  765. if(sys_path == '<front>'){
  766. if(!_is_mobile){
  767. if($link.is('.is-active') && _corpus_ready){
  768. _$corpus_canvas.trigger({'type':'shuffle-collection'});
  769. }else{
  770. backToFrontPage();
  771. }
  772. return false;
  773. }
  774. // else{
  775. // sys_path = edlp_mobile.mobile_home_path.replace(/^\//, '');
  776. // }
  777. }
  778. var view_mode = $link.attr('viewmode');
  779. var state = getSysPathState(sys_path, view_mode);
  780. state.url = $(this).attr('href');
  781. if(view_mode){
  782. state.url += "#"+view_mode;
  783. }
  784. if($link.is('[selector]')){
  785. state.selector = $link.attr('selector');
  786. }
  787. $link.addClass('ajax-loading');
  788. // ajaxLoadContent(url, sys_path, state.ajax_path, selector);
  789. ajaxLoadContent(state);
  790. return false;
  791. };
  792. // ___
  793. // / __|___ _ _ _ __ _ _ ___
  794. // | (__/ _ \ '_| '_ \ || (_-<
  795. // \___\___/_| | .__/\_,_/__/
  796. // |_|
  797. function onCorpusMapReady(e){
  798. //console.log('theme : onCorpusReady', e);
  799. _corpus_ready = true;
  800. _$corpus_canvas = $('canvas#corpus-map');
  801. _$corpus_canvas
  802. .on('corpus-cliked-on-map', function(e) {
  803. //console.log('theme : corpus-cliked-on-map');
  804. backToFrontPage();
  805. })
  806. .on('corpus-cliked-on-node', function(e) {
  807. //console.log('theme : corpus-cliked-on-node', e);
  808. _audioPlayer
  809. .emmit('stop-shuffle')
  810. // .setAutoOpenArticle(e.article)
  811. .openDocument(e.target_node);
  812. });
  813. _randomPlayer = new RandomPlayer(e.playlist);
  814. // mainly for articles link
  815. initAjaxLinks();
  816. _$body.attr('corpus-map', 'ready');
  817. }
  818. function openEntree(tid){
  819. if(tid){
  820. closeAllModals();
  821. _$body.removeClass();//.addClass(body_classes.join(' '));
  822. // open entree
  823. if(_corpus_ready){
  824. _$corpus_canvas.trigger({
  825. type:'open-entree',
  826. tid:tid
  827. });
  828. }else{
  829. // else : EdlpCorpus will check when ready if entry item (notice or index) is already .is-active
  830. $('li.entree[tid="'+tid+'"] a.term-link').addClass('is-active');
  831. }
  832. }
  833. };
  834. function checkVisibleCorpusMapSpace(){
  835. var left_limit = 0, right_limit = 0;
  836. _$row.find('.col').each(function(i,e){
  837. var $col = $(this);
  838. var offset = $col.offset();
  839. console.log('$col', $col);
  840. switch(true){
  841. case $col.is('.float-right'):
  842. right_limit = Math.max(right_limit, Math.abs(offset.left-15-window.innerWidth));
  843. break;
  844. default:
  845. left_limit = Math.max(left_limit, offset.left+$col.width()+15);
  846. break;
  847. }
  848. });
  849. console.log('checkVisibleCorpusMapSpace left_limit:'+left_limit+" right_limit:"+right_limit);
  850. if(_corpus_ready){
  851. _$body.trigger({
  852. type:'visible-space-changed',
  853. left_limit:left_limit,
  854. right_limit:right_limit
  855. });
  856. }else{
  857. _corpus_promise.done(function(){
  858. _$body.trigger({
  859. type:'visible-space-changed',
  860. left_limit:left_limit,
  861. right_limit:right_limit
  862. });
  863. });
  864. }
  865. };
  866. // _ _ _
  867. // /_\ _ _ __| (_)___
  868. // / _ \ || / _` | / _ \
  869. // /_/ \_\_,_\__,_|_\___/
  870. //
  871. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  872. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  873. // https://www.binarytides.com/using-html5-audio-element-javascript/
  874. function AudioPlayer(){
  875. var that = this;
  876. this.fid;
  877. this.audio = new Audio();
  878. // audio events
  879. this.audio_events = ["loadedmetadata","playing","pause","timeupdate","ended","error"]; // ,"canplay"
  880. // UI dom objects
  881. this.$container = $('<div id="audio-player">');
  882. // btns
  883. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  884. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  885. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  886. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  887. // timeline
  888. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  889. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  890. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  891. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  892. // time
  893. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  894. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  895. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  896. // favoris
  897. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  898. // cartel
  899. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  900. this.scndCartel_visible = 0;
  901. this.cartelSwitchIntervalMS = 7000;
  902. this.cartelSwitchInterval = false;
  903. // hiding
  904. this.hideTimer = false;
  905. this.hideTimeMS = 15000;
  906. // history
  907. this.currentHistoricIndex = null;
  908. this.historic = [];
  909. this.shuffle_is_active = false;
  910. // artciles
  911. this.auto_open_article = false;
  912. // object events
  913. this.event_handlers = {
  914. 'audio-open-document':[],
  915. 'audio-play':[],
  916. 'audio-pause':[],
  917. 'audio-play-next':[],
  918. 'audio-ended':[],
  919. 'stop-shuffle':[]
  920. };
  921. this.init();
  922. };
  923. AudioPlayer.prototype = {
  924. init(){
  925. this.$container_parent = $('header[role="banner"] .region-header');
  926. // append ui to document
  927. this.$container.appendTo(this.$container_parent);
  928. // record timeline width
  929. this.timeline_w = parseInt(this.$timeline.width());
  930. // init seeking
  931. this.$loader.on('click', this.seek.bind(this));
  932. // init audio events
  933. var fn = '';
  934. for (var i = 0; i < this.audio_events.length; i++) {
  935. fn = this.audio_events[i];
  936. // capitalize first letter of event (only cosmetic :p )
  937. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  938. this.audio.addEventListener(
  939. this.audio_events[i],
  940. this[fn].bind(this),
  941. true);
  942. }
  943. // init btns events
  944. this.$previous.on('click', this.playPrevious.bind(this));
  945. this.$playpause.on('click', this.togglePlayPause.bind(this));
  946. this.$next.on('click', this.playNext.bind(this));
  947. },
  948. openDocument(node, caller, historic_index){
  949. console.log('AudioPlayer openDocument', node, caller);
  950. if(typeof node == 'undefined'
  951. || typeof node.nid == 'undefined'
  952. || typeof node.audio_url == 'undefined'){
  953. console.warn('AudioPlayer openDocument() node is malformed', node);
  954. return false;
  955. }
  956. // if we don't come from history popstate
  957. if(typeof caller == 'undefined' || caller != 'popstate'){
  958. this.historic.push(node);
  959. this.currentHistoricIndex = this.historic.length-1;
  960. // this.shuffle_mode = shuffle_mode || false;
  961. // add the document opening to history to be able to share and play audio from any where
  962. if(caller != "history_first_load"){
  963. if(typeof node.document_url == 'undefined'){
  964. console.warn("AudioPlayer openDocument missing document_url, can't push state to history", caller, node);
  965. }else{
  966. var state = {
  967. audio:true,
  968. node:{
  969. nid:node.nid,
  970. audio_url:node.audio_url,
  971. document_url:node.document_url,
  972. title:node.title || null,
  973. },
  974. historic_index : this.currentHistoricIndex,
  975. };
  976. // console.log('AudioPlayer pushstate', state);
  977. var url = node.document_url + (caller == 'random' ? '#random' : '');
  978. history.pushState(state, null, url);
  979. }
  980. }
  981. }else{
  982. // if the call commes from popstate, we update the current position of index
  983. this.currentHistoricIndex = historic_index;
  984. }
  985. if(_$body.is('.path-frontpage') && caller !== 'lastdocs' && !_is_mobile){
  986. closeAllModals();
  987. }
  988. // TODO: update language switcher for document url
  989. // TODO: stop random player if playing from studio
  990. this.emmit('audio-open-document', {caller:caller});
  991. // piwik
  992. // debugger;
  993. if(typeof _paq !== 'undefined'){
  994. // page tracking
  995. // https://matomo.org/blog/2017/02/how-to-track-single-page-websites-using-piwik-analytics/
  996. // _paq.push(['setCustomUrl', state.url]);
  997. // _paq.push(['setDocumentTitle', data.title]);
  998. // _paq.push(['trackPageView']);
  999. // js event
  1000. // trackEvent(category, action, [name], [value])
  1001. if(typeof node.title != 'undefined'){
  1002. _paq.push(['trackEvent', 'Audio', 'play', node.title]);
  1003. }
  1004. }
  1005. this.launch();
  1006. },
  1007. launch(){
  1008. this.clearTimeOutToHide();
  1009. this.clearIntervalAutoCartelSwitch();
  1010. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  1011. if(!_is_mobile){
  1012. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  1013. }
  1014. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  1015. try {
  1016. _$corpus_canvas.trigger({
  1017. 'type':'audio-node-opened',
  1018. 'nid':this.historic[this.currentHistoricIndex].nid
  1019. });
  1020. } catch (e) {
  1021. console.info('AudioPlayer : _$corpus_canvas does not exists, testing promise');
  1022. var that = this;
  1023. _corpus_promise.done(function(){
  1024. _$corpus_canvas.trigger({
  1025. 'type':'audio-node-opened',
  1026. 'nid':that.historic[that.currentHistoricIndex].nid
  1027. });
  1028. });
  1029. }
  1030. this.showHidePreviousBtn();
  1031. this.showHideNextBtn();
  1032. this.show();
  1033. },
  1034. openSound(url, title){
  1035. this.hide();
  1036. this.clearTimeOutToHide();
  1037. this.$cartel.html("");
  1038. this.setSRC(url);
  1039. this.show();
  1040. if(typeof _paq !== 'undefined'){
  1041. _paq.push(['trackEvent', 'Audio', 'play', url]);
  1042. }
  1043. },
  1044. // audio functions
  1045. setSRC(url){
  1046. console.log('AudioPlayer setSRC : url', url);
  1047. this.audio.src = url;
  1048. // need to trigger play right now to prevent safari blocking auto-play
  1049. this.play();
  1050. },
  1051. // audio is ready to play
  1052. onLoadedmetadata(){
  1053. var rem = parseInt(this.audio.duration, 10),
  1054. mins = Math.floor(rem/60,10),
  1055. secs = rem - mins*60;
  1056. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  1057. this.updateLoadingBar();
  1058. },
  1059. updateLoadingBar(){
  1060. console.log("updateLoadingBar");
  1061. // var that = this;
  1062. // if (this.audio.readyState === 4){
  1063. if(this.audio.buffered.length>0){
  1064. // debugger;
  1065. this.$loader.css({
  1066. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  1067. });
  1068. if( this.audio.buffered.end(0) < this.audio.duration ){
  1069. // loop through this function until file is fully loaded
  1070. window.requestAnimationFrame(this.updateLoadingBar.bind(this));
  1071. }else{
  1072. console.log('Audio fully loaded');
  1073. }
  1074. }else{
  1075. // as audio buffer is not ready, recall updateLoadingBar in next frame
  1076. window.requestAnimationFrame(this.updateLoadingBar.bind(this));
  1077. }
  1078. },
  1079. // onCanplay(){
  1080. // // this.play();
  1081. // },
  1082. onError(){
  1083. console.warn("Audio Error " + this.audio.error.code + "; details: " + this.audio.error.message);
  1084. },
  1085. play(){
  1086. this.clearTimeOutToHide();
  1087. // this.audio.play();
  1088. // 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
  1089. var promise = this.audio.play();
  1090. // console.log('promise', promise);
  1091. if (promise !== undefined) {
  1092. promise.catch(function(error){
  1093. // Auto-play was prevented
  1094. // Show a UI element to let the user manually start playback
  1095. console.warn('autoplay failed, ask for playing', error);
  1096. }).then(function(){
  1097. // Auto-play started
  1098. console.log('autoplay succeed');
  1099. });
  1100. }
  1101. },
  1102. playPrevious(){
  1103. if(this.currentHistoricIndex > 0){
  1104. this.currentHistoricIndex -= 1;
  1105. this.launch();
  1106. // TODO: update history state
  1107. }
  1108. },
  1109. playNext(){
  1110. if(this.currentHistoricIndex < this.historic.length-1){
  1111. this.currentHistoricIndex += 1;
  1112. this.launch();
  1113. // TODO: update history state
  1114. }else{
  1115. this.emmit('audio-play-next');
  1116. }
  1117. },
  1118. togglePlayPause(e){
  1119. if(this.audio.paused){
  1120. // this.audio.play();
  1121. this.play();
  1122. }else{
  1123. // this.audio.pause();
  1124. this.stop();
  1125. }
  1126. },
  1127. stop(){
  1128. // console.log('AudioPlayer stop()');
  1129. // debugger;
  1130. this.audio.pause();
  1131. // don't close player if article or transcript is open
  1132. if(!(_$body.is('.path-node-'+this.historic[this.currentHistoricIndex].nid)
  1133. && (_$body.is('.view-mode-article') || _$body.is('.view-mode-transcript')))){
  1134. this.timeOutToHide();
  1135. }
  1136. },
  1137. seek(e){
  1138. var seek = e.originalEvent.layerX / this.timeline_w * this.audio.duration
  1139. console.log("Audio seeking : seek", seek);
  1140. this.audio.currentTime = seek;
  1141. },
  1142. // audio events
  1143. onPlaying(){
  1144. this.$btns.addClass('is-playing');
  1145. this.emmit('audio-play');
  1146. },
  1147. onPause(){
  1148. this.$btns.removeClass('is-playing');
  1149. this.emmit('audio-pause');
  1150. },
  1151. onTimeupdate(){
  1152. // move cursor
  1153. this.$cursor.css({
  1154. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  1155. });
  1156. // update time text display
  1157. var rem = parseInt(this.audio.currentTime, 10),
  1158. mins = Math.floor(rem/60,10),
  1159. secs = rem - mins*60;
  1160. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  1161. },
  1162. onEnded(){
  1163. console.log('AudioPlayer onEnded()');
  1164. this.stop();
  1165. this.emmit('audio-ended');
  1166. },
  1167. // cartel functions
  1168. loadNode(nid){
  1169. this.$cartel.addClass('loading');
  1170. var vm = 'player_cartel';
  1171. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  1172. var path = _origin + Drupal.url(ajax_path);
  1173. $.getJSON(path, {})
  1174. .done(this.onNodeLoaded.bind(this))
  1175. .fail(this.onNodeLoadFail.bind(this));
  1176. },
  1177. onNodeLoaded(data){
  1178. console.log('AudioPlayer node loaded', data);
  1179. this.$cartel.html(data.rendered).removeClass('loading');
  1180. _$body.trigger({'type':'new-audio-cartel-loaded'});
  1181. initAjaxLinks();
  1182. // cartel autoswitch
  1183. this.scndCartel_visible = 0;
  1184. this.$cartel.removeClass('second-visible');
  1185. // if second cartel has special info ?
  1186. if (this.$cartel.find('.second-cartel .col-left').children().length > 1
  1187. || this.$cartel.find('.second-cartel').children('.col-right').length){
  1188. // init cartel auto switch
  1189. this.cartelSwitchInterval = setInterval(this.switchCartel.bind(this), this.cartelSwitchIntervalMS);
  1190. }
  1191. // call drupal behaviours (for addtoany)
  1192. Drupal.attachBehaviors(this.$cartel);
  1193. // open automaticly tha article if needed
  1194. this.setAutoOpenArticle();
  1195. if(this.auto_open_article){
  1196. this.$cartel.find('a.link-article').trigger('click');
  1197. this.auto_open_article = false;
  1198. }
  1199. },
  1200. onNodeLoadFail(jqxhr, textStatus, error){
  1201. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  1202. this.$cartel.removeClass('loading').html('');
  1203. },
  1204. setAutoOpenArticle(art){
  1205. this.auto_open_article = $('a.articles-link').is('.is-active');
  1206. return this;
  1207. },
  1208. // global
  1209. show(){
  1210. this.$container_parent.addClass('audio-player-visible');
  1211. this.$container.addClass('visible');
  1212. },
  1213. showHidePreviousBtn(){
  1214. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  1215. this.$previous.addClass('is-active');
  1216. }else{
  1217. this.$previous.removeClass('is-active');
  1218. }
  1219. },
  1220. showHideNextBtn(){
  1221. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  1222. this.$next.addClass('is-active');
  1223. }else{
  1224. this.$next.removeClass('is-active');
  1225. }
  1226. },
  1227. timeOutToHide(){
  1228. // console.log('AudioPlayer timeOutToHide()');
  1229. this.clearTimeOutToHide();
  1230. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  1231. },
  1232. clearTimeOutToHide(){
  1233. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  1234. if(this.hideTimer){
  1235. clearTimeout(this.hideTimer);
  1236. this.hideTimer = false;
  1237. }
  1238. },
  1239. // cartel
  1240. switchCartel(){
  1241. // console.log("switchCartel", this.scndCartel_visible);
  1242. if (this.scndCartel_visible) {
  1243. // return to first cartel
  1244. this.$cartel.removeClass('second-visible');
  1245. this.scndCartel_visible = 0;
  1246. // kill the auto switch after one rotation
  1247. this.clearIntervalAutoCartelSwitch();
  1248. }else{
  1249. // switch to 2nd cartel
  1250. this.$cartel.addClass('second-visible');
  1251. this.scndCartel_visible = 1;
  1252. }
  1253. },
  1254. clearIntervalAutoCartelSwitch(){
  1255. if(this.cartelSwitchInterval){
  1256. clearInterval(this.cartelSwitchInterval);
  1257. this.cartelSwitchInterval = false;
  1258. }
  1259. },
  1260. hide(){
  1261. // console.log('AudioPlayer hide()');
  1262. this.$container_parent.removeClass('audio-player-visible');
  1263. this.$container.removeClass('visible');
  1264. // trigger highlighted node remove on corpus map
  1265. try {
  1266. _$corpus_canvas.trigger('audio-node-closed');
  1267. } catch (e) {
  1268. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  1269. }
  1270. },
  1271. deActivateRandom(){
  1272. this.shuffle_is_active = false;
  1273. this.showHideNextBtn();
  1274. },
  1275. // object events
  1276. on(event_name, handler){
  1277. if(typeof this.event_handlers[event_name] == 'undefined'){
  1278. console.warn('AudioPlayer : event '+event_name+' does not exists');
  1279. }
  1280. this.event_handlers[event_name].push(handler);
  1281. return this;
  1282. },
  1283. emmit(event_name, args){
  1284. console.log('AudioPlayer emmit()', event_name, args, this.event_handlers[event_name]);
  1285. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  1286. var handler;
  1287. var args = args || {};
  1288. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  1289. handler = this.event_handlers[event_name][i];
  1290. // console.log('AudioPlayer emmit() loop handler', handler);
  1291. (function(handler, args){
  1292. setTimeout(function(){
  1293. // console.log('AudioPlayer emmit() timeout handler', handler);
  1294. handler(args);
  1295. }, 0);
  1296. }(handler, args));
  1297. }
  1298. return this;
  1299. },
  1300. }
  1301. // ___ _ ___ _
  1302. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  1303. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  1304. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  1305. // |__/
  1306. function RandomPlayer(playlist){
  1307. this.active = false;
  1308. this.playlist = playlist;
  1309. this.path = _base_url+drupalSettings.path.pathPrefix+'#random'
  1310. this.$btn = $('<a>')
  1311. .html('Shuffle')
  1312. .attr('href',this.path)
  1313. .attr('alt', drupalSettings.edlp_corpus.random_link_title)
  1314. .addClass('random-player-btn');
  1315. this.init();
  1316. };
  1317. RandomPlayer.prototype = {
  1318. init(){
  1319. // this.shuffle();
  1320. $('<div>')
  1321. .addClass('block random-player')
  1322. .append(this.$btn)
  1323. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  1324. .prependTo('.region-footer-right');
  1325. // events
  1326. this.$btn.on('click', this.toggleActive.bind(this));
  1327. // attach an event on AudioPlayer
  1328. _audioPlayer
  1329. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  1330. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  1331. // .on('audio-ended', function(e){
  1332. // console.log('RandomPlayer, audio-ended', e);
  1333. // })
  1334. .on('stop-shuffle', this.stop.bind(this));
  1335. _$corpus_canvas
  1336. .on('update-random-playlist', this.updatePlaylist.bind(this));
  1337. // provide a link to auto start random player
  1338. if(window.location.hash == '#random'){
  1339. this.start();
  1340. // window.location.hash = '';
  1341. }
  1342. },
  1343. updatePlaylist(e){
  1344. // debugger;
  1345. this.playlist = e.playlist;
  1346. this.shuffle();
  1347. },
  1348. shuffle(){
  1349. var tempPLaylist = [];
  1350. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  1351. tempPLaylist.push(this.playlist[i]);
  1352. }
  1353. this.shuffledPlaylist = [];
  1354. while(tempPLaylist.length > 0){
  1355. var r = Math.floor(Math.random() * tempPLaylist.length);
  1356. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  1357. }
  1358. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  1359. },
  1360. toggleActive(e){
  1361. e.preventDefault();
  1362. if (this.active) {
  1363. this.stop();
  1364. }else{
  1365. this.start();
  1366. }
  1367. return false;
  1368. },
  1369. start(){
  1370. this.active = _audioPlayer.shuffle_is_active = true;
  1371. this.$btn.addClass('is-active');
  1372. this.shuffle();
  1373. this.next();
  1374. // piwik
  1375. if(typeof _paq !== 'undefined'){
  1376. // trackEvent(category, action, [name], [value])
  1377. _paq.push(['trackEvent', 'RandomPlayer', 'start']);
  1378. }
  1379. },
  1380. stop(){
  1381. this.active = false;
  1382. this.$btn.removeClass('is-active');
  1383. // stop audio player
  1384. // _audioPlayer.stop();
  1385. // remove hash
  1386. history.replaceState(history.state, null, window.location.pathname);
  1387. // tell to AudioPlayer to deactivate netx btn
  1388. _audioPlayer.deActivateRandom();
  1389. if(typeof _paq !== 'undefined'){
  1390. // trackEvent(category, action, [name], [value])
  1391. _paq.push(['trackEvent', 'RandomPlayer', 'stop']);
  1392. }
  1393. },
  1394. next(){
  1395. console.log('RandomPlayer next', this.active, this.shuffledPlaylist.length);
  1396. if(this.active && this.shuffledPlaylist.length > 0)
  1397. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0], 'random');
  1398. },
  1399. onAudioPlayNext(){
  1400. console.log('RandomPlayer : onAudioPlayNext()');
  1401. if(this.active){
  1402. this.next();
  1403. }
  1404. },
  1405. onAudioPlayerEnded(){
  1406. console.log('RandomPlayer : onAudioPlayerEnded()');
  1407. if(this.active){
  1408. this.next();
  1409. }
  1410. }
  1411. };
  1412. // ___ ___ _
  1413. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  1414. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  1415. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  1416. // |_| |__/
  1417. function CompoPlayer(){
  1418. this.active = false;
  1419. this.playing = false;
  1420. this.paused = false;
  1421. this.playlist = [];
  1422. this.current_index = 0;
  1423. this.$composer = null;
  1424. this.$compo = null;
  1425. this.$controls = null;
  1426. this.init();
  1427. };
  1428. CompoPlayer.prototype = {
  1429. init(){
  1430. // console.log('CompoPlayer init()');
  1431. // attach an event on AudioPlayer
  1432. _audioPlayer
  1433. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  1434. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  1435. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  1436. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  1437. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  1438. // this.newCompo();
  1439. },
  1440. newCompo(){
  1441. //console.log('CompoPlayer newCompo()');
  1442. // this.$compo = $('.composition_ui .composer .composition');
  1443. this.initControls();
  1444. },
  1445. initControls(){
  1446. //console.log('CompoPlayer initControls()');
  1447. this.$composer = $('.composition_ui .composer');
  1448. this.$compo = $('.composition_ui .composer .composition');
  1449. this.$controls = $('.composition_ui .composer .compo-player-controls');
  1450. if(!this.$controls.is('.ready') && this.$compo){
  1451. this.$previous = $('<div>').addClass('previous')
  1452. .on('click', this.prev.bind(this))
  1453. .appendTo(this.$controls);
  1454. this.$playpause = $('<div>').addClass('play-pause')
  1455. .on('click', this.togglePlayPause.bind(this))
  1456. .appendTo(this.$controls);
  1457. this.$next = $('<div>').addClass('next')
  1458. .on('click', this.next.bind(this))
  1459. .appendTo(this.$controls);
  1460. this.$controls.addClass('ready');
  1461. this.refresh();
  1462. this.active = true;
  1463. // this.newCompo();
  1464. }
  1465. },
  1466. refresh(){
  1467. // console.log('CompoPlayer refresh(), this', this);
  1468. this.stop();
  1469. // load new playlist
  1470. this.playlist = [];
  1471. var that = this;
  1472. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  1473. var $link = $('a.audio-link',this);
  1474. that.playlist.push({
  1475. item:$(this),
  1476. audio_url:$link.attr("audio_url"),
  1477. nid:$link.attr("nid"),
  1478. });
  1479. });
  1480. this.showHideControls();
  1481. },
  1482. togglePlayPause(){
  1483. // console.log('CompoPlayer togglePlayPause');
  1484. if (this.playing && !this.paused) {
  1485. this.pause();
  1486. }else{
  1487. if(this.playing && this.paused){
  1488. this.play();
  1489. }else{
  1490. this.start();
  1491. }
  1492. }
  1493. },
  1494. start(){
  1495. //console.log('start');
  1496. // console.log('CompoPlayer start()');
  1497. this.playing = true;
  1498. this.play();
  1499. },
  1500. play(){
  1501. // console.log('play');
  1502. if(this.paused){
  1503. this.paused = false;
  1504. _audioPlayer.play();
  1505. }else{
  1506. // _audioPlayer;
  1507. _audioPlayer.emmit('stop-shuffle').openDocument(this.playlist[this.current_index], this);
  1508. // TODO: stop random player
  1509. }
  1510. this.setActiveItem().showHideControls();
  1511. },
  1512. pause(){
  1513. //console.log('pause');
  1514. this.paused = true;
  1515. this.showHideControls();
  1516. _audioPlayer.stop();
  1517. },
  1518. next(){
  1519. // console.log('CompoPlayer next()');
  1520. if(this.playing){
  1521. this.current_index += 1;
  1522. if(this.current_index < this.playlist.length){
  1523. this.play();
  1524. }else{
  1525. this.stop();
  1526. }
  1527. }
  1528. },
  1529. prev(){
  1530. // console.log('CompoPlayer prev()');
  1531. if(this.playing){
  1532. this.current_index -= 1;
  1533. if(this.current_index >= 0){
  1534. this.play();
  1535. }else{
  1536. this.stop();
  1537. }
  1538. }
  1539. },
  1540. stop(){
  1541. if(this.playing){
  1542. _audioPlayer.stop();
  1543. }
  1544. this.reset();
  1545. },
  1546. reset(){
  1547. this.playing = false;
  1548. this.paused = false;
  1549. this.resetIndex();
  1550. },
  1551. resetIndex(){
  1552. this.current_index = 0;
  1553. this.showHideControls().resetActiveItems();
  1554. },
  1555. setActiveItem(){
  1556. this.resetActiveItems();
  1557. if(this.playing && this.current_index >= 0){
  1558. this.playlist[this.current_index].item.addClass('is-active');
  1559. }
  1560. // this call shoud not be here
  1561. this.showHideControls();
  1562. return this;
  1563. },
  1564. resetActiveItems(){
  1565. for (var n = 0; n < this.playlist.length; n++) {
  1566. // console.log('node',node);
  1567. this.playlist[n].item.removeClass('is-active');
  1568. }
  1569. return this;
  1570. },
  1571. showHideControls(){
  1572. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  1573. // global playing
  1574. if(this.$controls){
  1575. if(this.playing && !this.paused){
  1576. this.$controls.addClass('is-playing');
  1577. }else{
  1578. this.$controls.removeClass('is-playing');
  1579. }
  1580. }
  1581. // playpause
  1582. if(this.$playpause){
  1583. if(this.playlist.length > 0){
  1584. this.$playpause.addClass('is-active');
  1585. }else{
  1586. this.$playpause.removeClass('is-active');
  1587. }
  1588. }
  1589. // next
  1590. if(this.$next){
  1591. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  1592. this.$next.addClass('is-active');
  1593. }else{
  1594. this.$next.removeClass('is-active');
  1595. }
  1596. }
  1597. // previous
  1598. if(this.$previous){
  1599. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  1600. this.$previous.addClass('is-active');
  1601. }else{
  1602. this.$previous.removeClass('is-active');
  1603. }
  1604. }
  1605. return this;
  1606. },
  1607. deactivate(){
  1608. this.stop();
  1609. this.active = false;
  1610. },
  1611. // _audioPlayer events
  1612. onAudioOpenDocument(args){
  1613. if(args.caller !== this){
  1614. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  1615. this.reset();
  1616. }
  1617. // else{
  1618. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  1619. // }
  1620. },
  1621. onAudioPlayerPlay(){
  1622. if(this.playing && this.paused){
  1623. this.paused = false;
  1624. this.showHideControls();
  1625. }
  1626. },
  1627. onAudioPlayerPause(){
  1628. if(this.playing && !this.paused){
  1629. this.paused = true;
  1630. this.showHideControls();
  1631. }
  1632. },
  1633. onAudioPlayerEnded(){
  1634. if(this.playing){
  1635. this.next();
  1636. }
  1637. },
  1638. // onAudioPlayNext(){
  1639. // this.next();
  1640. // }
  1641. };
  1642. // ___ _ _
  1643. // / __|_ _(_)__| |
  1644. // | (_ | '_| / _` |
  1645. // \___|_| |_\__,_|
  1646. function initGrid(){
  1647. console.log('theme : initGrid');
  1648. if(!_is_mobile){
  1649. checkGridBlockHeight();
  1650. _$row.find('.col').addClass('offfield');
  1651. }
  1652. if(false){
  1653. var $grid = $('.grid',_$row).masonry({
  1654. itemSelector:'.col',
  1655. columnWidth:'.col-2',
  1656. containerStyle: null,
  1657. resizeContainer:false,
  1658. // horizontalOrder: true,
  1659. transitionDuration:0,//'0.2s',
  1660. // stagger:30,
  1661. // disable initial layout
  1662. // initLayout: false,
  1663. });
  1664. // layout Masonry after each image loads
  1665. $grid.imagesLoaded().progress( function() {
  1666. $grid.masonry('layout');
  1667. });
  1668. $grid.imagesLoaded(function(){
  1669. $grid.masonry('layout');
  1670. // checkProductionBlockVisible();
  1671. });
  1672. // bind event
  1673. // $grid.masonry( 'on', 'layoutComplete', function() {
  1674. // console.log('layout is complete');
  1675. // });
  1676. $grid.on('layoutComplete', checkGridBlockVisible);
  1677. }else{
  1678. // setTimeout(checkGridBlockVisible, 100);
  1679. if(!_is_mobile){
  1680. checkGridBlockVisible();
  1681. }
  1682. }
  1683. };
  1684. function checkGridBlockHeight(){
  1685. // console.log('checkGridBlockHeight');
  1686. // if(_is_mobile) return;
  1687. var $r_h = _$row.height();
  1688. var $this;
  1689. // console.log($r_h);
  1690. $('.grid .col', _$row).each(function(i,e){
  1691. $this = $(this);
  1692. if(!$this.is('[init-height]')){
  1693. $this.attr('init-height', $this.outerHeight())
  1694. }
  1695. // console.log($(this).height(), $(this).innerHeight(), $(this).outerHeight());
  1696. // if($this.height() > $r_h){
  1697. // $this.height($r_h);
  1698. // }else{
  1699. // $this.height('auto');
  1700. // }
  1701. $this.height(Math.min($this.attr('init-height'), _$row.height()));
  1702. });
  1703. };
  1704. function checkGridBlockVisible(){
  1705. // console.log('checkGridBlockVisible');
  1706. // if(_is_mobile) return;
  1707. var $r_h = _$row.height();
  1708. var $this,pos;
  1709. $('.grid .col', _$row).each(function(i,e){
  1710. // $(this).on('load',function(event){
  1711. $this = $(this);
  1712. pos = $this.position();
  1713. // console.log(pos.top, $this.height(), pos.top+$this.height(), $r_h, $this);
  1714. if(pos.top+$this.height() <= $r_h){
  1715. $this.removeClass('offfield');
  1716. }else{
  1717. $this.addClass('offfield');
  1718. }
  1719. // });
  1720. });
  1721. // for (var i in items) {
  1722. // if(!items[i].isTransitioning){
  1723. // console.log('y+outerHeight', items[i].position.y+items[i].size.outerHeight);
  1724. // console.log('_$row.height()', _$row.height());
  1725. // if(items[i].position.y+items[i].size.outerHeight < _$row.height()){
  1726. // $(items[i].element).removeClass('offfield');
  1727. // }else{
  1728. // // $(items[i].element).addClass('offfield');
  1729. // }
  1730. // }
  1731. // }
  1732. }
  1733. // ___ _ _ _
  1734. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  1735. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  1736. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  1737. // function initProductions(){
  1738. // console.log('theme : initProductions');
  1739. //
  1740. // // _$row.find('.col').addClass('offfield');
  1741. // var $grid = $('.grid',_$row).masonry({
  1742. // itemSelector:'.col',
  1743. // columnWidth:'.col-2',
  1744. // containerStyle: null,
  1745. // resizeContainer:false,
  1746. // // horizontalOrder: true,
  1747. // transitionDuration:0,//'0.2s',
  1748. // // stagger:30,
  1749. // // disable initial layout
  1750. // // initLayout: false,
  1751. // });
  1752. //
  1753. // // layout Masonry after each image loads
  1754. // $grid.imagesLoaded().progress( function() {
  1755. // $grid.masonry('layout');
  1756. // });
  1757. //
  1758. // $grid.imagesLoaded(function(){
  1759. // $grid.masonry('layout');
  1760. // // checkProductionBlockVisible();
  1761. // });
  1762. //
  1763. // // bind event
  1764. // // $grid.masonry( 'on', 'layoutComplete', function() {
  1765. // // console.log('layout is complete');
  1766. // // });
  1767. // // $grid.on('layoutComplete', checkProductionBlockVisible);
  1768. // };
  1769. // ___ _ _ _ _
  1770. // / __|___| | |___ __| |_(_)___ _ _
  1771. // | (__/ _ \ | / -_) _| _| / _ \ ' \
  1772. // \___\___/_|_\___\__|\__|_\___/_||_|
  1773. // mobile version of collection
  1774. function initCollectionNav(){
  1775. console.log('initCollectionNav');
  1776. // taxonomy-term.vocabulary-entrees.home_mobile
  1777. $('.field--name-field-notice, .index', '.taxonomy-term.vocabulary-entrees.home_mobile')
  1778. .addClass('closed');
  1779. $('.field--name-field-notice>.field__label', '.taxonomy-term.vocabulary-entrees.home_mobile')
  1780. .on('click', onClickCollectionNotice);
  1781. $('.index>.field__label', '.taxonomy-term.vocabulary-entrees.home_mobile')
  1782. .on('click', onClickCollectionIndex);
  1783. };
  1784. function onClickCollectionNotice(e){
  1785. // console.log('onClickCollectionNotice');
  1786. // var $part = $(this).parent();//parents('.taxonomy-term');
  1787. toggleEntreeOpening($(this).parent(), 'notice');
  1788. };
  1789. function onClickCollectionIndex(e){
  1790. // console.log('onClickCollectionIndex');
  1791. // var $part = $(this).parent();//parents('.taxonomy-term');
  1792. toggleEntreeOpening($(this).parent(), 'index');
  1793. };
  1794. function toggleEntreeOpening($e, part){
  1795. $e.toggleClass('closed')
  1796. .parents('.taxonomy-term.vocabulary-entrees.home_mobile').toggleClass(part+'-opened');
  1797. }
  1798. // ___ _
  1799. // / __| ___ __ _ _ _ __| |_
  1800. // \__ \/ -_) _` | '_/ _| ' \
  1801. // |___/\___\__,_|_| \__|_||_|
  1802. function initSearch(){
  1803. // if($('#edit-entries input:checked', '#edlp-search-form').length){
  1804. // $('#edit-entries--wrapper', '#edlp-search-form').toggleClass('opened');
  1805. // }
  1806. $('#edit-entries--wrapper legend', '#edlp-search-form').on('click', function(){
  1807. $(this).parent().toggleClass('opened');
  1808. });
  1809. };
  1810. // ___ _ _ _
  1811. // | __|_ _ _ _ ___ __ _(_)__| |_ _ _ ___ _ __ ___ _ _| |_
  1812. // | _|| ' \| '_/ -_) _` | (_-< _| '_/ -_) ' \/ -_) ' \ _|
  1813. // |___|_||_|_| \___\__, |_/__/\__|_| \___|_|_|_\___|_||_\__|
  1814. // |___/
  1815. function initEnregistrementTranscript(){
  1816. console.log('initEnregistrementTranscript');
  1817. var $node = _$row.find('article.node--type-enregistrement.node--view-mode-transcript');
  1818. var $nav = $('<nav>').prependTo($node);
  1819. $node.find('.field--name-field-transcript-vo').addClass('visible').find('.field__label')
  1820. .clone().appendTo($nav).addClass('is-active')
  1821. .attr('field_target', '.field--name-field-transcript-vo');
  1822. $node.find('.field--name-field-transcript-trad').find('.field__label')
  1823. .clone().appendTo($nav)
  1824. .attr('field_target', '.field--name-field-transcript-trad');
  1825. $nav.find('.field__label').on('click', function(){
  1826. var $this = $(this).addClass('is-active');
  1827. $this.siblings('.is-active').removeClass('is-active');
  1828. $this.parents('article.node').find('.field.visible').removeClass('visible');
  1829. $this.parents('article.node').find($this.attr('field_target')).addClass('visible');
  1830. });
  1831. };
  1832. // ___ _ ___
  1833. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  1834. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  1835. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  1836. // |___/
  1837. function backToFrontPage(pop_state){
  1838. console.log('backToFrontPage', pop_state);
  1839. closeAllModals();
  1840. // assume we are going back to front page
  1841. $('body').removeClass().addClass('path-frontpage');
  1842. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  1843. // close entrees
  1844. if(_corpus_ready){
  1845. _$corpus_canvas.trigger({'type':'close-all-entree'});
  1846. _$corpus_canvas.trigger({'type':'scramble-collection'});
  1847. }
  1848. if(typeof pop_state == "undefined" || !pop_state){
  1849. console.log('backToFrontPage push state');
  1850. history.pushState({home:true}, null, _base_url+drupalSettings.path.currentLanguage);
  1851. }
  1852. }
  1853. // function initHome(){
  1854. // addCloseModalBtnToCols();
  1855. // return;
  1856. // console.log('theme : initHome');
  1857. // var $grid = $('.grid',_$row).masonry({
  1858. // itemSelector:'.col',
  1859. // columnWidth:'.col-2',
  1860. // horizontalOrder: true,
  1861. // containerStyle: null,
  1862. // // disable initial layout
  1863. // // initLayout: false,
  1864. // });
  1865. // // bind event
  1866. // // $grid.masonry( 'on', 'layoutComplete', function() {
  1867. // // console.log('layout is complete');
  1868. // // });
  1869. //
  1870. // // layout Masonry after each image loads
  1871. // $grid.imagesLoaded().progress( function() {
  1872. // $grid.masonry('layout');
  1873. // });
  1874. //
  1875. // $grid.imagesLoaded(function(){
  1876. // $grid.masonry('layout');
  1877. // });
  1878. // }
  1879. // __ __ _ _
  1880. // | \/ |___ __| |__ _| |___
  1881. // | |\/| / _ \/ _` / _` | (_-<
  1882. // |_| |_\___/\__,_\__,_|_/__/
  1883. function closeAllModals(){
  1884. //console.log('theme : closeAllModals');
  1885. // TODO: animate the remove
  1886. _$row.html('');
  1887. _$ajaxLinks.removeClass('is-active');
  1888. _$body.trigger({'type':'all-modal-closed'});
  1889. // checkRowEmpty();
  1890. };
  1891. function checkRowEmpty(){
  1892. console.log('checkRowEmpty');
  1893. // if row is empty and we are not in productions or entree notice|index call closeAllModals()
  1894. if(!$('.col', _$row).length){
  1895. if(!_$body.is('.entity-type-taxonomy_term.bundle-entrees')){
  1896. if(!_$body.is('.entity-type-node.bundle-page')){
  1897. // we weren't on production or entree, so go back to front page
  1898. // debugger;
  1899. if(_$body.is('.bundle-enregistrement.view-mode-article')
  1900. && $('a.articles-link').is('.is-active')){
  1901. console.log('Closing article while article index is active');
  1902. // if node article and articles is active, reload index
  1903. $('a.articles-link').removeClass('is-active').trigger('click');
  1904. }else{
  1905. backToFrontPage();
  1906. }
  1907. // if(!$('a.articles-link').is('.is-active')){
  1908. // // don't go back to front page if articles filter is on
  1909. // backToFrontPage();
  1910. // }else{
  1911. // }
  1912. }else{
  1913. // if we were on production page just scramble collection in case of map was filtered
  1914. if(_corpus_ready){
  1915. _$corpus_canvas.trigger({'type':'scramble-collection'});
  1916. }
  1917. // reload production home
  1918. $('a[data-drupal-link-system-path="productions"]', '#block-mainnavigation')
  1919. .removeClass('is-active').trigger('click');
  1920. }
  1921. }else{
  1922. // remove is-active class from index or notice entree links
  1923. $('.entree-content a.is-active').removeClass('is-active');
  1924. // TODO: remove the hash index or notice
  1925. }
  1926. }
  1927. };
  1928. init();
  1929. } // end EdlpTheme()
  1930. $(document).ready(function($) {
  1931. if(drupalSettings.path.isFront){
  1932. var edlptheme = new EdlpTheme();
  1933. }else{
  1934. $('body').attr('booted', 'booted');
  1935. }
  1936. });
  1937. })(jQuery, Drupal, drupalSettings);