menu.js 4.8 KB

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