|
| 1 | +/** |
| 2 | + * @file |
| 3 | + * Provides Twig Pattern Library Editing Functionality. |
| 4 | + * |
| 5 | + * @external Drupal |
| 6 | + * |
| 7 | + * @external jQuery |
| 8 | + * |
| 9 | + * @external JSONEditor |
| 10 | + */ |
| 11 | + |
| 12 | +(function ($, Drupal) { |
| 13 | + Drupal.behaviors.patternkitEditor = { |
| 14 | + attach: function (context, settings) { |
| 15 | + var $target = $('#editor-shadow-injection-target', context); |
| 16 | + $target.once('patternkit-editor').each(function () { |
| 17 | + var shadow = this.attachShadow({mode: 'open'}); |
| 18 | + shadow.innerHTML = '<link rel="stylesheet" id="theme_stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.3/css/bootstrap.min.css"><link rel="stylesheet" id="icon_stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/font-awesome/4.0.3/css/font-awesome.css"><div id="editor_holder"></div>'; |
| 19 | + |
| 20 | + var data = {}; |
| 21 | + data.schema = JSON.parse(drupalSettings.patternkitEditor.schemaJson); |
| 22 | + data.starting = JSON.parse(drupalSettings.patternkitEditor.startingJson); |
| 23 | + |
| 24 | + if (data.starting !== null) { |
| 25 | + JSONEditor.defaults.options.startval = data.starting; |
| 26 | + } |
| 27 | + |
| 28 | + // Override how references are resolved. |
| 29 | + JSONEditor.prototype._loadExternalRefs = function (schema, callback) { |
| 30 | + var self = this; |
| 31 | + var refs = this._getExternalRefs(schema); |
| 32 | + |
| 33 | + var done = 0, waiting = 0, callback_fired = false; |
| 34 | + |
| 35 | + $.each(refs, function (url) { |
| 36 | + if (self.refs[url]) { |
| 37 | + return; |
| 38 | + } |
| 39 | + if (!self.options.ajax) { |
| 40 | + throw "Must set ajax option to true to load external ref " + url; |
| 41 | + } |
| 42 | + self.refs[url] = 'loading'; |
| 43 | + waiting++; |
| 44 | + |
| 45 | + var r = new XMLHttpRequest(); |
| 46 | + var uri = drupalSettings.path.baseUrl + url + '/schema'; |
| 47 | + |
| 48 | + r.open("GET", uri, true); |
| 49 | + r.onreadystatechange = function () { |
| 50 | + if (r.readyState !== 4) { |
| 51 | + return; |
| 52 | + } |
| 53 | + // Request succeeded. |
| 54 | + if (r.status === 200) { |
| 55 | + var response; |
| 56 | + try { |
| 57 | + response = JSON.parse(r.responseText); |
| 58 | + } |
| 59 | + catch (e) { |
| 60 | + window.console.log(e); |
| 61 | + throw "Failed to parse external ref " + url; |
| 62 | + } |
| 63 | + if (!response || typeof response !== "object") { |
| 64 | + throw "External ref does not contain a valid schema - " + url; |
| 65 | + } |
| 66 | + |
| 67 | + self.refs[url] = response; |
| 68 | + self._loadExternalRefs(response,function () { |
| 69 | + done++; |
| 70 | + if (done >= waiting && !callback_fired) { |
| 71 | + callback_fired = true; |
| 72 | + callback(); |
| 73 | + } |
| 74 | + }); |
| 75 | + } |
| 76 | + // Request failed. |
| 77 | + else { |
| 78 | + window.console.log(r); |
| 79 | + throw "Failed to fetch ref via ajax- " + url; |
| 80 | + } |
| 81 | + }; |
| 82 | + r.send(); |
| 83 | + }); |
| 84 | + |
| 85 | + if (!waiting) { |
| 86 | + callback(); |
| 87 | + } |
| 88 | + }; |
| 89 | + |
| 90 | + // Initialize the editor with a JSON schema. |
| 91 | + var editor = new JSONEditor( |
| 92 | + $target[0].shadowRoot.getElementById('editor_holder'), { |
| 93 | + schema: data.schema, |
| 94 | + theme: 'bootstrap3', |
| 95 | + iconlib: 'fontawesome4', |
| 96 | + keep_oneof_values: false, |
| 97 | + disable_edit_json: true, |
| 98 | + disable_collapse: true, |
| 99 | + ajax: true, |
| 100 | + refs: { } |
| 101 | + } |
| 102 | + ); |
| 103 | + JSONEditor.plugins.sceditor.emoticonsEnabled = false; |
| 104 | + |
| 105 | + editor.on('change', function () { |
| 106 | + document.getElementById('schema_instance_config').value = JSON.stringify(editor.getValue()); |
| 107 | + |
| 108 | + }); |
| 109 | + }); |
| 110 | + } |
| 111 | + }; |
| 112 | +})(jQuery, Drupal); |
0 commit comments