main.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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","progress","canplay","timeupdate","ended"];
  37. // UI dom objects
  38. this.$container = $('<div id="audio-player">')
  39. .appendTo('header[role="banner"] .region-header');
  40. this.$timeline = $('<div>').addClass('time-line').appendTo(this.$container);
  41. this.$loader = $('<div>').addClass('loader').appendTo(this.$timeline);
  42. this.$cursor = $('<div>').addClass('cursor').appendTo(this.$timeline);
  43. this.$duration = $('<div>').addClass('duration').appendTo(this.$container);
  44. this.$currentTime = $('<div>').addClass('current-time').appendTo(this.$container);
  45. if (typeof AudioPlayer.initialized == "undefined") {
  46. AudioPlayer.prototype.init = function(){
  47. // init audio events
  48. var fn = '';
  49. for (var i = 0; i < this.audio_events.length; i++) {
  50. fn = this.audio_events[i];
  51. // capitalize first letter of event (only cosmetic :p )
  52. fn = 'on'+fn.charAt(0).toUpperCase()+fn.slice(1);
  53. this.audio.addEventListener(
  54. this.audio_events[i],
  55. this[fn].bind(this),
  56. true);
  57. }
  58. };
  59. AudioPlayer.prototype.loadSound = function(url){
  60. console.log('AudioPlayer loadSound : url', url);
  61. this.audio.src = url;
  62. // this.play();
  63. };
  64. AudioPlayer.prototype.play = function(){
  65. console.log('AudioPlayer play()');
  66. this.audio.play();
  67. };
  68. AudioPlayer.prototype.onLoadedmetadata = function(){
  69. var rem = parseInt(this.audio.duration, 10),
  70. mins = Math.floor(rem/60,10),
  71. secs = rem - mins*60;
  72. this.$duration.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  73. };
  74. AudioPlayer.prototype.onProgress = function(){
  75. // var myBuffered = this.audio.buffered;
  76. // var mySeekable = this.audio.seekable;
  77. if( this.audio.buffered.length ){
  78. // var fromPercent = this.fromPercent;
  79. // var value = percentLoad - fromPercent;
  80. // if( value<0 ) value = 0;
  81. // this.$loadingBar.css({width: value + '%',marginLeft:fromPercent + '%'});
  82. this.$loader.css({
  83. 'width':parseInt(((this.audio.buffered.end(0) / this.audio.duration) * 100), 10)+'%'
  84. });
  85. }
  86. };
  87. AudioPlayer.prototype.onCanplay = function(){
  88. this.play();
  89. };
  90. AudioPlayer.prototype.onTimeupdate = function(){
  91. // console.log('Audio update()', this.audio.currentTime);
  92. this.$cursor.css({
  93. 'left':(this.audio.currentTime/this.audio.duration * 50)+"px"
  94. });
  95. var rem = parseInt(this.audio.currentTime, 10),
  96. mins = Math.floor(rem/60,10),
  97. secs = rem - mins*60;
  98. this.$currentTime.html('<span>'+(mins<10 ? '0':'')+mins+':'+(secs<10 ? '0':'')+secs+'</span>');
  99. };
  100. AudioPlayer.prototype.onEnded = function(){
  101. console.log('AudioPlayer onEnded');
  102. };
  103. AudioPlayer.initialized = true;
  104. this.init();
  105. }
  106. };
  107. // ___ _ _ ___
  108. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  109. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  110. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  111. function initScrollbars(){
  112. console.log("initScrollbars");
  113. $('.os-scroll').overlayScrollbars({
  114. overflowBehavior:{x:'h',y:'scroll'}
  115. });
  116. };
  117. // _ _
  118. // /_\ (_)__ ___ __
  119. // / _ \ | / _` \ \ /
  120. // /_/ \_\/ \__,_/_\_\
  121. // |__/
  122. // TODO: add url hash nav
  123. // TODO: implement history.js
  124. function initAjaxLinks(){
  125. console.log('initAjaxLinks');
  126. $('a', '#block-mainnavigation, #block-footer.menu--footer, #block-productions, article.node h2.node-title, .productions-subtree, .productions-parent').addClass('ajax-link');
  127. _$ajaxLinks = $('.ajax-link:not(.ajax-enabled)')
  128. .each(function(i,e){
  129. var $this = $(this);
  130. // avoid already ajaxified links
  131. // if($this.is('.ajax-enable')) return;
  132. var sys_path = $this.attr('data-drupal-link-system-path');
  133. if(sys_path){
  134. // convert node link to edlp_ajax_node module links
  135. m = sys_path.match(/^\/?(node\/\d+)$/g);
  136. if(m) $this.attr('data-drupal-link-system-path', 'edlp/'+m[0]);
  137. }
  138. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  139. })
  140. ;
  141. };
  142. function onClickAjaxLink(e){
  143. e.preventDefault();
  144. var $link = $(this);
  145. if($link.is('.is-active'))
  146. return false;
  147. var sys_path = $(this).attr('data-drupal-link-system-path');
  148. if(sys_path == '<front>'){
  149. backToFrontPage();
  150. return false;
  151. }
  152. var path = window.location.origin + drupalSettings.path.baseUrl + sys_path;
  153. _$body.addClass('ajax-loading');
  154. $link.addClass('ajax-loading');
  155. // $.getJSON(path, {}, function(data){
  156. // onAjaxLinkLoaded(data, $link, sys_path);
  157. // });
  158. $.getJSON(path+'/ajax', {})
  159. .done(function(data){
  160. onAjaxLinkLoaded(data, $link, sys_path);
  161. })
  162. .fail(function(jqxhr, textStatus, error){
  163. onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path);
  164. });
  165. return false;
  166. };
  167. function onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path){
  168. console.warn('ajaxlink load failed', jqxhr.responseText);
  169. $link.removeClass('ajax-loading');
  170. _$body.removeClass('ajax-loading');
  171. };
  172. function onAjaxLinkLoaded(data, $link, sys_path){
  173. console.log('ajax link loaded : data', data);
  174. _$body.removeClass('ajax-loading');
  175. // replace all content with newly loaded
  176. _$content_container.html(data.rendered);
  177. // add body class for currently loaded content
  178. _$body.removeClass().addClass('path-'+sys_path.replace(/\//g, '-'));
  179. // id node add a generic path-node class to body
  180. m = sys_path.match(/^\/?(edlp\/node\/\d+)$/g);
  181. if(m)
  182. _$body.addClass('path-edlp-node');
  183. // handle clicked link classes
  184. _$ajaxLinks.removeClass('is-active');
  185. $link.removeClass('ajax-loading').addClass('is-active');
  186. // if block attached (eg : from edlp_productions module)
  187. if(typeof data.block != 'undefined'){
  188. // if block not already added
  189. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  190. $('.region-'+data.block.region).append(data.block.rendered);
  191. }
  192. }
  193. initScrollbars();
  194. if(sys_path == "productions")
  195. initProductions();
  196. initAjaxLinks();
  197. };
  198. // ___
  199. // / __|___ _ _ _ __ _ _ ___
  200. // | (__/ _ \ '_| '_ \ || (_-<
  201. // \___\___/_| | .__/\_,_/__/
  202. // |_|
  203. function onCorpusMapReady(e){
  204. console.log('theme : onCorpusReady');
  205. _$corpus_map = $('canvas#corpus-map');
  206. _$corpus_map
  207. .on('corpus-cliked-on-map', function(e) {
  208. console.log('theme : corpus-cliked-on-map');
  209. backToFrontPage();
  210. })
  211. .on('corpus-cliked-on-node', function(e) {
  212. console.log('theme : corpus-cliked-on-node', e);
  213. _audio_player.loadSound(e.target_node.audio_url);
  214. });
  215. }
  216. // ___ _ _ _
  217. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  218. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  219. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  220. function initProductions(){
  221. console.log('theme : initProductions');
  222. var $grid = $('.row', _$content_container).masonry({
  223. itemSelector:'.col',
  224. columnWidth:'.col-2'
  225. });
  226. // layout Masonry after each image loads
  227. $grid.imagesLoaded().progress( function() {
  228. $grid.masonry('layout');
  229. });
  230. // var $grid = $('.row', _$content_container).imagesLoaded( function() {
  231. // // init Masonry after all images have loaded
  232. // $grid.masonry({
  233. // itemSelector:'.col',
  234. // columnWidth:'.col-2'
  235. // });
  236. // });
  237. };
  238. // ___ _ ___
  239. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  240. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  241. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  242. // |___/
  243. function backToFrontPage(){
  244. closeAllModals();
  245. // assume we are going back to front page
  246. $('body').removeClass().addClass('path-frontpage');
  247. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  248. }
  249. // __ __ _ _
  250. // | \/ |___ __| |__ _| |___
  251. // | |\/| / _ \/ _` / _` | (_-<
  252. // |_| |_\___/\__,_\__,_|_/__/
  253. function closeAllModals(){
  254. console.log('theme : closeAllModals');
  255. // TODO: animate the remove
  256. _$content_container.html('');
  257. _$ajaxLinks.removeClass('is-active');
  258. };
  259. init();
  260. } // end EdlpTheme()
  261. $(document).ready(function($) {
  262. var edlptheme = new EdlpTheme();
  263. });
  264. })(jQuery);