2019-08-03 15:03:33 +02:00
|
|
|
import * as utils from './utils'
|
|
|
|
import Point from './point'
|
|
|
|
import Path from './path'
|
|
|
|
import Snippet from './snippet'
|
|
|
|
import Attributes from './attributes'
|
|
|
|
import Hooks from './hooks'
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-08-05 18:19:48 +02:00
|
|
|
function Part() {
|
2019-08-03 15:03:33 +02:00
|
|
|
this.attributes = new Attributes()
|
|
|
|
this.points = {}
|
|
|
|
this.paths = {}
|
|
|
|
this.snippets = {}
|
|
|
|
this.freeId = 0
|
|
|
|
this.topLeft = false
|
|
|
|
this.bottomRight = false
|
|
|
|
this.width = false
|
|
|
|
this.height = false
|
|
|
|
this.render = true
|
|
|
|
this.utils = utils
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2018-07-24 14:38:03 +00:00
|
|
|
// Constructors so macros can create objects
|
2019-08-03 15:03:33 +02:00
|
|
|
this.Point = Point
|
|
|
|
this.Path = Path
|
|
|
|
this.Snippet = Snippet
|
2018-07-24 14:38:03 +00:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
this.hooks = new Hooks() // Hooks container
|
2018-12-18 15:35:07 +01:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
2018-07-23 20:14:32 +02:00
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.macroClosure = function (args) {
|
2019-08-03 15:03:33 +02:00
|
|
|
let self = this
|
2020-07-11 15:15:02 +02:00
|
|
|
let method = function (key, args) {
|
2019-08-03 15:03:33 +02:00
|
|
|
let macro = utils.macroName(key)
|
2020-07-11 15:15:02 +02:00
|
|
|
if (typeof self[macro] === 'function') self[macro](args)
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-07-23 17:35:06 +00:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return method
|
|
|
|
}
|
2018-07-23 11:12:06 +00:00
|
|
|
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.runHooks = function (hookName, data = false) {
|
2019-08-03 15:03:33 +02:00
|
|
|
if (data === false) data = this
|
|
|
|
let hooks = this.hooks[hookName]
|
2019-01-28 09:05:56 +01:00
|
|
|
if (hooks && hooks.length > 0) {
|
2018-12-18 15:35:07 +01:00
|
|
|
for (let hook of hooks) {
|
2019-08-03 15:03:33 +02:00
|
|
|
hook.method(data, hook.data)
|
2018-12-18 15:35:07 +01:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
2018-12-18 15:35:07 +01:00
|
|
|
|
2018-08-01 14:55:54 +02:00
|
|
|
/** Returns an unused ID */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.getId = function () {
|
2019-08-03 15:03:33 +02:00
|
|
|
this.freeId += 1
|
2018-08-01 14:55:54 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return '' + this.freeId
|
|
|
|
}
|
2018-08-01 14:55:54 +02:00
|
|
|
|
2018-08-01 18:18:29 +02:00
|
|
|
/** Returns a value formatted for units provided in settings */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.unitsClosure = function (value) {
|
2019-08-03 15:03:33 +02:00
|
|
|
let self = this
|
2020-07-11 15:15:02 +02:00
|
|
|
let method = function (value) {
|
2019-08-03 15:03:33 +02:00
|
|
|
return utils.units(value, self.context.settings.units)
|
|
|
|
}
|
2018-08-08 14:02:06 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return method
|
|
|
|
}
|
2018-08-01 14:55:54 +02:00
|
|
|
|
2018-08-01 18:18:29 +02:00
|
|
|
/** Calculates the part's bounding box and sets it */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.boundary = function () {
|
2019-08-03 15:03:33 +02:00
|
|
|
if (this.topLeft) return this // Cached
|
2018-08-01 18:18:29 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
let topLeft = new Point(Infinity, Infinity)
|
|
|
|
let bottomRight = new Point(-Infinity, -Infinity)
|
2018-08-01 18:18:29 +02:00
|
|
|
for (let key in this.paths) {
|
2019-08-03 15:03:33 +02:00
|
|
|
let path = this.paths[key].boundary()
|
2018-08-01 18:18:29 +02:00
|
|
|
if (path.render) {
|
2019-08-03 15:03:33 +02:00
|
|
|
if (path.topLeft.x < topLeft.x) topLeft.x = path.topLeft.x
|
|
|
|
if (path.topLeft.y < topLeft.y) topLeft.y = path.topLeft.y
|
|
|
|
if (path.bottomRight.x > bottomRight.x) bottomRight.x = path.bottomRight.x
|
|
|
|
if (path.bottomRight.y > bottomRight.y) bottomRight.y = path.bottomRight.y
|
2018-08-01 18:18:29 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-22 11:34:08 +02:00
|
|
|
for (let key in this.points) {
|
2019-08-03 15:03:33 +02:00
|
|
|
let point = this.points[key]
|
|
|
|
let radius = point.attributes.get('data-circle')
|
2018-08-22 11:34:08 +02:00
|
|
|
if (radius) {
|
2019-08-03 15:03:33 +02:00
|
|
|
radius = parseFloat(radius)
|
|
|
|
if (point.x - radius < topLeft.x) topLeft.x = point.x - radius
|
|
|
|
if (point.y - radius < topLeft.y) topLeft.y = point.y - radius
|
|
|
|
if (point.x + radius > bottomRight.x) bottomRight.x = point.x + radius
|
|
|
|
if (point.y + radius > bottomRight.y) bottomRight.y = point.y + radius
|
2018-08-22 11:34:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// Fix infinity if part has no paths
|
2019-08-03 15:03:33 +02:00
|
|
|
if (topLeft.x === Infinity) topLeft.x = 0
|
|
|
|
if (topLeft.y === Infinity) topLeft.y = 0
|
|
|
|
if (bottomRight.x === -Infinity) bottomRight.x = 0
|
|
|
|
if (bottomRight.y === -Infinity) bottomRight.y = 0
|
2018-09-11 11:39:50 +02:00
|
|
|
// Add margin
|
2019-08-03 15:03:33 +02:00
|
|
|
let margin = this.context.settings.margin
|
|
|
|
if (this.context.settings.paperless && margin < 10) margin = 10
|
|
|
|
this.topLeft = new Point(topLeft.x - margin, topLeft.y - margin)
|
|
|
|
this.bottomRight = new Point(bottomRight.x + margin, bottomRight.y + margin)
|
|
|
|
this.width = this.bottomRight.x - this.topLeft.x
|
|
|
|
this.height = this.bottomRight.y - this.topLeft.y
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
2018-08-01 18:18:29 +02:00
|
|
|
|
|
|
|
/** Stacks part so that its top left corner is in (0,0) */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.stack = function () {
|
2019-08-03 15:03:33 +02:00
|
|
|
if (this.topLeft !== false) return this
|
|
|
|
else this.boundary()
|
|
|
|
if (this.topLeft.x == 0 && this.topLeft.y == 0) return this
|
|
|
|
else this.attr('transform', `translate(${this.topLeft.x * -1}, ${this.topLeft.y * -1})`)
|
|
|
|
|
|
|
|
return this
|
|
|
|
}
|
2018-08-01 18:18:29 +02:00
|
|
|
|
|
|
|
/** Adds an attribute. This is here to make this call chainable in assignment */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.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-01 18:18:29 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
|
|
|
}
|
2018-08-01 18:18:29 +02:00
|
|
|
|
2018-08-05 15:52:37 +02:00
|
|
|
/** Copies point/path/snippet data from part orig into this */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.inject = function (orig) {
|
|
|
|
const findBasePoint = (p) => {
|
2019-05-10 17:38:47 +02:00
|
|
|
for (let i in orig.points) {
|
2019-08-03 15:03:33 +02:00
|
|
|
if (orig.points[i] === p) return i
|
2019-05-10 17:38:47 +02:00
|
|
|
}
|
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return false
|
|
|
|
}
|
2019-05-10 17:38:47 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
for (let i in orig.points) this.points[i] = orig.points[i].clone()
|
2019-05-10 17:38:47 +02:00
|
|
|
for (let i in orig.paths) {
|
2019-08-03 15:03:33 +02:00
|
|
|
this.paths[i] = orig.paths[i].clone()
|
2019-05-10 17:38:47 +02:00
|
|
|
// Keep link between points and path ops where possible
|
|
|
|
for (let j in orig.paths[i].ops) {
|
2019-08-03 15:03:33 +02:00
|
|
|
let op = orig.paths[i].ops[j]
|
|
|
|
if (op.type !== 'close') {
|
|
|
|
let toPoint = findBasePoint(op.to)
|
|
|
|
if (toPoint) this.paths[i].ops[j].to = this.points[toPoint]
|
2019-05-10 17:38:47 +02:00
|
|
|
}
|
2019-08-03 15:03:33 +02:00
|
|
|
if (op.type === 'curve') {
|
|
|
|
let cp1Point = findBasePoint(op.cp1)
|
|
|
|
if (cp1Point) this.paths[i].ops[j].cp1 = this.points[cp1Point]
|
|
|
|
let cp2Point = findBasePoint(op.cp2)
|
|
|
|
if (cp2Point) this.paths[i].ops[j].cp2 = this.points[cp2Point]
|
2018-08-05 15:52:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-10 17:38:47 +02:00
|
|
|
for (let i in orig.snippets) {
|
2019-08-03 15:03:33 +02:00
|
|
|
this.snippets[i] = orig.snippets[i].clone()
|
2019-05-10 17:38:47 +02:00
|
|
|
}
|
2018-08-05 15:52:37 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
return this
|
|
|
|
}
|
2018-08-05 15:52:37 +02:00
|
|
|
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.units = function (input) {
|
2019-08-03 15:03:33 +02:00
|
|
|
return utils.units(input, this.context.settings.units)
|
|
|
|
}
|
2018-08-08 14:02:06 +02:00
|
|
|
|
|
|
|
/** Returns an object with shorthand access for pattern design */
|
2020-07-11 15:15:02 +02:00
|
|
|
Part.prototype.shorthand = function () {
|
2019-08-03 15:03:33 +02:00
|
|
|
let complete = this.context.settings.complete ? true : false
|
|
|
|
let paperless = this.context.settings.paperless === true ? true : false
|
|
|
|
let sa = this.context.settings.complete ? this.context.settings.sa || 0 : 0
|
2018-08-08 14:02:06 +02:00
|
|
|
return {
|
2018-09-12 17:34:53 +02:00
|
|
|
sa,
|
2018-08-08 14:02:06 +02:00
|
|
|
measurements: this.context.settings.measurements || {},
|
2018-12-11 18:49:00 +01:00
|
|
|
options: this.context.settings.options || {},
|
2018-08-08 14:02:06 +02:00
|
|
|
store: this.context.store,
|
|
|
|
points: this.points || {},
|
|
|
|
paths: this.paths || {},
|
|
|
|
snippets: this.snippets || {},
|
|
|
|
macro: this.macroClosure(),
|
|
|
|
units: this.unitsClosure(),
|
2018-08-14 16:16:46 +02:00
|
|
|
utils: utils,
|
2018-08-08 14:02:06 +02:00
|
|
|
Point: this.Point,
|
|
|
|
Path: this.Path,
|
|
|
|
Snippet: this.Snippet,
|
2018-09-12 17:34:53 +02:00
|
|
|
complete,
|
2020-07-11 15:15:02 +02:00
|
|
|
paperless
|
2019-08-03 15:03:33 +02:00
|
|
|
}
|
|
|
|
}
|
2018-08-08 14:02:06 +02:00
|
|
|
|
2019-08-03 15:03:33 +02:00
|
|
|
export default Part
|