-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmotifs-edit.js
More file actions
193 lines (142 loc) · 5.17 KB
/
motifs-edit.js
File metadata and controls
193 lines (142 loc) · 5.17 KB
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
var motifs_edit = {
Fabric_Canvas: undefined,
active: false,
show: function(){
$(".cpanel#main").removeClass("cpanel-main-size1").addClass("cpanel-main-size3");//change window size
$("#cpanel-main-tabs").tabs("option", "disabled", true); // Disable main tab set
$("#motifs-view").hide();
$("#motifs-edit").show();
this.active = true;
// 1. Load Selected Motif (A side effect in the datamodel)
DM.EDmotf_LoadFrom(motifs_view.selected_row_i);
// 2. Update Title SmartInput
$("#motifs-edit .motif-title input#motif-name").SmartInput("update", {
underlying_obj: DM.EDITINGmotf,
data_change: true
});
// 3. Load all new elements (canvas & HTML)
$.each(DM.EDITINGmotf.Elements, function(index, Properties) {
var uid = Properties.PGTuid;
motifs_props.Fabric_AddShape(uid, Properties); // Add to Fabric Canvas
motifs_props.AddMotifElem_itemHTML(uid, Properties, {autoScroll: false}); // Add to HTML
});
},
hide: function(options){
options = options || {};
$(".cpanel#main").removeClass("cpanel-main-size3").addClass("cpanel-main-size1");//change window size
$("#cpanel-main-tabs").tabs("option", "disabled", false); // Enable main tab set
$("#motifs-view").show();
$("#motifs-edit").hide();
this.active = false;
// Clear old data in motifs edit: canvas objects & HTML
// (A) remove canvas elements
var canvas = this.Fabric_Canvas;
// this includes clearing any selections on canvas, and associated events
// (which I think is not correctly achieved when cycling canvas._objects and removing)
// this method fires both "before:selection:cleared" and "selection:cleared" events.
canvas.deactivateAllWithDispatch();
canvas.clear();
if(options.save){
//these functions would normally be part of the button click handler...
// i.e. cb atached to #motifs-edit .main-buttons #done
DM.EDmotf_Save(motifs_view.selected_row_i);
motifs_view.regenerate_table(); // Visual update
}
// (B) remove old motif element properties table HTML
$(".m-elem").each(function(index, $Elem) {
if($(this).attr("id") == "m-elem-template"){
return;
}
$(this).remove();
});
},
keyStrokeHandler: function(myKeycode, keyPressed, keyEvent){
var canvas = this.Fabric_Canvas;
if(keyEvent == "keydown"){
//Delete key pressed...
if(myKeycode == 46){
this.ActUponFabricSelection(function(fObj, uid){
motifs_edit.deleteMotifElement(uid);
}, {
groupDiscard: true
});
}
//an ARROW key pressed...
else if((myKeycode >= 37)&&(myKeycode <= 40)){
// determine the 1px MOVE, as a "change" object of properties...
this.ActUponFabricSelection(function(fObj, uid){
/*
left arrow 37
up arrow 38
right arrow 39
down arrow 40
*/
var cng = {};
if(myKeycode == 37){
cng.left = fObj.left - 1;
}else if(myKeycode == 38){
cng.top = fObj.top - 1;
}else if(myKeycode == 39){
cng.left = fObj.left + 1;
}else if(myKeycode == 40){
cng.top = fObj.top + 1;
}
motifs_edit.updateMotifElement(uid, cng);
});
}
// CTRL key pressed...
else if(myKeycode == 17){
//Lock aspect ratios.
canvas.uniScaleTransform = false;
}
}else if(keyEvent == "keyup"){
// CTRL key released...
if(myKeycode == 17){
//no lock on aspect ratios during transform
canvas.uniScaleTransform = true;
}
}
},
ActUponFabricSelection: function(CB_per_object, options){
options = options || {}; // in case of no options object provided...
var canvas = this.Fabric_Canvas;
var activeObject = canvas.getActiveObject();
var activeGroup = canvas.getActiveGroup();
// 1. a single object selected
if (activeObject) {
CB_per_object(activeObject, activeObject.PGTuid);
}
// 2. a group selected
else if (activeGroup) {
if(options.groupDiscard){
canvas.discardActiveGroup();
}
var objectsInGroup = activeGroup.getObjects();
objectsInGroup.forEach(function(object) {
CB_per_object(object, object.PGTuid);
});
}
},
// Function acts upon 1. Fabric; 2. DM; 3. HTML
deleteMotifElement: function(uid){
// get Fabric object via its PGTuid
var canvas = this.Fabric_Canvas;
var Fabric_Object = DM.GetByKey_( canvas._objects, "PGTuid", uid);
Fabric_Object.deleting = true; // removal from canvas triggers callbacks, which test for this...
canvas.remove(Fabric_Object); // 1. remove from canvas
DM.EDmotf_DeleteElement(uid); // 2. remove from DM structure
motifs_props.DeleteMotifElem_itemHTML(uid);// 3. remove from HTML
},
// Function acts upon 1. Fabric; 2. DM; 3. HTML
updateMotifElement: function(uid, propsCng){
// get Fabric object via its PGTuid
var canvas = this.Fabric_Canvas;
var Fabric_Object = DM.GetByKey_( canvas._objects, "PGTuid", uid);
// 1. update in canvas
Fabric_Object.set(propsCng);
Fabric_Object.setCoords(); // to recalculate Fabric's click detection for the object.
canvas.renderAll(); // n.b. setCoords() doesn't negate the need for this!
DM.EDmotf_UpdateElement(uid, propsCng); // 2. update in DM structure
motifs_props.UpdateMotifElem_itemHTML(uid, propsCng);// 3. update in HTML
}
};