1 | var builder = { |
---|
2 | url: null, // to be set layout.html |
---|
3 | modules: null, // to be set in builder-vX.Y.js |
---|
4 | assets: null, // to be set by callback |
---|
5 | setAssets: function(assets) { |
---|
6 | this.assets = assets; |
---|
7 | } |
---|
8 | }; |
---|
9 | |
---|
10 | Ext.onReady(function() { |
---|
11 | |
---|
12 | var assets = builder.assets; |
---|
13 | var modules = builder.modules; |
---|
14 | |
---|
15 | var checks = {}; |
---|
16 | var prefs = {}; |
---|
17 | |
---|
18 | // uncheck components that have not been specifically checked by the user |
---|
19 | var updateDeps = function() { |
---|
20 | for (var path in assets) { |
---|
21 | var check = checks[path]; |
---|
22 | if (check) { |
---|
23 | var checked = check.getValue(); |
---|
24 | if (checked && !prefs[path]) { |
---|
25 | // keep checked if required or included |
---|
26 | var depended = false; |
---|
27 | for (var p in assets) { |
---|
28 | var c = checks[p]; |
---|
29 | if (c && c.getValue() && ((path in assets[p].require) || (path in assets[p].include))) { |
---|
30 | depended = true; |
---|
31 | break; |
---|
32 | } |
---|
33 | } |
---|
34 | if (!depended) { |
---|
35 | check.setValue(false); |
---|
36 | check.enable(); |
---|
37 | } |
---|
38 | } |
---|
39 | } |
---|
40 | } |
---|
41 | }; |
---|
42 | |
---|
43 | // check additional requires and includes |
---|
44 | var checkDeps = function(path) { |
---|
45 | var check, asset = assets[path]; |
---|
46 | for (var include in asset.include) { |
---|
47 | check = checks[include]; |
---|
48 | if (check && !check.getValue()) { |
---|
49 | checks[include].setValue(true); |
---|
50 | } |
---|
51 | } |
---|
52 | for (var require in asset.require) { |
---|
53 | check = checks[require]; |
---|
54 | if (check && !check.getValue()) { |
---|
55 | checks[require].setValue(true); |
---|
56 | checks[require].disable(); |
---|
57 | } |
---|
58 | } |
---|
59 | }; |
---|
60 | |
---|
61 | // check form data before submitting |
---|
62 | var validate = function() { |
---|
63 | // for now, we just want to make sure something is checked |
---|
64 | var valid = false; |
---|
65 | for (var path in checks) { |
---|
66 | if (checks[path].getValue()) { |
---|
67 | valid = true; |
---|
68 | break; |
---|
69 | } |
---|
70 | } |
---|
71 | if (!valid) { |
---|
72 | Ext.Msg.show({ |
---|
73 | msg: "You must select at least one component for your build.", |
---|
74 | icon: Ext.MessageBox.WARNING |
---|
75 | }); |
---|
76 | } |
---|
77 | return valid; |
---|
78 | }; |
---|
79 | |
---|
80 | |
---|
81 | var items = []; |
---|
82 | var module, set, sets = {}; |
---|
83 | for (var path in modules) { |
---|
84 | module = modules[path]; |
---|
85 | set = new Ext.form.FieldSet({ |
---|
86 | title: module.title, |
---|
87 | autoHeight: true, |
---|
88 | collapsible: true, |
---|
89 | layout: "column", |
---|
90 | autoWidth: true, |
---|
91 | html: "<div class='module-desc'>" + module.description + "</div>", |
---|
92 | defaults: {width: 300} |
---|
93 | }); |
---|
94 | sets[path] = set; |
---|
95 | items.push(set); |
---|
96 | } |
---|
97 | |
---|
98 | var checkbox, parts, prefix, base, name; |
---|
99 | var updating = false; |
---|
100 | for (var path in assets) { |
---|
101 | parts = path.split("/"); |
---|
102 | name = parts.pop().replace(".js", ""); |
---|
103 | var base = parts.join("/"); |
---|
104 | var set = sets[base]; |
---|
105 | if (set) { |
---|
106 | prefix = modules[base].prefix; |
---|
107 | checkbox = new Ext.form.Checkbox({ |
---|
108 | boxLabel: prefix ? (prefix + "." + name) : name, |
---|
109 | name: "include[]", |
---|
110 | inputValue: path, |
---|
111 | ctCls: "component", |
---|
112 | handler: function(box, checked) { |
---|
113 | var path = box.getRawValue(); |
---|
114 | var old = updating; |
---|
115 | if (checked) { |
---|
116 | updating = true; |
---|
117 | checkDeps(path); |
---|
118 | updating = old; |
---|
119 | } else { |
---|
120 | updating = true; |
---|
121 | updateDeps(); |
---|
122 | updating = old; |
---|
123 | } |
---|
124 | if (!updating) { |
---|
125 | // check represents user preferene |
---|
126 | prefs[path] = checked; |
---|
127 | } |
---|
128 | } |
---|
129 | }); |
---|
130 | checks[path] = checkbox; |
---|
131 | set.add(checkbox); |
---|
132 | } |
---|
133 | } |
---|
134 | |
---|
135 | var dh = Ext.DomHelper; |
---|
136 | |
---|
137 | var formContainer = dh.insertAfter( |
---|
138 | Ext.get(Ext.select(".builder-form-text").first()), |
---|
139 | {tag: "div", cls: "builder-form"} |
---|
140 | ); |
---|
141 | |
---|
142 | var form = new Ext.FormPanel({ |
---|
143 | method: "POST", |
---|
144 | standardSubmit: true, |
---|
145 | border: false, |
---|
146 | bodyStyle: {padding: 10}, |
---|
147 | renderTo: formContainer, |
---|
148 | labelWidth: 100, |
---|
149 | autoWidth: true, |
---|
150 | items: items |
---|
151 | }); |
---|
152 | |
---|
153 | var buttonContainer = dh.insertAfter( |
---|
154 | Ext.get(Ext.select(".download-button-text").first()), |
---|
155 | {tag: "div", cls: "download-button"} |
---|
156 | ); |
---|
157 | |
---|
158 | var download = new Ext.Button({ |
---|
159 | renderTo: buttonContainer, |
---|
160 | text: "download", |
---|
161 | iconCls: "download", |
---|
162 | handler: function() { |
---|
163 | if (validate()) { |
---|
164 | var el = form.getForm().getEl().dom; |
---|
165 | el.action = builder.url, |
---|
166 | el.submit(); |
---|
167 | } |
---|
168 | } |
---|
169 | }); |
---|
170 | |
---|
171 | }); |
---|