ws-menu.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. if(navState.repo !== prevNavState.repo) {
  48. if(navState.repo === '') {
  49. _ws.events.emit('navToRoot');
  50. _ws.ui.addExample.$btn.removeClass('in');
  51. _ws.ui.addExample.hide();
  52. _ws.ui.menuExample.render({ examples: [] });
  53. _ws.ui.shortcutRepo.render({ repos: _ws.repos, _: _ws._ });
  54. _ws.ui.shortcutExample.hide();
  55. }
  56. else {
  57. rp.get('/parts/' + navState.repo, 'json')
  58. .then(function(parts) {
  59. _ws.repo = parts.repo;
  60. _ws.ui.addExample.$btn.addClass('in');
  61. _ws.ui.addExample.show();
  62. _ws.ui.detailsRepo.render(parts);
  63. _ws.ui.menuExample.render(parts);
  64. _ws.ui.shortcutExample.render(parts);
  65. _ws.ui.shortcutRepo.hide();
  66. _ws.events.emit('navToRepo', navState.repo);
  67. });
  68. }
  69. }
  70. else if(navState.example !== prevNavState.example) {
  71. if(navState.example !== '') {
  72. rp.get('/parts/' + navState.repo + '/' + navState.example, 'json')
  73. .then(function(parts) {
  74. _ws.example = parts.example;
  75. _ws.files = parts.files;
  76. $('#editor-wrapper').show();
  77. _ws.ui.shortcutExample.hide();
  78. _ws.ui.detailsExample.render(parts);
  79. _ws.ui.editor.render(parts);
  80. _ws.ui.tabs.render(parts);
  81. _ws.ui.addFile.show();
  82. _ws.ui.sandboxIframe.render('/examples/' + navState.repo + '/' + navState.example);
  83. });
  84. }
  85. else {
  86. $('#editor-wrapper').hide();
  87. _ws.ui.shortcutExample.show();
  88. _ws.ui.editor.hide();
  89. _ws.ui.tabs.hide();
  90. _ws.ui.addFile.hide();
  91. _ws.ui.sandboxIframe.render('/html/start-iframe.html');
  92. }
  93. }
  94. }
  95. function menuItemClicked(e) {
  96. e.preventDefault();
  97. var $link = $(e.target);
  98. var originalColor = $link.css('backgroundColor');
  99. toggleMainMenu();
  100. $link.animate({
  101. backgroundColor: '#aaa',
  102. }, 70);
  103. $link.animate({
  104. backgroundColor: originalColor,
  105. }, 70);
  106. _ws.navigateTo($link.prop('href'));
  107. }
  108. function shortcutItemClicked(e) {
  109. e.preventDefault();
  110. var $link = $(e.target);
  111. _ws.navigateTo($link.prop('href'));
  112. }
  113. function addExampleLink(cb) {
  114. return function(example, href) {
  115. var $targetList = this.$elem.find('ul[data-id="' + example.category + '"]');
  116. var $newItem = $('<li><a href="' + href + '">' + example.title + '</a></li>').appendTo($targetList);
  117. $newItem.find('a').on('click', cb);
  118. }
  119. }
  120. /**
  121. * Initialize menu-example view
  122. */
  123. _ws.makeView('menu-example', {
  124. addExampleLink: addExampleLink(menuItemClicked),
  125. events: {
  126. 'click a': menuItemClicked
  127. }
  128. });
  129. /**
  130. * Initialize menu-repo view
  131. */
  132. _ws.makeView('menu-repo', {
  133. events: {
  134. 'click a': menuItemClicked
  135. }
  136. });
  137. /**
  138. * Initialize shortcut-repo view
  139. */
  140. _ws.makeView('shortcut-repo', {
  141. events: {
  142. 'click a': shortcutItemClicked
  143. }
  144. });
  145. /**
  146. * Initialize shortcut-example view
  147. */
  148. _ws.makeView('shortcut-example', {
  149. addExampleLink: addExampleLink(shortcutItemClicked),
  150. events: {
  151. 'click a': shortcutItemClicked
  152. }
  153. });
  154. /**
  155. * Handle click on links outside #menu-example
  156. */
  157. // $('#menu-repo a').click(menuItemClicked);
  158. $('#nav-back-home').click(function(e) {
  159. e.preventDefault();
  160. $mainMenu.removeClass('in');
  161. _ws.navigateTo('/');
  162. });
  163. /**
  164. * Initialize details-example view
  165. */
  166. _ws.makeView('details-example');
  167. /**
  168. * Initialize details-repo view
  169. */
  170. _ws.makeView('details-repo');
  171. });
  172. })(jQuery);