ws-menu.js 4.0 KB

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