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 | 6 import json |
7 import logging | 7 import logging |
8 | 8 |
9 import compiled_file_system as compiled_fs | 9 import compiled_file_system as compiled_fs |
10 from third_party.json_schema_compiler.model import UnixName | 10 from third_party.json_schema_compiler.model import UnixName |
11 | 11 |
12 class SidenavDataSource(object): | 12 class SidenavDataSource(object): |
13 """This class reads in and caches a JSON file representing the side navigation | 13 """This class reads in and caches a JSON file representing the side navigation |
14 menu. | 14 menu. |
15 """ | 15 """ |
16 class Factory(object): | 16 class Factory(object): |
17 def __init__(self, compiled_fs_factory, json_path): | 17 def __init__(self, compiled_fs_factory, json_path, base_path): |
18 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, | 18 self._cache = compiled_fs_factory.Create(self._CreateSidenavDict, |
19 SidenavDataSource) | 19 SidenavDataSource) |
20 self._json_path = json_path | 20 self._json_path = json_path |
21 self._base_path = base_path | |
21 | 22 |
22 def Create(self, path): | 23 def Create(self, path): |
23 """Create a SidenavDataSource, binding it to |path|. |path| is the url | 24 """Create a SidenavDataSource, binding it to |path|. |path| is the url |
24 of the page that is being rendered. It is used to determine which item | 25 of the page that is being rendered. It is used to determine which item |
25 in the sidenav should be highlighted. | 26 in the sidenav should be highlighted. |
26 """ | 27 """ |
27 return SidenavDataSource(self._cache, self._json_path, path) | 28 return SidenavDataSource(self._cache, |
29 self._json_path, | |
30 path, | |
31 self._base_path) | |
28 | 32 |
29 def _AddLevels(self, items, level): | 33 def _AddLevels(self, items, level): |
30 """Levels represent how deeply this item is nested in the sidenav. We | 34 """Levels represent how deeply this item is nested in the sidenav. We |
31 start at 2 because the top <ul> is the only level 1 element. | 35 start at 2 because the top <ul> is the only level 1 element. |
32 """ | 36 """ |
33 for item in items: | 37 for item in items: |
34 item['level'] = level | 38 item['level'] = level |
35 if 'items' in item: | 39 if 'items' in item: |
36 self._AddLevels(item['items'], level + 1) | 40 self._AddLevels(item['items'], level + 1) |
37 | 41 |
38 def _CreateSidenavDict(self, json_path, json_str): | 42 def _CreateSidenavDict(self, json_path, json_str): |
39 items = json.loads(json_str) | 43 items = json.loads(json_str) |
40 self._AddLevels(items, 2); | 44 self._AddLevels(items, 2); |
41 return items | 45 return items |
42 | 46 |
43 def __init__(self, cache, json_path, path): | 47 def __init__(self, cache, json_path, path, base_path): |
44 self._cache = cache | 48 self._cache = cache |
45 self._json_path = json_path | 49 self._json_path = json_path |
46 self._file_name = path.split('/')[-1] | 50 self._file_name = path.split('/')[-1] |
51 self._base_path = base_path | |
47 | 52 |
48 def _AddSelected(self, items): | 53 def _AddSelected(self, items): |
49 for item in items: | 54 for item in items: |
50 if item.get('fileName', '') == self._file_name: | 55 if item.get('fileName', '') == self._file_name: |
51 item['selected'] = True | 56 item['selected'] = True |
52 return True | 57 return True |
53 if 'items' in item: | 58 if 'items' in item: |
54 if self._AddSelected(item['items']): | 59 if self._AddSelected(item['items']): |
55 item['child_selected'] = True | 60 item['child_selected'] = True |
56 return True | 61 return True |
57 return False | 62 return False |
58 | 63 |
64 def _ConvertToAbsolutePath(self, items, key): | |
65 for item in items: | |
66 fileName = item.get('fileName') | |
not at google - send to devlin
2013/05/24 16:58:29
file_name = ...
方觉(Fang Jue)
2013/05/25 00:08:39
Done.
| |
67 if fileName is not None and not fileName.startswith(('/', | |
68 'http://', | |
69 'https://')): | |
70 item['fileName'] = '%s/%s/%s' % (self._base_path, key, fileName) | |
71 if 'items' in item: | |
72 self._ConvertToAbsolutePath(item['items'], key) | |
73 | |
59 def get(self, key): | 74 def get(self, key): |
60 sidenav = copy.deepcopy(self._cache.GetFromFile( | 75 sidenav = copy.deepcopy(self._cache.GetFromFile( |
61 '%s/%s_sidenav.json' % (self._json_path, key))) | 76 '%s/%s_sidenav.json' % (self._json_path, key))) |
62 self._AddSelected(sidenav) | 77 self._AddSelected(sidenav) |
78 self._ConvertToAbsolutePath(sidenav, key) | |
not at google - send to devlin
2013/05/24 16:58:29
it would be more correct to use the path of the re
方觉(Fang Jue)
2013/05/25 00:08:39
path here is something like 'extensions/index.html
not at google - send to devlin
2013/05/28 14:53:37
Why is the 'extensions/index.html' example special
| |
63 return sidenav | 79 return sidenav |
OLD | NEW |