main.js 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080
  1. (function($, Drupal, drupalSettings) {
  2. EdlpTheme = function(){
  3. var _ajax_settings = drupalSettings.edlp_ajax;
  4. var _$body = $('body');
  5. var _is_front = _$body.is('.path-frontpage');
  6. var _$corpus_canvas;
  7. var _$row = $('main[role="main"]>.layout-content>.row');
  8. var _$ajaxLinks;
  9. var _audioPlayer;
  10. var _randomPlayer;
  11. var _compoPlayer;
  12. // ___ _ _
  13. // |_ _|_ _ (_) |_
  14. // | || ' \| | _|
  15. // |___|_||_|_|\__|
  16. function init(){
  17. console.log("EdlpTheme init()");
  18. initHistory();
  19. if(!drupalSettings.path.isFront)
  20. return;
  21. initEvents();
  22. _audioPlayer = new AudioPlayer();
  23. _compoPlayer = new CompoPlayer();
  24. initAjaxLinks();
  25. // if (_$body.is('.path-productions')) initProductions();
  26. // if(_$body.is('.path-frontpage')) initHome();
  27. // initScrollbars();
  28. };
  29. // ___ _
  30. // | __|_ _____ _ _| |_ ___
  31. // | _|\ V / -_) ' \ _(_-<
  32. // |___|\_/\___|_||_\__/__/
  33. function initEvents(){
  34. _$body
  35. .on('corpus-map-ready', onCorpusMapReady)
  36. .on('on-studio-chutier-updated', initAjaxLinks)
  37. .on('studio-initialized', function(e){
  38. _compoPlayer.newCompo();
  39. })
  40. .on('studio-not-active', function(e){
  41. _compoPlayer.deactivate();
  42. })
  43. .on('on-studio-compo-updated', function(e){
  44. initAjaxLinks();
  45. _compoPlayer.refresh();
  46. })
  47. .on('on-studio-compo-opened', function(e){
  48. initAjaxLinks();
  49. _compoPlayer.newCompo();
  50. })
  51. .on('search-results-loaded', initAjaxLinks)
  52. .on('open_entree', closeAllModals)
  53. .on('close_entree', backToFrontPage);
  54. }
  55. // ___ _ _ ___
  56. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  57. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  58. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  59. function initScrollbars(){
  60. // console.log("initScrollbars");
  61. // TODO: find a better js scroll than overlayScrollbars which does not handle well max-height + overflow-y:auto;
  62. // $('.os-scroll').overlayScrollbars({
  63. // overflowBehavior:{
  64. // x:'h',
  65. // y:'scroll',
  66. // clipAlways:false
  67. // }
  68. // });
  69. };
  70. // _ _
  71. // /_\ (_)__ ___ __
  72. // / _ \ | / _` \ \ /
  73. // /_/ \_\/ \__,_/_\_\
  74. // |__/
  75. function parseAjaxSysPath(sys_path, view_mode){
  76. // console.log('Theme : parseAjaxSysPath', sys_path);
  77. var ajax_path = sys_path;
  78. // convert node link to edlp_ajax_node module links
  79. var node_match = ajax_path.match(/^\/?(node\/\d+)$/g);
  80. var term_match = ajax_path.match(/^\/?(taxonomy\/term\/\d+)$/g);
  81. if(node_match){
  82. ajax_path = _ajax_settings.entityjson_path+'/'+node_match[0];
  83. // check for viewmode attribute
  84. if(view_mode){
  85. ajax_path += '/'+view_mode;
  86. }
  87. }else if(term_match){
  88. ajax_path = _ajax_settings.entityjson_path+'/'+term_match[0];
  89. ajax_path = ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  90. // check for viewmode attribute
  91. if(view_mode){
  92. ajax_path += '/'+view_mode;
  93. }
  94. }else{
  95. // convert other link to ajax
  96. // TODO: we assume that other links (no node, no term) are all from own modules (e.g. productions) !! may not be true !!
  97. ajax_path += '/ajax'
  98. }
  99. return ajax_path;
  100. };
  101. function ajaxLoadContent(url, sys_path, ajax_path, selector){
  102. console.log('ajaxLoadContent : url', url);
  103. _$body.addClass('ajax-loading');
  104. var path = window.location.origin + Drupal.url(ajax_path);
  105. $.getJSON(path, {})
  106. .done(function(data){
  107. onAjaxLoaded(data, url, ajax_path, sys_path, selector);
  108. })
  109. .fail(function(jqxhr, textStatus, error){
  110. onAjaxLoadError(jqxhr, textStatus, error, sys_path);
  111. });
  112. };
  113. function onAjaxLoadError(jqxhr, textStatus, error, sys_path){
  114. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  115. $('.ajax-loading').removeClass('ajax-loading');
  116. _$body.removeClass('ajax-loading');
  117. };
  118. function onAjaxLoaded(data, url, ajax_path, sys_path, selector){
  119. console.log('ajax loaded url:'+url+' ajax_path:'+ajax_path+' sys_path:'+sys_path);
  120. // reset all style may been added by other pages (like masonry for productions)
  121. // and replace all content with newly loaded
  122. // TODO: build a system to replace or append contents (like studio + search)
  123. _$row.removeAttr('style').html(data.rendered);
  124. // add close btn
  125. if(sys_path != 'productions'){
  126. addCloseBtnToCols();
  127. }
  128. // add body class for currently loaded content
  129. var body_classes = [
  130. 'path-'+sys_path.replace(/\//g, '-'),
  131. 'entity-type-'+data.entity_type,
  132. 'bundle-'+data.bundle,
  133. 'view-mode-'+data.view_mode
  134. ];
  135. _$body.removeClass().addClass(body_classes.join(' '));
  136. // id node add a generic path-node class to body
  137. m = sys_path.match(/^\/?(node\/\d+)$/g);
  138. if(m)
  139. _$body.addClass('path-edlp-node');
  140. // handle clicked link classes
  141. $('.ajax-loading').removeClass('ajax-loading');
  142. $('.is-active').removeClass('is-active');
  143. $('.is-active-trail').removeClass('is-active-trail');
  144. if(typeof selector != 'undefined'){
  145. $('a[selector="'+selector+'"]').addClass('is-active');
  146. }else{
  147. $('a[data-drupal-link-system-path="'+sys_path+'"]').addClass('is-active');
  148. }
  149. // if bundle page (productions) activate production links
  150. if (typeof data.bundle != 'undefined' && data.bundle == "page") {
  151. $('a[data-drupal-link-system-path="productions"]').addClass('is-active-trail');
  152. }
  153. // if node is in production menu tree, set first level of tree active, e.g. pieces sonores
  154. if (typeof data.menu_parents != 'undefined') {
  155. for (var i = 0; i < data.menu_parents.length; i++) {
  156. var menu_sys_path = data.menu_parents[i];
  157. $('a[data-drupal-link-system-path="'+menu_sys_path+'"]').addClass('is-active-trail');
  158. }
  159. }
  160. // if block attached (eg : from edlp_productions module)
  161. // not used anymore as production block is always present (but not visible)
  162. if(typeof data.block != 'undefined'){
  163. // if block not already added
  164. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  165. $('.region-'+data.block.region).append(data.block.rendered);
  166. }
  167. }
  168. // initScrollbars();
  169. if(sys_path == "productions")
  170. initProductions();
  171. initAjaxLinks();
  172. // trigger other modules behaviours
  173. _$body.trigger({'type':'new-content-ajax-loaded'});
  174. // and call druapl behaviours
  175. Drupal.attachBehaviors(_$row[0]);
  176. _$body.attr('booted', 'booted');
  177. _$body.removeClass('ajax-loading');
  178. var state = {
  179. ajax_path:ajax_path,
  180. sys_path:sys_path,
  181. };
  182. // url is null means that we are loading content on popState event
  183. // so we don't record the state again
  184. if(url){
  185. // console.log('url:'+url+' ; state',state);
  186. // we can not pushestate with absolute url
  187. history.pushState(state, null, url);
  188. }
  189. };
  190. function addCloseBtnToCols(){
  191. $('.col', _$row).each(function(index, el) {
  192. if($('span.close-col-btn', this).length)
  193. return true;
  194. $(this).children('.wrapper').append($('<span>')
  195. .addClass('close-col-btn')
  196. .on('click', function(e){
  197. // check for theme attribute and emmit event
  198. var $col = $(this).parents('.col');
  199. var theme = $col.attr('theme');
  200. if(theme != ''){
  201. _$body.trigger({'type':theme+'-col-closed'});
  202. }
  203. // remove the col
  204. $col.remove();
  205. // if row is empty call closeAllModals()
  206. if(!$('.col', _$row).length){
  207. backToFrontPage();
  208. }
  209. })
  210. );
  211. });
  212. };
  213. // _ _ _ _
  214. // | || (_)__| |_ ___ _ _ _ _
  215. // | __ | (_-< _/ _ \ '_| || |
  216. // |_||_|_/__/\__\___/_| \_, |
  217. // |__/
  218. function initHistory(){
  219. initFirstLoad();
  220. window.addEventListener('popstate', onHistoryPopState);
  221. };
  222. function initFirstLoad(){
  223. console.log('theme : initFirstLoad()', window.location);
  224. console.log(document.cookie);
  225. var url = window.location.pathname;
  226. // var origin_path = getCookie('edlp_origin_path');
  227. // console.log('origin_path', origin_path);
  228. var origin_sys_path = window.localStorage.getItem('edlp_origin_path');
  229. console.log('origin_sys_path', origin_sys_path);
  230. var origin_url = window.localStorage.getItem('edlp_origin_url');
  231. console.log('origin_url', origin_url);
  232. if(origin_sys_path){
  233. // create history state
  234. var state = {
  235. ajax_path: parseAjaxSysPath(origin_sys_path),
  236. sys_path: origin_sys_path,
  237. };
  238. console.log('state', state);
  239. // record history state
  240. history.replaceState(state, null, origin_url);
  241. // load content through ajax
  242. ajaxLoadContent(null, state.sys_path, state.ajax_path);
  243. // reset the storage
  244. window.localStorage.removeItem("edlp_origin_path");
  245. window.localStorage.removeItem("edlp_origin_url");
  246. }else{
  247. history.replaceState({home:true}, null, url);
  248. _$body.attr('booted', 'booted');
  249. }
  250. };
  251. function onHistoryPopState(e){
  252. //console.log('onPopState',e);
  253. if(e.state.home){
  254. backToFrontPage();
  255. }else{
  256. ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  257. }
  258. };
  259. // _ _ _ _ _
  260. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  261. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  262. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  263. // |__/
  264. function initAjaxLinks(){
  265. // console.log('initAjaxLinks');
  266. $('a', '#block-mainnavigation')
  267. .add('a', '#block-footer.menu--footer')
  268. .add('a', '#block-productions')
  269. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  270. .add('a', '.productions-subtree')
  271. .add('a', '.productions-parent')
  272. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  273. .addClass('ajax-link');
  274. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  275. .each(function(i,e){
  276. var $this = $(this);
  277. // avoid already ajaxified links
  278. if($this.is('.ajax-enable')) return;
  279. if($this.attr('data-drupal-link-system-path')){
  280. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  281. }
  282. });
  283. };
  284. function onClickAjaxLink(e){
  285. e.preventDefault();
  286. var $link = $(this);
  287. if($link.is('.is-active'))
  288. return false;
  289. // Audio links
  290. // launch audio player and stop here
  291. if($link.is('.audio-link')){
  292. _audioPlayer
  293. .emmit('stop-shuffle')
  294. .openDocument({
  295. nid:$link.attr('nid'),
  296. audio_url:$link.attr('audio_url')
  297. });
  298. return false;
  299. }
  300. // other links
  301. var url = $(this).attr('href');
  302. var sys_path = $(this).attr('data-drupal-link-system-path');
  303. // front page
  304. // just remove contents and stop here
  305. if(sys_path == '<front>'){
  306. backToFrontPage();
  307. return false;
  308. }
  309. var view_mode = $link.attr('viewmode');
  310. var ajax_path = parseAjaxSysPath(sys_path, view_mode);
  311. $link.addClass('ajax-loading');
  312. if($link.is('[selector]')){
  313. var selector = $link.attr('selector');
  314. }
  315. ajaxLoadContent(url, sys_path, ajax_path, selector);
  316. return false;
  317. };
  318. // ___
  319. // / __|___ _ _ _ __ _ _ ___
  320. // | (__/ _ \ '_| '_ \ || (_-<
  321. // \___\___/_| | .__/\_,_/__/
  322. // |_|
  323. function onCorpusMapReady(e){
  324. //console.log('theme : onCorpusReady', e);
  325. _$corpus_canvas = $('canvas#corpus-map');
  326. _$corpus_canvas
  327. .on('corpus-cliked-on-map', function(e) {
  328. //console.log('theme : corpus-cliked-on-map');
  329. backToFrontPage();
  330. })
  331. .on('corpus-cliked-on-node', function(e) {
  332. //console.log('theme : corpus-cliked-on-node', e);
  333. _audioPlayer
  334. .emmit('stop-shuffle')
  335. .openDocument(e.target_node);
  336. });
  337. _randomPlayer = new RandomPlayer(e.playlist);
  338. _$body.attr('corpus-map', 'ready');
  339. }
  340. // _ _ _
  341. // /_\ _ _ __| (_)___
  342. // / _ \ || / _` | / _ \
  343. // /_/ \_\_,_\__,_|_\___/
  344. //
  345. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  346. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  347. // https://www.binarytides.com/using-html5-audio-element-javascript/
  348. function AudioPlayer(){
  349. var that = this;
  350. this.fid;
  351. this.audio = new Audio();
  352. // audio events
  353. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended"];
  354. // UI dom objects
  355. this.$container = $('<div id="audio-player">');
  356. // btns
  357. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  358. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  359. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  360. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  361. // timeline
  362. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  363. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  364. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  365. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  366. // time
  367. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  368. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  369. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  370. // favoris
  371. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  372. // cartel
  373. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  374. // hiding
  375. this.hideTimer = false;
  376. this.hideTimeMS = 10000;
  377. // history
  378. this.currentHistoricIndex = null;
  379. this.historic = [];
  380. this.shuffle_is_active = false;
  381. // object events
  382. this.event_handlers = {
  383. 'audio-open-document':[],
  384. 'audio-play':[],
  385. 'audio-pause':[],
  386. 'audio-play-next':[],
  387. 'audio-ended':[],
  388. 'stop-shuffle':[]
  389. };
  390. this.init();
  391. };
  392. AudioPlayer.prototype = {
  393. init(){
  394. // append ui to document
  395. this.$container.appendTo('header[role="banner"] .region-header');
  396. // record timeline width
  397. this.timeline_w = parseInt(this.$timeline.width());
  398. // init audio events
  399. var fn = '';
  400. for (var i = 0; i < this.audio_events.length; i++) {
  401. fn = this.audio_events[i];
  402. // capitalize first letter of event (only cosmetic :p )
  403. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  404. this.audio.addEventListener(
  405. this.audio_events[i],
  406. this[fn].bind(this),
  407. true);
  408. }
  409. // init btns events
  410. this.$previous.on('click', this.playPrevious.bind(this));
  411. this.$playpause.on('click', this.togglePlayPause.bind(this));
  412. this.$next.on('click', this.playNext.bind(this));
  413. },
  414. openDocument(node, caller){
  415. // console.log('AudioPlayer openDocument', node);
  416. if(typeof node == 'undefined'
  417. || typeof node.nid == 'undefined'
  418. || typeof node.audio_url == 'undfined'){
  419. console.warn('AudioPlayer openDocument() node is malformed', node);
  420. return false;
  421. }
  422. this.historic.push(node);
  423. this.currentHistoricIndex = this.historic.length-1;
  424. // this.shuffle_mode = shuffle_mode || false;
  425. // TODO: add an hash tag to be able to share and play audio from any where
  426. this.emmit('audio-open-document', {caller:caller});
  427. this.launch();
  428. },
  429. launch(){
  430. this.clearTimeOutToHide();
  431. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  432. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  433. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  434. try {
  435. _$corpus_canvas.trigger({
  436. 'type':'audio-node-opened',
  437. 'nid':this.historic[this.currentHistoricIndex].nid
  438. });
  439. } catch (e) {
  440. console.info('AudioPlayer : _$corpus_canvas does not exists');
  441. }
  442. this.showHidePreviousBtn();
  443. this.showHideNextBtn();
  444. this.show();
  445. },
  446. // audio functions
  447. setSRC(url){
  448. // console.log('AudioPlayer setSRC : url', url);
  449. this.audio.src = url;
  450. },
  451. onLoadedmetadata(){
  452. var rem = parseInt(this.audio.duration, 10),
  453. mins = Math.floor(rem/60,10),
  454. secs = rem - mins*60;
  455. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  456. this.updateLoadingBar();
  457. },
  458. updateLoadingBar(){
  459. this.$loader.css({
  460. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  461. });
  462. if( this.audio.buffered.end(0) < this.audio.duration ){
  463. // loop through this function until file is fully loaded
  464. var that = this;
  465. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  466. }else{
  467. //console.log('Audio fully loaded');
  468. }
  469. },
  470. onCanplay(){
  471. this.play();
  472. },
  473. play(){
  474. this.clearTimeOutToHide();
  475. this.audio.play();
  476. },
  477. playPrevious(){
  478. if(this.currentHistoricIndex > 0){
  479. this.currentHistoricIndex -= 1;
  480. this.launch();
  481. }
  482. },
  483. playNext(){
  484. if(this.currentHistoricIndex < this.historic.length-1){
  485. this.currentHistoricIndex += 1;
  486. this.launch();
  487. }else{
  488. this.emmit('audio-play-next');
  489. }
  490. },
  491. togglePlayPause(e){
  492. if(this.audio.paused){
  493. this.audio.play();
  494. }else{
  495. this.audio.pause();
  496. }
  497. },
  498. stop(){
  499. // console.log('AudioPlayer stop()');
  500. this.audio.pause();
  501. this.timeOutToHide();
  502. },
  503. // audio events
  504. onPlaying(){
  505. this.$btns.addClass('is-playing');
  506. this.emmit('audio-play');
  507. },
  508. onPause(){
  509. this.$btns.removeClass('is-playing');
  510. this.emmit('audio-pause');
  511. },
  512. onTimeupdate(){
  513. // move cursor
  514. this.$cursor.css({
  515. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  516. });
  517. // update time text display
  518. var rem = parseInt(this.audio.currentTime, 10),
  519. mins = Math.floor(rem/60,10),
  520. secs = rem - mins*60;
  521. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  522. },
  523. onEnded(){
  524. this.emmit('audio-ended');
  525. this.stop();
  526. },
  527. // cartel functions
  528. loadNode(nid){
  529. this.$cartel.addClass('loading');
  530. var vm = 'player_cartel';
  531. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  532. var path = window.location.origin + Drupal.url(ajax_path);
  533. $.getJSON(path, {})
  534. .done(this.onNodeLoaded.bind(this))
  535. .fail(this.onNodeLoadFail.bind(this));
  536. },
  537. onNodeLoaded(data){
  538. // console.log('AudioPlayer node loaded');
  539. this.$cartel.html(data.rendered).removeClass('loading');
  540. _$body.trigger({'type':'new-audio-cartel-loaded'});
  541. initAjaxLinks();
  542. },
  543. onNodeLoadFail(jqxhr, textStatus, error){
  544. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  545. this.$cartel.removeClass('loading').html('');
  546. },
  547. // global
  548. show(){
  549. this.$container.addClass('visible');
  550. },
  551. showHidePreviousBtn(){
  552. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  553. this.$previous.addClass('is-active');
  554. }else{
  555. this.$previous.removeClass('is-active');
  556. }
  557. },
  558. showHideNextBtn(){
  559. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  560. this.$next.addClass('is-active');
  561. }else{
  562. this.$next.removeClass('is-active');
  563. }
  564. },
  565. timeOutToHide(){
  566. // console.log('AudioPlayer timeOutToHide()');
  567. this.clearTimeOutToHide();
  568. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  569. },
  570. clearTimeOutToHide(){
  571. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  572. if(this.hideTimer){
  573. clearTimeout(this.hideTimer);
  574. this.hideTimer = false;
  575. }
  576. },
  577. hide(){
  578. // console.log('AudioPlayer hide()');
  579. this.$container.removeClass('visible');
  580. // trigger highlighted node remove on corpus map
  581. try {
  582. _$corpus_canvas.trigger('audio-node-closed');
  583. } catch (e) {
  584. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  585. }
  586. },
  587. // object events
  588. on(event_name, handler){
  589. if(typeof this.event_handlers[event_name] == 'undefined'){
  590. console.warn('AudioPlayer : event '+event_name+' does not exists');
  591. }
  592. this.event_handlers[event_name].push(handler);
  593. return this;
  594. },
  595. emmit(event_name, args){
  596. // console.log('AudioPlayer emmit() event_name', event_name);
  597. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  598. var handler;
  599. var args = args || {};
  600. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  601. handler = this.event_handlers[event_name][i];
  602. // console.log('AudioPlayer emmit() loop handler', handler);
  603. setTimeout(function(){
  604. // console.log('AudioPlayer emmit() timeout handler', handler);
  605. handler(args);
  606. }, 0);
  607. }
  608. return this;
  609. },
  610. }
  611. // ___ _ ___ _
  612. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  613. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  614. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  615. // |__/
  616. function RandomPlayer(playlist){
  617. this.active = false;
  618. this.playlist = playlist;
  619. this.$btn = $('<a>').html('Shuffle').addClass('random-player-btn');
  620. this.init();
  621. };
  622. RandomPlayer.prototype = {
  623. init(){
  624. // this.shuffle();
  625. $('<div>')
  626. .addClass('block random-player')
  627. .append(this.$btn)
  628. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  629. .prependTo('.region-footer-right');
  630. // events
  631. this.$btn.on('click', this.toggleActive.bind(this));
  632. // attach an event on AudioPlayer
  633. _audioPlayer
  634. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  635. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  636. .on('stop-shuffle', this.stop.bind(this));
  637. },
  638. shuffle(){
  639. var tempPLaylist = [];
  640. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  641. tempPLaylist.push(this.playlist[i]);
  642. }
  643. this.shuffledPlaylist = [];
  644. while(tempPLaylist.length > 0){
  645. var r = Math.floor(Math.random() * tempPLaylist.length);
  646. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  647. }
  648. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  649. },
  650. toggleActive(e){
  651. if (this.active) {
  652. this.stop();
  653. }else{
  654. this.start();
  655. }
  656. },
  657. start(){
  658. this.active = _audioPlayer.shuffle_is_active = true;
  659. this.$btn.addClass('is-active');
  660. this.shuffle();
  661. this.next();
  662. },
  663. stop(){
  664. this.active = _audioPlayer.shuffle_is_active = false;
  665. this.$btn.removeClass('is-active');
  666. // stop audio player
  667. // _audioPlayer.stop();
  668. },
  669. next(){
  670. if(this.active && this.shuffledPlaylist.length > 0)
  671. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0]);
  672. },
  673. onAudioPlayNext(){
  674. //console.log('RandomPlayer : onAudioPlayNext()');
  675. this.next();
  676. },
  677. onAudioPlayerEnded(){
  678. //console.log('RandomPlayer : onAudioPlayerEnded()');
  679. this.next();
  680. }
  681. };
  682. // ___ ___ _
  683. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  684. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  685. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  686. // |_| |__/
  687. function CompoPlayer(){
  688. this.active = false;
  689. this.playing = false;
  690. this.paused = false;
  691. this.playlist = [];
  692. this.current_index = 0;
  693. this.$composer = null;
  694. this.$compo = null;
  695. this.$controls = null;
  696. this.init();
  697. };
  698. CompoPlayer.prototype = {
  699. init(){
  700. // console.log('CompoPlayer init()');
  701. // attach an event on AudioPlayer
  702. _audioPlayer
  703. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  704. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  705. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  706. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  707. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  708. // this.newCompo();
  709. },
  710. newCompo(){
  711. //console.log('CompoPlayer newCompo()');
  712. // this.$compo = $('.composition_ui .composer .composition');
  713. this.initControls();
  714. },
  715. initControls(){
  716. //console.log('CompoPlayer initControls()');
  717. this.$composer = $('.composition_ui .composer');
  718. this.$compo = $('.composition_ui .composer .composition');
  719. this.$controls = $('.composition_ui .composer .compo-player-controls');
  720. if(!this.$controls.is('.ready') && this.$compo){
  721. this.$previous = $('<div>').addClass('previous')
  722. .on('click', this.prev.bind(this))
  723. .appendTo(this.$controls);
  724. this.$playpause = $('<div>').addClass('play-pause')
  725. .on('click', this.togglePlayPause.bind(this))
  726. .appendTo(this.$controls);
  727. this.$next = $('<div>').addClass('next')
  728. .on('click', this.next.bind(this))
  729. .appendTo(this.$controls);
  730. this.$controls.addClass('ready');
  731. this.refresh();
  732. this.active = true;
  733. // this.newCompo();
  734. }
  735. },
  736. refresh(){
  737. // console.log('CompoPlayer refresh(), this', this);
  738. this.stop();
  739. // load new playlist
  740. this.playlist = [];
  741. var that = this;
  742. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  743. var $link = $('a.audio-link',this);
  744. that.playlist.push({
  745. item:$(this),
  746. audio_url:$link.attr("audio_url"),
  747. nid:$link.attr("nid"),
  748. });
  749. });
  750. this.showHideControls();
  751. },
  752. togglePlayPause(){
  753. // console.log('CompoPlayer togglePlayPause');
  754. if (this.playing && !this.paused) {
  755. this.pause();
  756. }else{
  757. if(this.playing && this.paused){
  758. this.play();
  759. }else{
  760. this.start();
  761. }
  762. }
  763. },
  764. start(){
  765. //console.log('start');
  766. // console.log('CompoPlayer start()');
  767. this.playing = true;
  768. this.play();
  769. },
  770. play(){
  771. // console.log('play');
  772. if(this.paused){
  773. this.paused = false;
  774. _audioPlayer.play();
  775. }else{
  776. _audioPlayer.openDocument(this.playlist[this.current_index], this);
  777. }
  778. this.setActiveItem().showHideControls();
  779. },
  780. pause(){
  781. //console.log('pause');
  782. this.paused = true;
  783. this.showHideControls();
  784. _audioPlayer.stop();
  785. },
  786. next(){
  787. // console.log('CompoPlayer next()');
  788. if(this.playing){
  789. this.current_index += 1;
  790. if(this.current_index < this.playlist.length){
  791. this.play();
  792. }else{
  793. this.stop();
  794. }
  795. }
  796. },
  797. prev(){
  798. // console.log('CompoPlayer prev()');
  799. if(this.playing){
  800. this.current_index -= 1;
  801. if(this.current_index >= 0){
  802. this.play();
  803. }else{
  804. this.stop();
  805. }
  806. }
  807. },
  808. stop(){
  809. _audioPlayer.stop();
  810. this.reset();
  811. },
  812. reset(){
  813. this.playing = false;
  814. this.paused = false;
  815. this.resetIndex();
  816. },
  817. resetIndex(){
  818. this.current_index = 0;
  819. this.showHideControls().resetActiveItems();
  820. },
  821. setActiveItem(){
  822. this.resetActiveItems();
  823. if(this.playing && this.current_index >= 0){
  824. this.playlist[this.current_index].item.addClass('is-active');
  825. }
  826. // this call shoud not be here
  827. this.showHideControls();
  828. return this;
  829. },
  830. resetActiveItems(){
  831. for (var n = 0; n < this.playlist.length; n++) {
  832. // console.log('node',node);
  833. this.playlist[n].item.removeClass('is-active');
  834. }
  835. return this;
  836. },
  837. showHideControls(){
  838. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  839. // global playing
  840. if(this.$controls){
  841. if(this.playing && !this.paused){
  842. this.$controls.addClass('is-playing');
  843. }else{
  844. this.$controls.removeClass('is-playing');
  845. }
  846. }
  847. // playpause
  848. if(this.$playpause){
  849. if(this.playlist.length > 0){
  850. this.$playpause.addClass('is-active');
  851. }else{
  852. this.$playpause.removeClass('is-active');
  853. }
  854. }
  855. // next
  856. if(this.$next){
  857. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  858. this.$next.addClass('is-active');
  859. }else{
  860. this.$next.removeClass('is-active');
  861. }
  862. }
  863. // previous
  864. if(this.$previous){
  865. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  866. this.$previous.addClass('is-active');
  867. }else{
  868. this.$previous.removeClass('is-active');
  869. }
  870. }
  871. return this;
  872. },
  873. deactivate(){
  874. this.stop();
  875. this.active = false;
  876. },
  877. // _audioPlayer events
  878. onAudioOpenDocument(args){
  879. if(args.caller !== this){
  880. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  881. this.reset();
  882. }
  883. // else{
  884. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  885. // }
  886. },
  887. onAudioPlayerPlay(){
  888. if(this.playing && this.paused){
  889. this.paused = false;
  890. this.showHideControls();
  891. }
  892. },
  893. onAudioPlayerPause(){
  894. if(this.playing && !this.paused){
  895. this.paused = true;
  896. this.showHideControls();
  897. }
  898. },
  899. onAudioPlayerEnded(){
  900. this.next();
  901. },
  902. // onAudioPlayNext(){
  903. // this.next();
  904. // }
  905. };
  906. // ___ _ ___
  907. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  908. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  909. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  910. // |___/
  911. function backToFrontPage(){
  912. closeAllModals();
  913. // assume we are going back to front page
  914. $('body').removeClass().addClass('path-frontpage');
  915. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  916. }
  917. function initHome(){
  918. addCloseBtnToCols();
  919. // console.log('theme : initHome');
  920. // console.log('theme : initProductions');
  921. var $grid = $('.grid',_$row).masonry({
  922. itemSelector:'.col',
  923. columnWidth:'.col-2',
  924. horizontalOrder: true,
  925. containerStyle: null,
  926. // disable initial layout
  927. // initLayout: false,
  928. });
  929. // bind event
  930. // $grid.masonry( 'on', 'layoutComplete', function() {
  931. // console.log('layout is complete');
  932. // });
  933. // layout Masonry after each image loads
  934. $grid.imagesLoaded().progress( function() {
  935. $grid.masonry('layout');
  936. });
  937. $grid.imagesLoaded(function(){
  938. $grid.masonry('layout');
  939. });
  940. }
  941. // ___ _ _ _
  942. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  943. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  944. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  945. function initProductions(){
  946. // console.log('theme : initProductions');
  947. var $grid = $('.grid',_$row).masonry({
  948. itemSelector:'.col',
  949. columnWidth:'.col-2',
  950. horizontalOrder: true,
  951. containerStyle: null,
  952. // disable initial layout
  953. // initLayout: false,
  954. });
  955. // bind event
  956. // $grid.masonry( 'on', 'layoutComplete', function() {
  957. // console.log('layout is complete');
  958. // });
  959. // layout Masonry after each image loads
  960. $grid.imagesLoaded().progress( function() {
  961. $grid.masonry('layout');
  962. });
  963. $grid.imagesLoaded(function(){
  964. $grid.masonry('layout');
  965. });
  966. };
  967. // __ __ _ _
  968. // | \/ |___ __| |__ _| |___
  969. // | |\/| / _ \/ _` / _` | (_-<
  970. // |_| |_\___/\__,_\__,_|_/__/
  971. function closeAllModals(){
  972. //console.log('theme : closeAllModals');
  973. // TODO: animate the remove
  974. _$row.html('');
  975. _$ajaxLinks.removeClass('is-active');
  976. _$body.trigger({'type':'all-modal-closed'});
  977. };
  978. // _ _ _
  979. // | || |___| |_ __ ___ _ _ ___
  980. // | __ / -_) | '_ \/ -_) '_(_-<
  981. // |_||_\___|_| .__/\___|_| /__/
  982. // |_|
  983. // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
  984. function getCookie(name) {
  985. var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  986. return v ? v[2] : null;
  987. }
  988. function setCookie(name, value, days) {
  989. var d = new Date;
  990. d.setTime(d.getTime() + 24*60*60*1000*days);
  991. document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
  992. }
  993. function deleteCookie(name) { setCookie(name, '', -1); }
  994. init();
  995. } // end EdlpTheme()
  996. $(document).ready(function($) {
  997. var edlptheme = new EdlpTheme();
  998. });
  999. })(jQuery, Drupal, drupalSettings);