Scaffold for user's logged-in dashboard

This change introduces a dependency of the routing behavior on
whether the user is logged in. Since the WebComponentsReady event
can fire before OR after the account info is retrieved, a Promise
is used to ensure deterministic ordering via `accountReady`.

There is also some minor cleanup of which element is being used
as the global `app` object.

Feature: Issue 3700

Change-Id: I9768f2eabd5cdb7c62ead16ae97df5c1c321eaf6
This commit is contained in:
Andrew Bonventre
2015-11-25 17:04:33 -05:00
parent 18f76a6190
commit a5fff8ebc9
5 changed files with 169 additions and 67 deletions

View File

@@ -20,9 +20,15 @@ limitations under the License.
<link rel="import" href="gr-account-manager.html">
<link rel="import" href="gr-change-list-view.html">
<link rel="import" href="gr-change-view.html">
<link rel="import" href="gr-dashboard-view.html">
<link rel="import" href="gr-diff-view.html">
<link rel="import" href="gr-search-bar.html">
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/app.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/util.js"></script>
<dom-module id="gr-app">
<template>
<style>
@@ -81,9 +87,12 @@ limitations under the License.
</div>
</header>
<main>
<template is="dom-if" if="{{_showChangeList}}">
<template is="dom-if" if="{{_showChangeListView}}" restamp="true">
<gr-change-list-view params="[[params]]"></gr-change-list-view>
</template>
<template is="dom-if" if="{{_showDashboardView}}" restamp="true">
<gr-dashboard-view params="[[params]]"></gr-dashboard-view>
</template>
<template is="dom-if" if="{{_showChangeView}}" restamp="true">
<gr-change-view params="[[params]]"></gr-change-view>
</template>
@@ -101,7 +110,20 @@ limitations under the License.
is: 'gr-app',
properties: {
account: Object,
account: {
type: Object,
observer: '_accountChanged',
},
accountReady: {
type: Object,
readOnly: true,
notify: true,
value: function() {
return new Promise(function(resolve) {
this._resolveAccountReady = resolve;
}.bind(this));
},
},
constrained: {
type: Boolean,
value: false,
@@ -112,11 +134,20 @@ limitations under the License.
type: Object,
value: {},
observer: '_routeChanged',
}
},
},
get loggedIn() {
return this.account && Object.keys(this.account).length > 0;
},
_accountChanged: function() {
this._resolveAccountReady();
},
_routeChanged: function(route) {
this.set('_showChangeList', route == 'gr-change-list');
this.set('_showChangeListView', route == 'gr-change-list-view');
this.set('_showDashboardView', route == 'gr-dashboard-view');
this.set('_showChangeView', route == 'gr-change-view');
this.set('_showDiffView', route == 'gr-diff-view');
this.constrained = route == 'gr-change-view';
@@ -126,8 +157,3 @@ limitations under the License.
})();
</script>
</dom-module>
<script src="../bower_components/page/page.js"></script>
<script src="../scripts/app.js"></script>
<script src="../scripts/changes.js"></script>
<script src="../scripts/util.js"></script>

View File

@@ -0,0 +1,59 @@
<!--
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.
-->
<link rel="import" href="../bower_components/polymer/polymer.html">
<dom-module id="gr-dashboard-view">
<template>
<iron-ajax id="xhr"
auto
url="/changes/"
params="[[_computeQueryParams()]]"
last-response="{{_results}}"
json-prefix=")]}'"
debounce-duration="300"></iron-ajax>
</template>
<script>
(function() {
'use strict';
Polymer({
is: 'gr-dashboard-view',
properties: {
_results: Array,
},
_computeQueryParams: function() {
var options = Changes.listChangesOptionsToHex(
Changes.ListChangesOption.LABELS,
Changes.ListChangesOption.DETAILED_ACCOUNTS,
Changes.ListChangesOption.REVIEWED
);
return {
O: options,
q: [
'is:open owner:self',
'is:open reviewer:self -owner:self',
'is:closed (owner:self OR reviewer:self) -age:4w limit:10',
],
};
},
});
})();
</script>
</dom-module>

View File

@@ -41,6 +41,4 @@ window.Polymer.dom = 'shadow';
<!-- endreplace-->
<body unresolved>
<template is="dom-bind" id="app">
<gr-app params="{{params}}" route="{{route}}"></gr-app>
</template>
<gr-app id="app"></gr-app>

View File

@@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
(function(document) {
'use strict';
// See https://github.com/Polymer/polymer/issues/1381
// 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) {
@@ -23,13 +25,32 @@
next();
});
function loadUser(ctx, next) {
app.accountReady.then(function() {
next();
});
}
// Routes.
page('/', function() {
page('/', loadUser, function() {
if (app.loggedIn) {
page.redirect('/dashboard/self');
} else {
page.redirect('/q/status:open');
}
});
page('/dashboard/*', loadUser, function(data) {
if (app.loggedIn) {
app.route = 'gr-dashboard-view';
app.params = data.params;
} else {
page.redirect('/login/' + encodeURIComponent(data.canonicalPath));
}
});
function queryHandler(data) {
app.route = 'gr-change-list';
app.route = 'gr-change-list-view';
app.params = data.params;
}
@@ -68,5 +89,3 @@
page.start();
});
})(document);

View File

@@ -101,7 +101,7 @@ type server struct{}
// Any path prefixes that should resolve to index.html.
var (
fePaths = []string{"/q/", "/c/"}
fePaths = []string{"/q/", "/c/", "/dashboard/"}
issueNumRE = regexp.MustCompile(`^\/\d+\/?$`)
)