-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
71 extend event #75
71 extend event #75
Changes from all commits
8358fde
14bf308
bd997d3
710b104
f8241fc
9eee1d7
2e37f10
c2c3146
ced3432
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,8 @@ | |
* @author Patrick Cuba <[email protected]> | ||
* @author Bryan Haberberger <[email protected]> | ||
* @version 0.7 | ||
|
||
|
||
* This code should serve as a basis for developers wishing to | ||
* use TinyThings as a RERUM proxy for an application for data entry, | ||
* especially within the Eventities model. | ||
|
@@ -31,18 +31,8 @@ async function renderChange(mutationsList) { | |
case DEER.LIST: | ||
let id = mutation.target.getAttribute(DEER.ID) | ||
if (id === "null" || mutation.target.getAttribute(DEER.COLLECTION)) return | ||
let obj = {} | ||
try { | ||
obj = JSON.parse(localStorage.getItem(id)) | ||
} catch (err) { } | ||
if (!obj || !obj["@id"]) { | ||
obj = await fetch(id).then(response => response.json()).catch(error => error) | ||
if (obj) { | ||
localStorage.setItem(obj["@id"] || obj.id, JSON.stringify(obj)) | ||
} else { | ||
return false | ||
} | ||
} | ||
let obj = await fetch(id).then(response => response.json()).catch(error => error) | ||
if (!obj) return false | ||
RENDER.element(mutation.target, obj) | ||
break | ||
case DEER.LISTENING: | ||
|
@@ -54,47 +44,62 @@ async function renderChange(mutationsList) { | |
}) | ||
} | ||
} | ||
if (mutation.type === 'childList') { | ||
RENDER.detectInsertions(elem) | ||
} | ||
} | ||
} | ||
|
||
const RENDER = {} | ||
RENDER.detectInsertions = elem => { | ||
let newViews = (elem.querySelectorAll(config.VIEW).length) ? elem.querySelectorAll(config.VIEW) : [] | ||
let newForms = (elem.querySelectorAll(config.FORM).length) ? elem.querySelectorAll(config.VIEW) : [] | ||
if (newForms.length) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. length might be more than is needed here, I forget why it was done. |
||
UTILS.broadcast(undefined, DEER.EVENTS.NEW_FORM, elem, { set: newForms }) | ||
} | ||
if (newViews.length) { | ||
UTILS.broadcast(undefined, DEER.EVENTS.NEW_VIEW, elem, { set: newViews }) | ||
} | ||
} | ||
RENDER.applyTemplate = (elem, obj, template) => { | ||
let options = { | ||
list: elem.getAttribute(DEER.LIST), | ||
link: elem.getAttribute(DEER.LINK), | ||
collection: elem.getAttribute(DEER.COLLECTION), | ||
key: elem.getAttribute(DEER.KEY), | ||
label: elem.getAttribute(DEER.LABEL), | ||
config: DEER | ||
} | ||
let templateResponse = template(obj, options) | ||
elem.innerHTML = (typeof templateResponse.html === "string") ? templateResponse.html : templateResponse | ||
//innerHTML may need a little time to finish to actually populate the template to the DOM, so do the timeout trick here. | ||
/** | ||
* A streamlined approach would treat each of these as a Promise-like node and the return of RENDER.element | ||
* would be a Promise. That way, something that looped and did may of these could do something like | ||
* Promise.all() before firing a completion/failure event (or something). | ||
*/ | ||
setTimeout(function () { | ||
UTILS.broadcast(undefined, DEER.EVENTS.VIEW_RENDERED, elem, obj) | ||
}, 0) | ||
|
||
if (typeof templateResponse.then === "function") { templateResponse.then(elem, obj, options) } | ||
//Note this is deprecated for the "deer-view-rendered" event. above. | ||
UTILS.broadcast(undefined, DEER.EVENTS.LOADED, elem, obj) | ||
} | ||
RENDER.element = function (elem, obj) { | ||
|
||
return UTILS.expand(obj).then(obj => { | ||
let tmplName = elem.getAttribute(DEER.TEMPLATE) || (elem.getAttribute(DEER.COLLECTION) ? "list" : "json") | ||
let template = DEER.TEMPLATES[tmplName] || DEER.TEMPLATES.json | ||
let options = { | ||
list: elem.getAttribute(DEER.LIST), | ||
link: elem.getAttribute(DEER.LINK), | ||
collection: elem.getAttribute(DEER.COLLECTION), | ||
key: elem.getAttribute(DEER.KEY), | ||
label: elem.getAttribute(DEER.LABEL), | ||
config: DEER | ||
UTILS.worker.addEventListener("message", event => { | ||
let templ = DEER.TEMPLATES[elem.getAttribute(DEER.TEMPLATE) || (elem.getAttribute(DEER.COLLECTION) ? "list" : "json")] | ||
if (event.data.action === "expanded" && event.data.id === elem.getAttribute(DEER.ID)) { | ||
RENDER.applyTemplate(elem, event.data.item, templ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe pass |
||
} | ||
let templateResponse = template(obj, options) | ||
elem.innerHTML = (typeof templateResponse.html === "string") ? templateResponse.html : templateResponse | ||
//innerHTML may need a little time to finish to actually populate the template to the DOM, so do the timeout trick here. | ||
/** | ||
* A streamlined approach would treat each of these as a Promise-like node and the return of RENDER.element | ||
* would be a Promise. That way, something that looped and did may of these could do something like | ||
* Promise.all() before firing a completion/failure event (or something). | ||
*/ | ||
setTimeout(function () { | ||
let newViews = (elem.querySelectorAll(config.VIEW).length) ? elem.querySelectorAll(config.VIEW) : [] | ||
let newForms = (elem.querySelectorAll(config.FORM).length) ? elem.querySelectorAll(config.VIEW) : [] | ||
if (newForms.length) { | ||
UTILS.broadcast(undefined, DEER.EVENTS.NEW_FORM, elem, { set: newForms }) | ||
} | ||
if (newViews.length) { | ||
UTILS.broadcast(undefined, DEER.EVENTS.NEW_VIEW, elem, { set: newViews }) | ||
} | ||
UTILS.broadcast(undefined, DEER.EVENTS.VIEW_RENDERED, elem, obj) | ||
}, 0) | ||
|
||
if (typeof templateResponse.then === "function") { templateResponse.then(elem, obj, options) } | ||
//Note this is deprecated for the "deer-view-rendered" event. above. | ||
UTILS.broadcast(undefined, DEER.EVENTS.LOADED, elem, obj) | ||
}) | ||
UTILS.postView(obj, undefined, { | ||
list: elem.getAttribute(DEER.LIST), | ||
link: elem.getAttribute(DEER.LINK), | ||
collection: elem.getAttribute(DEER.COLLECTION), | ||
key: elem.getAttribute(DEER.KEY), | ||
label: elem.getAttribute(DEER.LABEL), | ||
config: DEER | ||
}) | ||
} | ||
|
||
|
@@ -191,7 +196,7 @@ DEER.TEMPLATES.entity = function (obj, options = {}) { | |
let v = UTILS.getValue(value) | ||
if (typeof v === "object") { v = UTILS.getLabel(v) } | ||
if (v === "[ unlabeled ]") { v = v['@id'] || v.id || "[ complex value unknown ]" } | ||
list += (value['@id']) ? `<dd ${DEER.SOURCE}="${UTILS.getValue(value.source, "citationSource")}"><a href="${options.link || ""}#${value['@id']}">${v}</a></dd>` : `<dd ${DEER.SOURCE}="${UTILS.getValue(value, "citationSource")}">${v}</dd>` | ||
list += (value && value['@id']) ? `<dd ${DEER.SOURCE}="${UTILS.getValue(value.source, "citationSource")}"><a href="${options.link || ""}#${value['@id']}">${v}</a></dd>` : `<dd ${DEER.SOURCE}="${UTILS.getValue(value, "citationSource")}">${v}</dd>` | ||
} | ||
} | ||
} | ||
|
@@ -203,36 +208,18 @@ DEER.TEMPLATES.list = function (obj, options = {}) { | |
let tmpl = `<h2>${UTILS.getLabel(obj)}</h2>` | ||
if (options.list) { | ||
tmpl += `<ul>` | ||
obj[options.list].forEach((val, index) => { | ||
let name = UTILS.getLabel(val, (val.type || val['@type'] || index)) | ||
tmpl += (val["@id"] && options.link) ? `<li ${DEER.ID}="${val["@id"]}"><a href="${options.link}${val["@id"]}">${name}</a></li>` : `<li ${DEER.ID}="${val["@id"]}">${name}</li>` | ||
}) | ||
try { | ||
obj[options.list].forEach((val, index) => { | ||
let name = UTILS.getLabel(val, (val.type || val['@type'] || index)) | ||
tmpl += (val["@id"] && options.link) ? `<li ${DEER.ID}="${val["@id"]}"><a href="${options.link}${val["@id"]}">${name}</a></li>` : `<li ${DEER.ID}="${val["@id"]}">${name}</li>` | ||
}) | ||
} catch(meh) {} | ||
tmpl += `</ul>` | ||
} | ||
|
||
return tmpl | ||
} | ||
/** | ||
* The TEMPLATED renderer to draw JSON to the screen | ||
* @param {Object} obj some json of type Person to be drawn | ||
* @param {Object} options additional properties to draw with the Person | ||
*/ | ||
DEER.TEMPLATES.person = function (obj, options = {}) { | ||
try { | ||
let tmpl = `<h2>${UTILS.getLabel(obj)}</h2>` | ||
let dob = DEER.TEMPLATES.prop(obj, { key: "birthDate", label: "Birth Date" }) || `` | ||
let dod = DEER.TEMPLATES.prop(obj, { key: "deathDate", label: "Death Date" }) || `` | ||
let famName = (obj.familyName && UTILS.getValue(obj.familyName)) || "[ unknown ]" | ||
let givenName = (obj.givenName && UTILS.getValue(obj.givenName)) || "" | ||
tmpl += (obj.familyName || obj.givenName) ? `<div>Name: ${famName}, ${givenName}</div>` : `` | ||
tmpl += dob + dod | ||
tmpl += `<a href="#${obj["@id"]}">${name}</a>` | ||
return tmpl | ||
} catch (err) { | ||
return null | ||
} | ||
return null | ||
} | ||
|
||
/** | ||
* The TEMPLATED renderer to draw JSON to the screen | ||
* @param {Object} obj some json of type Event to be drawn | ||
|
@@ -245,7 +232,6 @@ DEER.TEMPLATES.event = function (obj, options = {}) { | |
} catch (err) { | ||
return null | ||
} | ||
return null | ||
} | ||
|
||
export default class DeerRender { | ||
|
@@ -357,4 +343,5 @@ export function initializeDeerViews(config) { | |
//Failed 5 times at 100 | ||
//Failed 0 times at 200 | ||
}) | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll put this back before merging. This should be our most recent release and the text version needs to change too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we did a fresh release and i was wondering if you wanted to up the version. We should do that today.