1
0
Fork 0
freesewing/packages/core/src/snippet.js

36 lines
983 B
JavaScript
Raw Normal View History

2019-08-03 15:03:33 +02:00
import Attributes from './attributes'
import Point from './point'
2018-07-23 11:12:06 +00:00
function Snippet(def, anchor, debug = false) {
this.debug = debug
if (this.debug) {
if (typeof def !== 'string')
raise.warning('Called `new Snippet(def, anchor)` but `def` is not a string')
if (anchor instanceof Point !== true)
raise.warning('Called `new Snippet(dev, anchor)` but `anchor` is not a `Point` object')
}
2019-08-03 15:03:33 +02:00
this.def = def
this.anchor = anchor
this.attributes = new Attributes()
2018-07-23 11:12:06 +00:00
2019-08-03 15:03:33 +02:00
return this
2018-07-23 11:12:06 +00:00
}
2018-08-17 20:24:10 +02:00
/** Adds an attribute. This is here to make this call chainable in assignment */
Snippet.prototype.attr = function (name, value, overwrite = false) {
2019-08-03 15:03:33 +02:00
if (overwrite) this.attributes.set(name, value)
else this.attributes.add(name, value)
2018-08-17 20:24:10 +02:00
2019-08-03 15:03:33 +02:00
return this
}
2018-08-17 20:24:10 +02:00
2018-08-03 14:20:28 +02:00
/** Returns a deep copy of this */
Snippet.prototype.clone = function () {
2019-08-03 15:03:33 +02:00
let clone = new Snippet(this.def, this.anchor.clone())
clone.attributes = this.attributes.clone()
2018-08-03 14:20:28 +02:00
2019-08-03 15:03:33 +02:00
return clone
}
2018-08-03 14:20:28 +02:00
2019-08-03 15:03:33 +02:00
export default Snippet