forked from swmitra/html-designer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContainmentEditHandler.js
186 lines (165 loc) · 7.1 KB
/
ContainmentEditHandler.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
/**
* @author Swagatam Mitra
*/
/*jslint vars: true, plusplus: true, devel: true, nomen: true, indent: 4, maxerr: 50 */
/*global define, document, console, brackets, $, Mustache */
define(function (require, exports, module) {
"use strict";
var startOffset = null;
var target = null;
var isDragged = false;
var dragSourceArea = null;
var currentParent = null;
var currentProspectiveParent = null;
var layout;
var preservedPosition = null;
function _doCleanUp(event){
if(event.which === 16){
$("#parent-highlighter").hide();
$("#prospective-parent-highlighter").hide();
if(startOffset){
layout.rollBack(preservedPosition);
layout.refresh();
}
startOffset = null;
currentParent = null;
$(window).off('keyup',_doCleanUp);
$(target).css('pointer-events','auto');
$('.controlDiv').css('pointer-events','all');
$("#html-design-editor").trigger("design.editor.drag.deactivated");
}
}
//Layered selection management
function _makePointerEventOpaque(elements){
var count = 0;
for(count = 0;count<elements.length;count++){
$(elements[count]).css("pointer-events",'auto');
}
}
function _isContainer(tagName){
switch(tagName){
case 'DIV':
case 'SPAN':
case 'BODY':
case 'P':return true;
default: return false;
}
}
//use iteration to find the next element in z-index
function _getContainerAtPoint(doc,point){
var toBeReverted = [];
var isContainerFound = false;
var targetElement = doc.elementFromPoint(point.x,point.y);
if(targetElement){
while(!isContainerFound){
if(_isContainer(targetElement.tagName) || target){
isContainerFound = true;
} else {
$(targetElement).css("pointer-events",'none');
toBeReverted.push(targetElement);
targetElement = doc.elementFromPoint(point.x,point.y);
if(!targetElement){
isContainerFound = true;
}
}
}
}
//revert pointer event opacity of modified elements
_makePointerEventOpaque(toBeReverted);
return targetElement;
}
function _highlightProspectiveParent(point){
var shadowedDoc = document.getElementById('htmldesignerIframe').contentWindow.document;
var prospectiveParent = _getContainerAtPoint(shadowedDoc,point);
if(!prospectiveParent){
prospectiveParent = document.getElementById('htmldesignerIframe').contentWindow.document.body;
}
if(prospectiveParent !== currentParent && prospectiveParent !== currentProspectiveParent){
var boundingRect = prospectiveParent.getBoundingClientRect();
currentProspectiveParent = prospectiveParent;
$("#prospective-parent-highlighter").css("top",boundingRect.top+23);
$("#prospective-parent-highlighter").css("left",boundingRect.left+23);
$("#prospective-parent-highlighter").css("width",boundingRect.width);
$("#prospective-parent-highlighter").css("height",boundingRect.height);
$("#prospective-parent-highlighter").css('-webkit-transform',$(prospectiveParent).css('-webkit-transform'));
$("#prospective-parent-highlighter").show();
}
}
function _highlightCurrentParent(element){
var boundingRect = element.getBoundingClientRect();
$("#parent-highlighter").css("top",boundingRect.top+23);
$("#parent-highlighter").css("left",boundingRect.left+23);
$("#parent-highlighter").css("width",boundingRect.width);
$("#parent-highlighter").css("height",boundingRect.height);
$("#parent-highlighter").css('-webkit-transform',$(element).css('-webkit-transform'));
$("#parent-highlighter").show();
}
function _isPinInEventOnDragSource(point){
if( point.x >= dragSourceArea.left && point.x <= (dragSourceArea.left + dragSourceArea.width)
&& point.y >= dragSourceArea.top && point.y <= (dragSourceArea.top + dragSourceArea.height)){
return true;
} else {
return false;
}
}
function _initCDrag(event,element,point){
if( target && _isPinInEventOnDragSource(point)){
startOffset = point;
isDragged = false;
currentParent = target.parentNode;
currentProspectiveParent = null;
_highlightCurrentParent(target.parentNode);
$(target).css('pointer-events','none');
$(window).on('keyup',_doCleanUp);
preservedPosition = layout.createSavePoint();
$('.controlDiv').css('pointer-events','none');
layout.open();
$("#html-design-editor").trigger("design.editor.drag.activated");
}
}
function _doCDrag(event,element,point){
if(target && startOffset){
_highlightProspectiveParent(point);
layout.changeX(startOffset.x - point.x);
layout.changeY(startOffset.y - point.y);
startOffset = point;
$("#html-design-editor").trigger("refresh.element.selection");
isDragged = true;
}
}
function _endCDrag(event,element,point){
if(target && startOffset){
$(target).css('pointer-events','auto');
$("#html-design-editor")
.trigger(
"design.editor.event"
,['change.element.parent'
,{ element : target, prospectiveParent:currentProspectiveParent,template:target.outerHTML},'html']
);
$("#html-design-editor").trigger("design.editor.drag.deactivated");
$("#hover-outline").hide();
startOffset = null;
isDragged = false;
$("#parent-highlighter").hide();
$("#prospective-parent-highlighter").hide();
startOffset = null;
$(window).off('keyup',_doCleanUp);
currentParent = null;
$('.controlDiv').css('pointer-events','all');
layout.close();
//$("#html-design-editor").trigger("select.element",[layout.boxModel.targetElement]);
}
}
$(document).on("layout.decision","#html-design-editor", function(event,layoutObj){
layout = layoutObj;
});
$(document).on("element.selected","#html-design-editor",function(event,element){
dragSourceArea = element.getBoundingClientRect();
target = element;
});
$(document).on("containmentmousedown","#html-design-editor", _initCDrag);
$(document).on("containmentmousemove","#html-design-editor", _doCDrag);
$(document)
.on("containmentmouseup containmentmouseout containmentmouseleave","#html-design-editor"
,_endCDrag);
});