menu.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. "use strict";
  2. (function($) {
  3. $(document).ready(function() {
  4. function SandboxNavigator() {
  5. this.$menu = $('#nav-menus');
  6. this.$menuBtn = $('#menu-btn');
  7. this.$menuExample = $('#menu-example');
  8. this.$menuBtn.click(this.toggleMenu.bind(this));
  9. this.$links = $('#nav-menus a,#nav-back-home');
  10. this.init();
  11. }
  12. SandboxNavigator.prototype.init = function() {
  13. this.nav = this.parsePath();
  14. this.bindAllLinksEvents();
  15. };
  16. SandboxNavigator.prototype.toggleMenu = function() {
  17. this.$menu.toggleClass('in');
  18. };
  19. SandboxNavigator.prototype.bindAllLinksEvents = function() {
  20. this.$links.each(this.bindLinkEvents.bind(this));
  21. };
  22. SandboxNavigator.prototype.bindLinkEvents = function(idx, linkEl) {
  23. var self = this;
  24. $(linkEl).click(function(e) {
  25. var $link = $(this);
  26. e.preventDefault();
  27. var originalColor = $link.css('backgroundColor');
  28. self.toggleMenu();
  29. $link.animate({
  30. backgroundColor: '#aaa',
  31. }, 70);
  32. $link.animate({
  33. backgroundColor: originalColor,
  34. }, 70);
  35. self.navigateTo($link.prop('href'));
  36. });
  37. };
  38. SandboxNavigator.prototype.parsePath = function() {
  39. const path = window.location.pathname.substr(1);
  40. const bits = path.split('/');
  41. return {
  42. repo: bits[0],
  43. example: bits.length === 1 ? '' : bits[1]
  44. };
  45. };
  46. SandboxNavigator.prototype.navigateTo = function(path) {
  47. history.pushState({}, 'New path', path);
  48. var oldNav = this.nav;
  49. this.nav = this.parsePath();
  50. console.log(this.nav, oldNav);
  51. if(this.nav.repo === oldNav.repo && this.nav.example === oldNav.example) {
  52. console.log('nav: nop');
  53. }
  54. // else if(this.nav.repo !== oldNav.repo && this.nav.example !== oldNav.example) {
  55. // console.log('nav: repo&example');
  56. // }
  57. else if(this.nav.repo === oldNav.repo) {
  58. if(this.nav.example === '') {
  59. console.log('nav: example empty');
  60. console.log(_ws.events);
  61. _ws.events.emit('navToRepoRoot');
  62. }
  63. else {
  64. console.log('nav: example changed');
  65. _ws.events.emit('navToExample', this.nav.repo);
  66. }
  67. }
  68. else if(this.nav.example === oldNav.example) {
  69. if(this.nav.repo === '') {
  70. console.log('nav: repo empty');
  71. _ws.events.emit('navToRoot');
  72. }
  73. else {
  74. console.log('nav: repo changed');
  75. _ws.events.emit('navToRepo', this.nav.repo);
  76. rp.get('/parts/' + this.nav.repo, 'json')
  77. .then(function(parts) {
  78. renderMenuExample(parts.menuExample);
  79. });
  80. }
  81. }
  82. };
  83. function renderMenuExample(menuExample) {
  84. var tmpl = $('#menu-example-tmpl').html();
  85. console.log(tmpl);
  86. $('#menu-example').html( Mustache.render( tmpl, { menuExample: menuExample } ) );
  87. }
  88. window._ws.navigator = new SandboxNavigator();
  89. console.log('navigator init', _ws);
  90. // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
  91. // .appendTo( $('#cat-' + example.category) );
  92. // notify('success', "Exemple créé !");
  93. // navigateTo('/'+ repoSlug + '/' + example.slug);
  94. // })
  95. // .catch(errText => notify('error', 'Erreur: ' + errText));
  96. // }
  97. // function clearAndCloseEditor() {
  98. // $exampleFormIn.val('');
  99. // toggleEditor();
  100. // }
  101. });
  102. })(jQuery);