Skip to content

Commit 95f6502

Browse files
authored
Feature/scripts improvements (#4)
* Improve how we open the sidebar by making the code more general * Resize captcha only if the sidebar is open * Close sidebar by clicking on an actual overlay element. * Add sidebar opening feature inside Audiencias Interativas
1 parent 1bee363 commit 95f6502

6 files changed

Lines changed: 60 additions & 53 deletions

File tree

edem-navigation.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
<nav class="actions">
2121
{# <a class="link">Fale conosco</a> #}
2222
{% if user.is_authenticated %}
23-
<a class="link JS-openSidebar JS-showProfile">Menu</a>
23+
<a class="link JS-openSidebar" data-sidebar-content="profile">Menu</a>
2424
{% else %}
25-
<a class="link JS-openSidebar JS-showSignin">Entrar</a>
26-
<a class="link -highlight JS-openSidebar JS-showSignup">Cadastrar</a>
25+
<a class="link JS-openSidebar" data-sidebar-content="signin">Entrar</a>
26+
<a class="link -highlight JS-openSidebar" data-sidebar-content="signup">Cadastrar</a>
2727
{% endif %}
2828
</nav>
2929
</div>

static/edem-navigation/js/edem-navigation.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
function closeEdemSidebar() {
2+
$('.JS-openSidebar').removeClass('-active');
3+
$('body').removeClass('-sidebaropen');
4+
}
5+
6+
function openEdemSidebar(option) {
7+
var contentSelector = '.JS-' + option + 'Content';
8+
var linkSelector = '.JS-openSidebar[data-sidebar-content="' + option + '"]';
9+
10+
$('body').addClass('-sidebaropen');
11+
$('.JS-sidebarContent').removeClass('-show');
12+
$('.JS-openSidebar').removeClass('-active');
13+
$(linkSelector).addClass('-active');
14+
$(contentSelector).addClass('-show');
15+
resizeRecaptcha();
16+
}
17+
118
function resizeRecaptcha() {
219
if (!$('.JS-signupContent').hasClass('JS-signupFinished')){ // Only run if signup is not completed
320
var accessWidth = $('.JS-signUpForm')[0].getBoundingClientRect().width; // Get value with decimals
@@ -38,49 +55,31 @@ function showSuccessSignupMessage() {
3855

3956
// Resize reCAPTCHA on window resize
4057
$(window).resize(function(){
41-
resizeRecaptcha();
58+
if ($('body').hasClass('-sidebaropen')) {
59+
resizeRecaptcha();
60+
}
4261
});
4362

44-
// eDemocracia open sidebar and its contents
63+
// Create and append to body an overlay div for when the sidebar is opened
64+
$('<div/>', {class: 'edem-overlay'}).appendTo('body');
65+
66+
// Open sidebar via the topbar
4567
$('.JS-openSidebar').click(function() {
4668
if ($(this).hasClass('-active')) {
47-
$(this).removeClass('-active');
48-
$('body').removeClass('-sidebaropen');
49-
69+
closeEdemSidebar();
5070
} else {
51-
$('body').addClass('-sidebaropen')
52-
$('.JS-sidebarContent').removeClass('-show');
53-
$('.JS-openSidebar').removeClass('-active');
54-
$(this).addClass('-active');
55-
56-
if ($(this).hasClass('JS-showSignin')) {
57-
$('.JS-signinContent').addClass('-show');
58-
59-
} else if ($(this).hasClass('JS-showSignup')) {
60-
$('.JS-signupContent').addClass('-show');
61-
resizeRecaptcha();
62-
63-
} else if ($(this).hasClass('JS-showProfile')) {
64-
$('.JS-profileContent').addClass('-show');
65-
}
71+
openEdemSidebar($(this).data('sidebarContent'));
6672
}
6773
});
6874

6975
// eDemocracia sidebar close button
7076
$('.JS-closeSidebar').click(function(){
71-
$('.JS-openSidebar').removeClass('-active');
72-
$('body').removeClass('-sidebaropen');
77+
closeEdemSidebar();
7378
});
7479

75-
// Close sidebar if click is outside of sidebar or topbar
76-
document.addEventListener('click', function(e) {
77-
var onEdemCore = $(e.target).closest('.edem-topbar, .edem-sidebar').length;
78-
var sidebarOpen = $('body').hasClass('-sidebaropen');
79-
80-
if (!onEdemCore && sidebarOpen ) {
81-
$('.JS-openSidebar').removeClass('-active');
82-
$('body').removeClass('-sidebaropen');
83-
}
80+
// Close sidebar if click on the overlay
81+
$('.edem-overlay').click(function(){
82+
closeEdemSidebar();
8483
});
8584

8685
// Detect when input is filled
@@ -195,3 +194,11 @@ $('.JS-signUpForm').submit(function(event) {
195194
});
196195
}
197196
});
197+
198+
// XXX This should be exclusively on Audiencias Plugin whenever is possible
199+
if (location.href.match(/audiencias/)) {
200+
$(document).on('click', 'a[href^="/home/?next="]', function(e){
201+
e.preventDefault();
202+
openEdemSidebar('signin');
203+
});
204+
}

static/edem-navigation/scss/base/_unclassed.scss

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@ body {
33
position: relative;
44
width: 100%;
55
transition: margin-left 0.3s $swift-out;
6-
7-
&::after {
8-
content: '';
9-
position: fixed;
10-
opacity: 0;
11-
width: 0;
12-
height: 0;
13-
top: 0;
14-
background-color: $edem-color-primary;
15-
z-index: 100000;
16-
transition: opacity 0.3s ease, width 0s 0.3s, height 0s 0.3s;
17-
}
186
}
197

208
// TODO Remove this after getting rid of Foundation!!
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.edem-overlay {
2+
position: fixed;
3+
opacity: 0;
4+
width: 0;
5+
height: 0;
6+
top: 0;
7+
background-color: $edem-color-primary;
8+
z-index: 100000;
9+
transition: opacity 0.3s ease, width 0s 0.3s, height 0s 0.3s;
10+
11+
body.-sidebaropen & {
12+
width: 100%;
13+
height: 100vh;
14+
opacity: 0.8;
15+
cursor: pointer;
16+
transition: opacity 0.3s 0s ease, width 0s 0s, height 0s 0s;
17+
}
18+
}

static/edem-navigation/scss/components/_edem-sidebar.scss

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,4 @@
154154
body.-sidebaropen {
155155
margin-left: calc(#{$edem-sidebar-width} * -1); // Calc to give the variable in negative value
156156
overflow: hidden;
157-
&::after {
158-
width: 100%;
159-
height: 100vh;
160-
opacity: 0.8;
161-
cursor: pointer;
162-
transition: opacity 0.3s 0s ease, width 0s 0s, height 0s 0s;
163-
}
164157
}

static/edem-navigation/scss/edem.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// Components
1515
@import 'components/edem-topbar';
1616
@import 'components/edem-sidebar';
17+
@import 'components/edem-overlay';
1718
@import 'components/form-field';
1819
@import 'components/edem-button';
1920
@import 'components/edem-close-button';

0 commit comments

Comments
 (0)