ws-menu.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. $('#server-alert').remove();
  44. history.pushState({}, 'New path', path);
  45. prevNavState = navState;
  46. navState = parsePath();
  47. console.log('previous&new nav states', prevNavState, navState);
  48. if(navState.repo !== prevNavState.repo) {
  49. if(navState.repo === '') {
  50. console.log('nav: back to root');
  51. _ws.events.emit('navToRoot');
  52. _ws.ui.menuExample.render({ examples: [] });
  53. }
  54. else {
  55. console.log('nav: repo changed');
  56. rp.get('/parts/' + navState.repo, 'json')
  57. .then(function(parts) {
  58. // renderMenuExample(parts.menuExample);
  59. console.log('render with parts', parts);
  60. _ws.repo = parts.repo;
  61. _ws.ui.detailsRepo.render(parts);
  62. _ws.ui.menuExample.render(parts);
  63. _ws.events.emit('navToRepo', navState.repo);
  64. });
  65. }
  66. }
  67. else if(navState.example !== prevNavState.example) {
  68. rp.get('/parts/' + navState.repo + '/' + navState.example, 'json')
  69. .then(function(parts) {
  70. console.log('nav: example changed');
  71. _ws.files = parts.files;
  72. console.log(_ws.ui);
  73. // renderDetailsExample(parts);
  74. _ws.ui.detailsExample.render(parts);
  75. _ws.ui.editor.render(parts);
  76. _ws.ui.tabs.render(parts);
  77. _ws.ui.sandboxIframe.render(navState);
  78. });
  79. }
  80. }
  81. function menuItemClicked(e) {
  82. e.preventDefault();
  83. var $link = $(e.target);
  84. console.log($link.prop('href'));
  85. var originalColor = $link.css('backgroundColor');
  86. toggleMainMenu();
  87. $link.animate({
  88. backgroundColor: '#aaa',
  89. }, 70);
  90. $link.animate({
  91. backgroundColor: originalColor,
  92. }, 70);
  93. navigateTo($link.prop('href'));
  94. }
  95. /**
  96. * Initialize menu-example view
  97. */
  98. _ws.makeView('menu-example', {
  99. events: {
  100. 'click a': menuItemClicked
  101. }
  102. });
  103. /**
  104. * Initialize menu-example view
  105. */
  106. // _ws.makeView('menu-repo', {
  107. // events: {
  108. // 'click a': menuItemClicked
  109. // }
  110. // });
  111. /**
  112. * Handle click on links outside #menu-example
  113. */
  114. $('#menu-repo a').click(menuItemClicked);
  115. $('#nav-back-home').click(function(e) {
  116. e.preventDefault();
  117. $mainMenu.removeClass('in');
  118. navigateTo('/');
  119. });
  120. /**
  121. * Initialize details-example view
  122. */
  123. _ws.makeView('details-example');
  124. /**
  125. * Initialize details-repo view
  126. */
  127. _ws.makeView('details-repo');
  128. // window._ws.navigator = new SandboxNavigator();
  129. // console.log('navigator init', _ws);
  130. // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
  131. // .appendTo( $('#cat-' + example.category) );
  132. // notify('success', "Exemple créé !");
  133. // navigateTo('/'+ repoSlug + '/' + example.slug);
  134. // })
  135. // .catch(errText => notify('error', 'Erreur: ' + errText));
  136. // }
  137. // function clearAndCloseEditor() {
  138. // $exampleFormIn.val('');
  139. // toggleEditor();
  140. // }
  141. });
  142. })(jQuery);