main.js 13 KB

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