main.js 32 KB

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