-
Notifications
You must be signed in to change notification settings - Fork 79
/
UploadFieldEntwine.js
88 lines (76 loc) · 2.22 KB
/
UploadFieldEntwine.js
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
/* global window */
import jQuery from 'jquery';
import React from 'react';
import ReactDOM from 'react-dom';
import { schemaMerge } from 'lib/schemaFieldValues';
import { loadComponent } from 'lib/Injector';
/**
* Shiv for inserting react UploadField into entwine forms
*/
jQuery.entwine('ss', ($) => {
/**
* See boot/index.js for `.react-boot` bootstrap
*/
$('.js-injector-boot input.entwine-uploadfield').entwine({
Component: null,
getContainer() {
let container = this.siblings('.uploadfield-holder')[0];
if (!container) {
const newContainer = $('<div class="uploadfield-holder"></div>');
this.before(newContainer);
container = newContainer[0];
}
return container;
},
onunmatch() {
this._super();
// solves errors given by ReactDOM "no matched root found" error.
ReactDOM.unmountComponentAtNode(this.siblings('.uploadfield-holder')[0]);
},
onmatch() {
const cmsContent = this.closest('.cms-content').attr('id');
const context = (cmsContent)
? { context: cmsContent }
: {};
const UploadField = loadComponent('UploadField', context);
this.setComponent(UploadField);
this._super();
this.hide();
this.refresh();
},
onclick(e) {
// we don't want the native upload dialog to show up
e.preventDefault();
},
refresh() {
const props = this.getAttributes();
const form = $(this).closest('form');
const onChange = () => {
// Trigger change detection (see jquery.changetracker.js)
setTimeout(() => {
form.trigger('change');
}, 0);
};
const UploadField = this.getComponent();
// TODO: rework entwine so that react has control of holder
ReactDOM.render(
<UploadField
{...props}
onChange={onChange}
noHolder
/>,
this.getContainer()
);
},
/**
* Find the selected node and get attributes associated to attach the data to the form
*
* @returns {Object}
*/
getAttributes() {
const state = $(this).data('state');
const schema = $(this).data('schema');
return schemaMerge(schema, state);
},
});
});