|
28 | 28 |
|
29 | 29 | // This forces Drupal to re-attach |
30 | 30 | $(window).on('dialog:afterclose', function (e, dialog, $element) { |
31 | | - Drupal.attachBehaviors($(':root'), settings); |
| 31 | + Drupal.attachBehaviors(); |
32 | 32 | }); |
33 | 33 |
|
| 34 | + var saveSchema = function () { |
| 35 | + $('#schema_instance_config').val(JSON.stringify(window.patternkitEditor.getValue())); |
| 36 | + if (window.M) { |
| 37 | + window.M.updateTextFields(); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
34 | 41 | var $target = $('#editor-shadow-injection-target', context); |
35 | 42 | $target.once('patternkit-editor').each(function () { |
36 | 43 | var shadow = this.attachShadow({mode: 'open'}); |
|
91 | 98 | '&media_library_remaining=1' + |
92 | 99 | '&' + encodeURIComponent('media_library_opener_parameters[field_widget_id]') + '=' + this.path; |
93 | 100 |
|
| 101 | + this.urlfield.addEventListener('change', function(e) { |
| 102 | + e.preventDefault(); |
| 103 | + e.stopPropagation(); |
| 104 | + self.value = this.value; |
| 105 | + self.refreshPreview(); |
| 106 | + }); |
94 | 107 | this.button.addEventListener('click', function(e) { |
95 | 108 | e.preventDefault(); |
96 | 109 | e.stopPropagation(); |
|
115 | 128 | this.container.appendChild(this.button); |
116 | 129 | } |
117 | 130 |
|
118 | | - window.requestAnimationFrame(function() { |
| 131 | + window.requestAnimationFrame(function () { |
119 | 132 | if (self.value) { |
120 | 133 | var img = document.createElement('img'); |
121 | 134 | img.style.maxWidth = '100%'; |
122 | 135 | img.style.maxHeight = '100px'; |
123 | 136 | img.onload = function (event) { |
124 | 137 | self.preview.appendChild(img); |
125 | 138 | }; |
126 | | - img.onerror = function(error) { |
127 | | - window.console.error('upload error', error); |
128 | | - }; |
129 | 139 | img.src = self.container.querySelector('a').href; |
130 | 140 | } |
131 | 141 | }); |
132 | | - |
133 | 142 | }, |
134 | | - refreshPreview: function() { |
135 | | - if (this.last_preview === this.preview_value) { |
| 143 | + refreshPreview: function () { |
| 144 | + if (this.last_preview === this.value) { |
136 | 145 | return; |
137 | 146 | } |
138 | | - this.last_preview = this.preview_value; |
139 | | - |
| 147 | + this.last_preview = this.value; |
140 | 148 | this.preview.innerHTML = ''; |
141 | | - |
142 | | - if (!this.preview_value) { |
| 149 | + if (!this.value) { |
143 | 150 | return; |
144 | 151 | } |
145 | 152 |
|
146 | | - var self = this; |
147 | | - |
148 | | - var mime = this.preview_value.match(/^data:([^;,]+)[;,]/); |
149 | | - if (mime) { |
150 | | - mime = mime[1]; |
151 | | - } |
152 | | - else { |
153 | | - mime = 'unknown'; |
154 | | - } |
155 | | - |
156 | | - var file = this.urlfield.files[0]; |
157 | | - |
158 | | - this.preview.innerHTML = '<strong>Type:</strong> '+mime+', <strong>Size:</strong> '+file.size+' bytes'; |
159 | | - if(mime.substr(0,5)==="image") { |
160 | | - this.preview.innerHTML += '<br>'; |
161 | | - var img = document.createElement('img'); |
162 | | - img.style.maxWidth = '100%'; |
163 | | - img.style.maxHeight = '100px'; |
164 | | - img.src = this.preview_value; |
165 | | - this.preview.appendChild(img); |
166 | | - } |
167 | | - |
168 | | - this.preview.innerHTML += '<br>'; |
169 | | - var uploadButton = this.getButton('Upload', 'upload', 'Upload'); |
170 | | - this.preview.appendChild(uploadButton); |
171 | | - uploadButton.addEventListener('click',function(event) { |
172 | | - event.preventDefault(); |
173 | | - |
174 | | - uploadButton.setAttribute("disabled", "disabled"); |
175 | | - self.theme.removeInputError(self.uploader); |
176 | | - |
177 | | - if (self.theme.getProgressBar) { |
178 | | - self.progressBar = self.theme.getProgressBar(); |
179 | | - self.preview.appendChild(self.progressBar); |
180 | | - } |
181 | | - |
182 | | - self.jsoneditor.options.upload(self.path, file, { |
183 | | - success: function(url) { |
184 | | - self.setValue(url); |
185 | | - |
186 | | - if (self.parent) { |
187 | | - self.parent.onChildEditorChange(self); |
188 | | - } |
189 | | - else { |
190 | | - self.jsoneditor.onChange(); |
191 | | - } |
192 | | - |
193 | | - if (self.progressBar) { |
194 | | - self.preview.removeChild(self.progressBar); |
195 | | - } |
196 | | - uploadButton.removeAttribute("disabled"); |
197 | | - }, |
198 | | - failure: function(error) { |
199 | | - self.theme.addInputError(self.uploader, error); |
200 | | - if (self.progressBar) { |
201 | | - self.preview.removeChild(self.progressBar); |
202 | | - } |
203 | | - uploadButton.removeAttribute("disabled"); |
204 | | - }, |
205 | | - updateProgress: function(progress) { |
206 | | - if (self.progressBar) { |
207 | | - if (progress) { |
208 | | - self.theme.updateProgressBar(self.progressBar, progress); |
209 | | - } |
210 | | - else { |
211 | | - self.theme.updateProgressBarUnknown(self.progressBar); |
212 | | - } |
213 | | - } |
214 | | - } |
215 | | - }); |
216 | | - }); |
217 | | - |
218 | | - if(this.jsoneditor.options.auto_upload || this.schema.options.auto_upload) { |
219 | | - uploadButton.dispatchEvent(new MouseEvent('click')); |
220 | | - this.preview.removeChild(uploadButton); |
221 | | - } |
| 153 | + var img = document.createElement('img'); |
| 154 | + img.style.maxWidth = '100%'; |
| 155 | + img.style.maxHeight = '100px'; |
| 156 | + img.src = this.value; |
| 157 | + this.preview.appendChild(img); |
222 | 158 | }, |
223 | | - enable: function() { |
224 | | - if(!this.always_disabled) { |
225 | | - if(this.urlfield) { |
| 159 | + enable: function () { |
| 160 | + if (!this.always_disabled) { |
| 161 | + if (this.urlfield) { |
226 | 162 | this.urlfield.disabled = false; |
227 | 163 | } |
228 | 164 | this._super(); |
229 | 165 | } |
230 | 166 | }, |
231 | | - disable: function(always_disabled) { |
232 | | - if(always_disabled) { |
| 167 | + disable: function (always_disabled) { |
| 168 | + if (always_disabled) { |
233 | 169 | this.always_disabled = true; |
234 | 170 | } |
235 | | - if(this.urlfield) { |
| 171 | + if (this.urlfield) { |
236 | 172 | this.urlfield.disabled = true; |
237 | 173 | } |
| 174 | + if (this.button) { |
| 175 | + this.button.disabled = true; |
| 176 | + } |
238 | 177 | this._super(); |
239 | 178 | }, |
240 | | - setValue: function(val) { |
241 | | - if(this.value !== val) { |
| 179 | + setValue: function (val) { |
| 180 | + if (this.value !== val) { |
242 | 181 | this.value = val; |
243 | 182 | this.urlfield.value = this.value; |
244 | | - this.onChange(); |
| 183 | + this.refreshPreview(); |
| 184 | + this.onChange(true); |
245 | 185 | } |
246 | 186 | }, |
247 | | - destroy: function() { |
| 187 | + destroy: function () { |
248 | 188 | if(this.preview && this.preview.parentNode) { |
249 | 189 | this.preview.parentNode.removeChild(this.preview); |
250 | 190 | } |
|
261 | 201 | this._super(); |
262 | 202 | } |
263 | 203 | }); |
264 | | - JSONEditor.defaults.resolvers.unshift(function(schema) { |
| 204 | + JSONEditor.defaults.resolvers.unshift(function (schema) { |
265 | 205 | if (schema.type === 'string' && schema.format === 'image') { |
266 | 206 | return 'drupal_image'; |
267 | 207 | } |
|
339 | 279 | } |
340 | 280 | window.patternkitEditor = new JSONEditor($target[0].shadowRoot.getElementById('editor_holder'), config); |
341 | 281 | JSONEditor.plugins.sceditor.emoticonsEnabled = false; |
342 | | - var saveSchema = function () { |
343 | | - $('#schema_instance_config').val(JSON.stringify(window.patternkitEditor.getValue())); |
344 | | - if (window.M) { |
345 | | - window.M.updateTextFields(); |
346 | | - } |
347 | | - }; |
348 | 282 |
|
349 | 283 | window.patternkitEditor.on('ready', function () { |
350 | 284 | if (window.M) { |
|
359 | 293 | // This is before allowing other events, so we need to add a pre-hook |
360 | 294 | // to trigger the editor update with latest field values. |
361 | 295 | // @TODO Add handling for AJAX errors and re-attach. |
362 | | - var beforeSubmitHandler = Drupal.Ajax.prototype.beforeSubmit; |
363 | | - Drupal.Ajax.prototype.beforeSubmit = function(formValues, element, options) { |
364 | | - if ( $(element).parents("#editor_holder").length !== 1 ) { |
365 | | - return beforeSubmitHandler(formValues, element, options); |
366 | | - } |
| 296 | + $('#edit-actions-submit').parent().submit(function (e) { |
| 297 | + e.preventDefault(); |
367 | 298 | window.patternkitEditor.disable(); |
368 | 299 | saveSchema(); |
369 | | - for (var v = 0; v < formValues.length; v++) { |
370 | | - if (formValues[v].name === 'settings[instance_config]') { |
371 | | - formValues[v].value = JSON.stringify(window.patternkitEditor.getValue()); |
372 | | - } |
373 | | - } |
374 | 300 | window.patternkitEditor.destroy(); |
375 | | - }; |
| 301 | + $(this).unbind('submit').submit(); |
| 302 | + }); |
376 | 303 | }); |
377 | 304 | } |
378 | 305 | }; |
|
0 commit comments