menu.js 4.5 KB

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