main.js 38 KB

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