keeweb/app/scripts/views/drag-view.js

66 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-01-31 07:50:28 +01:00
const Backbone = require('backbone');
2015-10-17 23:49:24 +02:00
2017-01-31 07:50:28 +01:00
const DragView = Backbone.View.extend({
2015-10-17 23:49:24 +02:00
events: {
'mousedown': 'mousedown'
},
2019-08-16 23:05:39 +02:00
initialize: function(coord) {
2015-11-21 15:55:42 +01:00
this.setCoord(coord);
2015-10-17 23:49:24 +02:00
this.mouseDownTime = -1;
this.mouseDownCount = 0;
},
2015-11-21 15:55:42 +01:00
setCoord: function(coord) {
this.coord = coord;
this.offsetProp = 'page' + coord.toUpperCase();
},
2015-10-17 23:49:24 +02:00
render: function() {
2019-08-16 23:05:39 +02:00
$('<div/>')
.addClass('drag-handle__inner')
.appendTo(this.$el);
2015-10-17 23:49:24 +02:00
},
mousedown: function(e) {
if (e.which === 1) {
2017-01-31 07:50:28 +01:00
const now = Date.now();
2015-10-17 23:49:24 +02:00
if (now - this.mouseDownTime < 500) {
this.mouseDownCount++;
if (this.mouseDownCount === 2) {
this.trigger('autosize', { coord: this.coord });
return;
}
} else {
this.mouseDownTime = now;
this.mouseDownCount = 1;
}
this.initialOffset = e[this.offsetProp];
2017-01-31 07:50:28 +01:00
const cursor = this.$el.css('cursor');
2019-08-16 23:05:39 +02:00
this.dragMask = $('<div/>', { 'class': 'drag-mask' })
.css('cursor', cursor)
.appendTo('body');
2015-10-17 23:49:24 +02:00
this.dragMask.on('mousemove', this.mousemove.bind(this));
this.dragMask.on('mouseup', this.mouseup.bind(this));
this.trigger('dragstart', { offset: this.initialOffset, coord: this.coord });
this.$el.addClass('dragging');
e.preventDefault();
}
},
mousemove: function(e) {
if (e.which === 0) {
this.mouseup();
} else {
this.trigger('drag', { offset: e[this.offsetProp] - this.initialOffset });
}
},
mouseup: function() {
this.dragMask.remove();
this.$el.removeClass('dragging');
}
});
module.exports = DragView;