ws-menu.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. _ws.navigateTo = function(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. _ws.ui.shortcutRepo.render({ repos: _ws.repos, _: _ws._ });
  56. _ws.ui.shortcutExample.hide();
  57. }
  58. else {
  59. console.log('nav: repo changed');
  60. rp.get('/parts/' + navState.repo, 'json')
  61. .then(function(parts) {
  62. // renderMenuExample(parts.menuExample);
  63. console.log('render with parts', parts);
  64. _ws.repo = parts.repo;
  65. _ws.ui.addExample.$btn.addClass('in');
  66. _ws.ui.addExample.show();
  67. _ws.ui.detailsRepo.render(parts);
  68. _ws.ui.menuExample.render(parts);
  69. _ws.ui.shortcutExample.render(parts);
  70. _ws.ui.shortcutRepo.hide();
  71. _ws.events.emit('navToRepo', navState.repo);
  72. });
  73. }
  74. }
  75. else if(navState.example !== prevNavState.example) {
  76. if(navState.example !== '') {
  77. rp.get('/parts/' + navState.repo + '/' + navState.example, 'json')
  78. .then(function(parts) {
  79. console.log('nav: example changed', parts);
  80. _ws.example = parts.example;
  81. _ws.files = parts.files;
  82. console.log(_ws.ui);
  83. // renderDetailsExample(parts);
  84. $('#editor-wrapper').show();
  85. _ws.ui.shortcutExample.hide();
  86. _ws.ui.detailsExample.render(parts);
  87. _ws.ui.editor.render(parts);
  88. _ws.ui.tabs.render(parts);
  89. _ws.ui.addFile.show();
  90. console.log('#### render iframe with navState', navState);
  91. _ws.ui.sandboxIframe.render('/examples/' + navState.repo + '/' + navState.example);
  92. });
  93. }
  94. else {
  95. $('#editor-wrapper').hide();
  96. _ws.ui.shortcutExample.show();
  97. _ws.ui.editor.hide();
  98. _ws.ui.tabs.hide();
  99. _ws.ui.addFile.hide();
  100. _ws.ui.sandboxIframe.render('/html/start-iframe.html');
  101. }
  102. }
  103. }
  104. function menuItemClicked(e) {
  105. e.preventDefault();
  106. var $link = $(e.target);
  107. console.log($link.prop('href'));
  108. var originalColor = $link.css('backgroundColor');
  109. toggleMainMenu();
  110. $link.animate({
  111. backgroundColor: '#aaa',
  112. }, 70);
  113. $link.animate({
  114. backgroundColor: originalColor,
  115. }, 70);
  116. _ws.navigateTo($link.prop('href'));
  117. }
  118. function shortcutItemClicked(e) {
  119. e.preventDefault();
  120. var $link = $(e.target);
  121. _ws.navigateTo($link.prop('href'));
  122. }
  123. /**
  124. * Initialize menu-example view
  125. */
  126. _ws.makeView('menu-example', {
  127. events: {
  128. 'click a': menuItemClicked
  129. }
  130. });
  131. /**
  132. * Initialize menu-repo view
  133. */
  134. _ws.makeView('menu-repo', {
  135. events: {
  136. 'click a': menuItemClicked
  137. }
  138. });
  139. /**
  140. * Initialize shortcut-repo view
  141. */
  142. _ws.makeView('shortcut-repo', {
  143. events: {
  144. 'click a': shortcutItemClicked
  145. }
  146. });
  147. /**
  148. * Initialize shortcut-example view
  149. */
  150. _ws.makeView('shortcut-example', {
  151. events: {
  152. 'click a': shortcutItemClicked
  153. }
  154. });
  155. /**
  156. * Handle click on links outside #menu-example
  157. */
  158. // $('#menu-repo a').click(menuItemClicked);
  159. $('#nav-back-home').click(function(e) {
  160. e.preventDefault();
  161. $mainMenu.removeClass('in');
  162. _ws.navigateTo('/');
  163. });
  164. /**
  165. * Initialize details-example view
  166. */
  167. _ws.makeView('details-example');
  168. /**
  169. * Initialize details-repo view
  170. */
  171. _ws.makeView('details-repo');
  172. // window._ws.navigator = new SandboxNavigator();
  173. // console.log('navigator init', _ws);
  174. // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
  175. // .appendTo( $('#cat-' + example.category) );
  176. // notify('success', "Exemple créé !");
  177. // _ws.navigateTo('/'+ repoSlug + '/' + example.slug);
  178. // })
  179. // .catch(errText => notify('error', 'Erreur: ' + errText));
  180. // }
  181. // function clearAndCloseEditor() {
  182. // $exampleFormIn.val('');
  183. // toggleEditor();
  184. // }
  185. });
  186. })(jQuery);