1
0
mirror of https://github.com/keeweb/keeweb.git synced 2024-06-22 07:16:38 +02:00
keeweb/app/scripts/framework/views/resizable.js

70 lines
2.1 KiB
JavaScript
Raw Normal View History

2019-09-16 22:57:56 +02:00
import { Events } from 'framework/events';
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const Resizable = {
2019-08-18 10:17:09 +02:00
listenDrag(dragView) {
2015-10-17 23:49:24 +02:00
this.listenTo(dragView, 'dragstart', this.dragStart);
this.listenTo(dragView, 'drag', this.drag);
this.listenTo(dragView, 'autosize', this.autoSize);
},
2019-08-18 10:17:09 +02:00
dragStart(e) {
2015-10-17 23:49:24 +02:00
this._dragInfo = this.getDragInfo(e.coord);
},
2019-08-18 10:17:09 +02:00
drag(e) {
2017-01-31 07:50:28 +01:00
const dragInfo = this._dragInfo;
let size = dragInfo.startSize + e.offset;
2015-10-17 23:49:24 +02:00
size = Math.max(dragInfo.min, Math.min(dragInfo.max, size));
this.$el[dragInfo.prop](size);
2019-09-16 17:43:57 +02:00
this.emit('view-resize', size);
2019-09-16 22:57:56 +02:00
Events.emit('page-geometry', { source: 'resizable' });
2015-10-17 23:49:24 +02:00
},
2019-08-18 10:17:09 +02:00
autoSize(e) {
2017-01-31 07:50:28 +01:00
const dragInfo = this.getDragInfo(e.coord);
2015-10-17 23:49:24 +02:00
if (dragInfo.auto !== undefined) {
this.$el.css(dragInfo.prop, dragInfo.auto);
} else {
this.$el.css(dragInfo.prop, 'auto');
}
this.fixSize(dragInfo);
2019-09-16 17:43:57 +02:00
this.emit('view-resize', null);
2019-09-16 22:57:56 +02:00
Events.emit('page-geometry', { source: 'resizable' });
2015-10-17 23:49:24 +02:00
},
2019-08-18 10:17:09 +02:00
fixSize(cfg) {
2017-01-31 07:50:28 +01:00
const size = this.$el[cfg.prop]();
const newSize = Math.max(cfg.min, Math.min(cfg.max, size));
2015-10-17 23:49:24 +02:00
if (newSize !== size) {
this.$el[cfg.prop](size);
}
},
// TODO: check size on window resize
2016-07-17 13:30:38 +02:00
// checkSize: function() {
// if (this.maxWidth) {
// this.fixSize(this.getDragInfo('x'));
// }
// if (this.maxHeight) {
// this.fixSize(this.getDragInfo('y'));
// }
// },
2015-10-17 23:49:24 +02:00
2019-08-18 10:17:09 +02:00
getDragInfo(coord) {
2017-01-31 07:50:28 +01:00
const prop = coord === 'x' ? 'Width' : 'Height';
const propLower = prop.toLowerCase();
const min = this.getSizeProp('min' + prop);
const max = this.getSizeProp('max' + prop);
const auto = this.getSizeProp('auto' + prop) || 'auto';
const startSize = this.$el[propLower]();
2019-08-18 10:17:09 +02:00
return { startSize, prop: propLower, min, max, auto };
2015-10-17 23:49:24 +02:00
},
2019-08-18 10:17:09 +02:00
getSizeProp(prop) {
2017-01-31 07:50:28 +01:00
const member = this[prop];
2015-10-17 23:49:24 +02:00
return typeof member === 'function' ? member.call(this) : member;
}
};
2019-09-15 14:16:32 +02:00
export { Resizable };