UNPKG

contacts-pane

Version:

Contacts Pane: Contacts manager for Address Book, Groups, and Individuals.

156 lines 7.22 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.autocompleteField = autocompleteField; /* Form field for doing autocompleete */ const rdflib_1 = require("rdflib"); const solid_logic_1 = require("solid-logic"); const solid_ui_1 = require("solid-ui"); const autocompletePicker_1 = require("./autocompletePicker"); // dbpediaParameters const AUTOCOMPLETE_THRESHOLD = 4; // don't check until this many characters typed const AUTOCOMPLETE_ROWS = 12; // 20? /** * Render a autocomplete form field * * The same function is used for many similar one-value fields, with different * regexps used to validate. * * @param dom The HTML Document object aka Document Object Model * @param container If present, the created widget will be appended to this * @param already A hash table of (form, subject) kept to prevent recursive forms looping * @param subject The thing about which the form displays/edits data * @param form The form or field to be rendered * @param doc The web document in which the data is * @param callbackFunction Called when data is changed? * * @returns The HTML widget created */ // eslint-disable-next-line complexity function autocompleteField(// @@ are they allowed too be async?? dom, container, already, subject, form, doc, callbackFunction) { function addOneIdAndRefresh(result, _name) { return __awaiter(this, void 0, void 0, function* () { const ds = kb.statementsMatching(subject, property); // remove any multiple values let is = ds.map(statement => (0, rdflib_1.st)(statement.subject, statement.predicate, result, statement.why)); // can include >1 doc if (is.length === 0) { // or none is = [(0, rdflib_1.st)(subject, property, result, doc)]; } try { yield kb.updater.updateMany(ds, is); } catch (err) { callbackFunction(false, err); box.appendChild(solid_ui_1.widgets.errorMessageBlock(dom, 'Autocomplete form data write error:' + err)); return; } callbackFunction(true, ''); }); } const kb = solid_logic_1.store; const formDoc = form.doc ? form.doc() : null; // @@ if blank no way to know const box = dom.createElement('tr'); if (container) container.appendChild(box); const lhs = dom.createElement('td'); lhs.setAttribute('class', 'formFieldName'); lhs.setAttribute('style', ' vertical-align: middle;'); box.appendChild(lhs); const rhs = dom.createElement('td'); rhs.setAttribute('class', 'formFieldValue'); box.appendChild(rhs); const property = kb.any(form, solid_ui_1.ns.ui('property')); if (!property) { box.appendChild(dom.createTextNode('Error: No property given for autocomplete field: ' + form)); return box; } const searchClass = kb.any(form, solid_ui_1.ns.ui('searchClass')); if (!searchClass) { box.appendChild(dom.createTextNode('Error: No searchClass given for autocomplete field: ' + form)); return box; } const endPoint = kb.any(form, solid_ui_1.ns.ui('endPoint')); if (!endPoint) { box.appendChild(dom.createTextNode('Error: No SPARQL endPoint given for autocomplete field: ' + form)); return box; } const queryTemplate = kb.any(form, solid_ui_1.ns.ui('queryTemplate')); if (!queryTemplate) { box.appendChild(dom.createTextNode('Error: No queryTemplate given for autocomplete field: ' + form)); return box; } // It can be cleaner to just remove empty fields if you can't edit them anyway const suppressEmptyUneditable = kb.anyJS(form, solid_ui_1.ns.ui('suppressEmptyUneditable'), null, formDoc); lhs.appendChild(solid_ui_1.widgets.fieldLabel(dom, property, form)); const uri = solid_ui_1.widgets.mostSpecificClassURI(form); let params = solid_ui_1.widgets.fieldParams[uri]; if (params === undefined) params = {}; // non-bottom field types can do this const theStyle = params.style || solid_ui_1.style.textInputStyle; const klass = kb.the(form, solid_ui_1.ns.ui('category'), null, formDoc); /* { label: string; logo: string; searchByNameQuery?: string; searchByNameURI?: string; insitituteDetailsQuery?: string; endPoint?: string; class: object } */ queryParams.endPoint = endPoint.uri; const searchByNameQuery = kb.the(form, solid_ui_1.ns.ui('searchByNameQuery'), null, formDoc); queryParams.searchByNameQuery = searchByNameQuery; var queryParams = { label: 'from form', logo: '', class: klass, endPoint, searchByNameQuery }; const options = { // acceptButton?: HTMLElement, class: klass, queryParams }; // const acWiget = rhs.appendChild(await renderAutoComplete(dom, options, addOneIdAndRefresh)) // @@ set existing value is any (0, autocompletePicker_1.renderAutoComplete)(dom, options, addOneIdAndRefresh).then(acWiget => rhs.appendChild(acWiget)); const field = dom.createElement('input'); field.style = solid_ui_1.style.textInputStyle; // Do we have to override length etc? rhs.appendChild(field); field.setAttribute('type', params.type ? params.type : 'text'); const size = kb.any(form, solid_ui_1.ns.ui('size')); // Form has precedence field.setAttribute('size', size ? '' + size : params.size ? '' + params.size : '20'); const maxLength = kb.any(form, solid_ui_1.ns.ui('maxLength')); field.setAttribute('maxLength', maxLength ? '' + maxLength : '4096'); doc = doc || solid_ui_1.widgets.fieldStore(subject, property, doc); let obj = kb.any(subject, property, undefined, doc); if (!obj) { obj = kb.any(form, solid_ui_1.ns.ui('default')); } if (obj) { /* istanbul ignore next */ field.value = obj.value || obj.value || ''; } field.setAttribute('style', solid_ui_1.style); if (!kb.updater) { throw new Error('kb has no updater'); } if (!kb.updater.editable(doc.uri)) { field.readOnly = true // was: disabled. readOnly is better ; field.style = solid_ui_1.style.textInputStyleUneditable; // backgroundColor = textInputBackgroundColorUneditable if (suppressEmptyUneditable && field.value === '') { box.style.display = 'none'; // clutter } return box; } return box; } // ends //# sourceMappingURL=autocompleteField.js.map