Skip to content
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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 4 additions & 33 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,6 @@ <h4>Entity template</h4>
deer-id="<a href="http://devstore.rerum.io/v1/id/5c9d155fe4b0a44e13e61706">https://example.com/id/001</a>">
&lt;/deer-view></pre>

<h4>Person template</h4>
<p class="col-12">The person template attempts to render the object properties as a person's profile.</p>

<div class="card col-6">
<header>Person Template</header>
<div class="deer-view" deer-id="http://devstore.rerum.io/v1/id/5c9d155fe4b0a44e13e61706" deer-template="person"></div>
</div>

<pre class="col-6">&lt;deer-view
deer-template="person"
deer-id="<a href="http://devstore.rerum.io/v1/id/5c9d155fe4b0a44e13e61706">https://example.com/id/001</a>">
&lt;/deer-view></pre>

<h4>List template</h4>
<p class="col-12">The list template shows items connected in an aggregation. The same template can be used for static and dynamic lists as explained <a href="https://github.com/CenterForDigitalHumanities/deer/wiki/Using-DEER-in-your-project#rendering-a-list-or-collection"
target="_blank">
Expand Down Expand Up @@ -220,23 +207,6 @@ <h4>Listening to Events</h4>
</footer>
</div>

<pre class="col-6">&lt;deer-view
id="exampleListen"
deer-template="list"
deer-list="itemListElement"
deer-id="<a href="http://devstore.rerum.io/v1/id/5c9d15afe4b0a44e13e6170c">https://example.com/id/001</a>">
&lt;/deer-view>

&lt;deer-view
deer-template="person"
deer-listening="exampleListen">
&lt;/deer-view></pre>

<div class="card col-6">
<header>Listening with a Person Template</header>
<div class="deer-view" deer-listening="exampleListen" deer-template="person"></div>
</div>

<div class="card col-6">
<header>Listening with an Entity Template</header>
<div class="col deer-view" deer-listening="exampleListen" deer-template="entity"></div>
Expand Down Expand Up @@ -353,7 +323,8 @@ <h4>Simple Forms</h4>
&lt;input type="submit">
&lt;/form></pre>

<div class="card col-6">
<!-- Not now... this is not rendering as expected.
<div class="card col-6">
<header>Thing Form</header>
<form deer-type="Thing" deer-creator="Anonymous User" deer-item-type="simple" deer-context="http://schema.org" deer-id="http://devstore.rerum.io/v1/id/5cf19371e4b07d216aab775e" id="simpleFormExample">
<div class="row">
Expand Down Expand Up @@ -400,7 +371,7 @@ <h4>Simple Forms</h4>
<div class="col deer-view" deer-id="http://devstore.rerum.io/v1/id/5cf19371e4b07d216aab775e" deer-template="entity"></div>
<small><em>The <code>first</code> property is on the base object, so it remains, even though the
form does not edit or update the value.</em></small>
</div>
</div> -->
</section>

<footer class="is-full-width is-center" style="flex:1;flex-direction:column;">
Expand All @@ -414,7 +385,7 @@ <h4>Simple Forms</h4>
</footer>


<script src="./releases/alpha-.10/deer.js" type="module"></script>
<script src="./js/deer.js" type="module"></script>
Copy link
Member Author

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.

Copy link
Member

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.

<!-- <script src="./js/deer.js" type="module"></script> -->
<script>
window.onhashchange = onload = () => hashExample.setAttribute("deer-id", window.location.hash.substr(1))
Expand Down
2 changes: 1 addition & 1 deletion js/deer-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,5 @@ export default {
cat: (obj) => `<h5>${obj.name}</h5><img src="http://placekitten.com/300/150" style="width:100%;">`
},

version: "alpha 0.10"
version: "1.0.0"
}
260 changes: 40 additions & 220 deletions js/deer-record.js

Large diffs are not rendered by default.

139 changes: 63 additions & 76 deletions js/deer-render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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) {
Copy link
Member Author

Choose a reason for hiding this comment

The 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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe pass options in here so we aren't checking twice

}
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
})
}

Expand Down Expand Up @@ -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>`
}
}
}
Expand All @@ -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
Expand All @@ -245,7 +232,6 @@ DEER.TEMPLATES.event = function (obj, options = {}) {
} catch (err) {
return null
}
return null
}

export default class DeerRender {
Expand Down Expand Up @@ -357,4 +343,5 @@ export function initializeDeerViews(config) {
//Failed 5 times at 100
//Failed 0 times at 200
})
}
}

Loading