OLD | NEW |
| (Empty) |
1 <!-- | |
2 Copyright 2014 The Chromium Authors. All rights reserved. | |
3 Use of this source code is governed by a BSD-style license that can be | |
4 found in the LICENSE file. | |
5 --> | |
6 | |
7 <polymer-element name="ct-view" attributes="path" hidden="true"> | |
8 <template> | |
9 <style> | |
10 ::content > * { | |
11 /* For independent scrolling of the view within a positioned element. */ | |
12 position: absolute; | |
13 overflow: auto; | |
14 top: 0; | |
15 right: 0; | |
16 bottom: 0; | |
17 left: 0; | |
18 } | |
19 </style> | |
20 <content select="*"></content> | |
21 </template> | |
22 <script> | |
23 Polymer("ct-view", { | |
24 parts: null, | |
25 regex: null, | |
26 | |
27 pathChanged: function(oldValue, newValue) { | |
28 var self = this; | |
29 this.parts = []; | |
30 var regex = newValue.replace(/\{([\w\d]+)\}/g, function(match, name) { | |
31 self.parts.push(name); | |
32 return "([^\/]+)"; | |
33 }); | |
34 regex = regex.replace(/\*([\w\d]+)/g, function(match, name) { | |
35 self.parts.push(name); | |
36 return "(.*)"; | |
37 }); | |
38 this.regex = new RegExp("^" + regex + "/?$", "i"); | |
39 }, | |
40 | |
41 showView: function(path) { | |
42 if (path) { | |
43 var params = this._matchPath(path); | |
44 if (!params) | |
45 return null; | |
46 } | |
47 if (!this.children.length) | |
48 return null; | |
49 if (!this.view) | |
50 this.view = this.children[0]; | |
51 if (params) { | |
52 Object.keys(params).forEach(function(name) { | |
53 this.view.setAttribute(name, decodeURIComponent(params[name])); | |
54 }, this); | |
55 } | |
56 this.hidden = false; | |
57 return this.view; | |
58 }, | |
59 | |
60 _matchPath: function(path) { | |
61 if (!this.regex || !this.parts) | |
62 return null; | |
63 var match = path.match(this.regex); | |
64 if (!match) | |
65 return null; | |
66 var result = {}; | |
67 this.parts.forEach(function(name, i) { | |
68 result[name] = match[i + 1]; | |
69 }); | |
70 return result; | |
71 }, | |
72 }); | |
73 </script> | |
74 </polymer-element> | |
OLD | NEW |