ws-menu.js 4.2 KB

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