Skip to content

Commit 2f3758e

Browse files
author
Jason Smith
committed
Fix the issue in jsoneditor
1 parent 42c4810 commit 2f3758e

File tree

2 files changed

+77
-11
lines changed

2 files changed

+77
-11
lines changed

src/PatternkitDrupalCachedLib.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ public function getCachedMetadata($subtype = NULL, $reset = FALSE) {
6565
// If we are requesting data for a specific module type, return just
6666
// that data.
6767
if ($subtype !== NULL && strtolower($subtype) !== 'none') {
68-
$lookup = substr($subtype, 3);
69-
if (!empty($cached_metadata[strtolower($lookup)])) {
70-
return $cached_metadata[strtolower($lookup)];
68+
if (substr($subtype, 0, 3) == 'pk_') {
69+
$subtype = substr($subtype, 3);
70+
}
71+
if (!empty($cached_metadata[strtolower($subtype)])) {
72+
return $cached_metadata[strtolower($subtype)];
7173
}
7274
_patternkit_show_error(
7375
'Patternkit module does not appear to exist (%module), verify module info/usage.',
74-
array('%module' => $lookup)
76+
array('%module' => $subtype)
7577
);
7678

7779
return NULL;

src/PatternkitDrupalTwigLib.php

Lines changed: 71 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,17 @@ public function getEditor($subtype = NULL,
8181
return t('Unable to lookup the schema for this subtype.');
8282
}
8383

84+
$hostname = $_SERVER['HTTP_HOST'];
85+
8486
$schema_json = drupal_json_encode($pattern->schema);
8587
$starting_json = $config !== NULL ? drupal_json_encode($config->fields)
8688
: $config;
8789
// @todo Move to own JS file & Drupal Settings config var.
8890
$markup = <<<HTML
8991
<div id="magic-pixie-dust"></div>
9092
<script type="text/javascript">
91-
let target = document.getElementById("magic-pixie-dust");
92-
let shadow = target.attachShadow({mode: 'open'});
93+
var target = document.getElementById("magic-pixie-dust");
94+
var shadow = target.attachShadow({mode: 'open'});
9395
9496
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>';
9597
@@ -105,14 +107,79 @@ public function getEditor($subtype = NULL,
105107
JSONEditor.defaults.options.startval = data.starting;
106108
}
107109
110+
// Override how references are resolved.
111+
JSONEditor.base_url = '//$hostname/'
112+
JSONEditor.prototype._loadExternalRefs = function(schema, callback) {
113+
var self = this;
114+
var refs = this._getExternalRefs(schema);
115+
116+
var done = 0, waiting = 0, callback_fired = false;
117+
118+
$each(refs,function(url) {
119+
if(self.refs[url]) return;
120+
if(!self.options.ajax) throw "Must set ajax option to true to load external ref "+url;
121+
self.refs[url] = 'loading';
122+
waiting++;
123+
124+
var r = new XMLHttpRequest();
125+
126+
var replacement = this.base_url + 'patternkit/ajax/webrh/$1/schema$2'
127+
var uri = url.replace(/(\w+)\.json(#.*)/, replacement);
128+
129+
r.open("GET", uri, true);
130+
r.onreadystatechange = function () {
131+
if (r.readyState != 4) return;
132+
// Request succeeded
133+
if(r.status === 200) {
134+
var response;
135+
try {
136+
response = JSON.parse(r.responseText);
137+
}
138+
catch(e) {
139+
window.console.log(e);
140+
throw "Failed to parse external ref "+url;
141+
}
142+
if(!response || typeof response !== "object") throw "External ref does not contain a valid schema - "+url;
143+
144+
self.refs[url] = response;
145+
self._loadExternalRefs(response,function() {
146+
done++;
147+
if(done >= waiting && !callback_fired) {
148+
callback_fired = true;
149+
callback();
150+
}
151+
});
152+
}
153+
// Request failed
154+
else {
155+
window.console.log(r);
156+
throw "Failed to fetch ref via ajax- "+url;
157+
}
158+
};
159+
r.send();
160+
});
161+
162+
if(!waiting) {
163+
callback();
164+
}
165+
}
166+
167+
108168
// Initialize the editor with a JSON schema
109169
var editor = new JSONEditor(
110170
target.shadowRoot.getElementById('editor_holder'), {
111171
schema: data.schema,
112172
theme: 'bootstrap3',
113173
iconlib: 'fontawesome4',
114174
keep_oneof_values: false,
115-
ajax: true
175+
disable_edit_json: true,
176+
disable_collapse: true,
177+
//disable_properties: true,
178+
//no_additional_properties: true,
179+
ajax: true,
180+
refs: {
181+
"config.json": "/sites/all/modules/custom/webrh/webrh/src/library/atoms/config/api/config.json"
182+
}
116183
}
117184
);
118185
JSONEditor.plugins.sceditor.emoticonsEnabled = false;
@@ -129,9 +196,6 @@ public function getEditor($subtype = NULL,
129196
'#type' => 'markup',
130197
'#markup' => $markup,
131198
'#attached' => array(
132-
'library' => array(
133-
array('system', 'ui'),
134-
),
135199
'js' => array(
136200
drupal_get_path('module', 'patternkit') . '/js/jsoneditor.js',
137201
),
@@ -180,7 +244,7 @@ protected function getRawMetadata() {
180244
$file_basename = $file->getBasename('.' . $file_ext);
181245

182246
// Build an array of all the filenames of interest, keyed by name.
183-
$components[$file_basename][$file_ext] = $file_path;
247+
$components[$file_basename][$file_ext] = "$file_path/$file_basename.$file_ext";
184248
}
185249

186250
foreach ($components as $module_name => $data) {

0 commit comments

Comments
 (0)