UNPKG

5.47 kBJavaScriptView Raw
1"use strict";
2
3var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
5Object.defineProperty(exports, "__esModule", {
6 value: true
7});
8exports.default = void 0;
9
10var _element = require("@wordpress/element");
11
12var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
13
14var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
15
16var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn"));
17
18var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf"));
19
20var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits"));
21
22var _assertThisInitialized2 = _interopRequireDefault(require("@babel/runtime/helpers/assertThisInitialized"));
23
24var _i18n = require("@wordpress/i18n");
25
26var _apiFetch = _interopRequireDefault(require("@wordpress/api-fetch"));
27
28var _compose = require("@wordpress/compose");
29
30var _WidgetEditDomManager = _interopRequireDefault(require("./WidgetEditDomManager"));
31
32/**
33 * WordPress dependencies
34 */
35
36/**
37 * Internal dependencies
38 */
39var WidgetEditHandler =
40/*#__PURE__*/
41function (_Component) {
42 (0, _inherits2.default)(WidgetEditHandler, _Component);
43
44 function WidgetEditHandler() {
45 var _this;
46
47 (0, _classCallCheck2.default)(this, WidgetEditHandler);
48 _this = (0, _possibleConstructorReturn2.default)(this, (0, _getPrototypeOf2.default)(WidgetEditHandler).apply(this, arguments));
49 _this.state = {
50 form: null,
51 idBase: null
52 };
53 _this.instanceUpdating = null;
54 _this.onInstanceChange = _this.onInstanceChange.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
55 _this.requestWidgetUpdater = _this.requestWidgetUpdater.bind((0, _assertThisInitialized2.default)((0, _assertThisInitialized2.default)(_this)));
56 return _this;
57 }
58
59 (0, _createClass2.default)(WidgetEditHandler, [{
60 key: "componentDidMount",
61 value: function componentDidMount() {
62 this.isStillMounted = true;
63 this.requestWidgetUpdater();
64 }
65 }, {
66 key: "componentDidUpdate",
67 value: function componentDidUpdate(prevProps) {
68 if (prevProps.instance !== this.props.instance && this.instanceUpdating !== this.props.instance) {
69 this.requestWidgetUpdater();
70 }
71
72 if (this.instanceUpdating === this.props.instance) {
73 this.instanceUpdating = null;
74 }
75 }
76 }, {
77 key: "componentWillUnmount",
78 value: function componentWillUnmount() {
79 this.isStillMounted = false;
80 }
81 }, {
82 key: "render",
83 value: function render() {
84 var _this2 = this;
85
86 var _this$props = this.props,
87 instanceId = _this$props.instanceId,
88 identifier = _this$props.identifier;
89 var _this$state = this.state,
90 id = _this$state.id,
91 idBase = _this$state.idBase,
92 form = _this$state.form;
93
94 if (!identifier) {
95 return (0, _i18n.__)('Not a valid widget.');
96 }
97
98 if (!form) {
99 return null;
100 }
101
102 return (0, _element.createElement)("div", {
103 className: "wp-block-legacy-widget__edit-container" // Display none is used because when we switch from edit to preview,
104 // we don't want to unmount the component.
105 // Otherwise when we went back to edit we wound need to trigger
106 // all widgets events again and some scripts may not deal well with this.
107 ,
108 style: {
109 display: this.props.isVisible ? 'block' : 'none'
110 }
111 }, (0, _element.createElement)(_WidgetEditDomManager.default, {
112 ref: function ref(_ref) {
113 _this2.widgetEditDomManagerRef = _ref;
114 },
115 onInstanceChange: this.onInstanceChange,
116 widgetNumber: instanceId * -1,
117 id: id,
118 idBase: idBase,
119 form: form
120 }));
121 }
122 }, {
123 key: "onInstanceChange",
124 value: function onInstanceChange(instanceChanges) {
125 var _this3 = this;
126
127 this.requestWidgetUpdater(instanceChanges, function (response) {
128 _this3.instanceUpdating = response.instance;
129
130 _this3.props.onInstanceChange(response.instance);
131 });
132 }
133 }, {
134 key: "requestWidgetUpdater",
135 value: function requestWidgetUpdater(instanceChanges, callback) {
136 var _this4 = this;
137
138 var _this$props2 = this.props,
139 identifier = _this$props2.identifier,
140 instanceId = _this$props2.instanceId,
141 instance = _this$props2.instance;
142
143 if (!identifier) {
144 return;
145 }
146
147 (0, _apiFetch.default)({
148 path: "/wp/v2/widgets/".concat(identifier, "/"),
149 data: {
150 identifier: identifier,
151 instance: instance,
152 // use negative ids to make sure the id does not exist on the database.
153 id_to_use: instanceId * -1,
154 instance_changes: instanceChanges
155 },
156 method: 'POST'
157 }).then(function (response) {
158 if (_this4.isStillMounted) {
159 _this4.setState({
160 form: response.form,
161 idBase: response.id_base,
162 id: response.id
163 });
164
165 if (callback) {
166 callback(response);
167 }
168 }
169 });
170 }
171 }]);
172 return WidgetEditHandler;
173}(_element.Component);
174
175var _default = (0, _compose.withInstanceId)(WidgetEditHandler);
176
177exports.default = _default;
178//# sourceMappingURL=WidgetEditHandler.js.map
\No newline at end of file