main.js 35 KB

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