menu.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. e.preventDefault();
  26. var $link = $(this);
  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) {
  52. console.log('nav: repo changed');
  53. _ws.events.emit('navToRepo', this.nav.repo);
  54. rp.get('/parts/' + this.nav.repo, 'json')
  55. .then(function(parts) {
  56. renderMenuExample(parts.menuExample);
  57. });
  58. }
  59. else if(this.nav.example !== oldNav.example) {
  60. console.log('nav: example changed');
  61. _ws.events.emit('navToExample', this.nav.example);
  62. rp.get('/parts/' + this.nav.repo + '/' + this.nav.example, 'json')
  63. .then(function(parts) {
  64. console.log(parts);
  65. renderDetailsExample(parts);
  66. renderTabs(parts);
  67. });
  68. }
  69. // if(this.nav.repo === oldNav.repo && this.nav.example === oldNav.example) {
  70. // console.log('nav: nop');
  71. // }
  72. // // else if(this.nav.repo !== oldNav.repo && this.nav.example !== oldNav.example) {
  73. // // console.log('nav: repo&example');
  74. // // }
  75. // else if(this.nav.repo === oldNav.repo) {
  76. // if(this.nav.example === '') {
  77. // console.log('nav: example empty');
  78. // console.log(_ws.events);
  79. // _ws.events.emit('navToRepoRoot');
  80. // }
  81. // else {
  82. // console.log('nav: example changed');
  83. // _ws.events.emit('navToExample', this.nav.repo);
  84. // }
  85. // }
  86. // else if(this.nav.example === oldNav.example) {
  87. // if(this.nav.repo === '') {
  88. // console.log('nav: repo empty');
  89. // _ws.events.emit('navToRoot');
  90. // }
  91. // else {
  92. // }
  93. // }
  94. };
  95. function renderInto(id, parts) {
  96. var tmpl = $('#' + id + '-tmpl').html();
  97. console.log(tmpl);
  98. $('#' + id).html( Mustache.render( tmpl, parts ) );
  99. }
  100. function renderTabs(parts) {
  101. renderInto('tabs', parts);
  102. _ws.events.emit('rendered:tabs');
  103. }
  104. function renderDetailsExample(parts) {
  105. var tmpl = $('#details-example-tmpl').html();
  106. console.log(tmpl);
  107. $('#details-example').html( Mustache.render( tmpl, parts ) );
  108. }
  109. function renderMenuExample(menuExample) {
  110. var tmpl = $('#menu-example-tmpl').html();
  111. console.log(tmpl);
  112. $('#menu-example').html( Mustache.render( tmpl, { menuExample: menuExample } ) );
  113. }
  114. window._ws.navigator = new SandboxNavigator();
  115. console.log('navigator init', _ws);
  116. // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
  117. // .appendTo( $('#cat-' + example.category) );
  118. // notify('success', "Exemple créé !");
  119. // navigateTo('/'+ repoSlug + '/' + example.slug);
  120. // })
  121. // .catch(errText => notify('error', 'Erreur: ' + errText));
  122. // }
  123. // function clearAndCloseEditor() {
  124. // $exampleFormIn.val('');
  125. // toggleEditor();
  126. // }
  127. });
  128. })(jQuery);