main.js 13 KB

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