| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- "use strict";
- (function($) {
- $(document).ready(function() {
- function SandboxNavigator() {
- _ws.makeView('menu-example', {
- events: {
- 'click a': function(e) {
- e.preventDefault();
- console.log('I was clicked', e.target, this);
- $(this).css('color', 'red');
- }
- }
- });
- console.log(_ws.ui);
- this.$menu = $('#nav-menus');
- this.$menuBtn = $('#menu-btn');
- this.$menuExample = $('#menu-example');
- this.$menuBtn.click(this.toggleMenu.bind(this));
- this.$links = $('#nav-menus a,#nav-back-home');
- this.init();
- }
- SandboxNavigator.prototype.init = function() {
- this.nav = this.parsePath();
- this.bindAllLinksEvents();
- };
- SandboxNavigator.prototype.toggleMenu = function() {
- this.$menu.toggleClass('in');
- };
- SandboxNavigator.prototype.bindAllLinksEvents = function() {
- this.$links.each(this.bindLinkEvents.bind(this));
- };
- SandboxNavigator.prototype.bindLinkEvents = function(idx, linkEl) {
- var self = this;
- $(linkEl).click(function(e) {
- e.preventDefault();
- var $link = $(this);
- var originalColor = $link.css('backgroundColor');
- self.toggleMenu();
- $link.animate({
- backgroundColor: '#aaa',
- }, 70);
- $link.animate({
- backgroundColor: originalColor,
- }, 70);
- self.navigateTo($link.prop('href'));
- });
- };
- SandboxNavigator.prototype.parsePath = function() {
- const path = window.location.pathname.substr(1);
- const bits = path.split('/');
- return {
- repo: bits[0],
- example: bits.length === 1 ? '' : bits[1]
- };
- };
- SandboxNavigator.prototype.navigateTo = function(path) {
- history.pushState({}, 'New path', path);
- var oldNav = this.nav;
- this.nav = this.parsePath();
- console.log(this.nav, oldNav);
- if(this.nav.repo !== oldNav.repo) {
- console.log('nav: repo changed');
- _ws.events.emit('navToRepo', this.nav.repo);
- rp.get('/parts/' + this.nav.repo, 'json')
- .then(function(parts) {
- // renderMenuExample(parts.menuExample);
- _ws.ui.menuExample.render(parts);
- });
- }
- else if(this.nav.example !== oldNav.example) {
- console.log('nav: example changed');
- _ws.events.emit('navToExample', this.nav.example);
- rp.get('/parts/' + this.nav.repo + '/' + this.nav.example, 'json')
- .then(function(parts) {
- console.log(parts);
- renderDetailsExample(parts);
- // renderTabs(parts);
- _ws.ui.tabs.render(parts);
- });
- }
- // if(this.nav.repo === oldNav.repo && this.nav.example === oldNav.example) {
- // console.log('nav: nop');
- // }
- // // else if(this.nav.repo !== oldNav.repo && this.nav.example !== oldNav.example) {
- // // console.log('nav: repo&example');
- // // }
- // else if(this.nav.repo === oldNav.repo) {
- // if(this.nav.example === '') {
- // console.log('nav: example empty');
- // console.log(_ws.events);
- // _ws.events.emit('navToRepoRoot');
- // }
- // else {
- // console.log('nav: example changed');
- // _ws.events.emit('navToExample', this.nav.repo);
- // }
- // }
- // else if(this.nav.example === oldNav.example) {
- // if(this.nav.repo === '') {
- // console.log('nav: repo empty');
- // _ws.events.emit('navToRoot');
- // }
- // else {
- // }
- // }
- };
- function renderInto(id, parts) {
- var tmpl = $('#' + id + '-tmpl').html();
- console.log(tmpl);
- $('#' + id).html( Mustache.render( tmpl, parts ) );
- }
- // function renderTabs(parts) {
- // renderInto('tabs', parts);
- // _ws.events.emit('rendered:tabs');
- // }
- function renderDetailsExample(parts) {
- var tmpl = $('#details-example-tmpl').html();
- console.log(tmpl);
- $('#details-example').html( Mustache.render( tmpl, parts ) );
- }
- function renderMenuExample(menuExample) {
- var tmpl = $('#menu-example-tmpl').html();
- console.log(tmpl);
- $('#menu-example').html( Mustache.render( tmpl, { menuExample: menuExample } ) );
- }
- window._ws.navigator = new SandboxNavigator();
- console.log('navigator init', _ws);
- // var $exMenuItem = $('<li><a href="/' + repoSlug + '/' + example.slug + '">' + example.title + '</a></li>')
- // .appendTo( $('#cat-' + example.category) );
- // notify('success', "Exemple créé !");
- // navigateTo('/'+ repoSlug + '/' + example.slug);
- // })
- // .catch(errText => notify('error', 'Erreur: ' + errText));
- // }
- // function clearAndCloseEditor() {
- // $exampleFormIn.val('');
- // toggleEditor();
- // }
- });
- })(jQuery);
|