lincd
Version:
LINCD is a JavaScript library for building user interfaces with linked data (also known as 'structured data', or RDF)
102 lines • 4.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ShapeValuesSet = void 0;
const ShapeSet_js_1 = require("./ShapeSet.js");
const ShapeClass_js_1 = require("../utils/ShapeClass.js");
class ShapeValuesSet extends ShapeSet_js_1.ShapeSet {
constructor(subject, property, shapeClass, allowSubShapes = false) {
//we have to construct the set empty because the native Set constructor will call 'this.add()', which wont work since subject & property are not set yet
super();
this.subject = subject;
this.property = property;
//get all the property values nodes and add each of them to this set as instances of the given shape
subject.getAll(property).forEach((object) => {
if (allowSubShapes) {
super.add((0, ShapeClass_js_1.getShapeOrSubShape)(object, shapeClass));
}
else {
super.add(new shapeClass(object));
}
});
//TODO: review, turned off for now due to memory leak
//listen for changes in the property set
// subject.onChange(property, (quads) => {
// quads.forEach((q) => {
// if (q.isRemoved) {
// this.some((shape) => {
// if (shape.node === q.object) {
// return super.delete(shape);
// }
// });
// } else {
// //it may have already been added if this very shapeset was used directly to add an item to
// //(we need to add it directly as that is expected behaviour when you add something to a set)
// //so if we don't have an existing shape in here for the added node, then we add it
// if (
// !this.some((shape) => {
// return shape.node === q.object;
// })
// ) {
// if (allowSubShapes) {
// super.add(getShapeOrSubShape(q.object, shapeClass));
// } else {
// super.add(new (shapeClass as any)(q.object));
// }
// }
// }
// });
// });
}
/**
* When cloned (by .filter() or .sort()) we switch to a ShapeSet of all the values
* And detach from the magic of PropertyValueShapeSets that automatically add and remove items
* @param args
*/
createNew(...args) {
return new ShapeSet_js_1.ShapeSet(...args);
}
/**
* Add a new Shape to this set of values.
* This creates a new quad in the local graph.
* This is equivalent to manually adding a new property value using `subject.set(predicate,object)`
* @param value the node to add
*/
add(value) {
if (value && value.node && !this.subject.has(this.property, value.node)) {
this.subject.set(this.property, value.node);
}
return super.add(value);
}
/**
* Remove a Shape from this set of values.
* Also removes a quad in the local graph (if the node was an existing value)
* This is equivalent to manually removing a property value using `subject.unset(predicate,object)`
* @param value the node to remove
*/
delete(value) {
if (value && value.node) {
this.subject.unset(this.property, value.node);
}
return super.delete(value);
}
/**
* Listen to any changes in the valueset for this subject + property combination
* If you provide context (usually 'this'), removing the onChange listener will remove all listeners for this property & context, regardless of what callback you provide. (this is helpful if you dont have access to the excact same callback function)
* @param callback
* @param context
*/
onChange(callback, context) {
this.subject.onChange(this.property, callback, context);
}
/**
* Remove listener for changes in the valueset for this subject + property combination
* If you provide context (usually 'this'), removing the onChange listener will remove all listeners for this property & context, regardless of what callback you provide. (this is helpful if you dont have access to the excact same callback function)
* @param callback
* @param context
*/
removeOnChange(callback, context) {
this.subject.removeOnChange(this.property, callback, context);
}
}
exports.ShapeValuesSet = ShapeValuesSet;
//# sourceMappingURL=ShapeValuesSet.js.map