JSONeditorAsView = {
return function (p) {
let {mode,dataView,initialValue,expandall} = p?p:{mode:'tree',initialValue:{}};
let _place = html`<div id="myPlace01"/>`;
let __value = { value: initialValue, selection: [] };
let __list =[];
let __editor = new jsoneditor(_place,{
mode:mode?mode:'tree',
onChange:function() {
let newVal = { value: __editor.get(), selection: __editor.getSelection() };
console.log("onChange", newVal);
__value=newVal;
_place.dispatchEvent({type: "input", value:newVal})
if (expandall) __editor.expandAll();
},
onSelectionChange:function(start,end) {
let newVal = { value: __value.value, selection: __editor.getSelection() };
console.log("onSelectionChange", newVal);
__value=newVal;
_place.dispatchEvent({type: "input", value:newVal})
if (expandall) __editor.expandAll();
}
},initialValue);
if (expandall) __editor.expandAll();
Object.defineProperty(_place, 'value' , {get: function() {return __value;},
set: function(value)
{
const nos = JSON.stringify(value);
const cos = JSON.stringify(__value);
if (cos==nos)
return;
let no = JSON.parse(nos);
__editor.set(no);
__value=no;
_place.dispatchEvent({type: "input",value:no})
}
}
);
_place.addEventListener = function(type, listener) {
if (type != "input" || __list.includes(listener)) return;
__list = [listener].concat(__list);
}
_place.removeEventListener = function(type, listener) {
if (type != "input") return;
__list = __list.filter(l => l !== listener);
}
_place.dispatchEvent = function(event) {
console.log("place dispatch event");
const p = Promise.resolve(event);
__list.forEach(l => p.then(l));
};
if (dataView)
bindVisualView2DataView(_place,dataView);
return _place;
}
}