this.dragLinkPos = null;
this.resizeLinkPos = null;
+ this.magnetValuesX = [];
+ this.magnetValuesY = [];
+
this.init();
}
LinkeditorLinks.prototype = {
init: function () {
- var $this = this;
+ let $this = this;
$(document).on('mousedown', '.link .corners div', function (e) {
e.preventDefault();
e.stopPropagation();
if (this.resizeLinkPos === null) {
return;
}
- var f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
- var dx = (this.linkeditor.mx - this.resizeLinkPos.x) * f;
- var dy = (this.linkeditor.my - this.resizeLinkPos.y) * f;
+ let f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
+ let dx = (this.linkeditor.mx - this.resizeLinkPos.x) * f;
+ let dy = (this.linkeditor.my - this.resizeLinkPos.y) * f;
- var top = 0, left = 0, width = 0, height = 0;
+ let top = 0, left = 0, width = 0, height = 0;
if (['n', 'ne', 'nw'].indexOf(this.resizeLinkPos.corner) >= 0) {
top = dy;
}
$(".link.selected").each(function () {
- var newWidth = $(this).data('drag-orig-width') + width;
- var newHeight = $(this).data('drag-orig-height') + height;
+ let newWidth = $(this).data('drag-orig-width') + width;
+ let newHeight = $(this).data('drag-orig-height') + height;
if (key.ctrl) {
// Keep ratio
- var ratio = $(this).data('drag-orig-width') / $(this).data('drag-orig-height');
- var xscale = newWidth / $(this).data('drag-orig-width');
- var yscale = newHeight / $(this).data('drag-orig-height');
+ let ratio = $(this).data('drag-orig-width') / $(this).data('drag-orig-height');
+ let xscale = newWidth / $(this).data('drag-orig-width');
+ let yscale = newHeight / $(this).data('drag-orig-height');
if (['ne', 'nw', 'se', 'sw'].indexOf(this.resizeLinkPos.corner) >= 0) {
if (xscale < yscale) {
newHeight = newWidth / ratio;
}
}
- var newLeft = $(this).data('drag-orig-left') + left;
- var newTop = $(this).data('drag-orig-top') + top;
+ let newLeft = $(this).data('drag-orig-left') + left;
+ let newTop = $(this).data('drag-orig-top') + top;
$(this).attr('fb-left', newLeft)
.attr('fb-top', newTop)
$(".link.selected").each(function () {
$(this).data('drag-orig-left', parseFloat($(this).attr('fb-left')));
$(this).data('drag-orig-top', parseFloat($(this).attr('fb-top')));
+ $(this).data('drag-orig-width', parseFloat($(this).attr('fb-width')));
+ $(this).data('drag-orig-height', parseFloat($(this).attr('fb-height')));
});
},
if (this.dragLinkPos === null) {
return;
}
- var f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
- var dx = (this.linkeditor.mx - this.dragLinkPos.x) * f;
- var dy = (this.linkeditor.my - this.dragLinkPos.y) * f;
+
+ let $this = this;
+ let f = 1 / (this.linkeditor.fs * this.linkeditor.zoom.zoom);
+ let dx = (this.linkeditor.mx - this.dragLinkPos.x) * f;
+ let dy = (this.linkeditor.my - this.dragLinkPos.y) * f;
$(".link.selected").each(function () {
- $(this).attr('fb-left', $(this).data('drag-orig-left') + dx).attr('fb-top', $(this).data('drag-orig-top') + dy).attr('fb-update', '1');
+ $(this).attr('fb-left', $this.linkeditor.utils.magnetize($(this).data('drag-orig-left') + dx, $this.magnetValuesX, $(this).data('drag-orig-width')))
+ .attr('fb-top', $this.linkeditor.utils.magnetize($(this).data('drag-orig-top') + dy, $this.magnetValuesY, $(this).data('drag-orig-height')))
+ .attr('fb-update', '1');
});
this.linkeditor.updateFBElements(false);
},
offsetSelectedLinks: function (dim, value) {
$('.link.selected').each(function () {
- var v = parseFloat($(this).attr('fb-' + dim));
+ let v = parseFloat($(this).attr('fb-' + dim));
$(this).attr('fb-' + dim, v + value).attr('fb-update', '1');
});
this.linkeditor.updateFBElements();
},
loadLinks: function (page, side) {
- var $this = this;
+ let $this = this;
$.each(LINKS, function (uid, link) {
if (link.page != page) {
return;
},
addLink: function (link) {
- var $this = this;
+ let $this = this;
if (link.uid === undefined) {
link.uid = generateUID();
LINKS[link.uid] = link;
this.linkeditor.hasChanged();
}
- var attrs = {};
+ let attrs = {};
$.each(link, function (k, v) {
if ($this.linkeditor.dimensionProperties.indexOf(k) >= 0) {
attrs['fb-' + k] = v;
}
});
- var e = $('<div class="link" fb-ref="editor" fb-update="1"></div>');
+ let e = $('<div class="link" fb-ref="editor" fb-update="1"></div>');
$(e).attr(attrs);
$("#linkeditor-links").append(e);
},
+ updateMagnetValues: function () {
+ let $this = this;
+ this.magnetValuesX = [];
+ this.magnetValuesY = [];
+ $.each(this.linkeditor.rulers.getRulersOfPage(), function (uid, ruler) {
+ if (ruler.type === 'y') {
+ $this.magnetValuesY.push(ruler.pos);
+ } else {
+ $this.magnetValuesX.push(ruler.pos);
+ }
+ });
+ },
clear: function () {
$("#linkeditor-links").html('');
loadRulers: function (page, side) {
let $this = this;
+ $.each(this.getRulersOfPage(page), function (uid, ruler) {
+ $this.addRuler(ruler.type, ruler.pos, ruler.uid);
+ });
+ this.linkeditor.links.updateMagnetValues();
+ },
+
+ getRulersOfPage(page) {
+ var pages = this.linkeditor.getCurrentPages(page);
+ var res = {};
$.each(RULERS, function (uid, ruler) {
- if (ruler.page != page) {
+ if (pages.indexOf(parseInt(ruler.page)) === -1) {
return;
}
- $this.addRuler(ruler.type, ruler.pos, ruler.uid);
+ res[uid] = ruler;
});
+ return res;
},
-
updateRulers: function () {
// Update rects
this.linkeditor.fluidbookRect = $("#linkeditor-fluidbook").get(0).getBoundingClientRect();
$(this.movingRuler).css(css).attr(attrs);
RULERS[$(this.movingRuler).data('uid')].pos = fbv;
this.linkeditor.updateFBElements();
+ this.linkeditor.links.updateMagnetValues();
},
addRuler: function (axis, pos, uid) {
delete RULERS[$(ruler).data('uid')];
$(ruler).remove();
this.movingRuler = null;
+ this.linkeditor.links.updateMagnetValues();
this.linkeditor.hasChanged();
},