/*
* Picture fields (pictureField(), functions/picture.php) - a person's picture,
* or a photo of an asset, a location or a rack, shown as the form shows it, on
* a button with a camera badge. Clicking it opens the Picture window, over the
* form's own window when it is in one:
*
* - drop a picture on it, paste one, or choose one
* - crop it to fit the shape it's shown in - drag it, zoom it (the wheel, a
* pinch, the slider, or + and -), turn it a quarter at a time, or pick
* another shape where the field has more than one
* - Use this picture, or Remove the one there is
*
* A field with data-crop="free" - a receipt, a logo - has the whole picture
* with a frame over it, its corners and edges dragged in to any shape, and
* with data-picture-trim a Trim the edges button that pulls the frame in to
* what's on the picture. A field whose file input takes application/pdf takes
* a PDF as it is. With data-picture-submit the form is sent once the picture
* is used - the receipt on an expense's quick look.
*
* The crop goes in the field's hidden file input (through a DataTransfer),
* as a JPG for a JPG, else - so see-through parts stay see-through - a WebP
* where the field takes WebP (PNG where the browser can't write it) or a PNG,
* no bigger than data-crop-max and without the
* photo's own data - where it was taken, the camera. Remove sets the field's
* remove input to 1. The form posts them as it always did. Opened again, the
* window has the picture picked as it was left, to change.
*
* A person's initials, where they show, follow the form field named in
* data-picture-name as it is typed.
*
* A browser that can't put a file in a field opens the file picker instead,
* and the picture goes as picked.
*/
(function () {
'use strict';
if (window.itflowPictureField) {
return;
}
window.itflowPictureField = true;
var SHAPES = {
'square': { w: 1, h: 1, label: 'Square' },
'4:3': { w: 4, h: 3, label: '4:3' },
'3:4': { w: 3, h: 4, label: '3:4' },
'16:9': { w: 16, h: 9, label: '16:9' },
'whole': { w: 0, h: 0, label: 'Whole' }
};
var TYPES = /^image\/(jpeg|png|gif|webp)$/;
var MARGIN = 24; // pixels of the picture shown, dimmed, round the frame
var FRAME_MAX_HEIGHT = 340;
var SOURCE_MAX = 4096; // a bigger picture is scaled down to this first
var ZOOM_MAX = 8; // times the smallest zoom that fills the frame
var canReplace = (function () {
try {
return typeof DataTransfer === 'function' && !!new DataTransfer().items && typeof File === 'function';
} catch (e) {
return false;
}
})();
function el(tag, className, text) {
var node = document.createElement(tag);
if (className) {
node.className = className;
}
if (text !== undefined) {
node.textContent = text;
}
return node;
}
// The picture, upright as the camera meant it (an follows its orientation), no bigger than SOURCE_MAX
function readPicture(file) {
return new Promise(function (resolve, reject) {
var url = URL.createObjectURL(file);
var img = new Image();
img.onload = function () {
URL.revokeObjectURL(url);
var k = Math.min(1, SOURCE_MAX / Math.max(img.naturalWidth, img.naturalHeight));
var canvas = document.createElement('canvas');
canvas.width = Math.max(1, Math.round(img.naturalWidth * k));
canvas.height = Math.max(1, Math.round(img.naturalHeight * k));
var context = canvas.getContext('2d');
context.imageSmoothingQuality = 'high';
context.drawImage(img, 0, 0, canvas.width, canvas.height);
resolve(canvas);
};
img.onerror = function () {
URL.revokeObjectURL(url);
reject(new Error('unreadable'));
};
img.src = url;
});
}
function toBlob(canvas, type) {
return new Promise(function (resolve, reject) {
canvas.toBlob(function (blob) {
if (blob) {
resolve(blob);
} else {
reject(new Error('not encoded'));
}
}, type, 0.9);
});
}
/*
* The crop - a picture in a frame on the stage, the rest of it dimmed.
* Its state is the frame's centre on the picture (cx, cy) and how far it
* is zoomed past filling the frame (zoom), so a stage that changes size,
* or another shape, keeps what is in the frame.
*/
function Cropper(stage, frame, slider) {
var self = this;
this.stage = stage;
this.frame = frame;
this.slider = slider;
this.pointers = {};
this.round = false;
this.shape = 'square';
this.onchange = function () {};
stage.addEventListener('pointerdown', function (e) {
if (!self.source || (e.pointerType === 'mouse' && e.button !== 0)) {
return;
}
stage.setPointerCapture(e.pointerId);
self.pointers[e.pointerId] = { x: e.clientX, y: e.clientY };
stage.classList.add('is-moving');
e.preventDefault();
});
stage.addEventListener('pointermove', function (e) {
var point = self.pointers[e.pointerId];
if (!point) {
return;
}
var ids = Object.keys(self.pointers);
if (ids.length === 1) {
self.move(e.clientX - point.x, e.clientY - point.y);
} else if (ids.length === 2) {
// A pinch zooms about the middle of the two fingers
var other = self.pointers[ids[0] === String(e.pointerId) ? ids[1] : ids[0]];
var before = Math.hypot(point.x - other.x, point.y - other.y);
var after = Math.hypot(e.clientX - other.x, e.clientY - other.y);
var rect = stage.getBoundingClientRect();
if (before > 0) {
self.zoomAt(self.scale * after / before, (e.clientX + other.x) / 2 - rect.left, (e.clientY + other.y) / 2 - rect.top);
}
}
point.x = e.clientX;
point.y = e.clientY;
});
var end = function (e) {
delete self.pointers[e.pointerId];
if (!Object.keys(self.pointers).length) {
stage.classList.remove('is-moving');
}
};
stage.addEventListener('pointerup', end);
stage.addEventListener('pointercancel', end);
stage.addEventListener('wheel', function (e) {
if (!self.source) {
return;
}
e.preventDefault();
var rect = stage.getBoundingClientRect();
var delta = e.deltaMode === 1 ? e.deltaY * 16 : e.deltaY;
self.zoomAt(self.scale * Math.exp(-delta * 0.0015), e.clientX - rect.left, e.clientY - rect.top);
}, { passive: false });
stage.addEventListener('keydown', function (e) {
if (!self.source) {
return;
}
var step = e.shiftKey ? 40 : 10;
var moves = { ArrowLeft: [step, 0], ArrowRight: [-step, 0], ArrowUp: [0, step], ArrowDown: [0, -step] };
if (moves[e.key]) {
self.move(moves[e.key][0], moves[e.key][1]);
} else if (e.key === '+' || e.key === '=') {
self.zoomAt(self.scale * 1.15);
} else if (e.key === '-' || e.key === '_') {
self.zoomAt(self.scale / 1.15);
} else if (e.key === '0') {
self.fit();
} else {
return;
}
e.preventDefault();
});
slider.addEventListener('input', function () {
self.zoomAt(self.minScale * Math.pow(ZOOM_MAX, slider.value / 1000));
});
if (window.ResizeObserver) {
new ResizeObserver(function () {
self.layout();
}).observe(stage);
}
}
Cropper.prototype.setSource = function (source, state) {
if (this.source && this.source.parentNode) {
this.source.remove();
}
this.source = source;
if (!source) {
return;
}
source.className = 'itflow-crop-image';
source.setAttribute('aria-hidden', 'true');
source.style.width = source.width + 'px';
source.style.height = source.height + 'px';
this.stage.insertBefore(source, this.frame);
state = state || {};
this.zoom = state.zoom || 1;
this.cx = state.cx !== undefined ? state.cx : source.width / 2;
this.cy = state.cy !== undefined ? state.cy : source.height / 2;
this.layout();
};
Cropper.prototype.state = function () {
return { zoom: this.zoom, cx: this.cx, cy: this.cy };
};
// The frame for the stage's width and the shape, and the picture placed in it
Cropper.prototype.layout = function () {
var source = this.source;
var width = this.stage.clientWidth;
if (!source || !width) {
return;
}
var shape = SHAPES[this.shape] || SHAPES.square;
var ratio = shape.w ? shape.w / shape.h : source.width / source.height;
var fw = Math.min(width - MARGIN * 2, this.round ? 300 : Infinity);
var fh = fw / ratio;
if (fh > FRAME_MAX_HEIGHT) {
fh = FRAME_MAX_HEIGHT;
fw = fh * ratio;
}
this.fw = fw;
this.fh = fh;
this.fx = (width - fw) / 2;
this.fy = MARGIN;
this.stage.style.height = Math.round(fh + MARGIN * 2) + 'px';
this.frame.style.left = this.fx + 'px';
this.frame.style.top = this.fy + 'px';
this.frame.style.width = fw + 'px';
this.frame.style.height = fh + 'px';
this.frame.classList.toggle('is-round', this.round);
this.minScale = Math.max(fw / source.width, fh / source.height);
this.zoom = Math.min(ZOOM_MAX, Math.max(1, this.zoom || 1));
this.scale = this.minScale * this.zoom;
var halfW = fw / 2 / this.scale;
var halfH = fh / 2 / this.scale;
this.cx = Math.min(source.width - halfW, Math.max(halfW, this.cx));
this.cy = Math.min(source.height - halfH, Math.max(halfH, this.cy));
var x = this.fx + fw / 2 - this.cx * this.scale;
var y = this.fy + fh / 2 - this.cy * this.scale;
source.style.transform = 'translate(' + x + 'px, ' + y + 'px) scale(' + this.scale + ')';
this.slider.value = String(Math.round(Math.log(this.zoom) / Math.log(ZOOM_MAX) * 1000));
this.onchange();
};
Cropper.prototype.fit = function () {
if (!this.source) {
return;
}
this.zoom = 1;
this.cx = this.source.width / 2;
this.cy = this.source.height / 2;
this.layout();
};
Cropper.prototype.setShape = function (shape) {
this.shape = shape;
this.zoom = 1;
this.layout();
};
Cropper.prototype.move = function (dx, dy) {
this.cx -= dx / this.scale;
this.cy -= dy / this.scale;
this.layout();
};
// Zoom keeping the point under (x, y) on the stage still - the frame's centre when not given
Cropper.prototype.zoomAt = function (scale, x, y) {
if (!this.source || !this.fw) {
return;
}
scale = Math.min(this.minScale * ZOOM_MAX, Math.max(this.minScale, scale));
if (x === undefined) {
x = this.fx + this.fw / 2;
y = this.fy + this.fh / 2;
}
var left = this.fx + this.fw / 2 - this.cx * this.scale;
var top = this.fy + this.fh / 2 - this.cy * this.scale;
var px = (x - left) / this.scale;
var py = (y - top) / this.scale;
this.cx = (this.fx + this.fw / 2 - (x - px * scale)) / scale;
this.cy = (this.fy + this.fh / 2 - (y - py * scale)) / scale;
this.zoom = scale / this.minScale;
this.layout();
};
Cropper.prototype.rotate = function () {
if (this.source) {
this.setSource(turnedQuarter(this.source));
}
};
// What is in the frame, the longest side no more than $max - its size, before encoding
Cropper.prototype.outputSize = function (max) {
var sw = this.fw / this.scale;
var sh = this.fh / this.scale;
var k = Math.min(1, max / Math.max(sw, sh));
return [Math.max(1, Math.round(sw * k)), Math.max(1, Math.round(sh * k))];
};
// ... as a file - { blob, width, height }
Cropper.prototype.encode = function (type, max) {
var sw = this.fw / this.scale;
var sh = this.fh / this.scale;
return encodeCanvas(this.source, this.cx - sw / 2, this.cy - sh / 2, sw, sh, this.outputSize(max), type);
};
/*
* The free crop - the whole picture on the stage, and a frame over it
* dragged by its corners and edges to any shape (data-crop="free"), or
* moved by its middle. Its state is the frame on the picture, in the
* picture's own pixels.
*/
var HANDLES = ['nw', 'n', 'ne', 'e', 'se', 's', 'sw', 'w'];
function FreeCropper(stage, frame) {
var self = this;
this.stage = stage;
this.frame = frame;
this.onchange = function () {};
HANDLES.forEach(function (handle) {
var dot = el('span', 'itflow-free-handle is-' + handle);
dot.setAttribute('data-handle', handle);
frame.appendChild(dot);
});
var drag = null;
stage.addEventListener('pointerdown', function (e) {
if (!self.source || (e.pointerType === 'mouse' && e.button !== 0)) {
return;
}
var handle = e.target.closest('[data-handle]');
var onFrame = e.target.closest('.itflow-free-frame');
if (!handle && !onFrame) {
return;
}
stage.setPointerCapture(e.pointerId);
drag = { id: e.pointerId, x: e.clientX, y: e.clientY, handle: handle ? handle.getAttribute('data-handle') : 'move', start: Object.assign({}, self.rect) };
stage.classList.add('is-moving');
e.preventDefault();
});
stage.addEventListener('pointermove', function (e) {
if (!drag || drag.id !== e.pointerId) {
return;
}
self.dragTo(drag, (e.clientX - drag.x) / self.scale, (e.clientY - drag.y) / self.scale);
});
var end = function (e) {
if (drag && drag.id === e.pointerId) {
drag = null;
stage.classList.remove('is-moving');
}
};
stage.addEventListener('pointerup', end);
stage.addEventListener('pointercancel', end);
frame.addEventListener('keydown', function (e) {
if (!self.source) {
return;
}
var step = (e.shiftKey ? 40 : 8) / self.scale;
var moves = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step] };
if (!moves[e.key]) {
return;
}
e.preventDefault();
self.dragTo({ handle: 'move', start: Object.assign({}, self.rect) }, moves[e.key][0], moves[e.key][1]);
});
if (window.ResizeObserver) {
new ResizeObserver(function () {
self.layout();
}).observe(stage);
}
}
FreeCropper.prototype.setSource = function (source, rect) {
if (this.source && this.source.parentNode) {
this.source.remove();
}
this.source = source;
if (!source) {
return;
}
source.className = 'itflow-crop-image';
source.setAttribute('aria-hidden', 'true');
source.style.width = source.width + 'px';
source.style.height = source.height + 'px';
this.stage.insertBefore(source, this.frame);
this.rect = rect ? Object.assign({}, rect) : { x: 0, y: 0, w: source.width, h: source.height };
this.layout();
};
FreeCropper.prototype.state = function () {
return Object.assign({}, this.rect);
};
// The picture fitted to the stage, and the frame drawn over it
FreeCropper.prototype.layout = function () {
var source = this.source;
var width = this.stage.clientWidth;
if (!source || !width) {
return;
}
// A receipt is tall - up to about half the screen high, to read it while cropping
var high = Math.max(FRAME_MAX_HEIGHT, Math.min(560, Math.round(window.innerHeight * 0.55)));
this.scale = Math.min((width - MARGIN * 2) / source.width, high / source.height);
this.ox = (width - source.width * this.scale) / 2;
this.oy = MARGIN;
this.stage.style.height = Math.round(source.height * this.scale + MARGIN * 2) + 'px';
source.style.transform = 'translate(' + this.ox + 'px, ' + this.oy + 'px) scale(' + this.scale + ')';
var r = this.rect;
this.frame.style.left = (this.ox + r.x * this.scale) + 'px';
this.frame.style.top = (this.oy + r.y * this.scale) + 'px';
this.frame.style.width = (r.w * this.scale) + 'px';
this.frame.style.height = (r.h * this.scale) + 'px';
this.onchange();
};
// A handle (or the middle) moved by (dx, dy) picture pixels from where the drag began - kept on the picture, no smaller than 24 pixels on screen
FreeCropper.prototype.dragTo = function (drag, dx, dy) {
var source = this.source;
var min = Math.min(24 / this.scale, source.width, source.height);
var s = drag.start;
var left = s.x, top = s.y, right = s.x + s.w, bottom = s.y + s.h;
if (drag.handle === 'move') {
dx = Math.min(source.width - right, Math.max(-left, dx));
dy = Math.min(source.height - bottom, Math.max(-top, dy));
left += dx;
right += dx;
top += dy;
bottom += dy;
} else {
if (drag.handle.indexOf('w') !== -1) {
left = Math.min(right - min, Math.max(0, left + dx));
}
if (drag.handle.indexOf('e') !== -1) {
right = Math.max(left + min, Math.min(source.width, right + dx));
}
if (drag.handle.indexOf('n') !== -1) {
top = Math.min(bottom - min, Math.max(0, top + dy));
}
if (drag.handle.indexOf('s') !== -1) {
bottom = Math.max(top + min, Math.min(source.height, bottom + dy));
}
}
this.rect = { x: left, y: top, w: right - left, h: bottom - top };
this.layout();
};
FreeCropper.prototype.fit = function () {
if (this.source) {
this.rect = { x: 0, y: 0, w: this.source.width, h: this.source.height };
this.layout();
}
};
FreeCropper.prototype.rotate = function () {
if (this.source) {
this.setSource(turnedQuarter(this.source));
}
};
/*
* Trim - the frame pulled in to what is on the picture: what isn't see-
* through, or for a picture with no see-through parts, what isn't the
* colour of its corners. False when there is nothing to trim to.
*/
FreeCropper.prototype.trim = function () {
var source = this.source;
if (!source) {
return false;
}
var k = Math.min(1, 400 / Math.max(source.width, source.height));
var w = Math.max(1, Math.round(source.width * k));
var h = Math.max(1, Math.round(source.height * k));
var probe = document.createElement('canvas');
probe.width = w;
probe.height = h;
var context = probe.getContext('2d', { willReadFrequently: true });
context.drawImage(source, 0, 0, w, h);
var data = context.getImageData(0, 0, w, h).data;
var clear = false;
for (var i = 3; i < data.length; i += 4) {
if (data[i] < 250) {
clear = true;
break;
}
}
// The background - the four corners' average
var bg = [0, 0, 0];
[0, w - 1, (h - 1) * w, h * w - 1].forEach(function (p) {
for (var c = 0; c < 3; c++) {
bg[c] += data[p * 4 + c] / 4;
}
});
var minX = w, minY = h, maxX = -1, maxY = -1;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
var p = (y * w + x) * 4;
var content = clear ? data[p + 3] > 16
: Math.abs(data[p] - bg[0]) > 28 || Math.abs(data[p + 1] - bg[1]) > 28 || Math.abs(data[p + 2] - bg[2]) > 28;
if (content) {
minX = Math.min(minX, x);
maxX = Math.max(maxX, x);
minY = Math.min(minY, y);
maxY = Math.max(maxY, y);
}
}
}
if (maxX < 0) {
return false;
}
// A pixel of room round it, back in the picture's own pixels
var pad = 1;
var left = Math.max(0, (minX - pad) / k);
var top = Math.max(0, (minY - pad) / k);
var right = Math.min(source.width, (maxX + 1 + pad) / k);
var bottom = Math.min(source.height, (maxY + 1 + pad) / k);
this.rect = { x: left, y: top, w: right - left, h: bottom - top };
this.layout();
return true;
};
FreeCropper.prototype.outputSize = function (max) {
var k = Math.min(1, max / Math.max(this.rect.w, this.rect.h));
return [Math.max(1, Math.round(this.rect.w * k)), Math.max(1, Math.round(this.rect.h * k))];
};
FreeCropper.prototype.encode = function (type, max) {
var r = this.rect;
return encodeCanvas(this.source, r.x, r.y, r.w, r.h, this.outputSize(max), type);
};
// A picture turned a quarter clockwise
function turnedQuarter(source) {
var turned = document.createElement('canvas');
turned.width = source.height;
turned.height = source.width;
var context = turned.getContext('2d');
context.translate(turned.width, 0);
context.rotate(Math.PI / 2);
context.drawImage(source, 0, 0);
return turned;
}
// Part of a picture, drawn at a size, as a file - { blob, width, height }. A JPG gets a white background; WebP falls back to PNG
function encodeCanvas(source, sx, sy, sw, sh, size, type) {
var out = document.createElement('canvas');
out.width = size[0];
out.height = size[1];
var context = out.getContext('2d');
context.imageSmoothingQuality = 'high';
if (type === 'image/jpeg') {
context.fillStyle = '#fff';
context.fillRect(0, 0, out.width, out.height);
}
context.drawImage(source, sx, sy, sw, sh, 0, 0, out.width, out.height);
return toBlob(out, type).then(function (blob) {
return blob.type === type || type !== 'image/webp' ? blob : toBlob(out, 'image/png');
}).then(function (blob) {
return { blob: blob, width: out.width, height: out.height };
});
}
// What a field's files are called, from its accept list - "JPG, PNG, GIF or WebP", "PNG or JPG", "... or PDF"
function typesText(accept) {
var names = { 'image/jpeg': 'JPG', 'image/png': 'PNG', 'image/gif': 'GIF', 'image/webp': 'WebP', 'image/*': 'JPG, PNG, GIF, WebP', 'application/pdf': 'PDF' };
var parts = [];
accept.split(',').forEach(function (type) {
(names[type.trim()] || '').split(', ').forEach(function (name) {
if (name && parts.indexOf(name) === -1) {
parts.push(name);
}
});
});
return parts.length > 1 ? parts.slice(0, -1).join(', ') + ' or ' + parts[parts.length - 1] : parts.join('');
}
// A file the field takes as it is - a PDF - on its button and in the window
function fileTile(name) {
var tile = el('span', 'itflow-picture-none is-file');
tile.appendChild(el('i', 'fas fa-file-pdf'));
tile.appendChild(el('span', 'text-truncate mw-100 px-2', name));
return tile;
}
/*
* The Picture window - one, set up for the field that opens it.
*/
var win = null;
function buildWindow() {
var modal = el('div', 'modal fade');
modal.tabIndex = -1;
modal.setAttribute('aria-hidden', 'true');
modal.setAttribute('aria-labelledby', 'itflowPictureTitle');
modal.innerHTML =
'