solid-ui 2.4.22-d939d9d0 → 2.4.22-e594bdfb

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,6 +22,12 @@ Object.defineProperty(exports, "field", {
22
22
  return _fieldFunction.field;
23
23
  }
24
24
  });
25
+ Object.defineProperty(exports, "fieldFunction", {
26
+ enumerable: true,
27
+ get: function get() {
28
+ return _fieldFunction.fieldFunction;
29
+ }
30
+ });
25
31
  Object.defineProperty(exports, "fieldLabel", {
26
32
  enumerable: true,
27
33
  get: function get() {
@@ -44,8 +50,15 @@ exports.findClosest = findClosest;
44
50
  exports.formsFor = formsFor;
45
51
  exports.makeDescription = makeDescription;
46
52
  exports.makeSelectForCategory = makeSelectForCategory;
53
+ exports.makeSelectForChoice = makeSelectForChoice;
47
54
  exports.makeSelectForNestedCategory = makeSelectForNestedCategory;
48
55
  exports.makeSelectForOptions = makeSelectForOptions;
56
+ Object.defineProperty(exports, "mostSpecificClassURI", {
57
+ enumerable: true,
58
+ get: function get() {
59
+ return _fieldFunction.mostSpecificClassURI;
60
+ }
61
+ });
49
62
  exports.newButton = newButton;
50
63
  exports.newThing = newThing;
51
64
  exports.promptForNew = promptForNew;
@@ -93,6 +106,8 @@ var _solidLogic = require("solid-logic");
93
106
 
94
107
  var utils = _interopRequireWildcard(require("../utils"));
95
108
 
109
+ var _multiSelect = require("./multiSelect");
110
+
96
111
  var widgets = _interopRequireWildcard(require("../widgets"));
97
112
 
98
113
  function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
@@ -155,6 +170,7 @@ _fieldFunction.field[ns.ui('Form').uri] = _fieldFunction.field[ns.ui('Group').ur
155
170
  var ui = ns.ui;
156
171
  if (container) container.appendChild(box); // Prevent loops
157
172
 
173
+ if (!form) return;
158
174
  var key = subject.toNT() + '|' + form.toNT();
159
175
 
160
176
  if (already[key]) {
@@ -264,47 +280,43 @@ _fieldFunction.field[ns.ui('Options').uri] = function (dom, container, already,
264
280
  values = kb.each(subject, dependingOn);
265
281
  }
266
282
 
267
- if (values.length === 0) {
268
- box.appendChild((0, _error.errorMessageBlock)(dom, "Can't select subform as no value of: " + dependingOn));
269
- } else {
270
- for (var i = 0; i < cases.length; i++) {
271
- var c = cases[i];
272
- var tests = kb.each(c, ui('for'), null, formDoc); // There can be multiple 'for'
283
+ for (var i = 0; i < cases.length; i++) {
284
+ var c = cases[i];
285
+ var tests = kb.each(c, ui('for'), null, formDoc); // There can be multiple 'for'
273
286
 
274
- var match = false;
287
+ var match = false;
275
288
 
276
- for (var j = 0; j < tests.length; j++) {
277
- var _iterator = _createForOfIteratorHelper(values),
278
- _step;
289
+ for (var j = 0; j < tests.length; j++) {
290
+ var _iterator = _createForOfIteratorHelper(values),
291
+ _step;
279
292
 
280
- try {
281
- for (_iterator.s(); !(_step = _iterator.n()).done;) {
282
- var value = _step.value;
283
- var test = tests[j];
293
+ try {
294
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
295
+ var value = _step.value;
296
+ var test = tests[j];
284
297
 
285
- if (value.sameTerm(tests) || value.termType === test.termType && value.value === test.value) {
286
- match = true;
287
- }
298
+ if (value.sameTerm(tests) || value.termType === test.termType && value.value === test.value) {
299
+ match = true;
288
300
  }
289
- } catch (err) {
290
- _iterator.e(err);
291
- } finally {
292
- _iterator.f();
293
301
  }
302
+ } catch (err) {
303
+ _iterator.e(err);
304
+ } finally {
305
+ _iterator.f();
294
306
  }
307
+ }
295
308
 
296
- if (match) {
297
- var _field3 = kb.the(c, ui('use'));
298
-
299
- if (!_field3) {
300
- box.appendChild((0, _error.errorMessageBlock)(dom, 'No "use" part for case in form ' + form));
301
- return box;
302
- } else {
303
- appendForm(dom, box, already, subject, _field3, dataDoc, callbackFunction);
304
- }
309
+ if (match) {
310
+ var _field3 = kb.the(c, ui('use'));
305
311
 
306
- break;
312
+ if (!_field3) {
313
+ box.appendChild((0, _error.errorMessageBlock)(dom, 'No "use" part for case in form ' + form));
314
+ return box;
315
+ } else {
316
+ appendForm(dom, box, already, subject, _field3, dataDoc, callbackFunction);
307
317
  }
318
+
319
+ break;
308
320
  }
309
321
  } // @@ Add box.refresh() to sync fields with values
310
322
 
@@ -1042,9 +1054,6 @@ _fieldFunction.field[ns.ui('Choice').uri] = function (dom, container, already, s
1042
1054
  var kb = _solidLogic.store;
1043
1055
  var formDoc = form.doc ? form.doc() : null; // @@ if blank no way to know
1044
1056
 
1045
- var uiMultipleInUse = false; // this signals to ui:choice that it is part of a ui:multiple
1046
-
1047
- var multiSelect = false;
1048
1057
  var p;
1049
1058
  var box = dom.createElement('div');
1050
1059
  box.setAttribute('class', 'choiceBox'); // Set flexDirection column?
@@ -1070,108 +1079,117 @@ _fieldFunction.field[ns.ui('Choice').uri] = function (dom, container, already, s
1070
1079
  }
1071
1080
 
1072
1081
  var subForm = kb.any(form, ui('use')); // Optional
1073
-
1074
- var follow = kb.anyJS(form, ui('follow'), null, formDoc); // data doc moves to new subject?
1075
-
1076
- var possible = [];
1077
- var possibleProperties;
1078
- var firstSelectOptionText = '* Select for ' + utils.label(subject, true) + ' *'; // if we do NOT have a container it means it is a ui:Multiple
1079
- // only important for the first option text in select
1080
-
1081
- if (!container) {
1082
- uiMultipleInUse = true;
1083
- firstSelectOptionText = utils.label(subject, true);
1084
- }
1085
-
1086
- if (subject.termType === 'BlankNode') {
1087
- firstSelectOptionText = '* Select for ' + utils.label(property) + ' *';
1088
- }
1082
+ // const follow = kb.anyJS(form, ui('follow'), null, formDoc) // data doc moves to new subject?
1089
1083
 
1090
1084
  var opts = {
1091
1085
  form: form,
1092
1086
  subForm: subForm,
1093
- multiSelect: multiSelect,
1094
- firstSelectOptionText: firstSelectOptionText,
1095
- uiMultipleInUse: uiMultipleInUse,
1096
1087
  disambiguate: false
1097
1088
  };
1098
- possible = kb.each(undefined, ns.rdf('type'), uiFrom, formDoc);
1099
1089
 
1100
- for (var x in kb.findMembersNT(uiFrom)) {
1101
- possible.push(kb.fromNT(x));
1102
- } // Use rdfs
1090
+ function getSelectorOptions() {
1091
+ var possible = [];
1092
+ var possibleProperties;
1093
+ possible = kb.each(undefined, ns.rdf('type'), uiFrom, formDoc);
1103
1094
 
1095
+ for (var x in kb.findMembersNT(uiFrom)) {
1096
+ possible.push(kb.fromNT(x));
1097
+ } // Use rdfs
1104
1098
 
1105
- if (uiFrom.sameTerm(ns.rdfs('Class'))) {
1106
- for (p in buttons.allClassURIs()) {
1107
- possible.push(kb.sym(p));
1108
- } // log.debug("%%% Choice field: possible.length 2 = "+possible.length)
1109
1099
 
1110
- } else if (uiFrom.sameTerm(ns.rdf('Property'))) {
1111
- possibleProperties = buttons.propertyTriage(kb);
1100
+ if (uiFrom.sameTerm(ns.rdfs('Class'))) {
1101
+ for (p in buttons.allClassURIs()) {
1102
+ possible.push(kb.sym(p));
1103
+ } // log.debug("%%% Choice field: possible.length 2 = "+possible.length)
1112
1104
 
1113
- for (p in possibleProperties.op) {
1114
- possible.push(kb.fromNT(p));
1115
- }
1105
+ } else if (uiFrom.sameTerm(ns.rdf('Property'))) {
1106
+ possibleProperties = buttons.propertyTriage(kb);
1116
1107
 
1117
- for (p in possibleProperties.dp) {
1118
- possible.push(kb.fromNT(p));
1119
- }
1108
+ for (p in possibleProperties.op) {
1109
+ possible.push(kb.fromNT(p));
1110
+ }
1120
1111
 
1121
- opts.disambiguate = true; // This is a big class, and the labels won't be enough.
1122
- } else if (uiFrom.sameTerm(ns.owl('ObjectProperty'))) {
1123
- possibleProperties = buttons.propertyTriage(kb);
1112
+ for (p in possibleProperties.dp) {
1113
+ possible.push(kb.fromNT(p));
1114
+ }
1124
1115
 
1125
- for (p in possibleProperties.op) {
1126
- possible.push(kb.fromNT(p));
1127
- }
1116
+ opts.disambiguate = true; // This is a big class, and the labels won't be enough.
1117
+ } else if (uiFrom.sameTerm(ns.owl('ObjectProperty'))) {
1118
+ possibleProperties = buttons.propertyTriage(kb);
1128
1119
 
1129
- opts.disambiguate = true;
1130
- } else if (uiFrom.sameTerm(ns.owl('DatatypeProperty'))) {
1131
- possibleProperties = buttons.propertyTriage(kb);
1120
+ for (p in possibleProperties.op) {
1121
+ possible.push(kb.fromNT(p));
1122
+ }
1123
+
1124
+ opts.disambiguate = true;
1125
+ } else if (uiFrom.sameTerm(ns.owl('DatatypeProperty'))) {
1126
+ possibleProperties = buttons.propertyTriage(kb);
1127
+
1128
+ for (p in possibleProperties.dp) {
1129
+ possible.push(kb.fromNT(p));
1130
+ }
1132
1131
 
1133
- for (p in possibleProperties.dp) {
1134
- possible.push(kb.fromNT(p));
1132
+ opts.disambiguate = true;
1135
1133
  }
1136
1134
 
1137
- opts.disambiguate = true;
1138
- }
1135
+ return possible; // return sortByLabel(possible)
1136
+ } // TODO: this checks for any occurrence, regardless of true or false setting
1139
1137
 
1140
- var sortedPossible = sortByLabel(possible); // TODO: this checks for any occurrence, regardless of true or false setting
1141
1138
 
1142
1139
  if (kb.any(form, ui('canMintNew'))) {
1143
1140
  opts.mint = '* Create new *'; // @@ could be better
1144
1141
  }
1145
1142
 
1146
- var selector = makeSelectForOptions(dom, kb, subject, property, sortedPossible, uiFrom, opts, dataDoc, callbackFunction);
1147
- rhs.appendChild(selector);
1148
- var object;
1143
+ var multiSelect = kb.any(form, ui('multiselect')); // Optional
1149
1144
 
1150
- if (selector.currentURI) {
1151
- object = $rdf.sym(selector.currentURI);
1152
- } else {
1153
- object = kb.any(subject, property);
1154
- }
1145
+ var selector;
1155
1146
 
1156
- if (object && subForm) {
1157
- removeNextSiblingsAfterElement(selector);
1158
- addSubFormChoice(dom, rhs, already, object, subForm, follow ? object.doc() : dataDoc, callbackFunction);
1159
- }
1147
+ rhs.refresh = function () {
1148
+ // from ui:property
1149
+ var selectedOptions = kb.each(subject, property, null, dataDoc).map(function (object) {
1150
+ return object.value;
1151
+ }); // from ui:from + ui:property
1160
1152
 
1161
- return box;
1162
- };
1163
- /**
1164
- * Removes all sibling elements after specified
1165
- * @param {HTMLElement} currentElement
1166
- * @private
1167
- */
1153
+ var possibleOptions = getSelectorOptions();
1154
+ possibleOptions.push(selectedOptions);
1155
+ possibleOptions = sortByLabel(possibleOptions);
1156
+ selector = makeSelectForChoice(dom, rhs, kb, subject, property, possibleOptions, selectedOptions, uiFrom, opts, dataDoc, callbackFunction);
1157
+ rhs.innerHTML = '';
1158
+ rhs.appendChild(selector);
1168
1159
 
1160
+ if (multiSelect) {
1161
+ var multiSelectDiv = new _multiSelect.IconicMultiSelect({
1162
+ placeholder: selector.selected,
1163
+ select: selector,
1164
+ container: rhs,
1165
+ textField: 'textField',
1166
+ valueField: 'valueField'
1167
+ });
1168
+ multiSelectDiv.init();
1169
+ multiSelectDiv.subscribe(function (event) {
1170
+ if (event.action === 'REMOVE_OPTION') {
1171
+ selectedOptions = selectedOptions.filter(function (value) {
1172
+ return value !== event.value;
1173
+ });
1174
+ }
1169
1175
 
1170
- function removeNextSiblingsAfterElement(currentElement) {
1171
- while (currentElement.nextElementSibling) {
1172
- currentElement.nextElementSibling.remove();
1173
- }
1174
- }
1176
+ if (event.action === 'CLEAR_ALL_OPTIONS') {
1177
+ selectedOptions = [];
1178
+ }
1179
+
1180
+ if (event.action === 'ADD_OPTION') {
1181
+ selectedOptions.push(event.value);
1182
+ }
1183
+
1184
+ selector.update(selectedOptions);
1185
+ });
1186
+ }
1187
+ };
1188
+
1189
+ rhs.refresh();
1190
+ if (selector && selector.refresh) selector.refresh();
1191
+ return box;
1192
+ };
1175
1193
 
1176
1194
  function addSubFormChoice(dom, selectDiv, already, subject, subForm, dataDoc, callbackFunction) {
1177
1195
  (0, _fieldFunction.fieldFunction)(dom, subForm)(dom, selectDiv, already, subject, subForm, dataDoc, callbackFunction);
@@ -1514,18 +1532,15 @@ function makeDescription(dom, kb, subject, predicate, dataDoc, callbackFunction)
1514
1532
  // @param subject - a term, the subject of the statement(s) being edited.
1515
1533
  // @param predicate - a term, the predicate of the statement(s) being edited
1516
1534
  // @param possible - a list of terms, the possible value the object can take
1517
- // @param options.multiSelect - Boolean - Whether more than one at a time is allowed
1518
- // @param options.firstSelectOptionText - a string to be displayed as the
1519
- // option for none selected (for non multiSelect)
1520
- // @param options.uiMultipleInUse - signals that the ui:choise is used with a ui:multiple
1521
- // @param options.mint - User may create thing if this sent to the prompt string eg "New foo"
1535
+ // @param options.nullLabel - a string to be displayed as the
1536
+ // option for none selected (for non multiple)
1522
1537
  // @param options.subForm - If mint, then the form to be used for minting the new thing
1523
1538
  // @param dataDoc - The web document being edited
1524
1539
  // @param callbackFunction - takes (boolean ok, string errorBody)
1525
1540
  */
1526
1541
 
1527
1542
 
1528
- function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, options, dataDoc, callbackFunction) {
1543
+ function makeSelectForOptions(dom, kb, subject, predicate, possible, options, dataDoc, callbackFunction) {
1529
1544
  log.debug('Select list length now ' + possible.length);
1530
1545
  var n = 0;
1531
1546
  var uris = {}; // Count them
@@ -1542,7 +1557,7 @@ function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, opt
1542
1557
  } // uris is now the set of possible options
1543
1558
 
1544
1559
 
1545
- if (n === 0 && !options.mint) {
1560
+ if (n === 0) {
1546
1561
  return (0, _error.errorMessageBlock)(dom, "Can't do selector with no options, subject= " + subject + ' property = ' + predicate + '.');
1547
1562
  }
1548
1563
 
@@ -1568,6 +1583,8 @@ function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, opt
1568
1583
  actual = getActual();
1569
1584
 
1570
1585
  var onChange = function onChange(_e) {
1586
+ select.disabled = true; // until data written back - gives user feedback too
1587
+
1571
1588
  var ds = [];
1572
1589
  var is = [];
1573
1590
 
@@ -1577,43 +1594,18 @@ function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, opt
1577
1594
  }
1578
1595
  };
1579
1596
 
1580
- var newObject;
1581
-
1582
1597
  for (var _i = 0; _i < select.options.length; _i++) {
1583
1598
  var opt = select.options[_i];
1584
-
1585
- if (opt.selected && opt.AJAR_mint) {
1586
- // not sure if this 'if' is used because I cannot find mintClass
1587
- if (options.mintClass) {
1588
- var thisForm = promptForNew(dom, kb, subject, predicate, options.mintClass, null, dataDoc, function (ok, body) {
1589
- if (!ok) {
1590
- callbackFunction(ok, body, {
1591
- change: 'new'
1592
- }); // @@ if ok, need some form of refresh of the select for the new thing
1593
- }
1594
- });
1595
- select.parentNode.appendChild(thisForm);
1596
- newObject = thisForm.AJAR_subject;
1597
- } else {
1598
- newObject = newThing(dataDoc);
1599
- }
1600
-
1601
- is.push($rdf.st(subject, predicate, kb.sym(newObject), dataDoc));
1602
- if (uiFrom) is.push($rdf.st(newObject, ns.rdf('type'), kb.sym(uiFrom), dataDoc)); // not sure if this 'if' is used because I cannot find mintStatementsFun
1603
-
1604
- if (options.mintStatementsFun) {
1605
- is = is.concat(options.mintStatementsFun(newObject));
1606
- }
1607
-
1608
- select.currentURI = newObject;
1609
- }
1610
-
1611
1599
  if (!opt.AJAR_uri) continue; // a prompt or mint
1612
1600
 
1613
1601
  if (opt.selected && !(opt.AJAR_uri in actual)) {
1614
1602
  // new class
1615
1603
  is.push($rdf.st(subject, predicate, kb.sym(opt.AJAR_uri), dataDoc));
1616
- select.currentURI = opt.AJAR_uri;
1604
+ }
1605
+
1606
+ if (!opt.selected && opt.AJAR_uri in actual) {
1607
+ // old class
1608
+ removeValue(kb.sym(opt.AJAR_uri));
1617
1609
  }
1618
1610
 
1619
1611
  if (opt.selected) select.currentURI = opt.AJAR_uri;
@@ -1633,29 +1625,25 @@ function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, opt
1633
1625
  sel = sel.superSelect;
1634
1626
  }
1635
1627
 
1636
- log.info('selectForOptions: data doc = ' + dataDoc); // refresh subForm
1628
+ log.info('selectForOptions: data doc = ' + dataDoc);
1629
+ kb.updater.update(ds, is, function (uri, ok, body) {
1630
+ actual = getActual(); // refresh
1637
1631
 
1638
- removeNextSiblingsAfterElement(select);
1639
- addSubFormChoice(dom, select.parentNode, {}, $rdf.sym(select.currentURI), options.subForm, dataDoc, function (ok, body) {
1640
1632
  if (ok) {
1641
- kb.updater.update(ds, is, function (uri, success, errorBody) {
1642
- actual = getActual(); // refresh
1643
-
1644
- if (!success) select.parentNode.appendChild((0, _error.errorMessageBlock)(dom, 'Error updating select: ' + errorBody));
1645
- });
1646
- if (callbackFunction) callbackFunction(ok, {
1647
- widget: 'select',
1648
- event: 'new'
1649
- });
1633
+ select.disabled = false; // data written back
1650
1634
  } else {
1651
- select.parentNode.appendChild((0, _error.errorMessageBlock)(dom, 'Error updating data in field of select: ' + body));
1635
+ return select.parentNode.appendChild((0, _error.errorMessageBlock)(dom, 'Error updating data in select: ' + body));
1652
1636
  }
1637
+
1638
+ if (callbackFunction) callbackFunction(ok, {
1639
+ widget: 'select',
1640
+ event: 'change'
1641
+ });
1653
1642
  });
1654
1643
  };
1655
1644
 
1656
1645
  var select = dom.createElement('select');
1657
1646
  select.setAttribute('style', style.formSelectSTyle);
1658
- if (options.multiSelect) select.setAttribute('multiSelect', 'true');
1659
1647
  select.currentURI = null;
1660
1648
 
1661
1649
  select.refresh = function () {
@@ -1698,22 +1686,11 @@ function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, opt
1698
1686
  select.appendChild(option);
1699
1687
  }
1700
1688
 
1701
- if (editable && options.mint) {
1702
- var mint = dom.createElement('option');
1703
- mint.appendChild(dom.createTextNode(options.mint));
1704
- mint.AJAR_mint = true; // Flag it
1705
-
1706
- select.insertBefore(mint, select.firstChild);
1707
- }
1708
-
1709
- if (!select.currentURI && options.uiMultipleInUse) {
1689
+ if (!select.currentURI) {
1710
1690
  var prompt = dom.createElement('option');
1711
- prompt.appendChild(dom.createTextNode(options.firstSelectOptionText));
1712
- prompt.disabled = true;
1713
- prompt.value = true;
1714
- prompt.hidden = true;
1715
- prompt.selected = true;
1691
+ prompt.appendChild(dom.createTextNode(options.nullLabel));
1716
1692
  select.insertBefore(prompt, select.firstChild);
1693
+ prompt.selected = true;
1717
1694
  }
1718
1695
 
1719
1696
  if (editable) {
@@ -1732,27 +1709,16 @@ function makeSelectForOptions(dom, kb, subject, predicate, possible, uiFrom, opt
1732
1709
  function makeSelectForCategory(dom, kb, subject, category, dataDoc, callbackFunction) {
1733
1710
  var du = kb.any(category, ns.owl('disjointUnionOf'));
1734
1711
  var subs;
1735
- var multiSelect = false;
1736
1712
 
1737
1713
  if (!du) {
1738
1714
  subs = kb.each(undefined, ns.rdfs('subClassOf'), category);
1739
- multiSelect = true;
1740
1715
  } else {
1741
1716
  subs = du.elements;
1742
1717
  }
1743
1718
 
1744
1719
  log.debug('Select list length ' + subs.length);
1745
-
1746
- if (subs.length === 0) {
1747
- return (0, _error.errorMessageBlock)(dom, "Can't do " + (multiSelect ? 'multiple ' : '') + 'selector with no subclasses of category: ' + category);
1748
- }
1749
-
1750
- if (subs.length === 1) {
1751
- return (0, _error.errorMessageBlock)(dom, "Can't do " + (multiSelect ? 'multiple ' : '') + 'selector with only 1 subclass of category: ' + category + ':' + subs[1]);
1752
- }
1753
-
1754
- return makeSelectForOptions(dom, kb, subject, ns.rdf('type'), subs, null, {
1755
- multiSelect: multiSelect
1720
+ return makeSelectForOptions(dom, kb, subject, ns.rdf('type'), subs, {
1721
+ nullLabel: '* Select type *'
1756
1722
  }, dataDoc, callbackFunction);
1757
1723
  }
1758
1724
  /** Make SELECT element to select subclasses recurively
@@ -1786,8 +1752,7 @@ function makeSelectForNestedCategory(dom, kb, subject, category, dataDoc, callba
1786
1752
  function onChange(ok, body) {
1787
1753
  if (ok) update();
1788
1754
  callbackFunction(ok, body);
1789
- } // eslint-disable-next-line prefer-const
1790
-
1755
+ }
1791
1756
 
1792
1757
  var select = makeSelectForCategory(dom, kb, subject, category, dataDoc, onChange);
1793
1758
  container.appendChild(select);
@@ -1942,4 +1907,262 @@ function newThing(doc) {
1942
1907
  var now = new Date();
1943
1908
  return $rdf.sym(doc.uri + '#' + 'id' + ('' + now.getTime()));
1944
1909
  }
1910
+ /** Make SELECT element to select options
1911
+ //
1912
+ // @param subject - a term, the subject of the statement(s) being edited.
1913
+ // @param predicate - a term, the predicate of the statement(s) being edited
1914
+ // @param possible - a list of terms, the possible value the object can take
1915
+ // @param options.mint - User may create thing if this sent to the prompt string eg "New foo"
1916
+ // @param options.subForm - If mint, then the form to be used for minting the new thing
1917
+ // @param dataDoc - The web document being edited
1918
+ // @param callbackFunction - takes (boolean ok, string errorBody)
1919
+ */
1920
+
1921
+
1922
+ function makeSelectForChoice(dom, container, kb, subject, predicate, inputPossibleOptions, selectedOptions, uiFrom, options, dataDoc, callbackFunction) {
1923
+ var optionsFromClassUIfrom = {}; // Count them
1924
+
1925
+ var editable = kb.updater.editable(dataDoc.uri);
1926
+
1927
+ for (var i = 0; i < inputPossibleOptions.length; i++) {
1928
+ var sub = inputPossibleOptions[i]; // @@ Maybe; make this so it works with blank nodes too
1929
+ // if (!sub.uri) debug.warn(`makeSelectForChoice: option does not have an uri: ${sub}, with predicate: ${predicate}`)
1930
+
1931
+ if (!sub.uri || sub.uri in optionsFromClassUIfrom) continue;
1932
+ optionsFromClassUIfrom[sub.uri] = true;
1933
+ }
1934
+
1935
+ var isEmpty = Object.keys(optionsFromClassUIfrom).length === 0;
1936
+
1937
+ if (isEmpty && !options.mint) {
1938
+ return (0, _error.errorMessageBlock)(dom, "Can't do selector with no options, subject= " + subject + ' property = ' + predicate + '.');
1939
+ }
1940
+
1941
+ log.debug('makeSelectForChoice: dataDoc=' + dataDoc);
1942
+
1943
+ function createDefaultSelectOptionText() {
1944
+ var firstSelectOptionText = '--- choice ---';
1945
+
1946
+ if (predicate && !(predicate.termType === 'BlankNode')) {
1947
+ firstSelectOptionText = '* Select for property: ' + utils.label(predicate) + ' *';
1948
+ }
1949
+
1950
+ if (subject && !(subject.termType === 'BlankNode')) {
1951
+ firstSelectOptionText = '* Select for ' + utils.label(subject, true) + ' *';
1952
+ }
1953
+
1954
+ return firstSelectOptionText;
1955
+ }
1956
+
1957
+ function createDefaultSelectOption() {
1958
+ var option = dom.createElement('option');
1959
+ option.appendChild(dom.createTextNode(createDefaultSelectOptionText()));
1960
+ option.disabled = true;
1961
+ option.value = true;
1962
+ option.hidden = true;
1963
+ option.selected = true;
1964
+ return option;
1965
+ }
1966
+
1967
+ var onChange = function onChange(_e) {
1968
+ container.removeChild(container.lastChild);
1969
+ select.refresh();
1970
+ };
1971
+
1972
+ var select = dom.createElement('select');
1973
+ select.setAttribute('style', style.formSelectSTyle);
1974
+ select.setAttribute('id', 'formSelect');
1975
+ select.currentURI = null;
1976
+
1977
+ for (var uri in optionsFromClassUIfrom) {
1978
+ select.appendChild(createOption(uri));
1979
+ }
1980
+
1981
+ if (editable && options.mint) {
1982
+ var mint = dom.createElement('option');
1983
+ mint.appendChild(dom.createTextNode(options.mint));
1984
+ mint.AJAR_mint = true; // Flag it
1985
+
1986
+ select.insertBefore(mint, select.firstChild);
1987
+ }
1988
+
1989
+ if (select.children.length === 0) select.insertBefore(createDefaultSelectOption(), select.firstChild);
1990
+
1991
+ select.update = function (newSelectedOptions) {
1992
+ selectedOptions = newSelectedOptions;
1993
+ var ds = [];
1994
+ var is = [];
1995
+
1996
+ var removeValue = function removeValue(t) {
1997
+ if (kb.holds(subject, predicate, t, dataDoc)) {
1998
+ ds.push($rdf.st(subject, predicate, t, dataDoc));
1999
+ }
2000
+ };
2001
+
2002
+ var addValue = function addValue(t) {
2003
+ if (!kb.holds(subject, predicate, t, dataDoc)) {
2004
+ is.push($rdf.st(subject, predicate, t, dataDoc)); // console.log("----value added " + t)
2005
+ }
2006
+
2007
+ if (uiFrom && !kb.holds(t, ns.rdf('type'), kb.sym(uiFrom), dataDoc)) {
2008
+ is.push($rdf.st(t, ns.rdf('type'), kb.sym(uiFrom), dataDoc)); // console.log("----added type to value " + uiFrom)
2009
+ }
2010
+ };
2011
+
2012
+ var existingValues = kb.each(subject, predicate, null, dataDoc).map(function (object) {
2013
+ return object.value;
2014
+ });
2015
+
2016
+ var _iterator2 = _createForOfIteratorHelper(existingValues),
2017
+ _step2;
2018
+
2019
+ try {
2020
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
2021
+ var value = _step2.value;
2022
+ if (!containsObject(value, selectedOptions)) removeValue($rdf.sym(value));
2023
+ }
2024
+ } catch (err) {
2025
+ _iterator2.e(err);
2026
+ } finally {
2027
+ _iterator2.f();
2028
+ }
2029
+
2030
+ var _iterator3 = _createForOfIteratorHelper(selectedOptions),
2031
+ _step3;
2032
+
2033
+ try {
2034
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
2035
+ var _value = _step3.value;
2036
+ if (!(_value in existingValues)) addValue($rdf.sym(_value));
2037
+ }
2038
+ } catch (err) {
2039
+ _iterator3.e(err);
2040
+ } finally {
2041
+ _iterator3.f();
2042
+ }
2043
+
2044
+ kb.updater.update(ds, is, function (uri, ok, body) {
2045
+ if (!ok) return select.parentNode.appendChild((0, _error.errorMessageBlock)(dom, 'Error updating data in select: ' + body));
2046
+ select.refresh();
2047
+ if (callbackFunction) callbackFunction(ok, {
2048
+ widget: 'select',
2049
+ event: 'change'
2050
+ });
2051
+ });
2052
+ };
2053
+
2054
+ select.refresh = function () {
2055
+ select.disabled = true; // unlocked any conflict we had got into
2056
+
2057
+ var is = [];
2058
+ var newObject;
2059
+
2060
+ for (var _i3 = 0; _i3 < select.options.length; _i3++) {
2061
+ var opt = select.options[_i3];
2062
+
2063
+ if (opt.selected && opt.AJAR_mint) {
2064
+ // not sure if this 'if' is used because I cannot find mintClass
2065
+ if (options.mintClass) {
2066
+ var thisForm = promptForNew(dom, kb, subject, predicate, options.mintClass, null, dataDoc, function (ok, body) {
2067
+ if (!ok) {
2068
+ callbackFunction(ok, body, {
2069
+ change: 'new'
2070
+ }); // @@ if ok, need some form of refresh of the select for the new thing
2071
+ }
2072
+ });
2073
+ select.parentNode.appendChild(thisForm);
2074
+ newObject = thisForm.AJAR_subject;
2075
+ } else {
2076
+ newObject = newThing(dataDoc);
2077
+ }
2078
+
2079
+ is.push($rdf.st(subject, predicate, kb.sym(newObject), dataDoc));
2080
+ if (uiFrom) is.push($rdf.st(newObject, ns.rdf('type'), kb.sym(uiFrom), dataDoc)); // not sure if this 'if' is used because I cannot find mintStatementsFun
2081
+
2082
+ if (options.mintStatementsFun) {
2083
+ is = is.concat(options.mintStatementsFun(newObject));
2084
+ }
2085
+
2086
+ select.currentURI = newObject;
2087
+ }
2088
+
2089
+ if (!opt.AJAR_uri) continue; // a prompt or mint
2090
+
2091
+ if (opt.selected && containsObject(opt.AJAR_uri, selectedOptions)) {
2092
+ select.currentURI = opt.AJAR_uri;
2093
+ }
2094
+
2095
+ if (!containsObject(opt.AJAR_uri, selectedOptions)) opt.removeAttribute('selected');
2096
+ if (containsObject(opt.AJAR_uri, selectedOptions)) opt.setAttribute('selected', 'true');
2097
+ }
2098
+
2099
+ log.info('selectForOptions: data doc = ' + dataDoc);
2100
+
2101
+ if (select.currentURI && options.subForm) {
2102
+ addSubFormChoice(dom, container, {}, $rdf.sym(select.currentURI), options.subForm, dataDoc, function (ok, body) {
2103
+ if (ok) {
2104
+ kb.updater.update([], is, function (uri, success, errorBody) {
2105
+ if (!success) container.appendChild((0, _error.errorMessageBlock)(dom, 'Error updating select: ' + errorBody));
2106
+ });
2107
+ if (callbackFunction) callbackFunction(ok, {
2108
+ widget: 'select',
2109
+ event: 'new'
2110
+ });
2111
+ } else {
2112
+ container.appendChild((0, _error.errorMessageBlock)(dom, 'Error updating data in field of select: ' + body));
2113
+ }
2114
+ });
2115
+ }
2116
+
2117
+ select.disabled = false;
2118
+ };
2119
+
2120
+ function createOption(uri) {
2121
+ var option = dom.createElement('option');
2122
+ var c = kb.sym(uri);
2123
+ var label;
2124
+
2125
+ if (options.disambiguate) {
2126
+ label = utils.labelWithOntology(c, true); // Init. cap
2127
+ } else {
2128
+ label = utils.label(c, true);
2129
+ }
2130
+
2131
+ option.appendChild(dom.createTextNode(label)); // Init.
2132
+
2133
+ option.setAttribute('value', uri);
2134
+ var backgroundColor = kb.any(c, kb.sym('http://www.w3.org/ns/ui#backgroundColor'));
2135
+
2136
+ if (backgroundColor) {
2137
+ option.setAttribute('style', 'background-color: ' + backgroundColor.value + '; ');
2138
+ }
2139
+
2140
+ option.AJAR_uri = uri;
2141
+
2142
+ if (containsObject(c.value, selectedOptions)) {
2143
+ option.setAttribute('selected', 'true');
2144
+ }
2145
+
2146
+ return option;
2147
+ }
2148
+
2149
+ if (editable) {
2150
+ select.addEventListener('change', onChange, false);
2151
+ }
2152
+
2153
+ return select;
2154
+ } // makeSelectForChoice
2155
+
2156
+
2157
+ function containsObject(obj, list) {
2158
+ var i;
2159
+
2160
+ for (i = 0; i < list.length; i++) {
2161
+ if (list[i] === obj) {
2162
+ return true;
2163
+ }
2164
+ }
2165
+
2166
+ return false;
2167
+ }
1945
2168
  //# sourceMappingURL=forms.js.map