Skip to content

Commit 1e54f06

Browse files
authored
Merge pull request #12 from cybtachyon/hotfix/20200105--perf-field-save
20200105: Hotfixes editor performance and saving
2 parents e4a987d + b90a522 commit 1e54f06

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

js/patternkit.jsoneditor.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@
2626
window.patternkitEditor.getEditor(response.selector).setValue(response.value);
2727
};
2828

29-
// This forces Drupal to re-attach
30-
$(window).on('dialog:afterclose', function (e, dialog, $element) {
31-
Drupal.attachBehaviors();
32-
});
33-
3429
var saveSchema = function () {
3530
$('#schema_instance_config').val(JSON.stringify(window.patternkitEditor.getValue()));
3631
if (window.M) {
@@ -286,19 +281,35 @@
286281
}
287282
});
288283
window.patternkitEditor.on('change', saveSchema);
289-
$('[data-drupal-selector="edit-actions-submit"]').on('input', function (e) {
290-
saveSchema();
291-
});
292284
// Drupal triggers Ajax submit via input events.
293285
// This is before allowing other events, so we need to add a pre-hook
294286
// to trigger the editor update with latest field values.
295287
// @TODO Add handling for AJAX errors and re-attach.
296-
$('#edit-actions-submit').parent().submit(function (e) {
297-
e.preventDefault();
298-
window.patternkitEditor.disable();
299-
saveSchema();
300-
window.patternkitEditor.destroy();
301-
$(this).unbind('submit').submit();
288+
var parent_call = Drupal.Ajax.prototype.beforeSubmit;
289+
Drupal.Ajax.prototype.beforeSubmit = function (formValues, elementSettings, options) {
290+
debugger;
291+
if (window.patternkitEditor) {
292+
window.patternkitEditor.disable();
293+
saveSchema();
294+
var index = formValues.findIndex(function (o) { return o.name == "settings[instance_config]"; });
295+
formValues[index] = {
296+
name: "settings[instance_config]",
297+
value: JSON.stringify(window.patternkitEditor.getValue()),
298+
type: "hidden",
299+
required: false
300+
};
301+
window.patternkitEditor.destroy()
302+
}
303+
parent_call.call(this, formValues, elementSettings, options);
304+
}
305+
$('[data-drupal-selector="edit-actions-submit"]').parent('form').once().each(function () {
306+
$(this).submit(function (e) {
307+
e.preventDefault();
308+
window.patternkitEditor.disable();
309+
saveSchema();
310+
window.patternkitEditor.destroy();
311+
$(this).unbind('submit').submit();
312+
});
302313
});
303314
});
304315
}

0 commit comments

Comments
 (0)