menu.js 4.4 KB

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