Skip to content

Commit

Permalink
Fix client-side router on site
Browse files Browse the repository at this point in the history
The JS for the group share page failed to load
because of an incorrect comparison of the result
of String.match() against a number and also
because of an attempt to load Bootstrap
unnecessarily on this pages.

This commit fixes the issue and prepares us for
adding new pages in future by:

 * Adding a client-side micro-router to simplify running
   page-specific JS

 * Only loading the Bootstrap components on pages that
   actually need it.
  • Loading branch information
robertknight committed Feb 3, 2016
1 parent 431f677 commit aa1ea43
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
3 changes: 3 additions & 0 deletions h/static/scripts/share-group-form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function ShareGroupFormController(element) {
if (!element.querySelector('.is-member-of-group')) {
return;
}
var shareLink = element.querySelector('.js-share-link');
shareLink.focus();
shareLink.select();
Expand Down
39 changes: 19 additions & 20 deletions h/static/scripts/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,31 @@ if (window.RAVEN_CONFIG) {
require('./raven').init(window.RAVEN_CONFIG);
}

var page = require('page');

var CreateGroupFormController = require('./create-group-form');
var DropdownMenuController = require('./dropdown-menu');
var InstallerController = require('./installer-controller');
var ShareGroupFormController = require('./share-group-form');

// load our customized version of Bootstrap which
// provides a few basic UI components (eg. modal dialogs)
require('../styles/vendor/bootstrap/bootstrap');
// setup components
new DropdownMenuController(document);

function setupGroupsController(path) {
if (path === '/groups/new') {
new CreateGroupFormController(document.body);
} else if (document.querySelector('.is-member-of-group')) {
new ShareGroupFormController(document.body);
}
}
page('/', function () {
// load our customized version of Bootstrap which
// provides a few basic UI components (eg. modal dialogs)
require('../styles/vendor/bootstrap/bootstrap');
new InstallerController(document.body);
});

document.addEventListener('DOMContentLoaded', function () {
// setup components
new DropdownMenuController(document);
page('/groups/new', function () {
new CreateGroupFormController(document.body);
});

page('/groups/:id/:name', function () {
new ShareGroupFormController(document.body);
});

// setup route
var route = document.location.pathname;
if (route === '/') {
new InstallerController(document.body);
} else if (route.match('^/groups') === 0) {
setupGroupsController(route);
}
document.addEventListener('DOMContentLoaded', function () {
page.start();
});
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"js-polyfills": "^0.1.11",
"ng-tags-input": "2.2.0",
"node-uuid": "^1.4.3",
"page": "^1.6.4",
"postcss": "^5.0.6",
"raf": "^3.1.0",
"raven-js": "^2.0.2",
Expand Down

0 comments on commit aa1ea43

Please sign in to comment.