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 logging | 6 import logging |
7 import posixpath | 7 import posixpath |
8 | 8 |
9 from compiled_file_system import Cache, SingleFile, Unicode | 9 from compiled_file_system import Cache, SingleFile, Unicode |
10 from data_source import DataSource | 10 from data_source import DataSource |
11 from extensions_paths import JSON_TEMPLATES | 11 from extensions_paths import JSON_TEMPLATES |
12 from future import Future | 12 from future import Future |
13 from third_party.json_schema_compiler.json_parse import Parse | 13 from third_party.json_schema_compiler.json_parse import Parse |
14 | 14 |
15 | 15 |
16 def _AddLevels(items, level): | 16 def _AddLevels(items, level): |
17 '''Add a 'level' key to each item in |items|. 'level' corresponds to how deep | 17 '''Add a 'level' key to each item in |items|. 'level' corresponds to how deep |
18 in |items| an item is. |level| sets the starting depth. | 18 in |items| an item is. |level| sets the starting depth. |
19 ''' | 19 ''' |
20 for item in items: | 20 for item in items: |
21 item['level'] = level | 21 item['level'] = level |
22 if 'items' in item: | 22 if 'items' in item: |
23 _AddLevels(item['items'], level + 1) | 23 _AddLevels(item['items'], level + 1) |
24 | 24 |
25 | 25 |
26 def _AddAnnotations(items, path, parent=None): | 26 def _AddAnnotations(items, path, parent=None): |
27 '''Add 'selected', 'child_selected' and 'related' properties to | 27 '''Add 'selected', 'child_selected' and 'related' properties to |
28 |items| so that the sidenav can be expanded to show which menu item has | 28 |items| so that the sidenav can be expanded to show which menu item has |
29 been selected and the related pages section can be drawn. 'related' | 29 been selected and the related pages section can be drawn. 'related' |
30 is added to all items with the same parent as the selected item. | 30 is added to all items with the same parent as the selected item. |
31 If more than one item exactly matches the path, the deepest one is considered | 31 If more than one item exactly matches the path, the deepest one is considered |
32 'selected'. A 'parent' property is added to the selected path. | 32 'selected'. A 'parent' property is added to the selected path. |
33 | 33 |
34 Returns True if an item was marked 'selected'. | 34 Returns True if an item was marked 'selected'. |
35 ''' | 35 ''' |
36 for item in items: | 36 for item in items: |
37 if 'items' in item: | 37 if 'items' in item: |
38 if _AddAnnotations(item['items'], path, item): | 38 if _AddAnnotations(item['items'], path, item): |
39 item['child_selected'] = True | 39 item['child_selected'] = True |
40 return True | 40 return True |
41 | 41 |
42 if item.get('href', '') == path: | 42 if item.get('href', '') == path: |
43 item['selected'] = True | 43 item['selected'] = True |
44 if parent: | 44 if parent: |
45 item['parent'] = { 'title': parent.get('title', None), | 45 item['parent'] = { 'title': parent.get('title', None), |
46 'href': parent.get('href', None) } | 46 'href': parent.get('href', None) } |
47 | 47 |
48 for sibling in items: | 48 for sibling in items: |
49 sibling['related'] = True | 49 sibling['related'] = True |
50 | 50 |
51 return True | 51 return True |
52 | 52 |
53 return False | 53 return False |
54 | 54 |
55 | 55 |
56 class SidenavDataSource(DataSource): | 56 class SidenavDataSource(DataSource): |
57 '''Provides templates with access to JSON files used to create the side | 57 '''Provides templates with access to JSON files used to create the side |
58 navigation bar. | 58 navigation bar. |
59 ''' | 59 ''' |
60 def __init__(self, server_instance, request): | 60 def __init__(self, server_instance, request): |
(...skipping 24 matching lines...) Expand all Loading... |
85 self._QualifyHrefs(item['items']) | 85 self._QualifyHrefs(item['items']) |
86 | 86 |
87 href = item.get('href') | 87 href = item.get('href') |
88 if href is not None and not href.startswith(('http://', 'https://')): | 88 if href is not None and not href.startswith(('http://', 'https://')): |
89 if not href.startswith('/'): | 89 if not href.startswith('/'): |
90 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) | 90 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) |
91 else: | 91 else: |
92 href = href.lstrip('/') | 92 href = href.lstrip('/') |
93 item['href'] = self._server_instance.base_path + href | 93 item['href'] = self._server_instance.base_path + href |
94 | 94 |
95 def Cron(self): | 95 def Refresh(self, path=None): |
96 return self._cache.GetFromFile( | 96 return self._cache.GetFromFile( |
97 posixpath.join(JSON_TEMPLATES, 'chrome_sidenav.json')) | 97 posixpath.join(JSON_TEMPLATES, 'chrome_sidenav.json')) |
98 | 98 |
99 def get(self, key): | 99 def get(self, key): |
100 # TODO(mangini/kalman): Use |key| to decide which sidenav to use, | 100 # TODO(mangini/kalman): Use |key| to decide which sidenav to use, |
101 # which will require a more complex Cron method. | 101 # which will require a more complex Refresh method. |
102 sidenav = self._cache.GetFromFile( | 102 sidenav = self._cache.GetFromFile( |
103 posixpath.join(JSON_TEMPLATES, 'chrome_sidenav.json')).Get() | 103 posixpath.join(JSON_TEMPLATES, 'chrome_sidenav.json')).Get() |
104 sidenav = copy.deepcopy(sidenav) | 104 sidenav = copy.deepcopy(sidenav) |
105 _AddAnnotations(sidenav, | 105 _AddAnnotations(sidenav, |
106 self._server_instance.base_path + self._request.path) | 106 self._server_instance.base_path + self._request.path) |
107 return sidenav | 107 return sidenav |
OLD | NEW |