main.min.js 14 KB

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