Files
@ 17b268d2b7f3
Branch filter:
Location: light9/light9/web/resource-display.html
17b268d2b7f3
4.5 KiB
text/html
ws fix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | <link rel="import" href="/lib/polymer/polymer-element.html">
<link rel="import" href="/lib/paper-dialog/paper-dialog.html">
<link rel="import" href="/lib/paper-button/paper-button.html">
<dom-module id="resource-display">
<template>
<style>
:host {
display: inline-block;
}
a.resource {
color: inherit;
text-decoration: none;
}
.resource {
border: 1px solid #545454;
border-radius: 5px;
padding: 1px;
margin: 2px;
background: rgb(49, 49, 49);
display: inline-block;
text-shadow: 1px 1px 2px black;
}
.resource.minor {
background: none;
border: none;
}
.resource a {
color: rgb(150, 150, 255);
padding: 1px;
display: inline-block;
}
.resource.minor a {
text-decoration: none;
color: rgb(155, 155, 193);
padding: 0;
}
</style>
<span class$="[[resClasses]]">
<a href="{{href}}" id="uri">
<!-- type icon goes here -->{{label}}</a>
</span>
<template is="dom-if" if="{{rename}}">
<button on-click="onRename">Rename</button>
<paper-dialog id="renameDialog" modal
on-iron-overlay-closed="onRenameClosed">
<p>
New label:
<input id="renameTo" autofocus type="text"
value="{{renameTo::input}}"
on-keydown="onRenameKey">
</p>
<div class="buttons">
<paper-button dialog-dismiss>Cancel</paper-button>
<paper-button dialog-confirm>OK</paper-button>
</div>
</paper-dialog>
</template>
</template>
<script>
class ResourceDisplay extends Polymer.Element {
static get is() { return "resource-display"; }
static get properties() {
return {
graph: { type: Object },
// callers might set this as string or NamedNode.
uri: { type: Object }, // Use .value for the string
href: { type: String },
label: { type: String },
rename: { type: Boolean },
minor: { type: Boolean },
resClasses: { type: String, computed: '_resClasses(minor)', value: 'resource' },
renameTo: { type: String, notify: true },
};
}
static get observers() { return ['onUri(graph, uri)']; }
_resClasses(minor) {
return minor ? 'resource minor' : 'resource';
}
onUri(graph, uri) {
if (!this.graph) {
this.label = "...";
this.href = "javascript:;'";
return;
}
if (!this.uri) {
this.setLabel();
return;
}
if (typeof uri === 'string') {
uri = this.graph.Uri(uri);
}
this.graph.runHandler(this.setLabel.bind(this),
`label ${uri.value}`);
}
setLabel(patch) {
if (!this.uri) {
this.label = "<no uri>";
this.href = "javascript:;";
return;
}
if (patch !== null &&
!SyncedGraph.patchContainsPreds(patch,
[this.graph.Uri('rdfs:label')])) {
return;
}
let uri = this.uri;
if (typeof uri === 'string') {
uri = this.graph.Uri(uri);
}
this.label = this.graph.labelOrTail(uri);
this.href = uri.value;
}
onRename() {
this.renameTo = this.label;
this.shadowRoot.querySelector("#renameDialog").open();
this.shadowRoot.querySelector("#renameTo").setSelectionRange(0, -1);
}
onRenameKey(ev) {
if (ev.key == 'Enter') {
this.shadowRoot.querySelector("[dialog-confirm]").click();
}
if (ev.key == 'Escape') {
this.shadowRoot.querySelector("[dialog-dismiss]").click();
}
}
onRenameClosed() {
var dialog = this.shadowRoot.querySelector("#renameDialog");
if (dialog.closingReason.confirmed) {
var label = this.graph.Uri('rdfs:label');
var ctxs = this.graph.contextsWithPattern(this.uri, label, null);
if (ctxs.length != 1) {
throw new Error(
`${ctxs.length} label stmts for ${this.uri.label}`);
}
this.graph.patchObject(
((typeof this.uri) === 'string' ?
this.graph.Uri(this.uri) : this.uri),
label,
this.graph.Literal(this.renameTo),
ctxs[0]);
}
}
}
customElements.define(ResourceDisplay.is, ResourceDisplay);
</script>
</dom-module>
|