main.js 12 KB

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