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 | 7 |
8 from compiled_file_system import SingleFile, Unicode | 8 from compiled_file_system import SingleFile, Unicode |
9 from data_source import DataSource | 9 from data_source import DataSource |
10 from extensions_paths import JSON_TEMPLATES | 10 from extensions_paths import JSON_TEMPLATES |
11 from future import Gettable, Future | 11 from future import Gettable, Future |
12 from third_party.json_schema_compiler.json_parse import Parse | 12 from third_party.json_schema_compiler.json_parse import Parse |
13 | 13 |
14 | 14 |
15 def _AddLevels(items, level): | 15 def _AddLevels(items, level): |
16 '''Add a 'level' key to each item in |items|. 'level' corresponds to how deep | 16 '''Add a 'level' key to each item in |items|. 'level' corresponds to how deep |
17 in |items| an item is. |level| sets the starting depth. | 17 in |items| an item is. |level| sets the starting depth. |
18 ''' | 18 ''' |
19 for item in items: | 19 for item in items: |
20 item['level'] = level | 20 item['level'] = level |
21 if 'items' in item: | 21 if 'items' in item: |
22 _AddLevels(item['items'], level + 1) | 22 _AddLevels(item['items'], level + 1) |
23 | 23 |
not at google - send to devlin
2013/12/18 05:10:14
need an extra \n here
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
24 | 24 def _AddAnnotations(items, path, parent=None): |
25 def _AddSelected(items, path): | 25 '''Add 'selected', 'child_selected' and 'related' properties to |
26 '''Add 'selected' and 'child_selected' properties to |items| so that the | 26 |items| so that the sidenav can be expanded to show which menu item has |
27 sidenav can be expanded to show which menu item has been selected. Returns | 27 been selected and the related pages section can be drawn. 'related' |
28 True if an item was marked 'selected'. | 28 is added to all items with the same parent as the selected item. |
29 If more than one item exactly matches the path, the deepest one is considered | |
30 'selected'. A 'parent' property is added to the selected path. | |
31 | |
32 Returns True if an item was marked 'selected'. | |
29 ''' | 33 ''' |
30 for item in items: | 34 for item in items: |
31 if item.get('href', '') == path: | 35 # depth first search |
not at google - send to devlin
2013/12/18 05:10:14
Comment is ok to leave out I think.
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
32 item['selected'] = True | |
33 return True | |
34 if 'items' in item: | 36 if 'items' in item: |
35 if _AddSelected(item['items'], path): | 37 if _AddAnnotations(item['items'], path, item): |
36 item['child_selected'] = True | 38 item['child_selected'] = True |
37 return True | 39 return True |
38 | 40 |
41 if item.get('href', '') == path: | |
42 item['selected'] = True | |
43 if parent: | |
44 item['parent'] = parent | |
45 | |
46 # navigate on siblings, marking them as related | |
not at google - send to devlin
2013/12/18 05:10:14
the docstring on this function adequately covers t
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
47 for sibling in items: | |
48 sibling['related'] = True | |
49 | |
50 return True | |
51 | |
39 return False | 52 return False |
40 | 53 |
41 | 54 |
55 | |
not at google - send to devlin
2013/12/18 05:10:14
need 1 less \n here
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
42 class SidenavDataSource(DataSource): | 56 class SidenavDataSource(DataSource): |
43 '''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 |
44 navigation bar. | 58 navigation bar. |
45 ''' | 59 ''' |
46 def __init__(self, server_instance, request): | 60 def __init__(self, server_instance, request): |
47 self._cache = server_instance.compiled_fs_factory.Create( | 61 self._cache = server_instance.compiled_fs_factory.Create( |
48 server_instance.host_file_system_provider.GetTrunk(), | 62 server_instance.host_file_system_provider.GetTrunk(), |
49 self._CreateSidenavDict, | 63 self._CreateSidenavDict, |
50 SidenavDataSource) | 64 SidenavDataSource) |
51 self._server_instance = server_instance | 65 self._server_instance = server_instance |
(...skipping 19 matching lines...) Expand all Loading... | |
71 | 85 |
72 href = item.get('href') | 86 href = item.get('href') |
73 if href is not None and not href.startswith(('http://', 'https://')): | 87 if href is not None and not href.startswith(('http://', 'https://')): |
74 if not href.startswith('/'): | 88 if not href.startswith('/'): |
75 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) | 89 logging.warn('Paths in sidenav must be qualified. %s is not.' % href) |
76 else: | 90 else: |
77 href = href.lstrip('/') | 91 href = href.lstrip('/') |
78 item['href'] = self._server_instance.base_path + href | 92 item['href'] = self._server_instance.base_path + href |
79 | 93 |
80 def Cron(self): | 94 def Cron(self): |
81 futures = [self._cache.GetFromFile('%s/%s_sidenav.json' % | 95 future = self._cache.GetFromFile('%s/chrome_sidenav.json' % |
82 (JSON_TEMPLATES, platform)) | 96 (JSON_TEMPLATES)) |
83 for platform in ('apps', 'extensions')] | 97 return future |
not at google - send to devlin
2013/12/18 05:10:14
inline this (no need for the extra |future| variab
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
84 return Future(delegate=Gettable(lambda: [f.Get() for f in futures])) | |
85 | 98 |
86 def get(self, key): | 99 def get(self, key): |
not at google - send to devlin
2013/12/18 05:10:14
add:
# TODO(mangini/kalman): Use |key| to decide
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
87 sidenav = copy.deepcopy(self._cache.GetFromFile( | 100 sidenav = self._cache.GetFromFile( |
88 '%s/%s_sidenav.json' % (JSON_TEMPLATES, key)).Get()) | 101 '%s/chrome_sidenav.json' % JSON_TEMPLATES).Get() |
89 _AddSelected(sidenav, self._server_instance.base_path + self._request.path) | 102 sidenav = copy.deepcopy(sidenav) |
90 return sidenav | 103 _AddAnnotations(sidenav, |
104 self._server_instance.base_path + self._request.path) | |
not at google - send to devlin
2013/12/18 05:10:14
indent -= 3 to align with the (
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
105 return sidenav | |
not at google - send to devlin
2013/12/18 05:10:14
no trailing space
Renato Mangini (chromium)
2013/12/18 13:25:30
Done.
| |
OLD | NEW |