Skip to content

Commit 84cdd4b

Browse files
committed
move utils to folder and extract labels function to dedicated file
1 parent 10a09b5 commit 84cdd4b

6 files changed

Lines changed: 123 additions & 116 deletions

File tree

src/acl/access-controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { AccessGroups } from './access-groups'
99
import { DataBrowserContext } from 'pane-registry'
1010
import { shortNameForFolder } from './acl-control'
1111
import { currentUser } from '../authn/authn'
12-
import * as utils from '../utils.js'
12+
import * as utils from '../utils'
1313
import * as debug from '../debug'
1414
import ns from '../ns'
1515

src/acl/acl-control.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
import * as ns from '../ns'
9-
import * as utils from '../utils.js'
9+
import * as utils from '../utils'
1010
import { getACLorDefault, getProspectiveHolder } from './acl'
1111
import { IndexedFormula, NamedNode } from 'rdflib'
1212
import { DataBrowserContext } from 'pane-registry'

src/authn/authn.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import { PaneDefinition } from 'pane-registry'
2525
import { Signup } from './signup'
2626
import * as widgets from '../widgets'
2727
import * as ns from '../ns.js'
28-
import * as utils from '../utils.js'
28+
import * as utils from '../utils'
2929
import { alert } from '../log'
3030
import { AppDetails, AuthenticationContext } from './types'
3131
import * as debug from '../debug'

src/utils.js renamed to src/utils/index.js

Lines changed: 6 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33
//
44
// This must load AFTER the rdflib.js and log-ext.js (or log.js).
55
//
6-
import * as log from './log'
7-
import { store } from './logic'
8-
import * as ns from './ns'
6+
import * as log from '../log'
7+
import { store } from '../logic'
8+
import * as ns from '../ns'
99
import * as rdf from 'rdflib' // pull in first avoid cross-refs
10+
import { label } from './label'
1011

1112
const UI = { log, ns, rdf, store }
1213

@@ -366,8 +367,8 @@ function getEyeFocus (element, instantly, isBottom, myWindow) {
366367
myWindow.scrollBy(
367368
0,
368369
elementPosY +
369-
element.clientHeight -
370-
(myWindow.scrollY + myWindow.innerHeight)
370+
element.clientHeight -
371+
(myWindow.scrollY + myWindow.innerHeight)
371372
)
372373
return
373374
}
@@ -495,112 +496,6 @@ function labelWithOntology (x, initialCap) {
495496
return label(x, initialCap)
496497
}
497498

498-
// This ubiquitous function returns the best label for a thing
499-
//
500-
// The hacks in this code make a major difference to the usability
501-
//
502-
// @returns string
503-
//
504-
function label (x, initialCap) {
505-
// x is an object
506-
function doCap (s) {
507-
// s = s.toString()
508-
if (initialCap) return s.slice(0, 1).toUpperCase() + s.slice(1)
509-
return s
510-
}
511-
function cleanUp (s1) {
512-
let s2 = ''
513-
if (s1.slice(-1) === '/') s1 = s1.slice(0, -1) // chop trailing slash
514-
for (let i = 0; i < s1.length; i++) {
515-
if (s1[i] === '_' || s1[i] === '-') {
516-
s2 += ' '
517-
continue
518-
}
519-
s2 += s1[i]
520-
if (
521-
i + 1 < s1.length &&
522-
s1[i].toUpperCase() !== s1[i] &&
523-
s1[i + 1].toLowerCase() !== s1[i + 1]
524-
) {
525-
s2 += ' '
526-
}
527-
}
528-
if (s2.slice(0, 4) === 'has ') s2 = s2.slice(4)
529-
return doCap(s2)
530-
}
531-
532-
// Hard coded known label predicates
533-
// @@ TBD: Add subproperties of rdfs:label
534-
535-
const kb = UI.store
536-
const lab1 =
537-
kb.any(x, UI.ns.ui('label')) || // Prioritize ui:label
538-
kb.any(x, UI.ns.link('message')) ||
539-
kb.any(x, UI.ns.vcard('fn')) ||
540-
kb.any(x, UI.ns.foaf('name')) ||
541-
kb.any(x, UI.ns.dct('title')) ||
542-
kb.any(x, UI.ns.dc('title')) ||
543-
kb.any(x, UI.ns.rss('title')) ||
544-
kb.any(x, UI.ns.contact('fullName')) ||
545-
kb.any(x, kb.sym('http://www.w3.org/2001/04/roadmap/org#name')) ||
546-
kb.any(x, UI.ns.cal('summary')) ||
547-
kb.any(x, UI.ns.foaf('nick')) ||
548-
kb.any(x, UI.ns.rdfs('label'))
549-
550-
if (lab1) {
551-
return doCap(lab1.value)
552-
}
553-
554-
// Default to label just generated from the URI
555-
556-
if (x.termType === 'BlankNode') {
557-
return '...'
558-
}
559-
if (x.termType === 'Collection') {
560-
return '(' + x.elements.length + ')'
561-
}
562-
let s = x.uri
563-
if (typeof s === 'undefined') return x.toString() // can't be a symbol
564-
// s = decodeURI(s) // This can crash is random valid @ signs are presentation
565-
// The idea was to clean up eg URIs encoded in query strings
566-
// Also encoded character in what was filenames like @ [] {}
567-
try {
568-
s = s
569-
.split('/')
570-
.map(decodeURIComponent)
571-
.join('/') // If it is properly encoded
572-
} catch (e) {
573-
// try individual decoding of ASCII code points
574-
for (let i = s.length - 3; i > 0; i--) {
575-
const hex = '0123456789abcefABCDEF' // The while upacks multiple layers of encoding
576-
while (
577-
s[i] === '%' &&
578-
hex.indexOf(s[i + 1]) >= 0 &&
579-
hex.indexOf(s[i + 2]) >= 0
580-
) {
581-
s =
582-
s.slice(0, i) +
583-
String.fromCharCode(parseInt(s.slice(i + 1, i + 3), 16)) +
584-
s.slice(i + 3)
585-
}
586-
}
587-
}
588-
if (s.slice(-5) === '#this') s = s.slice(0, -5)
589-
else if (s.slice(-3) === '#me') s = s.slice(0, -3)
590-
591-
const hash = s.indexOf('#')
592-
if (hash >= 0) return cleanUp(s.slice(hash + 1))
593-
594-
if (s.slice(-9) === '/foaf.rdf') s = s.slice(0, -9)
595-
else if (s.slice(-5) === '/foaf') s = s.slice(0, -5)
596-
597-
// Eh? Why not do this? e.g. dc:title needs it only trim URIs, not rdfs:labels
598-
const slash = s.lastIndexOf('/', s.length - 2) // (len-2) excludes trailing slash
599-
if (slash >= 0 && slash < x.uri.length) return cleanUp(s.slice(slash + 1))
600-
601-
return doCap(decodeURIComponent(x.uri))
602-
}
603-
604499
function escapeForXML (str) {
605500
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;')
606501
}

src/utils/label.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import * as log from '../log'
2+
import { store } from '../logic'
3+
import * as ns from '../ns'
4+
import * as rdf from 'rdflib' // pull in first avoid cross-refs
5+
6+
const UI = { log, ns, rdf, store }
7+
8+
// This ubiquitous function returns the best label for a thing
9+
//
10+
// The hacks in this code make a major difference to the usability
11+
//
12+
// @returns string
13+
//
14+
export function label (x, initialCap) {
15+
// x is an object
16+
function doCap (s) {
17+
// s = s.toString()
18+
if (initialCap) return s.slice(0, 1).toUpperCase() + s.slice(1)
19+
return s
20+
}
21+
function cleanUp (s1) {
22+
let s2 = ''
23+
if (s1.slice(-1) === '/') s1 = s1.slice(0, -1) // chop trailing slash
24+
for (let i = 0; i < s1.length; i++) {
25+
if (s1[i] === '_' || s1[i] === '-') {
26+
s2 += ' '
27+
continue
28+
}
29+
s2 += s1[i]
30+
if (
31+
i + 1 < s1.length &&
32+
s1[i].toUpperCase() !== s1[i] &&
33+
s1[i + 1].toLowerCase() !== s1[i + 1]
34+
) {
35+
s2 += ' '
36+
}
37+
}
38+
if (s2.slice(0, 4) === 'has ') s2 = s2.slice(4)
39+
return doCap(s2)
40+
}
41+
42+
// Hard coded known label predicates
43+
// @@ TBD: Add subproperties of rdfs:label
44+
45+
const kb = UI.store
46+
const lab1 =
47+
kb.any(x, UI.ns.ui('label')) || // Prioritize ui:label
48+
kb.any(x, UI.ns.link('message')) ||
49+
kb.any(x, UI.ns.vcard('fn')) ||
50+
kb.any(x, UI.ns.foaf('name')) ||
51+
kb.any(x, UI.ns.dct('title')) ||
52+
kb.any(x, UI.ns.dc('title')) ||
53+
kb.any(x, UI.ns.rss('title')) ||
54+
kb.any(x, UI.ns.contact('fullName')) ||
55+
kb.any(x, kb.sym('http://www.w3.org/2001/04/roadmap/org#name')) ||
56+
kb.any(x, UI.ns.cal('summary')) ||
57+
kb.any(x, UI.ns.foaf('nick')) ||
58+
kb.any(x, UI.ns.rdfs('label'))
59+
60+
if (lab1) {
61+
return doCap(lab1.value)
62+
}
63+
64+
// Default to label just generated from the URI
65+
66+
if (x.termType === 'BlankNode') {
67+
return '...'
68+
}
69+
if (x.termType === 'Collection') {
70+
return '(' + x.elements.length + ')'
71+
}
72+
let s = x.uri
73+
if (typeof s === 'undefined') return x.toString() // can't be a symbol
74+
// s = decodeURI(s) // This can crash is random valid @ signs are presentation
75+
// The idea was to clean up eg URIs encoded in query strings
76+
// Also encoded character in what was filenames like @ [] {}
77+
try {
78+
s = s
79+
.split('/')
80+
.map(decodeURIComponent)
81+
.join('/') // If it is properly encoded
82+
} catch (e) {
83+
// try individual decoding of ASCII code points
84+
for (let i = s.length - 3; i > 0; i--) {
85+
const hex = '0123456789abcefABCDEF' // The while upacks multiple layers of encoding
86+
while (
87+
s[i] === '%' &&
88+
hex.indexOf(s[i + 1]) >= 0 &&
89+
hex.indexOf(s[i + 2]) >= 0
90+
) {
91+
s =
92+
s.slice(0, i) +
93+
String.fromCharCode(parseInt(s.slice(i + 1, i + 3), 16)) +
94+
s.slice(i + 3)
95+
}
96+
}
97+
}
98+
if (s.slice(-5) === '#this') s = s.slice(0, -5)
99+
else if (s.slice(-3) === '#me') s = s.slice(0, -3)
100+
101+
const hash = s.indexOf('#')
102+
if (hash >= 0) return cleanUp(s.slice(hash + 1))
103+
104+
if (s.slice(-9) === '/foaf.rdf') s = s.slice(0, -9)
105+
else if (s.slice(-5) === '/foaf') s = s.slice(0, -5)
106+
107+
// Eh? Why not do this? e.g. dc:title needs it only trim URIs, not rdfs:labels
108+
const slash = s.lastIndexOf('/', s.length - 2) // (len-2) excludes trailing slash
109+
if (slash >= 0 && slash < x.uri.length) return cleanUp(s.slice(slash + 1))
110+
111+
return doCap(decodeURIComponent(x.uri))
112+
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { silenceDebugMessages } from '../helpers/setup'
1+
import { silenceDebugMessages } from '../../helpers/setup'
22
import { JSDOM } from 'jsdom'
33
import {
44
addLoadEvent, // not used anywhere
@@ -29,7 +29,7 @@ import {
2929
stackString,
3030
syncTableToArray,
3131
syncTableToArrayReOrdered
32-
} from '../../src/utils'
32+
} from '../../../src/utils'
3333
import { sym } from 'rdflib'
3434

3535
silenceDebugMessages()

0 commit comments

Comments
 (0)