solid-ui 2.4.22-d2646734 → 2.4.22-dc79c0e3

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