forked from jbillimoria/JavaScriptButtons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.js
More file actions
92 lines (70 loc) · 2.04 KB
/
factory.js
File metadata and controls
92 lines (70 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
var DataStore = require('./util/datastore'),
constants = require('./constants'),
button = require('./button'),
css = require('browserlib').css,
form = require('./form'),
hasCss = false;
module.exports = function factory(business, raw, config) {
var data, wrapper, html, key, label, type;
if (!business) {
return false;
}
// Normalize incoming data if needed
if (raw.items) {
data = raw;
} else {
data = new DataStore();
for (key in raw) {
if (raw.hasOwnProperty(key)) {
data.add(key, raw[key]);
}
}
}
// Defaults
config = config || {};
label = config.label || constants.DEFAULT_LABEL;
type = config.type || constants.DEFAULT_TYPE;
// Cart
if (type === 'cart') {
data.add('cmd', '_cart');
data.add('add', true);
// Donation
} else if (type === 'donate') {
data.add('cmd', '_donations');
// Subscribe
} else if (type === 'subscribe') {
data.add('cmd', '_xclick-subscriptions');
if (data.get('amount') && !data.get('a3')) {
data.add('a3', data.pluck('amount'));
}
// Buy Now
} else if (data.get('hosted_button_id')) {
data.add('cmd', '_s-xclick');
} else {
data.add('cmd', '_xclick');
}
// Add common data
data.add('business', business);
data.add('bn', constants.BN_CODE.replace(/\{label\}/, label));
// Build the UI components
if (type === 'button') {
html = button(label, data, config);
} else {
html = form(label, data, config);
}
// Inject the CSS onto the page
if (!hasCss) {
hasCss = true;
css.inject(document.getElementsByTagName('head')[0], constants.STYLES);
}
// Wrap it up all nice and neat and return it
wrapper = document.createElement('div');
wrapper.className = constants.WIDGET_NAME;
wrapper.innerHTML = html;
return {
label: label,
type: type,
el: wrapper
};
};