main.js 14 KB

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