ws-menu.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. "use strict";
  2. (function($) {
  3. $(document).ready(function() {
  4. console.log('ws: init menu');
  5. /**
  6. * Main menu
  7. */
  8. var $mainMenu = $('#nav-menus');
  9. /**
  10. * Main menu toggle button
  11. */
  12. var $menuBtn = $('#menu-btn');
  13. $menuBtn.click(toggleMainMenu);
  14. /**
  15. * Navigation state
  16. */
  17. var navState = parsePath();
  18. /**
  19. * Previous navigation state
  20. */
  21. var prevNavState;
  22. /**
  23. * Toggle main menu
  24. */
  25. function toggleMainMenu() {
  26. $mainMenu.toggleClass('in');
  27. };
  28. /**
  29. * Extract bits (repo&example slugs) from requested url path
  30. */
  31. function parsePath() {
  32. const path = window.location.pathname.substr(1);
  33. const bits = path.split('/');
  34. return {
  35. repo: bits[0],
  36. example: bits.length === 1 ? '' : bits[1]
  37. };
  38. }
  39. /**
  40. * Handle navigation
  41. */
  42. function navigateTo(path) {
  43. history.pushState({}, 'New path', path);
  44. prevNavState = navState;
  45. navState = parsePath();
  46. console.log('previous&new nav states', prevNavState, navState);
  47. if(navState.repo !== prevNavState.repo) {
  48. if(navState.repo === '') {
  49. console.log('nav: back to root');
  50. _ws.events.emit('navToRoot');
  51. }
  52. else {
  53. console.log('nav: repo changed');
  54. rp.get('/parts/' + navState.repo, 'json')
  55. .then(function(parts) {
  56. // renderMenuExample(parts.menuExample);
  57. console.log('render with parts', parts);
  58. _ws.ui.detailsRepo.render(parts);
  59. _ws.ui.menuExample.render(parts);
  60. _ws.events.emit('navToRepo', navState.repo);
  61. });
  62. }
  63. }
  64. else if(navState.example !== prevNavState.example) {
  65. rp.get('/parts/' + navState.repo + '/' + navState.example, 'json')
  66. .then(function(parts) {
  67. console.log('nav: example changed');
  68. _ws.files = parts.files;
  69. console.log(_ws.ui);
  70. // renderDetailsExample(parts);
  71. _ws.ui.detailsExample.render(parts);
  72. _ws.ui.editor.render(parts);
  73. _ws.ui.tabs.render(parts);
  74. _ws.ui.sandboxIframe.render(navState);
  75. });
  76. }
  77. }
  78. function menuItemClicked(e) {
  79. e.preventDefault();
  80. var $link = $(this);
  81. console.log($link.prop('href'));
  82. var originalColor = $link.css('backgroundColor');
  83. toggleMainMenu();
  84. $link.animate({
  85. backgroundColor: '#aaa',
  86. }, 70);
  87. $link.animate({
  88. backgroundColor: originalColor,
  89. }, 70);
  90. navigateTo($link.prop('href'));
  91. }
  92. /**
  93. * Initialize menu-example view
  94. */
  95. _ws.makeView('menu-example', {
  96. events: {
  97. 'click a': menuItemClicked
  98. }
  99. });
  100. /**
  101. * Initialize menu-example view
  102. */
  103. // _ws.makeView('menu-repo', {
  104. // events: {
  105. // 'click a': menuItemClicked
  106. // }
  107. // });
  108. /**
  109. * Handle click on links outside #menu-example
  110. */
  111. $('#menu-repo a').click(menuItemClicked);
  112. $('#nav-back-home').click(function(e) {
  113. e.preventDefault();
  114. $mainMenu.removeClass('in');
  115. navigateTo('/');
  116. });
  117. /**
  118. * Initialize details-example view
  119. */
  120. _ws.makeView('details-example');
  121. /**
  122. * Initialize details-repo view
  123. */
  124. _ws.makeView('details-repo');
  125. // window._ws.navigator = new SandboxNavigator();
  126. // console.log('navigator init', _ws);
  127. // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
  128. // .appendTo( $('#cat-' + example.category) );
  129. // notify('success', "Exemple créé !");
  130. // navigateTo('/'+ repoSlug + '/' + example.slug);
  131. // })
  132. // .catch(errText => notify('error', 'Erreur: ' + errText));
  133. // }
  134. // function clearAndCloseEditor() {
  135. // $exampleFormIn.val('');
  136. // toggleEditor();
  137. // }
  138. });
  139. })(jQuery);