main.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. (function($) {
  2. EdlpTheme = function(){
  3. var _$body = $('body');
  4. var _is_front = _$body.is('.path-frontpage');
  5. var _$corpus_map;
  6. var _$content_container = $('.layout-container>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. initScrollbars();
  13. initAjaxLinks();
  14. if (_$body.is('.path-productions')) {
  15. initProductions();
  16. }
  17. initAudioPlayer();
  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. // hiding
  54. this.hideTimer = false;
  55. this.hideTimeMS = 10000;
  56. this.init();
  57. };
  58. AudioPlayer.prototype = {
  59. init(){
  60. // append ui to document
  61. this.$container.appendTo('header[role="banner"] .region-header');
  62. // record timeline width
  63. this.timeline_w = parseInt(this.$timeline.width());
  64. // init audio events
  65. var fn = '';
  66. for (var i = 0; i < this.audio_events.length; i++) {
  67. fn = this.audio_events[i];
  68. // capitalize first letter of event (only cosmetic :p )
  69. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  70. this.audio.addEventListener(
  71. this.audio_events[i],
  72. this[fn].bind(this),
  73. true);
  74. }
  75. // init btns events
  76. this.$playpause.on('click', this.togglePlayPause.bind(this));
  77. // TODO: previous and next btns
  78. },
  79. setSRC(url){
  80. console.log('AudioPlayer setSRC : url', url);
  81. this.clearTimeOutToHide();
  82. this.audio.src = url;
  83. this.show();
  84. },
  85. onLoadedmetadata(){
  86. var rem = parseInt(this.audio.duration, 10),
  87. mins = Math.floor(rem/60,10),
  88. secs = rem - mins*60;
  89. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  90. this.updateLoadingBar();
  91. },
  92. updateLoadingBar(){
  93. this.$loader.css({
  94. 'width':parseInt((100 * this.audio.buffered.end(0) / this.audio.duration), 10)+'%'
  95. });
  96. if( this.audio.buffered.end(0) < this.audio.duration ){
  97. // loop through this function until file is fully loaded
  98. var that = this;
  99. window.requestAnimationFrame(that.updateLoadingBar.bind(that));
  100. }else{
  101. console.log('Audio fully loaded');
  102. }
  103. },
  104. onCanplay(){
  105. this.play();
  106. },
  107. play(){
  108. this.audio.play();
  109. },
  110. togglePlayPause(){
  111. if(this.audio.paused){
  112. this.audio.play();
  113. }else{
  114. this.audio.pause();
  115. }
  116. },
  117. onPlaying(){
  118. this.$container.addClass('is-playing');
  119. },
  120. onPause(){
  121. this.$container.removeClass('is-playing');
  122. },
  123. onTimeupdate(){
  124. // move cursor
  125. this.$cursor.css({
  126. 'left':(this.audio.currentTime/this.audio.duration * this.timeline_w)+"px"
  127. });
  128. // update time text display
  129. var rem = parseInt(this.audio.currentTime, 10),
  130. mins = Math.floor(rem/60,10),
  131. secs = rem - mins*60;
  132. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  133. },
  134. onEnded(){
  135. this.$container.removeClass('is-playing');
  136. this.timeOutToHide();
  137. },
  138. show(){
  139. this.$container.addClass('visible');
  140. },
  141. timeOutToHide(){
  142. this.clearTimeOutToHide();
  143. this.hideTimer = setTimeout(this.hide.bind(this), this.hideTimeMS);
  144. },
  145. clearTimeOutToHide(){
  146. if(this.hideTimer){
  147. clearTimeout(this.hideTimer);
  148. }
  149. },
  150. hide(){
  151. this.$container.removeClass('visible');
  152. }
  153. }
  154. // ___ _ _ ___
  155. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  156. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  157. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  158. function initScrollbars(){
  159. console.log("initScrollbars");
  160. $('.os-scroll').overlayScrollbars({
  161. overflowBehavior:{x:'h',y:'scroll'}
  162. });
  163. };
  164. // _ _
  165. // /_\ (_)__ ___ __
  166. // / _ \ | / _` \ \ /
  167. // /_/ \_\/ \__,_/_\_\
  168. // |__/
  169. // TODO: add url hash nav
  170. // TODO: implement history.js
  171. function initAjaxLinks(){
  172. console.log('initAjaxLinks');
  173. $('a', '#block-mainnavigation, #block-footer.menu--footer, #block-productions, article.node h2.node-title, .productions-subtree, .productions-parent').addClass('ajax-link');
  174. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  175. .each(function(i,e){
  176. var $this = $(this);
  177. // avoid already ajaxified links
  178. // if($this.is('.ajax-enable')) return;
  179. var sys_path = $this.attr('data-drupal-link-system-path');
  180. if(sys_path){
  181. // convert node link to edlp_ajax_node module links
  182. m = sys_path.match(/^\/?(node\/\d+)$/g);
  183. if(m) $this.attr('data-drupal-link-system-path', 'edlp/'+m[0]);
  184. }
  185. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  186. })
  187. ;
  188. };
  189. function onClickAjaxLink(e){
  190. e.preventDefault();
  191. var $link = $(this);
  192. if($link.is('.is-active'))
  193. return false;
  194. var sys_path = $(this).attr('data-drupal-link-system-path');
  195. if(sys_path == '<front>'){
  196. backToFrontPage();
  197. return false;
  198. }
  199. var path = window.location.origin + drupalSettings.path.baseUrl + sys_path;
  200. _$body.addClass('ajax-loading');
  201. $link.addClass('ajax-loading');
  202. // $.getJSON(path, {}, function(data){
  203. // onAjaxLinkLoaded(data, $link, sys_path);
  204. // });
  205. $.getJSON(path+'/ajax', {})
  206. .done(function(data){
  207. onAjaxLinkLoaded(data, $link, sys_path);
  208. })
  209. .fail(function(jqxhr, textStatus, error){
  210. onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path);
  211. });
  212. return false;
  213. };
  214. function onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path){
  215. console.warn('ajaxlink load failed', jqxhr.responseText);
  216. $link.removeClass('ajax-loading');
  217. _$body.removeClass('ajax-loading');
  218. };
  219. function onAjaxLinkLoaded(data, $link, sys_path){
  220. console.log('ajax link loaded : data', data);
  221. _$body.removeClass('ajax-loading');
  222. // replace all content with newly loaded
  223. _$content_container.html(data.rendered);
  224. // add body class for currently loaded content
  225. _$body.removeClass().addClass('path-'+sys_path.replace(/\//g, '-'));
  226. // id node add a generic path-node class to body
  227. m = sys_path.match(/^\/?(edlp\/node\/\d+)$/g);
  228. if(m)
  229. _$body.addClass('path-edlp-node');
  230. // handle clicked link classes
  231. _$ajaxLinks.removeClass('is-active');
  232. $link.removeClass('ajax-loading').addClass('is-active');
  233. // if block attached (eg : from edlp_productions module)
  234. if(typeof data.block != 'undefined'){
  235. // if block not already added
  236. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  237. $('.region-'+data.block.region).append(data.block.rendered);
  238. }
  239. }
  240. initScrollbars();
  241. if(sys_path == "productions")
  242. initProductions();
  243. initAjaxLinks();
  244. };
  245. // ___
  246. // / __|___ _ _ _ __ _ _ ___
  247. // | (__/ _ \ '_| '_ \ || (_-<
  248. // \___\___/_| | .__/\_,_/__/
  249. // |_|
  250. function onCorpusMapReady(e){
  251. console.log('theme : onCorpusReady');
  252. _$corpus_map = $('canvas#corpus-map');
  253. _$corpus_map
  254. .on('corpus-cliked-on-map', function(e) {
  255. console.log('theme : corpus-cliked-on-map');
  256. backToFrontPage();
  257. })
  258. .on('corpus-cliked-on-node', function(e) {
  259. console.log('theme : corpus-cliked-on-node', e);
  260. _audio_player.setSRC(e.target_node.audio_url);
  261. });
  262. }
  263. // ___ _ _ _
  264. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  265. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  266. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  267. function initProductions(){
  268. console.log('theme : initProductions');
  269. var $grid = $('.row', _$content_container).masonry({
  270. itemSelector:'.col',
  271. columnWidth:'.col-2'
  272. });
  273. // layout Masonry after each image loads
  274. $grid.imagesLoaded().progress( function() {
  275. $grid.masonry('layout');
  276. });
  277. // var $grid = $('.row', _$content_container).imagesLoaded( function() {
  278. // // init Masonry after all images have loaded
  279. // $grid.masonry({
  280. // itemSelector:'.col',
  281. // columnWidth:'.col-2'
  282. // });
  283. // });
  284. };
  285. // ___ _ ___
  286. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  287. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  288. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  289. // |___/
  290. function backToFrontPage(){
  291. closeAllModals();
  292. // assume we are going back to front page
  293. $('body').removeClass().addClass('path-frontpage');
  294. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  295. }
  296. // __ __ _ _
  297. // | \/ |___ __| |__ _| |___
  298. // | |\/| / _ \/ _` / _` | (_-<
  299. // |_| |_\___/\__,_\__,_|_/__/
  300. function closeAllModals(){
  301. console.log('theme : closeAllModals');
  302. // TODO: animate the remove
  303. _$content_container.html('');
  304. _$ajaxLinks.removeClass('is-active');
  305. };
  306. init();
  307. } // end EdlpTheme()
  308. $(document).ready(function($) {
  309. var edlptheme = new EdlpTheme();
  310. });
  311. })(jQuery);