|
2 | 2 |
|
3 | 3 |
|
4 | 4 | var constants = require('./constants'), |
5 | | - Button = require('./button'); |
| 5 | + template = require('./util/template'), |
| 6 | + Button = require('./button'); |
6 | 7 |
|
7 | 8 |
|
8 | 9 | module.exports = function Form(type, data, config) { |
9 | | - var form = document.createElement('form'), |
10 | | - hidden = document.createElement('input'), |
11 | | - paraElem = document.createElement('p'), |
12 | | - labelElem = document.createElement('label'), |
13 | | - inputTextElem = document.createElement('input'), |
14 | | - selectElem = document.createElement('select'), |
15 | | - optionElem = document.createElement('option'), |
16 | | - items = data.items, |
17 | | - optionFieldArr = [], |
18 | | - item, child, label, input, key, selector, optionField, fieldDetails = {}, fieldDetail, fieldValue, field, labelText, btn; |
19 | | - |
20 | | - // Defaults |
21 | | - config = config || {}; |
22 | | - config.host = config.host || constants.DEFAULT_HOST; |
23 | | - |
24 | | - form.method = 'post'; |
25 | | - form.action = constants.PAYPAL_URL.replace('{host}', config.host); |
26 | | - form.className = 'paypal-button-widget'; |
27 | | - form.target = '_top'; |
28 | | - |
29 | | - inputTextElem.type = 'text'; |
30 | | - inputTextElem.className = 'paypal-input'; |
31 | | - paraElem.className = 'paypal-group'; |
32 | | - labelElem.className = 'paypal-label'; |
33 | | - selectElem.className = 'paypal-select'; |
34 | | - |
35 | | - hidden.type = 'hidden'; |
36 | | - |
37 | | - for (key in items) { |
38 | | - item = items[key]; |
39 | | - |
40 | | - |
41 | | - if (item.hasOptions) { |
42 | | - optionFieldArr.push(item); |
43 | | - } else if (item.isEditable) { |
44 | | - input = inputTextElem.cloneNode(true); |
45 | | - input.name = item.key; |
46 | | - input.value = item.value; |
47 | | - |
48 | | - label = labelElem.cloneNode(true); |
49 | | - // FIXME: This needs to resolve to user strings |
50 | | - labelText = item.key; |
51 | | - label.htmlFor = item.key; |
52 | | - label.appendChild(document.createTextNode(labelText)); |
53 | | - label.appendChild(input); |
54 | | - |
55 | | - child = paraElem.cloneNode(true); |
56 | | - child.appendChild(label); |
57 | | - form.appendChild(child); |
58 | | - } else { |
59 | | - input = child = hidden.cloneNode(true); |
60 | | - input.name = item.key; |
61 | | - input.value = item.value; |
62 | | - form.appendChild(child); |
63 | | - } |
64 | | - } |
| 10 | + // var form = document.createElement('form'), |
| 11 | + // hidden = document.createElement('input'), |
| 12 | + // paraElem = document.createElement('p'), |
| 13 | + // labelElem = document.createElement('label'), |
| 14 | + // inputTextElem = document.createElement('input'), |
| 15 | + // selectElem = document.createElement('select'), |
| 16 | + // optionElem = document.createElement('option'), |
| 17 | + // items = data.items, |
| 18 | + // optionFieldArr = [], |
| 19 | + // item, child, label, input, key, selector, optionField, fieldDetails = {}, fieldDetail, fieldValue, field, labelText, btn; |
| 20 | + |
| 21 | + // // Defaults |
| 22 | + // config = config || {}; |
| 23 | + // config.host = config.host || constants.DEFAULT_HOST; |
| 24 | + |
| 25 | + // form.method = 'post'; |
| 26 | + // form.action = constants.PAYPAL_URL.replace('{host}', config.host); |
| 27 | + // form.className = 'paypal-button-widget'; |
| 28 | + // form.target = '_top'; |
| 29 | + |
| 30 | + // inputTextElem.type = 'text'; |
| 31 | + // inputTextElem.className = 'paypal-input'; |
| 32 | + // paraElem.className = 'paypal-group'; |
| 33 | + // labelElem.className = 'paypal-label'; |
| 34 | + // selectElem.className = 'paypal-select'; |
| 35 | + |
| 36 | + // hidden.type = 'hidden'; |
| 37 | + |
| 38 | + // for (key in items) { |
| 39 | + // item = items[key]; |
| 40 | + |
| 41 | + |
| 42 | + // if (item.hasOptions) { |
| 43 | + // optionFieldArr.push(item); |
| 44 | + // } else if (item.isEditable) { |
| 45 | + // input = inputTextElem.cloneNode(true); |
| 46 | + // input.name = item.key; |
| 47 | + // input.value = item.value; |
| 48 | + |
| 49 | + // label = labelElem.cloneNode(true); |
| 50 | + // // FIXME: This needs to resolve to user strings |
| 51 | + // labelText = item.key; |
| 52 | + // label.htmlFor = item.key; |
| 53 | + // label.appendChild(document.createTextNode(labelText)); |
| 54 | + // label.appendChild(input); |
| 55 | + |
| 56 | + // child = paraElem.cloneNode(true); |
| 57 | + // child.appendChild(label); |
| 58 | + // form.appendChild(child); |
| 59 | + // } else { |
| 60 | + // input = child = hidden.cloneNode(true); |
| 61 | + // input.name = item.key; |
| 62 | + // input.value = item.value; |
| 63 | + // form.appendChild(child); |
| 64 | + // } |
| 65 | + // } |
65 | 66 |
|
66 | 67 | // optionFieldArr = sortOptionFields(optionFieldArr); |
67 | 68 |
|
@@ -128,34 +129,28 @@ module.exports = function Form(type, data, config) { |
128 | 129 | // form.appendChild(child); |
129 | 130 | // } |
130 | 131 | // } |
131 | | - |
132 | | - btn = new Button(type, data, config); |
133 | | - |
134 | | - // Safari won't let you set read-only attributes on buttons. |
135 | | - try { |
136 | | - btn.type = 'submit'; |
137 | | - } catch (e) { |
138 | | - btn.setAttribute('type', 'submit'); |
139 | | - } |
140 | | - |
141 | | - // Add the correct button |
142 | | - form.appendChild(btn); |
143 | | - |
144 | | - return form; |
| 132 | + |
| 133 | + var model = { |
| 134 | + data: data.items, |
| 135 | + button: new Button(type, data, config), |
| 136 | + url: constants.PAYPAL_URL.replace('{host}', config.host || constants.DEFAULT_HOST) |
| 137 | + }; |
| 138 | + |
| 139 | + return template(constants.TEMPLATE.form, model); |
145 | 140 | }; |
146 | 141 |
|
147 | 142 |
|
148 | 143 |
|
149 | | -/** |
150 | | - * Sort Optional Fields by display order |
151 | | - */ |
152 | | -function sortOptionFields(optionFieldArr) { |
153 | | - optionFieldArr.sort(function (a, b) { |
154 | | - return a.displayOrder - b.displayOrder; |
155 | | - }); |
| 144 | +// /** |
| 145 | +// * Sort Optional Fields by display order |
| 146 | +// */ |
| 147 | +// function sortOptionFields(optionFieldArr) { |
| 148 | +// optionFieldArr.sort(function (a, b) { |
| 149 | +// return a.displayOrder - b.displayOrder; |
| 150 | +// }); |
156 | 151 |
|
157 | | - return optionFieldArr; |
158 | | -} |
| 152 | +// return optionFieldArr; |
| 153 | +// } |
159 | 154 |
|
160 | 155 |
|
161 | 156 |
|
0 commit comments