Skip to content

Commit 6fd4347

Browse files
authored
Merge pull request #3 from stackb/select-4040
Add not found fallback template mechanism
2 parents f567c5c + 5278580 commit 6fd4347

2 files changed

Lines changed: 34 additions & 8 deletions

File tree

js/ui/BUILD.bazel

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package(default_visibility = ["//visibility:public"])
1+
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_library", "closure_js_test")
22

3-
load("@io_bazel_rules_closure//closure:defs.bzl", "closure_js_binary", "closure_js_library", "closure_js_test")
3+
package(default_visibility = ["//visibility:public"])
44

55
closure_js_library(
66
name = "core",
@@ -86,6 +86,7 @@ closure_js_library(
8686
],
8787
deps = [
8888
":core",
89+
":template",
8990
"@io_bazel_rules_closure//closure/library/asserts",
9091
"@io_bazel_rules_closure//closure/library/dom",
9192
"@io_bazel_rules_closure//closure/library/dom:classlist",

js/ui/select.js

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
goog.module('stack.ui.Select');
55

66
const TabEvent = goog.require('stack.ui.TabEvent');
7+
const Template = goog.require('stack.ui.Template');
78
const asserts = goog.require('goog.asserts');
89
const dom = goog.require('goog.dom');
910
const { Component, Route } = goog.require('stack.ui');
@@ -38,7 +39,6 @@ class Select extends Component {
3839
* @private @type {?string}
3940
*/
4041
this.prev_ = null;
41-
4242
}
4343

4444
/**
@@ -169,7 +169,6 @@ class Select extends Component {
169169
}
170170
}
171171

172-
173172
/**
174173
* @param {string} name
175174
* @param {!Route} route
@@ -184,14 +183,33 @@ class Select extends Component {
184183
}
185184
}
186185

187-
188186
/**
189187
* @param {string} name
190188
* @param {!Route} route
191189
*/
192190
selectFail(name, route) {
193-
route.fail(this, 'No tab for ' + name + ' in ' + JSON.stringify(this.name2id_));
194-
// this.getApp().handle404(route);
191+
const notFound = this.getNotFoundTemplate();
192+
if (notFound) {
193+
this.selectNotFound(name, route, notFound);
194+
} else {
195+
route.fail(this, `No tab for ${name} in ${JSON.stringify(this.name2id_)}`);
196+
}
197+
}
198+
199+
/**
200+
* The base case when the tab is not found.
201+
* @param {string} name
202+
* @param {!Route} route
203+
* @param {!Function} template
204+
*/
205+
selectNotFound(name, route, template) {
206+
this.addTab(name, new Template(template, {
207+
name: name,
208+
route: route,
209+
}, {
210+
pathURL: this.getPathUrl(),
211+
}));
212+
this.select(name, route);
195213
}
196214

197215
/**
@@ -206,7 +224,6 @@ class Select extends Component {
206224
return null;
207225
}
208226

209-
210227
/**
211228
* Hide the current tab and make it the previous.
212229
* @return {?Component}
@@ -224,6 +241,14 @@ class Select extends Component {
224241
return prev;
225242
}
226243

244+
/**
245+
* A function to be overridden by subclasses to opt-in
246+
* to the not-found template.
247+
* @returns {?Function} template
248+
*/
249+
getNotFoundTemplate() {
250+
return null;
251+
}
227252
}
228253

229254
exports = Select;

0 commit comments

Comments
 (0)