OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import copy | 5 import copy |
6 import json | |
7 import logging | 6 import logging |
8 | 7 |
9 class SidenavDataSource(object): | 8 from data_source import DataSource |
10 """This class reads in and caches a JSON file representing the side navigation | 9 from third_party.json_schema_compiler.json_parse import Parse |
11 menu. | |
12 """ | |
13 class Factory(object): | |
14 def __init__(self, compiled_fs_factory, json_path): | |
15 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, | |
16 SidenavDataSource) | |
17 self._json_path = json_path | |
18 | 10 |
19 def Create(self, path): | |
20 """Create a SidenavDataSource, binding it to |path|. |path| is the url | |
21 of the page that is being rendered. It is used to determine which item | |
22 in the sidenav should be highlighted. | |
23 """ | |
24 return SidenavDataSource(self._cache, self._json_path, path) | |
25 | 11 |
26 def _AddLevels(self, items, level): | 12 def _AddLevels(items, level): |
27 """Levels represent how deeply this item is nested in the sidenav. We | 13 '''Add a 'level' key to each item in |items|. 'level' corresponds to how deep |
28 start at 2 because the top <ul> is the only level 1 element. | 14 in |items| an item is. |level| sets the starting depth. |
29 """ | 15 ''' |
30 for item in items: | 16 for item in items: |
31 item['level'] = level | 17 item['level'] = level |
32 if 'items' in item: | 18 if 'items' in item: |
33 self._AddLevels(item['items'], level + 1) | 19 _AddLevels(item['items'], level + 1) |
34 | 20 |
35 def _CreateSidenavDict(self, json_path, json_str): | |
36 items = json.loads(json_str) | |
37 self._AddLevels(items, 2); | |
38 return items | |
39 | 21 |
40 def __init__(self, cache, json_path, path): | 22 def _AddSelected(items, path): |
41 self._cache = cache | 23 '''Add 'selected' and 'child_selected' properties to |items| so that the |
42 self._json_path = json_path | 24 sidenav can be expanded to show which menu item has been selected. Returns |
43 self._href = '/' + path | 25 True if an item was marked 'selected'. |
| 26 ''' |
| 27 for item in items: |
| 28 if item.get('href', '') == '/' + path: |
| 29 item['selected'] = True |
| 30 return True |
| 31 if 'items' in item: |
| 32 if _AddSelected(item['items'], path): |
| 33 item['child_selected'] = True |
| 34 return True |
44 | 35 |
45 def _AddSelected(self, items): | 36 return False |
46 for item in items: | |
47 if item.get('href', '') == self._href: | |
48 item['selected'] = True | |
49 return True | |
50 if 'items' in item: | |
51 if self._AddSelected(item['items']): | |
52 item['child_selected'] = True | |
53 return True | |
54 return False | |
55 | 37 |
56 def _QualifyHrefs(self, items): | |
57 for item in items: | |
58 if 'items' in item: | |
59 self._QualifyHrefs(item['items']) | |
60 | 38 |
61 href = item.get('href') | 39 def _QualifyHrefs(items): |
62 if href is not None and not href.startswith(('http://', 'https://')): | 40 '''Force hrefs in |items| to either be absolute (http://...) or qualified |
63 if not href.startswith('/'): | 41 (begins with a slash (/)). Other hrefs emit a warning and should be updated. |
64 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) | 42 ''' |
65 href = '/' + href | 43 for item in items: |
66 item['href'] = href | 44 if 'items' in item: |
| 45 _QualifyHrefs(item['items']) |
| 46 |
| 47 href = item.get('href') |
| 48 if href is not None and not href.startswith(('http://', 'https://')): |
| 49 if not href.startswith('/'): |
| 50 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) |
| 51 href = '/' + href |
| 52 item['href'] = href |
| 53 |
| 54 |
| 55 def _CreateSidenavDict(_, content): |
| 56 items = Parse(content) |
| 57 # Start at level 2, the top <ul> element is level 1. |
| 58 _AddLevels(items, level=2) |
| 59 _QualifyHrefs(items) |
| 60 return items |
| 61 |
| 62 |
| 63 class SidenavDataSource(DataSource): |
| 64 '''Provides templates with access to JSON files used to create the side |
| 65 navigation bar. |
| 66 ''' |
| 67 def __init__(self, server_instance, request): |
| 68 self._cache = server_instance.compiled_host_fs_factory.Create( |
| 69 _CreateSidenavDict, SidenavDataSource) |
| 70 self._json_path = server_instance.sidenav_json_base_path |
| 71 self._request = request |
| 72 |
| 73 def Cron(self): |
| 74 for platform in ['apps', 'extensions']: |
| 75 self._cache.GetFromFile( |
| 76 '%s/%s_sidenav.json' % (self._json_path, platform)) |
67 | 77 |
68 def get(self, key): | 78 def get(self, key): |
69 sidenav = copy.deepcopy(self._cache.GetFromFile( | 79 sidenav = copy.deepcopy(self._cache.GetFromFile( |
70 '%s/%s_sidenav.json' % (self._json_path, key))) | 80 '%s/%s_sidenav.json' % (self._json_path, key))) |
71 self._AddSelected(sidenav) | 81 _AddSelected(sidenav, self._request.path) |
72 self._QualifyHrefs(sidenav) | |
73 return sidenav | 82 return sidenav |
OLD | NEW |