-
Notifications
You must be signed in to change notification settings - Fork 0
/
bindingHandler.fabric.js
303 lines (273 loc) · 12.9 KB
/
bindingHandler.fabric.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
ko.bindingHandlers.fabricDrawing = {
init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
var d = this;
// instantiate the canvas object
d.canvas = this.__canvas = new fabric.Canvas(element);
//
// draggable/selectable vs. drawing mode ?
// console.log(d.controlNameObservable());
d.controlDraggableObservable = valueAccessor().canvas.draggable;
d.controlDraggableObservable.subscribe(function (newValue) {
//console.log(newValue);
d.canvas.isDrawingMode = !newValue;
d.canvas.selection = newValue;
d.canvas.forEachObject(function (o) {
if (o.type == 'path') {
o.selectable = newValue;
}
});
d.canvas.renderAll.bind(d.canvas);
});
d.controlDraggableObservable.valueHasMutated();
//
// manually apply binding of id and name of element with controlNAmeObservable
// console.log(d.controlNameObservable());
d.controlNameObservable = valueAccessor().canvas.name;
ko.applyBindingsToNode(element, { attr: { name: d.controlNameObservable, id: d.controlNameObservable } });
d.controlNameObservable.subscribe(function (newValue) {
// console.log(newValue);
//console.log(element.name);
});
d.controlNameObservable.valueHasMutated();
//
// the actual value observable for control:
// apply a default for initialization purposes
// console.log(d.controlValueObservable());
d.controlValueObservable = valueAccessor().canvas.value;
d.controlValueObservable(d.controlValueObservable() || []);
d.controlValueObservable.subscribe(function (newValue) {
//console.log(d.canvas.toDatalessJSON());
//console.log(newValue);
});
//
// the actual image base 64 observable representation of the user input on this drawing control instance
// apply a default for initialization purposes
// console.log(d.controlImage64Observable());
d.controlImage64Observable = valueAccessor().canvas.image64;
d.controlImage64Observable.subscribe(function (newValue) {
//console.log(d.canvas.toDataURL());
//console.log(newValue);
// console.log(newValue.length);
});
d.controlImage64Observable('data:image/png;base64,');
//
// this might be a default value, or it could be data to populate a drawing (for editing or viewing) from the database
// console.log(d.controlInitialStaticValue);
d.controlInitialStaticValue = valueAccessor().canvas.initial;
if (d.controlInitialStaticValue) {
d.canvas.loadFromJSON(d.controlInitialStaticValue, d.canvas.renderAll.bind(d.canvas));
d.controlValueObservable(d.controlInitialStaticValue);
}
d.updateSigLine = function () {
if (d.sigLineStaticObject) {
d.sigLineStaticObject.set({
x1: parseInt(d.sigLineOffsetXObservable()),
y1: parseInt(d.controlHeightObservable() - d.sigLineOffsetYObservable()),
x2: parseInt(d.controlWidthObservable() - d.sigLineOffsetXObservable()),
y2: parseInt(d.controlHeightObservable() - d.sigLineOffsetYObservable()),
fill: d.sigLineColorObservable(),
stroke: d.sigLineColorObservable(),
strokeWidth: parseInt(d.sigLineWidthObservable()),
visible: d.sigLineVisibleObservable()
});
d.canvas.trigger('mouse:down', {});
d.canvas.renderAll.bind(d.canvas);
// d.canvas.setActiveObject(d.sigLineStaticObject);
// console.log('signature line updated');
}
// else{
// console.log('signature line not updated because it hasn\'t been added to the canvas yet);
// }
}
//
// the actual width of the control
// for image (display mode) or drawing (input mode)
// apply the bindings to the root node
// console.log(d.controlWidthObservable());
d.controlWidthObservable = valueAccessor().canvas.width;
ko.applyBindingsToNode(element, { attr: { width: d.controlWidthObservable } });
d.controlWidthObservable.subscribe(function (newValue) {
// console.log(newValue);
d.canvas.setWidth(parseInt(newValue));
d.canvas.calcOffset();
d.updateSigLine();
//d.canvas.renderAll.bind(d.canvas);
});
d.controlWidthObservable.valueHasMutated();
//
// the actual height of the control
// for image (display mode) or drawing (input mode)
// console.log(d.controlHeightObservable());
d.controlHeightObservable = valueAccessor().canvas.height;
ko.applyBindingsToNode(element, { attr: { height: d.controlHeightObservable() + 'px' } });
d.controlHeightObservable.subscribe(function (newValue) {
// console.log(newValue);
d.canvas.setHeight(parseInt(newValue));
d.canvas.calcOffset();
d.updateSigLine();
//d.canvas.renderAll.bind(d.canvas);
});
d.controlHeightObservable.valueHasMutated();
//
// the visibility of the canvas input portion of the drawing control
d.controlVisibleObservable = valueAccessor().canvas.visible;
d.controlVisibleObservable.subscribe(function (newValue) {
console.log(newValue);
var visibility = 'none';
if (newValue) {
visibility = 'block';
}
element.parentNode.style.display = visibility;
});
d.controlVisibleObservable.valueHasMutated();
// //
// // the actual display mode of the control
// // for image (display mode) or drawing (input mode)
// d.controlModeObservable = valueAccessor().canvas.mode;
// d.controlModeObservable.subscribe(function(newValue){
// console.log(newValue);
// });
//
// the actual width of the optional signature line of the control
d.sigLineWidthObservable = valueAccessor().line.width;
d.sigLineWidthObservable.subscribe(function (newValue) {
// console.log(newValue);
d.updateSigLine();
});
//
// the actual color of the optional signature line of the control
d.sigLineColorObservable = valueAccessor().line.color;
d.sigLineColorObservable.subscribe(function (newValue) {
// console.log(newValue);
d.updateSigLine();
});
//
// the offset in the x direction of the optional signature line of the control
d.sigLineOffsetXObservable = valueAccessor().line.offsetX;
d.sigLineOffsetXObservable.subscribe(function (newValue) {
// console.log(newValue);
d.updateSigLine();
});
//
// the offset in the y direction of the optional signature line of the control
d.sigLineOffsetYObservable = valueAccessor().line.offsetY;
d.sigLineOffsetYObservable.subscribe(function (newValue) {
// console.log(newValue);
d.updateSigLine();
});
//
// the visibility of the optional signature line of the control
d.sigLineVisibleObservable = valueAccessor().line.visible;
d.sigLineVisibleObservable.subscribe(function (newValue) {
console.log(newValue);
d.updateSigLine();
});
function signatureLine() {
var coords = [
parseInt(d.sigLineOffsetXObservable()),
parseInt(d.controlHeightObservable() - d.sigLineOffsetYObservable()),
parseInt(d.controlWidthObservable() - d.sigLineOffsetXObservable()),
parseInt(d.controlHeightObservable() - d.sigLineOffsetYObservable()),
];
// // var coords = [25, 130, 450, 130];
// console.log(coords);
var sigLine = new fabric.Line(coords, {
fill: d.sigLineColorObservable(),
stroke: d.sigLineColorObservable(),
strokeWidth: parseInt(d.sigLineWidthObservable()),
selectable: false,
visible: d.sigLineVisibleObservable()
});
// console.log(sigLine);
return sigLine;
}
d.sigLineStaticObject = new signatureLine();
d.canvas.add(d.sigLineStaticObject);
// simulate a setting change to fire the update
//d.sigLineWidthObservable.valueHasMutated();
//
// the actual width of the drawing line of the control
d.brushWidthObservable = valueAccessor().brush.width;
d.brushWidthObservable.subscribe(function (newValue) {
// console.log(newValue);
});
d.brushWidthObservable.valueHasMutated();
//
// the actual color of the drawing line of the control
d.brushColorObservable = valueAccessor().brush.color;
d.brushColorObservable.subscribe(function (newValue) {
// console.log(newValue);
});
d.brushColorObservable.valueHasMutated();
//
// update items from the canvas into the model
// console.log('updateModel');
d.updateModel = function () {
d.controlValueObservable(d.canvas.toDatalessJSON());
d.controlImage64Observable(d.canvas.toDataURL());
}
//
// this is kind of a reverse binding experiment
// bind the dummy variable function from the control
// to the canvas clear function used internally in this handler
// console.log(valueAccessor().canvas.clear);
valueAccessor().canvas.clear = function (resetInitialValueBool) {
// console.log('clearing canvas, and view model. If "resetInitialValueBool" is true, then the initial value will be restored.');
// if (resetInitialValueBool && d.controlInitialStaticValue) {
// // load the json and automatically update the canvas
// d.canvas.loadFromJSON(d.controlInitialStaticValue, d.canvas.renderAll.bind(d.canvas));
// }
// else {
// d.canvas.clear();
// }
d.canvas.clear();
d.sigLineStaticObject = new signatureLine();
d.canvas.add(d.sigLineStaticObject);
d.updateModel();
}
//
// this is kind of a reverse binding experiment
// bind the dummy variable function from the control
// to the canvas deleteSelected function used internally in this handler
// console.log(valueAccessor().canvas.deleteSelected);
valueAccessor().canvas.deleteSelected = function () {
var selected = d.canvas.getActiveObject();
if (selected) {
selected.remove();
}
}
//
// this funtion activates the bindings with fabric for this handler
d.activateBinding = function () {
// listen to the canvas for drawing updates
ko.utils.arrayForEach(['mouse:down', 'mouse:up'],
function (eventName) {
d.canvas.on(eventName, function (e) {
// console.log(eventName);
d.updateModel();
});
});
// automatically update the brush width & color of new drawings to the currently selected color
ko.utils.arrayForEach(['object:added'], //'path:created',
function (eventName) {
d.canvas.on(eventName, function (e) {
if (e.target.type == 'path') {
var lastDrawing = e.target;
lastDrawing.set({
// the fill variable will literally fill in any circle you draw
// cool, but unintended consequences
// fill: d.brushColorObservable(),
stroke: d.brushColorObservable(),
strokeWidth: parseInt(d.brushWidthObservable())
});
// d.canvas.renderAll.bind(d.canvas);
}
});
});
}
// listen to the canvas for drawing updates
d.activateBinding();
d.updateModel();
return d;
}
}