Add gr-router to replace app.js for url routing

Change-Id: I4cc070a4702267196cb001e9b413bb58f7cf4c82
This commit is contained in:
Andrew Bonventre
2016-03-29 13:30:10 -04:00
parent 273ca99db0
commit f3b33a04d2
4 changed files with 123 additions and 98 deletions

View File

@@ -0,0 +1,20 @@
<!--
Copyright (C) 2016 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<link rel="import" href="../../../bower_components/polymer/polymer.html">
<link rel="import" href="../../shared/gr-rest-api-interface/gr-rest-api-interface.html">
<script src="../../../bower_components/page/page.js"></script>
<script src="gr-router.js"></script>

View File

@@ -0,0 +1,102 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
(function() {
'use strict';
// Polymer makes `app` intrinsically defined on the window by virtue of the
// custom element having the id "app", but it is made explicit here.
var app = document.querySelector('#app');
var restAPI = document.createElement('gr-rest-api-interface');
window.addEventListener('WebComponentsReady', function() {
// Middleware
page(function(ctx, next) {
document.body.scrollTop = 0;
next();
});
function loadUser(ctx, next) {
restAPI.getLoggedIn().then(function() {
next();
})
}
// Routes.
page('/', loadUser, function(data) {
// For backward compatibility with GWT links.
if (data.hash) {
page.redirect(data.hash);
return;
}
restAPI.getLoggedIn().then(function(loggedIn) {
if (loggedIn) {
page.redirect('/dashboard/self');
} else {
page.redirect('/q/status:open');
}
});
});
page('/dashboard/(.*)', loadUser, function(data) {
restAPI.getLoggedIn().then(function(loggedIn) {
if (loggedIn) {
data.params.view = 'gr-dashboard-view';
app.params = data.params;
} else {
page.redirect('/login/' + encodeURIComponent(data.canonicalPath));
}
});
});
function queryHandler(data) {
data.params.view = 'gr-change-list-view';
app.params = data.params;
}
page('/q/:query,:offset', queryHandler);
page('/q/:query', queryHandler);
page(/^\/(\d+)\/?/, function(ctx) {
page.redirect('/c/' + ctx.params[0]);
});
page('/c/:changeNum/:patchNum?', function(data) {
data.params.view = 'gr-change-view';
app.params = data.params;
});
page(/^\/c\/(\d+)\/((\d+)(\.\.(\d+))?)\/(.+)/, function(ctx) {
var params = {
changeNum: ctx.params[0],
basePatchNum: ctx.params[2],
patchNum: ctx.params[4],
path: ctx.params[5],
view: 'gr-diff-view',
};
// Don't allow diffing the same patch number against itself because WHY?
if (params.basePatchNum == params.patchNum) {
page.redirect('/c/' + params.changeNum + '/' + params.patchNum + '/' +
params.path);
return;
}
if (!params.patchNum) {
params.patchNum = params.basePatchNum;
delete(params.basePatchNum);
}
app.params = params;
});
page.start();
});
})();

View File

@@ -20,6 +20,7 @@ limitations under the License.
<link rel="import" href="./core/gr-keyboard-shortcuts-dialog/gr-keyboard-shortcuts-dialog.html">
<link rel="import" href="./core/gr-main-header/gr-main-header.html">
<link rel="import" href="./core/gr-router/gr-router.html">
<link rel="import" href="./change-list/gr-change-list-view/gr-change-list-view.html">
<link rel="import" href="./change-list/gr-dashboard-view/gr-dashboard-view.html">
@@ -29,8 +30,6 @@ limitations under the License.
<link rel="import" href="./shared/gr-overlay/gr-overlay.html">
<link rel="import" href="./shared/gr-rest-api-interface/gr-rest-api-interface.html">
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/app.js"></script>
<script src="../scripts/util.js"></script>
<dom-module id="gr-app">

View File

@@ -1,96 +0,0 @@
// Copyright (C) 2015 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
'use strict';
// Polymer makes `app` intrinsically defined on the window by virtue of the
// custom element having the id "app", but it is made explicit here.
var app = document.querySelector('#app');
window.addEventListener('WebComponentsReady', function() {
// Middleware
page(function(ctx, next) {
document.body.scrollTop = 0;
next();
});
function loadUser(ctx, next) {
app.accountReady.then(function() {
next();
});
}
// Routes.
page('/', loadUser, function(data) {
// For backward compatibility with GWT links.
if (data.hash) {
page.redirect(data.hash);
return;
}
if (app.loggedIn) {
page.redirect('/dashboard/self');
} else {
page.redirect('/q/status:open');
}
});
page('/dashboard/(.*)', loadUser, function(data) {
if (app.loggedIn) {
data.params.view = 'gr-dashboard-view';
app.params = data.params;
} else {
page.redirect('/login/' + encodeURIComponent(data.canonicalPath));
}
});
function queryHandler(data) {
data.params.view = 'gr-change-list-view';
app.params = data.params;
}
page('/q/:query,:offset', queryHandler);
page('/q/:query', queryHandler);
page(/^\/(\d+)\/?/, function(ctx) {
page.redirect('/c/' + ctx.params[0]);
});
page('/c/:changeNum/:patchNum?', function(data) {
data.params.view = 'gr-change-view';
app.params = data.params;
});
page(/^\/c\/(\d+)\/((\d+)(\.\.(\d+))?)\/(.+)/, function(ctx) {
var params = {
changeNum: ctx.params[0],
basePatchNum: ctx.params[2],
patchNum: ctx.params[4],
path: ctx.params[5],
view: 'gr-diff-view',
};
// Don't allow diffing the same patch number against itself because WHY?
if (params.basePatchNum == params.patchNum) {
page.redirect('/c/' + params.changeNum + '/' + params.patchNum + '/' +
params.path);
return;
}
if (!params.patchNum) {
params.patchNum = params.basePatchNum;
delete(params.basePatchNum);
}
app.params = params;
});
page.start();
});