-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathstripe-element.js
More file actions
133 lines (106 loc) · 3.52 KB
/
Copy pathstripe-element.js
File metadata and controls
133 lines (106 loc) · 3.52 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import Component from '@ember/component';
import { inject as service } from '@ember/service';
import { computed, get, set } from '@ember/object';
export default Component.extend({
classNames: ['ember-stripe-element'],
autofocus: false,
options: null,
stripeElement: null,
stripeError: null,
type: null, // Set in components that extend from `stripe-element`
stripev3: service(),
elements: computed(function() {
return get(this, 'stripev3.elements')();
}),
didInsertElement() {
this._super(...arguments);
// Fetch user options
let options = get(this, 'options') || {};
// Fetch `type` set by child component
let type = get(this, 'type');
// `stripeElement` instead of `element` to distinguish from `this.element`
let stripeElement = get(this, 'elements').create(type, options);
// Mount the Stripe Element onto the mount point
stripeElement.mount(this.element.querySelector('[role="mount-point"]'));
// Make the element available to the component
set(this, 'stripeElement', stripeElement);
// Set the event listeners
this.setEventListeners();
},
didRender() {
this._super(...arguments);
// Fetch autofocus, set by user
let autofocus = get(this, 'autofocus');
let stripeElement = get(this, 'stripeElement');
let iframe = this.element.querySelector('iframe');
if (autofocus && iframe) {
iframe.onload = () => {
stripeElement.focus();
};
}
},
didUpdateAttrs() {
this._super(...arguments);
let options = get(this, 'options') || {};
get(this, 'stripeElement').update(options);
},
willDestroyElement() {
this._super(...arguments);
get(this, 'stripeElement').unmount();
},
setEventListeners() {
let stripeElement = get(this, 'stripeElement');
stripeElement.on('ready', (event) => this.sendAction('ready', stripeElement, event));
stripeElement.on('blur', (event) => this.sendAction('blur', stripeElement, event));
stripeElement.on('focus', (event) => this.sendAction('focus', stripeElement, event));
stripeElement.on('change', (...args) => {
let [{ complete, error: stripeError }] = args;
this.sendAction('change', stripeElement, ...args);
if (complete) {
this.sendAction('complete', stripeElement);
} else if (stripeError) {
this.sendAction('error', stripeError);
}
set(this, 'stripeError', stripeError);
});
}
/*setEventListeners() {
let stripeElement = get(this, 'stripeElement');
stripeElement.on('ready', (event) => {
this._invokeAction('ready', stripeElement, event)
});
stripeElement.on('blur', (event) => {
this._invokeAction('blur', stripeElement, event)
});
stripeElement.on('focus', (event) => {
this._invokeAction('focus', stripeElement, event)
});
stripeElement.on('change', (...args) => {
if (this.isDestroying || this.isDestroyed) {
return;
}
let [{ complete, error: stripeError }] = args;
this.change(stripeElement, ...args);
if (complete) {
this._invokeAction('complete', stripeElement)
} else if (stripeError) {
this._invokeAction('error', stripeError)
}
set(this, 'stripeError', stripeError);
});
},
_invokeAction(method, ...args) {
if (this.isDestroying || this.isDestroyed) {
return;
}
if (typeof this[method] === 'function') {
this[method](...args)
}
},
ready() { },
blur() { },
focus() { },
change() { },
complete() { },
error() { }*/
});