menu.js 4.5 KB

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