main.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077
  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 : data', data);
  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. if(origin_path){
  229. var path_elements = origin_path.split('~~');
  230. // console.log(path_elements);
  231. // create history state
  232. var state = {
  233. ajax_path: parseAjaxSysPath(path_elements[0]),
  234. sys_path: path_elements[0],
  235. };
  236. // record history state
  237. history.replaceState(state, null, path_elements[1]);
  238. // load content through ajax
  239. ajaxLoadContent(null, state.sys_path, state.ajax_path);
  240. // reset the cookie
  241. deleteCookie('edlp_origin_path');
  242. }else{
  243. history.replaceState({home:true}, null, url);
  244. _$body.attr('booted', 'booted');
  245. }
  246. };
  247. function onHistoryPopState(e){
  248. //console.log('onPopState',e);
  249. if(e.state.home){
  250. backToFrontPage();
  251. }else{
  252. ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  253. }
  254. };
  255. // _ _ _ _ _
  256. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  257. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  258. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  259. // |__/
  260. function initAjaxLinks(){
  261. // console.log('initAjaxLinks');
  262. $('a', '#block-mainnavigation')
  263. .add('a', '#block-footer.menu--footer')
  264. .add('a', '#block-productions')
  265. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  266. .add('a', '.productions-subtree')
  267. .add('a', '.productions-parent')
  268. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  269. .addClass('ajax-link');
  270. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  271. .each(function(i,e){
  272. var $this = $(this);
  273. // avoid already ajaxified links
  274. if($this.is('.ajax-enable')) return;
  275. if($this.attr('data-drupal-link-system-path')){
  276. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  277. }
  278. });
  279. };
  280. function onClickAjaxLink(e){
  281. e.preventDefault();
  282. var $link = $(this);
  283. if($link.is('.is-active'))
  284. return false;
  285. // Audio links
  286. // launch audio player and stop here
  287. if($link.is('.audio-link')){
  288. _audioPlayer
  289. .emmit('stop-shuffle')
  290. .openDocument({
  291. nid:$link.attr('nid'),
  292. audio_url:$link.attr('audio_url')
  293. });
  294. return false;
  295. }
  296. // other links
  297. var url = $(this).attr('href');
  298. var sys_path = $(this).attr('data-drupal-link-system-path');
  299. // front page
  300. // just remove contents and stop here
  301. if(sys_path == '<front>'){
  302. backToFrontPage();
  303. return false;
  304. }
  305. var view_mode = $link.attr('viewmode');
  306. var ajax_path = parseAjaxSysPath(sys_path, view_mode);
  307. $link.addClass('ajax-loading');
  308. if($link.is('[selector]')){
  309. var selector = $link.attr('selector');
  310. }
  311. ajaxLoadContent(url, sys_path, ajax_path, selector);
  312. return false;
  313. };
  314. // ___
  315. // / __|___ _ _ _ __ _ _ ___
  316. // | (__/ _ \ '_| '_ \ || (_-<
  317. // \___\___/_| | .__/\_,_/__/
  318. // |_|
  319. function onCorpusMapReady(e){
  320. //console.log('theme : onCorpusReady', e);
  321. _$corpus_canvas = $('canvas#corpus-map');
  322. _$corpus_canvas
  323. .on('corpus-cliked-on-map', function(e) {
  324. //console.log('theme : corpus-cliked-on-map');
  325. backToFrontPage();
  326. })
  327. .on('corpus-cliked-on-node', function(e) {
  328. //console.log('theme : corpus-cliked-on-node', e);
  329. _audioPlayer
  330. .emmit('stop-shuffle')
  331. .openDocument(e.target_node);
  332. });
  333. _randomPlayer = new RandomPlayer(e.playlist);
  334. _$body.attr('corpus-map', 'ready');
  335. }
  336. // _ _ _
  337. // /_\ _ _ __| (_)___
  338. // / _ \ || / _` | / _ \
  339. // /_/ \_\_,_\__,_|_\___/
  340. //
  341. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  342. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  343. // https://www.binarytides.com/using-html5-audio-element-javascript/
  344. function AudioPlayer(){
  345. var that = this;
  346. this.fid;
  347. this.audio = new Audio();
  348. // audio events
  349. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended"];
  350. // UI dom objects
  351. this.$container = $('<div id="audio-player">');
  352. // btns
  353. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  354. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  355. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  356. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  357. // timeline
  358. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  359. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  360. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  361. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  362. // time
  363. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  364. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  365. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  366. // favoris
  367. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  368. // cartel
  369. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  370. // hiding
  371. this.hideTimer = false;
  372. this.hideTimeMS = 10000;
  373. // history
  374. this.currentHistoricIndex = null;
  375. this.historic = [];
  376. this.shuffle_is_active = false;
  377. // object events
  378. this.event_handlers = {
  379. 'audio-open-document':[],
  380. 'audio-play':[],
  381. 'audio-pause':[],
  382. 'audio-play-next':[],
  383. 'audio-ended':[],
  384. 'stop-shuffle':[]
  385. };
  386. this.init();
  387. };
  388. AudioPlayer.prototype = {
  389. init(){
  390. // append ui to document
  391. this.$container.appendTo('header[role="banner"] .region-header');
  392. // record timeline width
  393. this.timeline_w = parseInt(this.$timeline.width());
  394. // init audio events
  395. var fn = '';
  396. for (var i = 0; i < this.audio_events.length; i++) {
  397. fn = this.audio_events[i];
  398. // capitalize first letter of event (only cosmetic :p )
  399. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  400. this.audio.addEventListener(
  401. this.audio_events[i],
  402. this[fn].bind(this),
  403. true);
  404. }
  405. // init btns events
  406. this.$previous.on('click', this.playPrevious.bind(this));
  407. this.$playpause.on('click', this.togglePlayPause.bind(this));
  408. this.$next.on('click', this.playNext.bind(this));
  409. },
  410. openDocument(node, caller){
  411. // console.log('AudioPlayer openDocument', node);
  412. if(typeof node == 'undefined'
  413. || typeof node.nid == 'undefined'
  414. || typeof node.audio_url == 'undfined'){
  415. console.warn('AudioPlayer openDocument() node is malformed', node);
  416. return false;
  417. }
  418. this.historic.push(node);
  419. this.currentHistoricIndex = this.historic.length-1;
  420. // this.shuffle_mode = shuffle_mode || false;
  421. // TODO: add an hash tag to be able to share and play audio from any where
  422. this.emmit('audio-open-document', {caller:caller});
  423. this.launch();
  424. },
  425. launch(){
  426. this.clearTimeOutToHide();
  427. this.setSRC(this.historic[this.currentHistoricIndex].audio_url);
  428. this.loadNode(this.historic[this.currentHistoricIndex].nid);
  429. // emmit new playing doc (e.g.: corpus map nowing that audio played from RandomPlayer)
  430. try {
  431. _$corpus_canvas.trigger({
  432. 'type':'audio-node-opened',
  433. 'nid':this.historic[this.currentHistoricIndex].nid
  434. });
  435. } catch (e) {
  436. console.info('AudioPlayer : _$corpus_canvas does not exists');
  437. }
  438. this.showHidePreviousBtn();
  439. this.showHideNextBtn();
  440. this.show();
  441. },
  442. // audio functions
  443. setSRC(url){
  444. // console.log('AudioPlayer setSRC : url', url);
  445. this.audio.src = url;
  446. },
  447. onLoadedmetadata(){
  448. var rem = parseInt(this.audio.duration, 10),
  449. mins = Math.floor(rem/60,10),
  450. secs = rem - mins*60;
  451. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  452. this.updateLoadingBar();
  453. },
  454. updateLoadingBar(){
  455. this.$loader.css({
  456. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  457. });
  458. if( this.audio.buffered.end(0) < this.audio.duration ){
  459. // loop through this function until file is fully loaded
  460. var that = this;
  461. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  462. }else{
  463. //console.log('Audio fully loaded');
  464. }
  465. },
  466. onCanplay(){
  467. this.play();
  468. },
  469. play(){
  470. this.clearTimeOutToHide();
  471. this.audio.play();
  472. },
  473. playPrevious(){
  474. if(this.currentHistoricIndex > 0){
  475. this.currentHistoricIndex -= 1;
  476. this.launch();
  477. }
  478. },
  479. playNext(){
  480. if(this.currentHistoricIndex < this.historic.length-1){
  481. this.currentHistoricIndex += 1;
  482. this.launch();
  483. }else{
  484. this.emmit('audio-play-next');
  485. }
  486. },
  487. togglePlayPause(e){
  488. if(this.audio.paused){
  489. this.audio.play();
  490. }else{
  491. this.audio.pause();
  492. }
  493. },
  494. stop(){
  495. // console.log('AudioPlayer stop()');
  496. this.audio.pause();
  497. this.timeOutToHide();
  498. },
  499. // audio events
  500. onPlaying(){
  501. this.$btns.addClass('is-playing');
  502. this.emmit('audio-play');
  503. },
  504. onPause(){
  505. this.$btns.removeClass('is-playing');
  506. this.emmit('audio-pause');
  507. },
  508. onTimeupdate(){
  509. // move cursor
  510. this.$cursor.css({
  511. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  512. });
  513. // update time text display
  514. var rem = parseInt(this.audio.currentTime, 10),
  515. mins = Math.floor(rem/60,10),
  516. secs = rem - mins*60;
  517. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  518. },
  519. onEnded(){
  520. this.emmit('audio-ended');
  521. this.stop();
  522. },
  523. // cartel functions
  524. loadNode(nid){
  525. this.$cartel.addClass('loading');
  526. var vm = 'player_cartel';
  527. var ajax_path = _ajax_settings.entityjson_path+'/node/'+nid+'/'+vm;
  528. var path = window.location.origin + Drupal.url(ajax_path);
  529. $.getJSON(path, {})
  530. .done(this.onNodeLoaded.bind(this))
  531. .fail(this.onNodeLoadFail.bind(this));
  532. },
  533. onNodeLoaded(data){
  534. // console.log('AudioPlayer node loaded');
  535. this.$cartel.html(data.rendered).removeClass('loading');
  536. _$body.trigger({'type':'new-audio-cartel-loaded'});
  537. initAjaxLinks();
  538. },
  539. onNodeLoadFail(jqxhr, textStatus, error){
  540. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  541. this.$cartel.removeClass('loading').html('');
  542. },
  543. // global
  544. show(){
  545. this.$container.addClass('visible');
  546. },
  547. showHidePreviousBtn(){
  548. if(this.historic.length > 1 && this.currentHistoricIndex > 0){
  549. this.$previous.addClass('is-active');
  550. }else{
  551. this.$previous.removeClass('is-active');
  552. }
  553. },
  554. showHideNextBtn(){
  555. if(this.currentHistoricIndex < this.historic.length-1 || this.shuffle_is_active){
  556. this.$next.addClass('is-active');
  557. }else{
  558. this.$next.removeClass('is-active');
  559. }
  560. },
  561. timeOutToHide(){
  562. // console.log('AudioPlayer timeOutToHide()');
  563. this.clearTimeOutToHide();
  564. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  565. },
  566. clearTimeOutToHide(){
  567. // console.log('AudioPlayer clearTimeOutToHide()',this.hideTimer);
  568. if(this.hideTimer){
  569. clearTimeout(this.hideTimer);
  570. this.hideTimer = false;
  571. }
  572. },
  573. hide(){
  574. // console.log('AudioPlayer hide()');
  575. this.$container.removeClass('visible');
  576. // trigger highlighted node remove on corpus map
  577. try {
  578. _$corpus_canvas.trigger('audio-node-closed');
  579. } catch (e) {
  580. console.info('AudioPlayer hide() : _$corpus_canvas does not exists');
  581. }
  582. },
  583. // object events
  584. on(event_name, handler){
  585. if(typeof this.event_handlers[event_name] == 'undefined'){
  586. console.warn('AudioPlayer : event '+event_name+' does not exists');
  587. }
  588. this.event_handlers[event_name].push(handler);
  589. return this;
  590. },
  591. emmit(event_name, args){
  592. // console.log('AudioPlayer emmit() event_name', event_name);
  593. // console.log('AudioPlayer emmit() handlers', this.event_handlers[event_name]);
  594. var handler;
  595. var args = args || {};
  596. for (var i = this.event_handlers[event_name].length-1; i >= 0 ; i--) {
  597. handler = this.event_handlers[event_name][i];
  598. // console.log('AudioPlayer emmit() loop handler', handler);
  599. setTimeout(function(){
  600. // console.log('AudioPlayer emmit() timeout handler', handler);
  601. handler(args);
  602. }, 0);
  603. }
  604. return this;
  605. },
  606. }
  607. // ___ _ ___ _
  608. // | _ \__ _ _ _ __| |___ _ __ | _ \ |__ _ _ _ ___ _ _
  609. // | / _` | ' \/ _` / _ \ ' \| _/ / _` | || / -_) '_|
  610. // |_|_\__,_|_||_\__,_\___/_|_|_|_| |_\__,_|\_, \___|_|
  611. // |__/
  612. function RandomPlayer(playlist){
  613. this.active = false;
  614. this.playlist = playlist;
  615. this.$btn = $('<a>').html('Shuffle').addClass('random-player-btn');
  616. this.init();
  617. };
  618. RandomPlayer.prototype = {
  619. init(){
  620. // this.shuffle();
  621. $('<div>')
  622. .addClass('block random-player')
  623. .append(this.$btn)
  624. // .insertAfter('#block-userlogin, #block-studiolinkblock');
  625. .prependTo('.region-footer-right');
  626. // events
  627. this.$btn.on('click', this.toggleActive.bind(this));
  628. // attach an event on AudioPlayer
  629. _audioPlayer
  630. .on('audio-ended', this.onAudioPlayerEnded.bind(this))
  631. .on('audio-play-next', this.onAudioPlayNext.bind(this))
  632. .on('stop-shuffle', this.stop.bind(this));
  633. },
  634. shuffle(){
  635. var tempPLaylist = [];
  636. for (var i = this.playlist.length-1; i >= 0 ; i--) {
  637. tempPLaylist.push(this.playlist[i]);
  638. }
  639. this.shuffledPlaylist = [];
  640. while(tempPLaylist.length > 0){
  641. var r = Math.floor(Math.random() * tempPLaylist.length);
  642. this.shuffledPlaylist.push(tempPLaylist.splice(r,1)[0]);
  643. }
  644. //console.log('RandomPlayer, this.shuffledPlaylist', this.shuffledPlaylist);
  645. },
  646. toggleActive(e){
  647. if (this.active) {
  648. this.stop();
  649. }else{
  650. this.start();
  651. }
  652. },
  653. start(){
  654. this.active = _audioPlayer.shuffle_is_active = true;
  655. this.$btn.addClass('is-active');
  656. this.shuffle();
  657. this.next();
  658. },
  659. stop(){
  660. this.active = _audioPlayer.shuffle_is_active = false;
  661. this.$btn.removeClass('is-active');
  662. // stop audio player
  663. // _audioPlayer.stop();
  664. },
  665. next(){
  666. if(this.active && this.shuffledPlaylist.length > 0)
  667. _audioPlayer.openDocument(this.shuffledPlaylist.splice(0,1)[0]);
  668. },
  669. onAudioPlayNext(){
  670. //console.log('RandomPlayer : onAudioPlayNext()');
  671. this.next();
  672. },
  673. onAudioPlayerEnded(){
  674. //console.log('RandomPlayer : onAudioPlayerEnded()');
  675. this.next();
  676. }
  677. };
  678. // ___ ___ _
  679. // / __|___ _ __ _ __ ___| _ \ |__ _ _ _ ___ _ _
  680. // | (__/ _ \ ' \| '_ \/ _ \ _/ / _` | || / -_) '_|
  681. // \___\___/_|_|_| .__/\___/_| |_\__,_|\_, \___|_|
  682. // |_| |__/
  683. function CompoPlayer(){
  684. this.active = false;
  685. this.playing = false;
  686. this.paused = false;
  687. this.playlist = [];
  688. this.current_index = 0;
  689. this.$composer = null;
  690. this.$compo = null;
  691. this.$controls = null;
  692. this.init();
  693. };
  694. CompoPlayer.prototype = {
  695. init(){
  696. // console.log('CompoPlayer init()');
  697. // attach an event on AudioPlayer
  698. _audioPlayer
  699. .on('audio-open-document', this.onAudioOpenDocument.bind(this))
  700. .on('audio-play', this.onAudioPlayerPlay.bind(this))
  701. .on('audio-pause', this.onAudioPlayerPause.bind(this))
  702. .on('audio-ended', this.onAudioPlayerEnded.bind(this));
  703. // .on('audio-play-next', this.onAudioPlayNext.bind(this));
  704. // this.newCompo();
  705. },
  706. newCompo(){
  707. //console.log('CompoPlayer newCompo()');
  708. // this.$compo = $('.composition_ui .composer .composition');
  709. this.initControls();
  710. },
  711. initControls(){
  712. //console.log('CompoPlayer initControls()');
  713. this.$composer = $('.composition_ui .composer');
  714. this.$compo = $('.composition_ui .composer .composition');
  715. this.$controls = $('.composition_ui .composer .compo-player-controls');
  716. if(!this.$controls.is('.ready') && this.$compo){
  717. this.$previous = $('<div>').addClass('previous')
  718. .on('click', this.prev.bind(this))
  719. .appendTo(this.$controls);
  720. this.$playpause = $('<div>').addClass('play-pause')
  721. .on('click', this.togglePlayPause.bind(this))
  722. .appendTo(this.$controls);
  723. this.$next = $('<div>').addClass('next')
  724. .on('click', this.next.bind(this))
  725. .appendTo(this.$controls);
  726. this.$controls.addClass('ready');
  727. this.refresh();
  728. this.active = true;
  729. // this.newCompo();
  730. }
  731. },
  732. refresh(){
  733. // console.log('CompoPlayer refresh(), this', this);
  734. this.stop();
  735. // load new playlist
  736. this.playlist = [];
  737. var that = this;
  738. $('.field--name-documents .field__item',this.$compo).each(function(i,el){
  739. var $link = $('a.audio-link',this);
  740. that.playlist.push({
  741. item:$(this),
  742. audio_url:$link.attr("audio_url"),
  743. nid:$link.attr("nid"),
  744. });
  745. });
  746. this.showHideControls();
  747. },
  748. togglePlayPause(){
  749. // console.log('CompoPlayer togglePlayPause');
  750. if (this.playing && !this.paused) {
  751. this.pause();
  752. }else{
  753. if(this.playing && this.paused){
  754. this.play();
  755. }else{
  756. this.start();
  757. }
  758. }
  759. },
  760. start(){
  761. //console.log('start');
  762. // console.log('CompoPlayer start()');
  763. this.playing = true;
  764. this.play();
  765. },
  766. play(){
  767. // console.log('play');
  768. if(this.paused){
  769. this.paused = false;
  770. _audioPlayer.play();
  771. }else{
  772. _audioPlayer.openDocument(this.playlist[this.current_index], this);
  773. }
  774. this.setActiveItem().showHideControls();
  775. },
  776. pause(){
  777. //console.log('pause');
  778. this.paused = true;
  779. this.showHideControls();
  780. _audioPlayer.stop();
  781. },
  782. next(){
  783. // console.log('CompoPlayer next()');
  784. if(this.playing){
  785. this.current_index += 1;
  786. if(this.current_index < this.playlist.length){
  787. this.play();
  788. }else{
  789. this.stop();
  790. }
  791. }
  792. },
  793. prev(){
  794. // console.log('CompoPlayer prev()');
  795. if(this.playing){
  796. this.current_index -= 1;
  797. if(this.current_index >= 0){
  798. this.play();
  799. }else{
  800. this.stop();
  801. }
  802. }
  803. },
  804. stop(){
  805. _audioPlayer.stop();
  806. this.reset();
  807. },
  808. reset(){
  809. this.playing = false;
  810. this.paused = false;
  811. this.resetIndex();
  812. },
  813. resetIndex(){
  814. this.current_index = 0;
  815. this.showHideControls().resetActiveItems();
  816. },
  817. setActiveItem(){
  818. this.resetActiveItems();
  819. if(this.playing && this.current_index >= 0){
  820. this.playlist[this.current_index].item.addClass('is-active');
  821. }
  822. // this call shoud not be here
  823. this.showHideControls();
  824. return this;
  825. },
  826. resetActiveItems(){
  827. for (var n = 0; n < this.playlist.length; n++) {
  828. // console.log('node',node);
  829. this.playlist[n].item.removeClass('is-active');
  830. }
  831. return this;
  832. },
  833. showHideControls(){
  834. // console.log('CompoPlayer showHideNextBtn(), playing:'+this.playing+', paused:'+this.paused);
  835. // global playing
  836. if(this.$controls){
  837. if(this.playing && !this.paused){
  838. this.$controls.addClass('is-playing');
  839. }else{
  840. this.$controls.removeClass('is-playing');
  841. }
  842. }
  843. // playpause
  844. if(this.$playpause){
  845. if(this.playlist.length > 0){
  846. this.$playpause.addClass('is-active');
  847. }else{
  848. this.$playpause.removeClass('is-active');
  849. }
  850. }
  851. // next
  852. if(this.$next){
  853. if(this.playing && this.playlist.length > 1 && this.current_index < this.playlist.length -1){
  854. this.$next.addClass('is-active');
  855. }else{
  856. this.$next.removeClass('is-active');
  857. }
  858. }
  859. // previous
  860. if(this.$previous){
  861. if(this.playing && this.playlist.length > 1 && this.current_index > 0){
  862. this.$previous.addClass('is-active');
  863. }else{
  864. this.$previous.removeClass('is-active');
  865. }
  866. }
  867. return this;
  868. },
  869. deactivate(){
  870. this.stop();
  871. this.active = false;
  872. },
  873. // _audioPlayer events
  874. onAudioOpenDocument(args){
  875. if(args.caller !== this){
  876. // console.log('CompoPlayer onAudioOpenDocument() called by other');
  877. this.reset();
  878. }
  879. // else{
  880. // // console.log('CompoPlayer onAudioOpenDocument() self calling');
  881. // }
  882. },
  883. onAudioPlayerPlay(){
  884. if(this.playing && this.paused){
  885. this.paused = false;
  886. this.showHideControls();
  887. }
  888. },
  889. onAudioPlayerPause(){
  890. if(this.playing && !this.paused){
  891. this.paused = true;
  892. this.showHideControls();
  893. }
  894. },
  895. onAudioPlayerEnded(){
  896. this.next();
  897. },
  898. // onAudioPlayNext(){
  899. // this.next();
  900. // }
  901. };
  902. // ___ _ ___
  903. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  904. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  905. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  906. // |___/
  907. function backToFrontPage(){
  908. closeAllModals();
  909. // assume we are going back to front page
  910. $('body').removeClass().addClass('path-frontpage');
  911. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  912. }
  913. function initHome(){
  914. addCloseBtnToCols();
  915. // console.log('theme : initHome');
  916. // console.log('theme : initProductions');
  917. var $grid = $('.grid',_$row).masonry({
  918. itemSelector:'.col',
  919. columnWidth:'.col-2',
  920. horizontalOrder: true,
  921. containerStyle: null,
  922. // disable initial layout
  923. // initLayout: false,
  924. });
  925. // bind event
  926. // $grid.masonry( 'on', 'layoutComplete', function() {
  927. // console.log('layout is complete');
  928. // });
  929. // layout Masonry after each image loads
  930. $grid.imagesLoaded().progress( function() {
  931. $grid.masonry('layout');
  932. });
  933. $grid.imagesLoaded(function(){
  934. $grid.masonry('layout');
  935. });
  936. }
  937. // ___ _ _ _
  938. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  939. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  940. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  941. function initProductions(){
  942. // console.log('theme : initProductions');
  943. var $grid = $('.grid',_$row).masonry({
  944. itemSelector:'.col',
  945. columnWidth:'.col-2',
  946. horizontalOrder: true,
  947. containerStyle: null,
  948. // disable initial layout
  949. // initLayout: false,
  950. });
  951. // bind event
  952. // $grid.masonry( 'on', 'layoutComplete', function() {
  953. // console.log('layout is complete');
  954. // });
  955. // layout Masonry after each image loads
  956. $grid.imagesLoaded().progress( function() {
  957. $grid.masonry('layout');
  958. });
  959. $grid.imagesLoaded(function(){
  960. $grid.masonry('layout');
  961. });
  962. };
  963. // __ __ _ _
  964. // | \/ |___ __| |__ _| |___
  965. // | |\/| / _ \/ _` / _` | (_-<
  966. // |_| |_\___/\__,_\__,_|_/__/
  967. function closeAllModals(){
  968. //console.log('theme : closeAllModals');
  969. // TODO: animate the remove
  970. _$row.html('');
  971. _$ajaxLinks.removeClass('is-active');
  972. _$body.trigger({'type':'all-modal-closed'});
  973. };
  974. // _ _ _
  975. // | || |___| |_ __ ___ _ _ ___
  976. // | __ / -_) | '_ \/ -_) '_(_-<
  977. // |_||_\___|_| .__/\___|_| /__/
  978. // |_|
  979. // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
  980. function getCookie(name) {
  981. var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  982. return v ? v[2] : null;
  983. }
  984. function setCookie(name, value, days) {
  985. var d = new Date;
  986. d.setTime(d.getTime() + 24*60*60*1000*days);
  987. document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
  988. }
  989. function deleteCookie(name) { setCookie(name, '', -1); }
  990. init();
  991. } // end EdlpTheme()
  992. $(document).ready(function($) {
  993. var edlptheme = new EdlpTheme();
  994. });
  995. })(jQuery, Drupal, drupalSettings);