File size: 7,006 Bytes
20fd4b7
 
 
 
 
f143746
20fd4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f143746
 
 
 
 
 
 
 
 
20fd4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e35c677
 
20fd4b7
 
 
f143746
 
 
 
 
20fd4b7
 
 
 
 
 
e35c677
 
20fd4b7
 
 
f143746
 
 
 
 
20fd4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1598108
ba690ef
1598108
 
 
20fd4b7
 
ba690ef
 
 
 
20fd4b7
 
ba690ef
20fd4b7
 
5a24119
 
 
 
20fd4b7
 
 
 
 
 
 
 
 
 
 
 
ba690ef
 
 
 
5a24119
 
ba690ef
5a24119
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1598108
ba690ef
 
20fd4b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f143746
 
 
 
20fd4b7
 
 
 
 
f143746
 
 
 
20fd4b7
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// ────────────────────────────── static/sidebar.js ──────────────────────────────
(function() {
  // DOM elements
  const sidebar = document.getElementById('sidebar');
  const sidebarToggle = document.getElementById('sidebar-toggle');
  const sidebarOverlay = document.getElementById('sidebar-overlay');
  const mainContent = document.querySelector('.main-content');
  const pageTitle = document.getElementById('page-title');
  const menuItems = document.querySelectorAll('.menu-item');

  // State
  let isSidebarOpen = true;
  let currentSection = 'projects';

  // Initialize
  init();

  function init() {
    setupEventListeners();
    updatePageTitle();
    
    // Check if we should start with collapsed sidebar on mobile
    if (window.innerWidth <= 1024) {
      collapseSidebar();
    }
  }

  function setupEventListeners() {
    // Sidebar toggle
    sidebarToggle.addEventListener('click', toggleSidebar);
    
    // Menu navigation
    menuItems.forEach(item => {
      item.addEventListener('click', (e) => {
        e.preventDefault();
        const section = item.dataset.section;
        navigateToSection(section);
      });
    });
    
    // Close sidebar when clicking overlay on mobile
    if (sidebarOverlay) {
      sidebarOverlay.addEventListener('click', () => {
        if (window.innerWidth <= 1024 && isSidebarOpen) {
          collapseSidebar();
        }
      });
    }
    
    // Close sidebar when clicking outside on mobile
    document.addEventListener('click', (e) => {
      if (window.innerWidth <= 1024 && isSidebarOpen) {
        if (!sidebar.contains(e.target) && !sidebarToggle.contains(e.target)) {
          collapseSidebar();
        }
      }
    });
    
    // Handle window resize
    window.addEventListener('resize', handleResize);
  }

  function toggleSidebar() {
    if (isSidebarOpen) {
      collapseSidebar();
    } else {
      expandSidebar();
    }
  }

  function expandSidebar() {
    sidebar.classList.remove('collapsed');
    // On mobile, use the 'open' class to slide in (as defined in CSS @media <=1024px)
    sidebar.classList.add('open');
    mainContent.classList.remove('sidebar-collapsed');
    isSidebarOpen = true;
    
    // Show overlay on mobile
    if (sidebarOverlay && window.innerWidth <= 1024) {
      sidebarOverlay.classList.add('active');
    }
    
    // Update hamburger icon to close icon
    updateHamburgerIcon();
  }

  function collapseSidebar() {
    sidebar.classList.add('collapsed');
    // On mobile, remove the 'open' class to hide
    sidebar.classList.remove('open');
    mainContent.classList.add('sidebar-collapsed');
    isSidebarOpen = false;
    
    // Hide overlay on mobile
    if (sidebarOverlay && window.innerWidth <= 1024) {
      sidebarOverlay.classList.remove('active');
    }
    
    // Update hamburger icon to menu icon
    updateHamburgerIcon();
  }

  function updateHamburgerIcon() {
    const svg = sidebarToggle.querySelector('svg');
    if (isSidebarOpen) {
      // Show close icon (X)
      svg.innerHTML = `
        <line x1="18" y1="6" x2="6" y2="18"/>
        <line x1="6" y1="6" x2="18" y2="18"/>
      `;
    } else {
      // Show hamburger icon (3 lines)
      svg.innerHTML = `
        <line x1="3" y1="6" x2="21" y2="6"/>
        <line x1="3" y1="12" x2="21" y2="12"/>
        <line x1="3" y1="18" x2="21" y2="18"/>
      `;
    }
  }

  function navigateToSection(section) {
    // Update active menu item
    menuItems.forEach(item => {
      item.classList.remove('active');
      if (item.dataset.section === section) {
        item.classList.add('active');
      }
    });
    
    currentSection = section;
    updatePageTitle();
    
    // Handle section-specific actions
    switch (section) {
      case 'projects':
        // Projects section shows upload by default, but keep chat visible too
        showSection('upload');
        // Also show chat section when in projects view
        const chat = document.getElementById('chat-section');
        if (chat) chat.style.display = 'block';
        break;
      case 'files':
        showSection('files');
        if (window.__sb_show_files_section) {
          window.__sb_show_files_section();
        }
        break;
      case 'chat':
        showSection('chat');
        break;
      case 'analytics':
        showSection('analytics');
        if (window.__sb_load_analytics) {
          window.__sb_load_analytics();
        }
        break;
      case 'settings':
        // Could show user settings or preferences
        break;
    }
    
    // Close sidebar on mobile after navigation
    if (window.innerWidth <= 1024) {
      collapseSidebar();
    }
  }

  function showSection(name) {
    const upload = document.getElementById('upload-section');
    const chat = document.getElementById('chat-section');
    const files = document.getElementById('files-section');
    const analytics = document.getElementById('analytics-section');
    
    if (!upload || !chat || !files) return;
    
    // Hide all sections first
    upload.style.display = 'none';
    chat.style.display = 'none';
    files.style.display = 'none';
    if (analytics) analytics.style.display = 'none';
    
    // Show selected section
    switch (name) {
      case 'upload':
        upload.style.display = 'block';
        break;
      case 'chat':
        chat.style.display = 'block';
        if (window.__sb_enable_chat) {
          window.__sb_enable_chat();
        }
        break;
      case 'files':
        files.style.display = 'block';
        break;
      case 'analytics':
        if (analytics) analytics.style.display = 'block';
        break;
    }
  }

  function updatePageTitle() {
    const titles = {
      'projects': 'Projects',
      'files': 'Files',
      'chat': 'Chat',
      'analytics': 'Analytics',
      'settings': 'Settings'
    };
    
    pageTitle.textContent = titles[currentSection] || 'StudyBuddy';
  }

  function setPageTitle(title) {
    pageTitle.textContent = title;
  }

  function handleResize() {
    if (window.innerWidth <= 1024) {
      // On mobile/tablet, start with collapsed sidebar
      if (!sidebar.classList.contains('collapsed')) {
        collapseSidebar();
      }
      // Hide overlay on desktop
      if (sidebarOverlay) {
        sidebarOverlay.classList.remove('active');
      }
    } else {
      // On desktop, ensure sidebar is visible
      if (sidebar.classList.contains('collapsed')) {
        expandSidebar();
      }
      // Hide overlay on desktop
      if (sidebarOverlay) {
        sidebarOverlay.classList.remove('active');
      }
    }
  }

  // Public API
  window.__sb_toggle_sidebar = toggleSidebar;
  window.__sb_collapse_sidebar = collapseSidebar;
  window.__sb_expand_sidebar = expandSidebar;
  window.__sb_navigate_to_section = navigateToSection;
  window.__sb_update_page_title = setPageTitle;
})();