main.js 6.1 KB

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