main.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. (function($) {
  2. EdlpTheme = function(){
  3. var _$body = $('body');
  4. var _is_front = _$body.is('.path-frontpage');
  5. var _$corpus_canvas;
  6. var _$content_container = $('main[role="main"]>.layout-content');
  7. var _$ajaxLinks;
  8. function init(){
  9. console.log("EdlpTheme init()");
  10. // TODO: redirect all no-front pages to front with write hash
  11. _$body.on('corpus-map-ready', onCorpusMapReady);
  12. initAudioPlayer();
  13. initAjaxLinks();
  14. if (_$body.is('.path-productions')) {
  15. initProductions();
  16. }
  17. initScrollbars();
  18. initEvents();
  19. };
  20. function initEvents(){
  21. $('body')
  22. .on('on-studio-chutier-updated', function(e){
  23. initAjaxLinks();
  24. })
  25. .on('open_entree', function(e){
  26. // e.tid available
  27. closeAllModals();
  28. })
  29. .on('close_entree', function(e){
  30. // e.tid available
  31. closeAllModals();
  32. });
  33. }
  34. // _ _ _
  35. // /_\ _ _ __| (_)___
  36. // / _ \ || / _` | / _ \
  37. // /_/ \_\_,_\__,_|_\___/
  38. //
  39. // https://developer.mozilla.org/en-US/docs/Web/API/HTMLMediaElement
  40. // https://docs.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/samples/gg589528%28v%3dvs.85%29
  41. // https://www.binarytides.com/using-html5-audio-element-javascript/
  42. //
  43. function initAudioPlayer(){
  44. _audio_player = new AudioPlayer();
  45. };
  46. function AudioPlayer(){
  47. var that = this;
  48. this.fid;
  49. this.audio = new Audio();
  50. // audio events
  51. this.audio_events = ["loadedmetadata","canplay","playing","pause","timeupdate","ended"];
  52. // UI dom objects
  53. this.$container = $('<div id="audio-player">');
  54. // btns
  55. this.$btns = $('<div>').addClass('btns').appendTo(this.$container);
  56. this.$previous = $('<div>').addClass('previous').appendTo(this.$btns);
  57. this.$playpause = $('<div>').addClass('play-pause').appendTo(this.$btns);
  58. this.$next = $('<div>').addClass('next').appendTo(this.$btns);
  59. // timeline
  60. this.$timelinecont= $('<div>').addClass('time-line-container').appendTo(this.$container);
  61. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$timelinecont);
  62. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  63. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  64. // time
  65. this.$time = $('<div>').addClass('time').appendTo(this.$container);
  66. this.$currentTime = $('<div>').addClass('current-time').html('00:00').appendTo(this.$time);
  67. this.$duration = $('<div>').addClass('duration').html('00:00').appendTo(this.$time);
  68. // favoris
  69. this.$fav = $('<div>').addClass('favoris').appendTo(this.$container);
  70. // cartel
  71. this.$cartel = $('<div>').addClass('cartel').appendTo(this.$container);
  72. // hiding
  73. this.hideTimer = false;
  74. this.hideTimeMS = 10000;
  75. this.init();
  76. };
  77. AudioPlayer.prototype = {
  78. init(){
  79. // append ui to document
  80. this.$container.appendTo('header[role="banner"] .region-header');
  81. // record timeline width
  82. this.timeline_w = parseInt(this.$timeline.width());
  83. // init audio events
  84. var fn = '';
  85. for (var i = 0; i < this.audio_events.length; i++) {
  86. fn = this.audio_events[i];
  87. // capitalize first letter of event (only cosmetic :p )
  88. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  89. this.audio.addEventListener(
  90. this.audio_events[i],
  91. this[fn].bind(this),
  92. true);
  93. }
  94. // init btns events
  95. this.$playpause.on('click', this.togglePlayPause.bind(this));
  96. // TODO: previous and next btns
  97. },
  98. openDocument(node){
  99. console.log('AudioPlayer openDocument', node);
  100. this.setSRC(node.audio_url);
  101. this.loadNode(node.nid);
  102. },
  103. // cartel functions
  104. loadNode(nid){
  105. this.$cartel.addClass('loading');
  106. $.getJSON('/edlp/ajax/json/node/'+nid+'/player_cartel', {})
  107. .done(this.onNodeLoaded.bind(this))
  108. .fail(this.onNodeLoadFail.bind(this));
  109. },
  110. onNodeLoaded(data){
  111. console.log('AudioPlayer node loaded', data);
  112. this.$cartel.html(data.rendered).removeClass('loading');
  113. _$body.trigger({'type':'new-audio-cartel-loaded'});
  114. initAjaxLinks();
  115. },
  116. onNodeLoadFail(jqxhr, textStatus, error){
  117. console.warn('AudioPlayer node load failed', jqxhr.responseText);
  118. this.$cartel.removeClass('loading').html('');
  119. },
  120. // audio functions
  121. setSRC(url){
  122. // console.log('AudioPlayer setSRC : url', url);
  123. this.clearTimeOutToHide();
  124. this.audio.src = url;
  125. this.show();
  126. },
  127. onLoadedmetadata(){
  128. var rem = parseInt(this.audio.duration, 10),
  129. mins = Math.floor(rem/60,10),
  130. secs = rem - mins*60;
  131. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  132. this.updateLoadingBar();
  133. },
  134. updateLoadingBar(){
  135. this.$loader.css({
  136. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  137. });
  138. if( this.audio.buffered.end(0) < this.audio.duration ){
  139. // loop through this function until file is fully loaded
  140. var that = this;
  141. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  142. }else{
  143. console.log('Audio fully loaded');
  144. }
  145. },
  146. onCanplay(){
  147. this.play();
  148. },
  149. play(){
  150. this.audio.play();
  151. },
  152. togglePlayPause(){
  153. if(this.audio.paused){
  154. this.audio.play();
  155. }else{
  156. this.audio.pause();
  157. }
  158. },
  159. onPlaying(){
  160. this.$container.addClass('is-playing');
  161. },
  162. onPause(){
  163. this.$container.removeClass('is-playing');
  164. },
  165. onTimeupdate(){
  166. // move cursor
  167. this.$cursor.css({
  168. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  169. });
  170. // update time text display
  171. var rem = parseInt(this.audio.currentTime, 10),
  172. mins = Math.floor(rem/60,10),
  173. secs = rem - mins*60;
  174. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  175. },
  176. onEnded(){
  177. this.$container.removeClass('is-playing');
  178. this.timeOutToHide();
  179. },
  180. // global
  181. show(){
  182. this.$container.addClass('visible');
  183. },
  184. timeOutToHide(){
  185. this.clearTimeOutToHide();
  186. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  187. },
  188. clearTimeOutToHide(){
  189. if(this.hideTimer){
  190. clearTimeout(this.hideTimer);
  191. }
  192. },
  193. hide(){
  194. this.$container.removeClass('visible');
  195. // trigger highlighted node remove on corpus map
  196. _$corpus_canvas.trigger('audio-node-closed');
  197. }
  198. }
  199. // ___ _ _ ___
  200. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  201. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  202. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  203. function initScrollbars(){
  204. console.log("initScrollbars");
  205. $('.os-scroll').overlayScrollbars({
  206. overflowBehavior:{x:'h',y:'scroll'}
  207. });
  208. };
  209. // _ _
  210. // /_\ (_)__ ___ __
  211. // / _ \ | / _` \ \ /
  212. // /_/ \_\/ \__,_/_\_\
  213. // |__/
  214. // TODO: add url hash nav
  215. // TODO: implement history.js
  216. function initAjaxLinks(){
  217. console.log('initAjaxLinks');
  218. $('a', '#block-mainnavigation')
  219. .add('a', '#block-footer.menu--footer')
  220. .add('a', '#block-productions')
  221. .add('a', 'article.node:not(.node--type-enregistrement) h2.node-title')
  222. .add('a', '.productions-subtree')
  223. .add('a', '.productions-parent')
  224. // .add('a.index-link, a.notice-link', '#block-edlpentreesblock')
  225. .addClass('ajax-link');
  226. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  227. .each(function(i,e){
  228. var $this = $(this);
  229. // avoid already ajaxified links
  230. if($this.is('.ajax-enable')) return;
  231. if($this.attr('data-drupal-link-system-path')){
  232. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  233. }
  234. });
  235. };
  236. function onClickAjaxLink(e){
  237. e.preventDefault();
  238. var $link = $(this);
  239. if($link.is('.is-active'))
  240. return false;
  241. // Audio links
  242. if($link.is('.audio-link')){
  243. _audio_player.openDocument({
  244. nid:$link.attr('nid'),
  245. audio_url:$link.attr('audio_url')
  246. });
  247. return false;
  248. }
  249. // other links
  250. var sys_path = $(this).attr('data-drupal-link-system-path');
  251. var ajax_path = sys_path;
  252. if(sys_path == '<front>'){
  253. backToFrontPage();
  254. return false;
  255. }
  256. // convert node link to edlp_ajax_node module links
  257. var node_match = ajax_path.match(/^\/?(node\/\d+)$/g);
  258. var term_match = ajax_path.match(/^\/?(taxonomy\/term\/\d+)$/g);
  259. if(node_match){
  260. ajax_path = 'edlp/ajax/json/'+node_match[0];
  261. // check for viewmode attribute
  262. if($link.attr('viewmode')){
  263. ajax_path += '/'+$link.attr('viewmode');
  264. }
  265. }else if(term_match){
  266. ajax_path = 'edlp/ajax/json/'+term_match[0];
  267. ajax_path = ajax_path.replace(/taxonomy\/term/, 'taxonomy_term');
  268. // check for viewmode attribute
  269. if($link.attr('viewmode')){
  270. ajax_path += '/'+$link.attr('viewmode');
  271. }
  272. }else{
  273. // convert other link to ajax
  274. ajax_path += '/ajax'
  275. }
  276. _$body.addClass('ajax-loading');
  277. $link.addClass('ajax-loading');
  278. var path = window.location.origin + drupalSettings.path.baseUrl + ajax_path;
  279. $.getJSON(path, {})
  280. .done(function(data){
  281. onAjaxLinkLoaded(data, $link, sys_path);
  282. })
  283. .fail(function(jqxhr, textStatus, error){
  284. onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path);
  285. });
  286. return false;
  287. };
  288. function onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path){
  289. console.warn('ajaxlink load failed for '+sys_path+' : '+error, jqxhr.responseText);
  290. $link.removeClass('ajax-loading');
  291. _$body.removeClass('ajax-loading');
  292. };
  293. function onAjaxLinkLoaded(data, $link, sys_path){
  294. console.log('ajax link loaded : data', data);
  295. _$body.removeClass('ajax-loading');
  296. // replace all content with newly loaded
  297. _$content_container.html(data.rendered);
  298. // add body class for currently loaded content
  299. var body_classes = [
  300. 'path-'+sys_path.replace(/\//g, '-'),
  301. 'entity-type-'+data.entity_type,
  302. 'bundle-'+data.bundle,
  303. 'view-mode-'+data.view_mode
  304. ];
  305. _$body.removeClass().addClass(body_classes.join(' '));
  306. // id node add a generic path-node class to body
  307. m = sys_path.match(/^\/?(node\/\d+)$/g);
  308. if(m)
  309. _$body.addClass('path-edlp-node');
  310. // handle clicked link classes
  311. _$ajaxLinks.removeClass('is-active');
  312. $link.removeClass('ajax-loading').addClass('is-active');
  313. // if block attached (eg : from edlp_productions module)
  314. if(typeof data.block != 'undefined'){
  315. // if block not already added
  316. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  317. $('.region-'+data.block.region).append(data.block.rendered);
  318. }
  319. }
  320. initScrollbars();
  321. if(sys_path == "productions")
  322. initProductions();
  323. initAjaxLinks();
  324. _$body.trigger({'type':'new-content-ajax-loaded'});
  325. };
  326. // ___
  327. // / __|___ _ _ _ __ _ _ ___
  328. // | (__/ _ \ '_| '_ \ || (_-<
  329. // \___\___/_| | .__/\_,_/__/
  330. // |_|
  331. function onCorpusMapReady(e){
  332. console.log('theme : onCorpusReady');
  333. _$corpus_canvas = $('canvas#corpus-map');
  334. _$corpus_canvas
  335. .on('corpus-cliked-on-map', function(e) {
  336. console.log('theme : corpus-cliked-on-map');
  337. backToFrontPage();
  338. })
  339. .on('corpus-cliked-on-node', function(e) {
  340. console.log('theme : corpus-cliked-on-node', e);
  341. _audio_player.openDocument(e.target_node);
  342. });
  343. }
  344. // ___ _ _ _
  345. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  346. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  347. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  348. function initProductions(){
  349. console.log('theme : initProductions');
  350. var $grid = $('.row', _$content_container).masonry({
  351. itemSelector:'.col',
  352. columnWidth:'.col-2'
  353. });
  354. // layout Masonry after each image loads
  355. $grid.imagesLoaded().progress( function() {
  356. $grid.masonry('layout');
  357. });
  358. // var $grid = $('.row', _$content_container).imagesLoaded( function() {
  359. // // init Masonry after all images have loaded
  360. // $grid.masonry({
  361. // itemSelector:'.col',
  362. // columnWidth:'.col-2'
  363. // });
  364. // });
  365. };
  366. // ___ _ ___
  367. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  368. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  369. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  370. // |___/
  371. function backToFrontPage(){
  372. closeAllModals();
  373. // assume we are going back to front page
  374. $('body').removeClass().addClass('path-frontpage');
  375. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  376. }
  377. // __ __ _ _
  378. // | \/ |___ __| |__ _| |___
  379. // | |\/| / _ \/ _` / _` | (_-<
  380. // |_| |_\___/\__,_\__,_|_/__/
  381. function closeAllModals(){
  382. console.log('theme : closeAllModals');
  383. // TODO: animate the remove
  384. _$content_container.html('');
  385. _$ajaxLinks.removeClass('is-active');
  386. };
  387. init();
  388. } // end EdlpTheme()
  389. $(document).ready(function($) {
  390. var edlptheme = new EdlpTheme();
  391. });
  392. })(jQuery);