main.js 71 KB

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