Skip to content

Commit 11f726f

Browse files
committed
refactor to be more extendable
1 parent 9d50cd6 commit 11f726f

File tree

3 files changed

+82
-46
lines changed

3 files changed

+82
-46
lines changed

index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@
5454
<aside id="sidebar" class="sidebar">
5555
<nav class="sidebar-nav">
5656
<ul class="items">
57-
<li><a class="select-list active" href="#">List</a></li>
58-
<li><a class="select-facet" href="#">Filter</a></li>
57+
<li><a data-select="list" class="sidebar-action active" href="#">List</a></li>
58+
<li><a data-select="facets" class="sidebar-action" href="#">Filter</a></li>
5959
</ul>
6060
</nav>
6161
<div id="list" class="control sidebar-item"></div>

src/ui/sidebar.js

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,56 +6,74 @@ define(function(require, exports, module) {
66

77
module.exports = flight.component(function sidebar() {
88

9+
var potentialSidebarItems = ['facets', 'list'];
10+
var presentItems = [];
11+
912
this.defaultAttrs({
10-
listButton: '.select-list',
11-
facetButton: '.select-facet',
12-
list: '#list',
13-
facet: '#facets'
13+
toggleButtons: '.sidebar-action',
14+
sidebarItems: '.sidebar-item'
1415
});
1516

17+
this.configureSidebar = function(e, config){
18+
var configAttr = _.keys(config);
19+
presentItems = _.intersection(potentialSidebarItems, configAttr);
1620

17-
this.showList = function(e){
18-
e.preventDefault();
19-
this.select('list').show();
20-
this.select('facet').hide();
21-
this.select('listButton').addClass('active');
22-
this.select('facetButton').removeClass('active');
21+
var $sidebar = this.$node;
22+
23+
// add class .sidebar-items-X based on active sidebar items
24+
$sidebar.addClass('sidebar-items-'+presentItems.length);
25+
// activate configured sidebar items
26+
_.each(presentItems, function(item){
27+
$sidebar.find(item).show();
28+
$sidebar.find('[data-select="'+item+'"]').parent().show();
29+
});
30+
31+
if( presentItems.length === 1) {
32+
$sidebar.find('.sidebar-nav').hide();
33+
$sidebar.addClass('no-tabs');
34+
}
2335
};
2436

25-
this.showFacet = function(e){
37+
this.showItem = function(e, elemObj) {
38+
// generic toggle fn for sidebar tabs
2639
e.preventDefault();
27-
this.select('facet').show();
28-
this.select('list').hide();
29-
this.select('facetButton').addClass('active');
30-
this.select('listButton').removeClass('active');
40+
var $el = $(elemObj.el);
41+
var itemTitle = $el.data('select');
42+
this.select('sidebarItems').hide();
43+
this.$node.find('#'+itemTitle).show();
44+
this.select('toggleButtons').removeClass('active');
45+
$el.addClass('active');
3146
};
3247

3348
this.showFullSidebar = function() {
34-
this.select('facet').show();
35-
this.select('list').show();
49+
var $sidebar = this.$node;
50+
_.each(presentItems, function(item){
51+
$sidebar.find(item).show();
52+
});
3653
};
3754

38-
this.toggleSidebar = function(e) {
55+
this.toggleHiddenSidebar = function(e) {
3956
e.preventDefault();
4057
this.$node.toggleClass('open');
4158
$('.sidebar-toggle').toggleClass('active');
4259
};
4360

4461
this.after('initialize', function() {
62+
this.on(document, 'config', this.configureSidebar);
63+
4564
this.on('click', {
46-
listButton: this.showList,
47-
facetButton: this.showFacet
65+
toggleButtons: this.showItem
4866
});
4967

5068
// This is outside of the component, but it controls it.. perhaps refactor to include within root element?
51-
$(document).on('click', '.sidebar-toggle', this.toggleSidebar.bind(this) );
69+
$(document).on('click', '.sidebar-toggle', this.toggleHiddenSidebar.bind(this) );
70+
5271
$(window).on('resize', function(){
5372
if( document.body.clientWidth > 955 ) {
5473
this.showFullSidebar();
5574
}
5675
}.bind(this));
5776

58-
5977
});
6078
});
6179
});

styles/style.css

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ body > div.container {
4848
color: #69579c;
4949
}
5050

51+
.navbar-form {
52+
width: 80%;
53+
}
54+
5155
.navbar-form .btn {
5256
border:none;
5357
outline:none;
@@ -255,12 +259,15 @@ body > div.container {
255259
margin-left: 0;
256260
}
257261

258-
@media (max-width: 955px) {
262+
263+
@media (max-width: 1200px) {
259264
.sidebar {
260-
width: 30%;
265+
width: 33%;
261266
margin-left: 0%;
262267
padding-top: 50px;
263-
268+
}
269+
.sidebar.no-tabs {
270+
padding-top: 0;
264271
}
265272
}
266273

@@ -279,12 +286,19 @@ body > div.container {
279286
height: 100%;
280287
overflow-y: scroll;
281288
overflow-x: hidden;
282-
width: 50%;
283289
float: left;
284290
padding: 0 10px;
291+
display: none;
292+
}
293+
294+
.sidebar-items-2 .control {
295+
width: 50%;
296+
}
297+
.sidebar-items-1 .control {
298+
width: 100%;
285299
}
286300

287-
@media (max-width: 955px) {
301+
@media (max-width: 1200px) {
288302
.sidebar .control {
289303
width: 100%;
290304
float: none;
@@ -302,7 +316,7 @@ body > div.container {
302316
}
303317

304318
/* Only display this when we have a tabbed view (mid-screen size & lower) */
305-
@media (max-width: 955px) {
319+
@media (max-width: 1200px) {
306320
.sidebar-nav {
307321
display: initial;
308322
}
@@ -316,8 +330,14 @@ body > div.container {
316330
}
317331

318332
.sidebar-nav .items li{
319-
width: 50%;
320333
float: left;
334+
display: none;
335+
}
336+
.sidebar-items-2 .sidebar-nav .items li{
337+
width: 50%;
338+
}
339+
.sidebar-items-1 .sidebar-nav .items li{
340+
width: 100%;
321341
}
322342

323343
/*buttons*/
@@ -334,37 +354,35 @@ body > div.container {
334354

335355

336356
/* map offset should be zero no matter the configuration of the sidebar since sidebar is an overlay*/
337-
body.has-facets.has-list #map {
357+
body #map {
338358
left: 0;
339359
}
340360

341361
/* At larger sizes, adjust map to fit sidebar space depending on configuration */
342362
@media (min-width: 768px) {
343-
body.has-facets #map {
344-
left: 15%;
345-
}
346-
body.has-list #map {
363+
.sidebar-items-1 + #map {
347364
left: 15%;
348365
}
349-
body.has-facets.has-list #map {
366+
.sidebar-items-2 + #map {
350367
left: 30%;
351368
}
352369
}
353-
@media (min-width: 956px) {
354-
body.has-facets #map {
355-
left: 22.5%;
370+
@media (min-width: 1201px) {
371+
.sidebar-items-1 {
372+
width: 330px;
356373
}
357-
body.has-list #map {
358-
left: 22.5%;
374+
.sidebar-items-2 {
375+
width: 500px;
359376
}
360-
body.has-facets.has-list #map {
361-
left: 45%;
377+
.sidebar-items-1 + #map {
378+
left: 330px;
379+
}
380+
.sidebar-items-2 + #map {
381+
left: 500px;
362382
}
363383
}
364384

365385

366-
367-
368386
#facets .checkbox.selected label {
369387
font-weight: bold;
370388
}

0 commit comments

Comments
 (0)