OLD | NEW |
| (Empty) |
1 # Copyright 2015 The Chromium Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import util | |
6 import os | |
7 import datetime | |
8 | |
9 HTML_FILE_TEMPLATE = \ | |
10 """<!-- Copyright %(year)d The Chromium Authors. All rights reserved. | |
11 Use of this source code is governed by a BSD-style license that can be | |
12 found in the LICENSE file. --> | |
13 | |
14 <!-- NOTE: this file is generated from "%(source)s". Do not modify directly. --> | |
15 | |
16 <!doctype html> | |
17 <html><head> | |
18 <link rel="import" href="chrome://resources/polymer/polymer/polymer.html"> | |
19 <link rel="import" href="/webui_generator/webui-view.html"> | |
20 %(children_includes)s | |
21 </head><body> | |
22 <polymer-element name="%(element_name)s" extends="webui-view"> | |
23 </polymer-element> | |
24 <script src="%(js_file_path)s"></script> | |
25 </body></html> | |
26 """ | |
27 | |
28 JS_FILE_TEMPLATE = \ | |
29 """// Copyright %(year)d The Chromium Authors. All rights reserved. | |
30 // Use of this source code is governed by a BSD-style license that can be | |
31 // found in the LICENSE file. | |
32 | |
33 // NOTE: this file is generated from "%(source)s". Do not modify directly. | |
34 | |
35 | |
36 Polymer('%(element_name)s', (function() { | |
37 'use strict'; | |
38 return { | |
39 getType: function() { | |
40 return '%(type)s'; | |
41 }, | |
42 | |
43 initialize: function() {}, | |
44 contextChanged: function(changedKeys) {}, | |
45 | |
46 initChildren_: function() { | |
47 %(init_children_body)s | |
48 }, | |
49 | |
50 initContext_: function() { | |
51 %(init_context_body)s | |
52 } | |
53 } | |
54 })()); | |
55 """ | |
56 | |
57 def GetCommonSubistitutions(declaration): | |
58 subs = {} | |
59 subs['year'] = datetime.date.today().year | |
60 subs['element_name'] = declaration.type.replace('_', '-') + '-view' | |
61 subs['source'] = declaration.path | |
62 return subs | |
63 | |
64 def GenChildrenIncludes(children): | |
65 lines = [] | |
66 for declaration in set(children.itervalues()): | |
67 lines.append('<link rel="import" href="/%s">' % | |
68 declaration.html_view_html_include_path) | |
69 return '\n'.join(lines) | |
70 | |
71 def GenHTMLFile(declaration): | |
72 subs = GetCommonSubistitutions(declaration) | |
73 subs['js_file_path'] = subs['element_name'] + '.js' | |
74 subs['children_includes'] = GenChildrenIncludes(declaration.children) | |
75 return HTML_FILE_TEMPLATE % subs | |
76 | |
77 def GenInitChildrenBody(children): | |
78 lines = [] | |
79 lines.append(' var child = null;'); | |
80 for id in children: | |
81 lines.append(' child = this.shadowRoot.querySelector(\'[wugid="%s"]\');' | |
82 % id); | |
83 lines.append(' if (!child)'); | |
84 lines.append(' console.error(this.path_ + \'$%s not found.\');' % id); | |
85 lines.append(' else'); | |
86 lines.append(' child.setPath_(this.path_ + \'$%s\');' % id) | |
87 return '\n'.join(lines) | |
88 | |
89 def GenInitContextBody(fields): | |
90 lines = [] | |
91 for field in fields: | |
92 value = '' | |
93 if field.type in ['integer', 'double']: | |
94 value = str(field.default_value) | |
95 elif field.type == 'boolean': | |
96 value = 'true' if field.default_value else 'false' | |
97 elif field.type == 'string': | |
98 value = '\'%s\'' % field.default_value | |
99 elif field.type == 'string_list': | |
100 value = '[' + \ | |
101 ', '.join('\'%s\'' % s for s in field.default_value) + ']' | |
102 lines.append(' this.context.set(\'%s\', %s);' % (field.id, value)) | |
103 lines.append(' this.context.getChangesAndReset();') | |
104 return '\n'.join(lines) | |
105 | |
106 def GenJSFile(declaration): | |
107 subs = GetCommonSubistitutions(declaration) | |
108 subs['type'] = declaration.type | |
109 subs['init_children_body'] = GenInitChildrenBody(declaration.children) | |
110 subs['init_context_body'] = GenInitContextBody(declaration.fields) | |
111 return JS_FILE_TEMPLATE % subs | |
OLD | NEW |