main.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. _$body.on('corpus-map-ready', onCorpusMapReady);
  11. initScrollbars();
  12. initAjaxLinks();
  13. if (_$body.is('.path-productions')) {
  14. initProductions();
  15. }
  16. };
  17. // ___ _ _ ___
  18. // / __| __ _ _ ___| | | _ ) __ _ _ _ ___
  19. // \__ \/ _| '_/ _ \ | | _ \/ _` | '_(_-<
  20. // |___/\__|_| \___/_|_|___/\__,_|_| /__/
  21. function initScrollbars(){
  22. console.log("initScrollbars");
  23. $('.os-scroll').overlayScrollbars({
  24. overflowBehavior:{x:'h',y:'scroll'}
  25. });
  26. };
  27. // _ _
  28. // /_\ (_)__ ___ __
  29. // / _ \ | / _` \ \ /
  30. // /_/ \_\/ \__,_/_\_\
  31. // |__/
  32. // TODO: add url hash nav
  33. // TODO: implement history.js
  34. function initAjaxLinks(){
  35. console.log('initAjaxLinks');
  36. $('a', '#block-mainnavigation, #block-footer.menu--footer, #block-productions').addClass('ajax-link');
  37. _$ajaxLinks = $('.ajax-link')
  38. .each(function(i,e){
  39. var $this = $(this);
  40. // avoid already ajaxified links
  41. if($this.is('.ajax-enable')) return;
  42. var sys_path = $this.attr('data-drupal-link-system-path');
  43. if(sys_path){
  44. // convert node link to edlp_ajax_node module links
  45. m = sys_path.match(/^\/?(node\/\d+)$/g);
  46. if(m) $this.attr('data-drupal-link-system-path', 'edlp/'+m[0]);
  47. }
  48. $this.on('click', onClickAjaxLink).addClass('ajax-enable');
  49. })
  50. ;
  51. };
  52. function onClickAjaxLink(e){
  53. e.preventDefault();
  54. var $link = $(this);
  55. if($link.is('.is-active'))
  56. return false;
  57. var sys_path = $(this).attr('data-drupal-link-system-path');
  58. if(sys_path == '<front>'){
  59. backToFrontPage();
  60. return false;
  61. }
  62. var path = window.location.origin + drupalSettings.path.baseUrl + sys_path;
  63. _$body.addClass('ajax-loading');
  64. $link.addClass('ajax-loading');
  65. // $.getJSON(path, {}, function(data){
  66. // onAjaxLinkLoaded(data, $link, sys_path);
  67. // });
  68. $.getJSON(path+'/ajax', {})
  69. .done(function(data){
  70. onAjaxLinkLoaded(data, $link, sys_path);
  71. })
  72. .fail(function(jqxhr, textStatus, error){
  73. onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path);
  74. });
  75. return false;
  76. };
  77. function onAjaxLinkLoadError(jqxhr, textStatus, error, $link, sys_path){
  78. console.warn('fail : error', jqxhr.responseText);
  79. $link.removeClass('ajax-loading');
  80. _$body.removeClass('ajax-loading');
  81. };
  82. function onAjaxLinkLoaded(data, $link, sys_path){
  83. console.log('ajax link loaded : data', data);
  84. _$body.removeClass('ajax-loading');
  85. // replace all content with newly loaded
  86. _$content_container.html(data.rendered);
  87. // add body class for currently loaded content
  88. _$body.removeClass().addClass('path-'+sys_path.replace(/\//g, '-'));
  89. // id node add a generic path-node class to body
  90. m = sys_path.match(/^\/?(edlp\/node\/\d+)$/g);
  91. if(m)
  92. _$body.addClass('path-edlp-node');
  93. // handle clicked link classes
  94. _$ajaxLinks.removeClass('is-active');
  95. $link.removeClass('ajax-loading').addClass('is-active');
  96. // if block attached (eg : from edlp_productions module)
  97. if(typeof data.block != 'undefined'){
  98. // if block not already added
  99. if(!$('#'+data.block.id, '.region-'+data.block.region).length){
  100. $('.region-'+data.block.region).append(data.block.rendered);
  101. }
  102. }
  103. initScrollbars();
  104. if(sys_path == "productions")
  105. initProductions();
  106. initAjaxLinks();
  107. };
  108. // ___
  109. // / __|___ _ _ _ __ _ _ ___
  110. // | (__/ _ \ '_| '_ \ || (_-<
  111. // \___\___/_| | .__/\_,_/__/
  112. // |_|
  113. function onCorpusMapReady(e){
  114. console.log('theme : onCorpusReady');
  115. _$corpus_map = $('canvas#corpus-map');
  116. _$corpus_map.on('corpus-cliked-on-map', function(e) {
  117. console.log('theme : corpus-cliked-on-map');
  118. backToFrontPage();
  119. });
  120. }
  121. // ___ _ _ _
  122. // | _ \_ _ ___ __| |_ _ __| |_(_)___ _ _ ___
  123. // | _/ '_/ _ \/ _` | || / _| _| / _ \ ' \(_-<
  124. // |_| |_| \___/\__,_|\_,_\__|\__|_\___/_||_/__/
  125. function initProductions(){
  126. console.log('theme : initProductions');
  127. var $grid = $('.row', _$content_container).masonry({
  128. itemSelector:'.col',
  129. columnWidth:'.col-2'
  130. });
  131. // layout Masonry after each image loads
  132. $grid.imagesLoaded().progress( function() {
  133. $grid.masonry('layout');
  134. });
  135. // var $grid = $('.row', _$content_container).imagesLoaded( function() {
  136. // // init Masonry after all images have loaded
  137. // $grid.masonry({
  138. // itemSelector:'.col',
  139. // columnWidth:'.col-2'
  140. // });
  141. // });
  142. };
  143. // ___ _ ___
  144. // | __| _ ___ _ _| |_| _ \__ _ __ _ ___
  145. // | _| '_/ _ \ ' \ _| _/ _` / _` / -_)
  146. // |_||_| \___/_||_\__|_| \__,_\__, \___|
  147. // |___/
  148. function backToFrontPage(){
  149. closeAllModals();
  150. // assume we are going back to front page
  151. $('body').removeClass().addClass('path-frontpage');
  152. $('a[data-drupal-link-system-path="<front>"]').addClass('is-active');
  153. }
  154. // __ __ _ _
  155. // | \/ |___ __| |__ _| |___
  156. // | |\/| / _ \/ _` / _` | (_-<
  157. // |_| |_\___/\__,_\__,_|_/__/
  158. function closeAllModals(){
  159. console.log('theme : closeAllModals');
  160. // TODO: animate the remove
  161. _$content_container.html('');
  162. _$ajaxLinks.removeClass('is-active');
  163. };
  164. init();
  165. } // end EdlpTheme()
  166. $(document).ready(function($) {
  167. var edlptheme = new EdlpTheme();
  168. });
  169. })(jQuery);