main.js 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076
  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. // redirect all no-front pages to front with right url ajax load
  19. initHistory();
  20. _$body.on('corpus-map-ready', onCorpusMapReady);
  21. _audioPlayer = new AudioPlayer();
  22. _compoPlayer = new CompoPlayer();
  23. initAjaxLinks();
  24. if (_$body.is('.path-productions')) initProductions();
  25. if(_$body.is('.path-frontpage')) initHome();
  26. // initScrollbars();
  27. initEvents();
  28. };
  29. // ___ _
  30. // | __|_ _____ _ _| |_ ___
  31. // | _|\ V / -_) ' \ _(_-<
  32. // |___|\_/\___|_||_\__/__/
  33. function initEvents(){
  34. $('body')
  35. .on('on-studio-chutier-updated', initAjaxLinks)
  36. .on('studio-initialized', function(e){
  37. _compoPlayer.newCompo();
  38. })
  39. .on('studio-not-active', function(e){
  40. _compoPlayer.deactivate();
  41. })
  42. .on('on-studio-compo-updated', function(e){
  43. initAjaxLinks();
  44. _compoPlayer.refresh();
  45. })
  46. .on('on-studio-compo-opened', function(e){
  47. initAjaxLinks();
  48. _compoPlayer.newCompo();
  49. })
  50. .on('search-results-loaded', initAjaxLinks)
  51. .on('open_entree', closeAllModals)
  52. .on('close_entree', backToFrontPage);
  53. }
  54. // ___ _ _ ___
  55. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  56. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  57. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  58. function initScrollbars(){
  59. // console.log("initScrollbars");
  60. // TODO: find a better js scroll than overlayScrollbars which does not handle well max-height + overflow-y:auto;
  61. // $('.os-scroll').overlayScrollbars({
  62. // overflowBehavior:{
  63. // x:'h',
  64. // y:'scroll',
  65. // clipAlways:false
  66. // }
  67. // });
  68. };
  69. // _ _
  70. // /_\ (_)__ ___ __
  71. // / _ \ | / _` \ \ /
  72. // /_/ \_\/ \__,_/_\_\
  73. // |__/
  74. function parseAjaxSysPath(sys_path, view_mode){
  75. // console.log('Theme : parseAjaxSysPath', sys_path);
  76. var ajax_path = sys_path;
  77. // convert node link to edlp_ajax_node module links
  78. var node_match = ajax_path.match(/^\/?(node\/\d+)$/g);
  79. var term_match = ajax_path.match(/^\/?(taxonomy\/term\/\d+)$/g);
  80. if(node_match){
  81. ajax_path = _ajax_settings.entityjson_path+'/'+node_match[0];
  82. // check for viewmode attribute
  83. if(view_mode){
  84. ajax_path += '/'+view_mode;
  85. }
  86. }else if(term_match){
  87. ajax_path = _ajax_settings.entityjson_path+'/'+term_match[0];
  88. ajax_path = ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  89. // check for viewmode attribute
  90. if(view_mode){
  91. ajax_path += '/'+view_mode;
  92. }
  93. }else{
  94. // convert other link to ajax
  95. // TODO: we assume that other links (no node, no term) are all from own modules (e.g. productions) !! may not be true !!
  96. ajax_path += '/ajax'
  97. }
  98. return ajax_path;
  99. };
  100. function ajaxLoadContent(url, sys_path, ajax_path, selector){
  101. //console.log('ajaxLoadContent : ajax_path', ajax_path);
  102. _$body.addClass('ajax-loading');
  103. var path = window.location.origin + Drupal.url(ajax_path);
  104. $.getJSON(path, {})
  105. .done(function(data){
  106. onAjaxLoaded(data, sys_path, selector);
  107. })
  108. .fail(function(jqxhr, textStatus, error){
  109. onAjaxLoadError(jqxhr, textStatus, error, sys_path);
  110. });
  111. var state = {
  112. ajax_path:ajax_path,
  113. sys_path:sys_path,
  114. };
  115. // url is null means that we are loading content on popState event
  116. // so we don't record the state again
  117. if(url){
  118. // console.log('url:'+url+' ; state',state);
  119. // we can not pushestate with absolute url
  120. history.pushState(state, null, url);
  121. }
  122. };
  123. function onAjaxLoadError(jqxhr, textStatus, error, sys_path){
  124. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  125. $('.ajax-loading').removeClass('ajax-loading');
  126. _$body.removeClass('ajax-loading');
  127. };
  128. function onAjaxLoaded(data, sys_path, selector){
  129. console.log('ajax link loaded : data', data);
  130. // reset all style may been added by other pages (like masonry for productions)
  131. // and replace all content with newly loaded
  132. // TODO: build a system to replace or append contents (like studio + search)
  133. _$row.removeAttr('style').html(data.rendered);
  134. // add close btn
  135. if(sys_path != 'productions'){
  136. addCloseBtnToCols();
  137. }
  138. // add body class for currently loaded content
  139. var body_classes = [
  140. 'path-'+sys_path.replace(/\//g, '-'),
  141. 'entity-type-'+data.entity_type,
  142. 'bundle-'+data.bundle,
  143. 'view-mode-'+data.view_mode
  144. ];
  145. _$body.removeClass().addClass(body_classes.join(' '));
  146. // id node add a generic path-node class to body
  147. m = sys_path.match(/^\/?(node\/\d+)$/g);
  148. if(m)
  149. _$body.addClass('path-edlp-node');
  150. // handle clicked link classes
  151. _$ajaxLinks.removeClass('is-active');
  152. $('.ajax-loading').removeClass('ajax-loading');
  153. // TODO: break entrees links wich all have same sys_path
  154. if(typeof selector != 'undefined'){
  155. $('a[selector="'+selector+'"]').addClass('is-active');
  156. }else{
  157. $('a[data-drupal-link-system-path="'+sys_path+'"]').addClass('is-active');
  158. }
  159. // if bundle page (productions) activate production links
  160. if (typeof data.bundle != 'undefined' && data.bundle == "page") {
  161. $('a[data-drupal-link-system-path="productions"]').addClass('is-active');
  162. }
  163. // if node is in production menu tree, set first level of tree active, e.g. pieces sonores
  164. if (typeof data.menu_parents != 'undefined') {
  165. for (var i = 0; i < data.menu_parents.length; i++) {
  166. var menu_sys_path = data.menu_parents[i];
  167. $('a[data-drupal-link-system-path="'+menu_sys_path+'"]').addClass('is-active');
  168. }
  169. }
  170. // if block attached (eg : from edlp_productions module)
  171. // not used anymore as production block is always present (but not visible)
  172. if(typeof data.block != 'undefined'){
  173. // if block not already added
  174. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  175. $('.region-'+data.block.region).append(data.block.rendered);
  176. }
  177. }
  178. // initScrollbars();
  179. if(sys_path == "productions")
  180. initProductions();
  181. initAjaxLinks();
  182. // trigger other modules behaviours
  183. _$body.trigger({'type':'new-content-ajax-loaded'});
  184. // and call druapl behaviours
  185. Drupal.attachBehaviors(_$row[0]);
  186. _$body.attr('booted', 'booted');
  187. _$body.removeClass('ajax-loading');
  188. };
  189. function addCloseBtnToCols(){
  190. $('.col', _$row).each(function(index, el) {
  191. if($('span.close-col-btn', this).length)
  192. return true;
  193. $(this).children('.wrapper').append($('<span>')
  194. .addClass('close-col-btn')
  195. .on('click', function(e){
  196. // check for theme attribute and emmit event
  197. var $col = $(this).parents('.col');
  198. var theme = $col.attr('theme');
  199. if(theme != ''){
  200. _$body.trigger({'type':theme+'-col-closed'});
  201. }
  202. // remove the col
  203. $col.remove();
  204. // if row is empty call closeAllModals()
  205. if(!$('.col', _$row).length){
  206. backToFrontPage();
  207. }
  208. })
  209. );
  210. });
  211. };
  212. // _ _ _ _
  213. // | || (_)__| |_ ___ _ _ _ _
  214. // | __ | (_-< _/ _ \ '_| || |
  215. // |_||_|_/__/\__\___/_| \_, |
  216. // |__/
  217. function initHistory(){
  218. initFirstLoad();
  219. window.addEventListener('popstate', onHistoryPopState);
  220. };
  221. function initFirstLoad(){
  222. // console.log('theme : initFirstLoad()', window.location);
  223. // console.log(document.cookie);
  224. var url = window.location.pathname;
  225. if(url != '' && url != '/'){
  226. var origin_path = getCookie('edlp_origin_path');
  227. // console.log('origin_path', origin_path);
  228. if(origin_path){
  229. var state = {
  230. ajax_path: parseAjaxSysPath(origin_path),
  231. sys_path: origin_path,
  232. };
  233. ajaxLoadContent(null, state.sys_path, state.ajax_path);
  234. history.replaceState(state, null, url);
  235. // reset the cookie
  236. deleteCookie('edlp_origin_path');
  237. deleteCookie('edlp_sys_path');
  238. }
  239. }else{
  240. history.replaceState({home:true}, null, url);
  241. _$body.attr('booted', 'booted');
  242. }
  243. };
  244. function onHistoryPopState(e){
  245. //console.log('onPopState',e);
  246. if(e.state.home){
  247. backToFrontPage();
  248. }else{
  249. ajaxLoadContent(null, e.state.sys_path, e.state.ajax_path)
  250. }
  251. };
  252. // _ _ _ _ _
  253. // /_\ (_)__ ___ _| | (_)_ _ | |__ ___
  254. // / _ \ | / _` \ \ / |__| | ' \| / /(_-<
  255. // /_/ \_\/ \__,_/_\_\____|_|_||_|_\_\/__/
  256. // |__/
  257. function initAjaxLinks(){
  258. // console.log('initAjaxLinks');
  259. $('a', '#block-mainnavigation')
  260. .add('a', '#block-footer.menu--footer')
  261. .add('a', '#block-productions')
  262. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  263. .add('a', '.productions-subtree')
  264. .add('a', '.productions-parent')
  265. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  266. // .addClass('use-ajax')
  267. .addClass('ajax-link');
  268. Drupal.ajax.bindAjaxLinks('#block-footer.menu--footer');
  269. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  270. .each(function(i,e){
  271. var $this = $(this);
  272. // avoid already ajaxified links
  273. if($this.is('.ajax-enable')) return;
  274. if($this.attr('data-drupal-link-system-path')){
  275. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  276. }
  277. });
  278. };
  279. function onClickAjaxLink(e){
  280. e.preventDefault();
  281. var $link = $(this);
  282. if($link.is('.is-active'))
  283. return false;
  284. // Audio links
  285. // launch audio player and stop here
  286. if($link.is('.audio-link')){
  287. _audioPlayer
  288. .emmit('stop-shuffle')
  289. .openDocument({
  290. nid:$link.attr('nid'),
  291. audio_url:$link.attr('audio_url')
  292. });
  293. return false;
  294. }
  295. // other links
  296. var url = $(this).attr('href');
  297. var sys_path = $(this).attr('data-drupal-link-system-path');
  298. // front page
  299. // just remove contents and stop here
  300. if(sys_path == '<front>'){
  301. backToFrontPage();
  302. return false;
  303. }
  304. var view_mode = $link.attr('viewmode');
  305. var ajax_path = parseAjaxSysPath(sys_path, view_mode);
  306. $link.addClass('ajax-loading');
  307. if($link.is('[selector]')){
  308. var selector = $link.attr('selector');
  309. }
  310. ajaxLoadContent(url, sys_path, ajax_path, selector);
  311. return false;
  312. };
  313. // ___
  314. // / __|___ _ _ _ __ _ _ ___
  315. // | (__/ _ \ '_| '_ \ || (_-<
  316. // \___\___/_| | .__/\_,_/__/
  317. // |_|
  318. function onCorpusMapReady(e){
  319. //console.log('theme : onCorpusReady', e);
  320. _$corpus_canvas = $('canvas#corpus-map');
  321. _$corpus_canvas
  322. .on('corpus-cliked-on-map', function(e) {
  323. //console.log('theme : corpus-cliked-on-map');
  324. backToFrontPage();
  325. })
  326. .on('corpus-cliked-on-node', function(e) {
  327. //console.log('theme : corpus-cliked-on-node', e);
  328. _audioPlayer
  329. .emmit('stop-shuffle')
  330. .openDocument(e.target_node);
  331. });
  332. _randomPlayer = new RandomPlayer(e.playlist);
  333. _$body.attr('corpus-map', 'ready');
  334. }
  335. // _ _ _
  336. // /_\ _ _ __| (_)___
  337. // / _ \ || / _` | / _ \
  338. // /_/ \_\_,_\__,_|_\___/
  339. //
  340. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  341. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  342. // https://www.binarytides.com/using-html5-audio-element-javascript/
  343. function AudioPlayer(){
  344. var that = this;
  345. this.fid;
  346. this.audio = new Audio();
  347. // audio events
  348. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended"];
  349. // UI dom objects
  350. this.$container = $('<div id="audio-player">');
  351. // btns
  352. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  353. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  354. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  355. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  356. // timeline
  357. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  358. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  359. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  360. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  361. // time
  362. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  363. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  364. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  365. // favoris
  366. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  367. // cartel
  368. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  369. // hiding
  370. this.hideTimer = false;
  371. this.hideTimeMS = 10000;
  372. // history
  373. this.currentHistoricIndex = null;
  374. this.historic = [];
  375. this.shuffle_is_active = false;
  376. // object events
  377. this.event_handlers = {
  378. 'audio-open-document':[],
  379. 'audio-play':[],
  380. 'audio-pause':[],
  381. 'audio-play-next':[],
  382. 'audio-ended':[],
  383. 'stop-shuffle':[]
  384. };
  385. this.init();
  386. };
  387. AudioPlayer.prototype = {
  388. init(){
  389. // append ui to document
  390. this.$container.appendTo('header[role="banner"] .region-header');
  391. // record timeline width
  392. this.timeline_w = parseInt(this.$timeline.width());
  393. // init audio events
  394. var fn = '';
  395. for (var i = 0; i < this.audio_events.length; i++) {
  396. fn = this.audio_events[i];
  397. // capitalize first letter of event (only cosmetic :p )
  398. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  399. this.audio.addEventListener(
  400. this.audio_events[i],
  401. this[fn].bind(this),
  402. true);
  403. }
  404. // init btns events
  405. this.$previous.on('click', this.playPrevious.bind(this));
  406. this.$playpause.on('click', this.togglePlayPause.bind(this));
  407. this.$next.on('click', this.playNext.bind(this));
  408. // TODO: previous and next btns
  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; // TODO: set active false
  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. $grid.imagesLoaded(function(){
  930. $grid.masonry();
  931. });
  932. }
  933. // ___ _ _ _
  934. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  935. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  936. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  937. function initProductions(){
  938. // console.log('theme : initProductions');
  939. var $grid = $('.grid',_$row).masonry({
  940. itemSelector:'.col',
  941. columnWidth:'.col-2',
  942. horizontalOrder: true,
  943. containerStyle: null,
  944. // disable initial layout
  945. // initLayout: false,
  946. });
  947. // bind event
  948. // $grid.masonry( 'on', 'layoutComplete', function() {
  949. // console.log('layout is complete');
  950. // });
  951. // layout Masonry after each image loads
  952. $grid.imagesLoaded().progress( function() {
  953. $grid.masonry('layout');
  954. });
  955. $grid.imagesLoaded(function(){
  956. $grid.masonry('layout');
  957. });
  958. };
  959. // __ __ _ _
  960. // | \/ |___ __| |__ _| |___
  961. // | |\/| / _ \/ _` / _` | (_-<
  962. // |_| |_\___/\__,_\__,_|_/__/
  963. function closeAllModals(){
  964. //console.log('theme : closeAllModals');
  965. // TODO: animate the remove
  966. _$row.html('');
  967. _$ajaxLinks.removeClass('is-active');
  968. _$body.trigger({'type':'all-modal-closed'});
  969. };
  970. // _ _ _
  971. // | || |___| |_ __ ___ _ _ ___
  972. // | __ / -_) | '_ \/ -_) '_(_-<
  973. // |_||_\___|_| .__/\___|_| /__/
  974. // |_|
  975. // https://plainjs.com/javascript/utilities/set-cookie-get-cookie-and-delete-cookie-5/
  976. function getCookie(name) {
  977. var v = document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
  978. return v ? v[2] : null;
  979. }
  980. function setCookie(name, value, days) {
  981. var d = new Date;
  982. d.setTime(d.getTime() + 24*60*60*1000*days);
  983. document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
  984. }
  985. function deleteCookie(name) { setCookie(name, '', -1); }
  986. init();
  987. } // end EdlpTheme()
  988. $(document).ready(function($) {
  989. var edlptheme = new EdlpTheme();
  990. });
  991. })(jQuery, Drupal, drupalSettings);