-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbackbone.KonvaView.js
129 lines (118 loc) · 4.04 KB
/
backbone.KonvaView.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
(function(root, factory) {
if (typeof define === 'function' && define.amd) {
define(['underscore', 'backbone', 'konva'], function(_, Backbone, Konva) {
Backbone.KonvaView = factory(root, _, Backbone, Konva);
return Backbone;
});
} else if (typeof exports !== 'undefined') {
var Backbone_ = require('backbone');
Backbone_.KonvaView = factory(root, require('underscore'), Backbone_, require('konva'));
module.exports = Backbone_.KonvaView;
} else {
root.Backbone.KonvaView = factory(root, _, Backbone, Konva);
}
}(this, function(root, _, Backbone, Konva) {
var KonvaView = function(options) {
this.cid = _.uniqueId('view');
options || (options = {});
_.extend(this, _.pick(options, viewOptions));
this._ensureElement();
this.initialize.apply(this, arguments);
};
KonvaView.extend = Backbone.Model.extend;
var delegateEventSplitter = /^(\S+)\s*(.*)$/;
var viewOptions = ['model', 'collection', 'el', 'id', 'attributes', 'events', 'nodeType'];
_.extend(KonvaView.prototype, Backbone.Events, {
nodeType : 'Group',
initialize: function(){},
render : function(){
return this;
},
remove: function() {
this.undelegateEvents();
this.el.destroy();
this.stopListening();
return this;
},
setElement: function(element) {
this.undelegateEvents();
this.el = element;
this.delegateEvents();
return this;
},
_deleteEventFromNode : function(node) {
var _this = this;
_.each(_.keys(node.eventListeners),function(eventType){
node.off(eventType+'.delegateEvents' + _this.cid);
});
},
undelegateEvents: function() {
var _this = this;
if (!this.el) {
return this;
}
this._deleteEventFromNode(this.el);
_.each(this.el.children, function(child){
_this._deleteEventFromNode(child);
});
return this;
},
undelegate: function(eventName, selector) {
eventName += '.delegateEvents' + this.cid;
if (selector === '' || !selector) {
this.el.off(eventName);
} else {
this.el.find && this.el.find(selector).each(function(child){
child.off(eventName);
});
}
},
delegateEvents: function(events) {
if (!(events || (events = _.result(this, 'events')))) return this;
this.undelegateEvents();
for (var key in events) {
var method = events[key];
if (!_.isFunction(method)) method = this[events[key]];
if (!method) continue;
var match = key.match(delegateEventSplitter);
this.delegate(match[1], match[2], _.bind(method, this));
}
return this;
},
delegate: function(eventName, selector, listener) {
// listener = selector || listener;
eventName += '.delegateEvents' + this.cid;
if (selector === '') {
this.el.on(eventName, listener);
} else if (!listener) {
this.el.on(eventName, selector);
} else {
this.el.find && this.el.find(selector).each(function(child){
child.on(eventName, listener);
});
}
},
_configure: function(options) {
if (this.options) options = _.extend({}, _.result(this, 'options'), options);
_.extend(this, _.pick(options, viewOptions));
this.options = options;
},
_createElement: function(nodeType) {
return new Konva[nodeType];
},
_ensureElement: function() {
if (!this.el) {
var attrs = _.extend({}, _.result(this, 'attributes'));
if (this.id) attrs.id = _.result(this, 'id');
this.setElement(this._createElement(_.result(this, 'nodeType')));
this._setAttributes(attrs);
} else {
this.setElement(_.result(this, 'el'));
}
},
_setAttributes: function(attributes) {
this.el.setAttrs(attributes);
}
});
return KonvaView;
}));