main.js 35 KB

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