-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
BlockView.js
62 lines (52 loc) · 1.49 KB
/
BlockView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
var Backbone = require('backbone');
module.exports = Backbone.View.extend({
events: {
mousedown: 'startDrag'
},
initialize(o, config) {
_.bindAll(this, 'endDrag');
this.config = config || {};
this.ppfx = this.config.pStylePrefix || '';
this.listenTo(this.model, 'destroy remove', this.remove);
this.doc = $(document);
},
/**
* Start block dragging
* @private
*/
startDrag(e) {
//Right or middel click
if (e.button !== 0) {
return;
}
if(!this.config.getSorter) {
return;
}
this.config.em.refreshCanvas();
var sorter = this.config.getSorter();
sorter.setDragHelper(this.el, e);
sorter.startSort(this.el);
sorter.setDropContent(this.model.get('content'));
this.doc.on('mouseup', this.endDrag);
},
/**
* Drop block
* @private
*/
endDrag(e) {
this.doc.off('mouseup', this.endDrag);
const sorter = this.config.getSorter();
// After dropping the block in the canvas the mouseup event is not yet
// triggerd on 'this.doc' and so clicking outside, the sorter, tries to move
// things (throws false positives). As this method just need to drop away
// the block helper I use the trick of 'moved = 0' to void those errors.
sorter.moved = 0;
sorter.endMove();
},
render() {
var className = this.ppfx + 'block';
this.$el.addClass(className);
this.el.innerHTML = '<div class="' + className + '-label">' + this.model.get('label') + '</div>';
return this;
},
});