menu.js 4.8 KB

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