11'use strict' ;
22
33
4- var Button = require ( './button' ) ,
5- Form = require ( './form' ) ,
4+ var DataStore = require ( './util/datastore' ) ,
5+ constants = require ( './constants' ) ,
6+ button = require ( './button' ) ,
7+ css = require ( './util/css' ) ,
8+ form = require ( './form' ) ,
69 QR = require ( './qr' ) ,
7- DataStore = require ( './util/datastore' ) ,
8- constants = require ( './constants' ) ;
10+ hasCss = false ;
911
1012
1113
1214module . exports = function factory ( business , raw , config ) {
13- var data , el , key , label , type , env ;
15+ var data , wrapper , html , key , label , type , env ;
1416
1517 if ( ! business ) { return false ; }
1618
19+
1720 // Normalize incoming data if needed
1821 if ( raw . items ) {
1922 data = raw ;
@@ -25,28 +28,28 @@ module.exports = function factory(business, raw, config) {
2528 }
2629 }
2730
31+
2832 // Defaults
2933 config = config || { } ;
3034 label = config . label || constants . DEFAULT_LABEL ;
3135 type = config . type || constants . DEFAULT_TYPE ;
3236
33- // Cart buttons
37+
38+ // Cart
3439 if ( type === 'cart' ) {
3540 data . add ( 'cmd' , '_cart' ) ;
3641 data . add ( 'add' , true ) ;
37- // Donation buttons
42+ // Donation
3843 } else if ( type === 'donate' ) {
3944 data . add ( 'cmd' , '_donations' ) ;
40- // Subscribe buttons
45+ // Subscribe
4146 } else if ( type === 'subscribe' ) {
4247 data . add ( 'cmd' , '_xclick-subscriptions' ) ;
4348
44- // TODO: "amount" cannot be used in prettyParams since it's overloaded
45- // Find a better way to do this
4649 if ( data . get ( 'amount' ) && ! data . get ( 'a3' ) ) {
47- data . add ( 'a3' , data . get ( 'amount' ) ) ;
50+ data . add ( 'a3' , data . pluck ( 'amount' ) ) ;
4851 }
49- // Buy Now buttons
52+ // Buy Now
5053 } else {
5154 if ( data . get ( 'hosted_button_id' ) ) {
5255 data . add ( 'cmd' , '_s-xclick' ) ;
@@ -59,21 +62,32 @@ module.exports = function factory(business, raw, config) {
5962 data . add ( 'business' , business ) ;
6063 data . add ( 'bn' , constants . BN_CODE . replace ( / \{ l a b e l \} / , label ) ) ;
6164
65+
6266 // Build the UI components
6367 if ( type === 'qr' ) {
64- el = QR ( data , config ) ;
68+ html = QR ( data , config ) ;
6569 } else if ( type === 'button' ) {
66- el = Button ( label , data , config ) ;
70+ html = button ( label , data , config ) ;
6771 } else {
68- el = Form ( label , data , config ) ;
72+ html = form ( label , data , config ) ;
6973 }
7074
71- // Inject CSS
72- // injectCSS();
75+
76+ // Inject the CSS onto the page
77+ if ( ! hasCss ) {
78+ hasCss = true ;
79+ css . inject ( document . getElementsByTagName ( 'head' ) [ 0 ] , constants . STYLES ) ;
80+ }
81+
82+
83+ // Wrap it up all nice and neat and return it
84+ wrapper = document . createElement ( 'div' ) ;
85+ wrapper . className = constants . WIDGET_NAME ;
86+ wrapper . innerHTML = html ;
7387
7488 return {
7589 label : label ,
7690 type : type ,
77- el : document . createElement ( 'div' ) . innerHTML = el
91+ el : wrapper
7892 } ;
7993} ;
0 commit comments