This repository has been archived by the owner on Dec 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 90
/
SelectionHandler.js
193 lines (166 loc) · 7.14 KB
/
SelectionHandler.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
/**
* @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";
require("ResizeHandler");
var lastSelectedElement = null;
var defferedSelect = [];
var multiselectArea = null;
function _isSelectEventOnMultiSelectArea(point){
if(multiselectArea && _isPointWithinGivenArea(point)){
return true;
} else {
return false;
}
}
function _isPointWithinGivenArea(point){
if( point.x >= multiselectArea.left && point.x <= multiselectArea.right &&
point.y >= multiselectArea.top && point.y <= multiselectArea.bottom ){
return true;
}else{
return false;
}
}
function getOffset( target ) {
var _x = 0;
var _y = 0;
var targetParam = target;
var _brdrTop,_brdrLeft;
while( target && !isNaN( target.offsetLeft ) && !isNaN( target.offsetTop ) ) {
_x += target.offsetLeft - target.scrollLeft;
_y += target.offsetTop - target.scrollTop;
target = target.offsetParent;
if(target){
_brdrTop = parseInt($(target).css("border-top-width")) || 0;
_brdrLeft = parseInt($(target).css("border-left-width")) || 0;
_x += _brdrLeft;
_y += _brdrTop;
}
}
return { top: _y, left: _x, width: $(targetParam).outerWidth(false), height: $(targetParam).outerHeight(false) };
}
function _showControls(element,refresh){
var transform = $(element).css('transform');
var offset = null;
if(transform && transform!== 'none'){
offset = getOffset(element);
} else {
offset = element.getBoundingClientRect();
}
//var offset = element.getBoundingClientRect();//getOffset(element);
/*var width = $(element).outerWidth(false),
height = $(element).outerHeight(false);*/
$("#selection-outline").css("top",offset.top+23);
$("#selection-outline").css("left",offset.left+23);
/*$("#selection-outline").css("width",width - 2);
$("#selection-outline").css("height",height - 2);*/
$("#selection-outline").css("width",offset.width - 2);
$("#selection-outline").css("height",offset.height - 2);
$("#selection-outline").css('transform',$(element).css('transform'));
$("#selection-outline").css('transform-origin',$(element).css('transform-origin'));
lastSelectedElement = element;
//$(".controlDiv").show();
//$("#html-design-editor").trigger("selection-area-computed",[offset,width,height]);
if(refresh){
$("#html-design-editor").trigger("element.selection.refreshed",[element]);
} else {
$("#html-design-editor").trigger("element.selected",[element]);
}
}
function _handleSelection(element){
$(".multiSelectStyle").removeClass("multiSelectStyle");
$('#multiselection-toolbox').hide();
_handleDeselection();
_showControls(element);
$("#selection-outline").show();
defferedSelect = [];
//_handleAfterPseudo(element);
}
function _handleDeselection(){
$("#selection-outline").hide();
lastSelectedElement = null;
}
function _refreshSelection(event,force){
var asynchPromise = new $.Deferred();
if(lastSelectedElement){
_showControls(lastSelectedElement,force ? null : true);
}
asynchPromise.resolve();
return asynchPromise.promise();
}
$(document).on('deselect.all',"#html-design-editor",_handleDeselection);
$(document).on("targetdom.element.click","#html-design-editor", function(event,element,point){
if(!_isSelectEventOnMultiSelectArea(point)){
_handleSelection(element);
multiselectArea = null;
}
});
$(document).on('refresh.element.selection',"#html-design-editor",_refreshSelection);
//Layered selection management
function _makePointerEventOpaque(elements){
var count = 0;
for(count = 0;count<elements.length;count++){
$(elements[count]).css("pointer-events",'auto');
}
}
//use iteration to find the next element in z-index
function _getNextElementAtPoint(point,doc,currentSelectedElement){
var toBeReverted = [];
var targetElement = doc.elementFromPoint(point.x,point.y);
var isContainerFound = false;
if(targetElement){
//Reach the current selected element first
while(!isContainerFound){
if(targetElement === currentSelectedElement){
isContainerFound = true;
} else {
$(targetElement).css("pointer-events",'none');
toBeReverted.push(targetElement);
targetElement = doc.elementFromPoint(point.x,point.y);
if(!targetElement){
isContainerFound = true;
}
}
}
}
//Find the next element in z-index now
if(targetElement === currentSelectedElement){
$(targetElement).css("pointer-events",'none');
toBeReverted.push(targetElement);
targetElement = doc.elementFromPoint(point.x,point.y);
} else { //Beyond current selection so return null as we are exiting layers
targetElement = null;
}
//revert pointer event opacity of modified elements
_makePointerEventOpaque(toBeReverted);
return targetElement;
}
$(document).on("layeredselect.click","#html-design-editor", function(event,element,point){
// Change to be done here. Below code to be executed if the selection point for layered select is within the selected area
var elementRect = lastSelectedElement.getBoundingClientRect();
if( (point.x >= elementRect.left && point.x < elementRect.right) &&
(point.y >= elementRect.top && point.y < elementRect.bottom)
){
var doc = document.getElementById('htmldesignerIframe').contentWindow.document;
var toBeSelected = _getNextElementAtPoint(point, doc,lastSelectedElement);
if(toBeSelected){
_handleSelection(toBeSelected);
}
}
});
$(document).on("select.element","#html-design-editor", function(event,element,point){
if(element){
_handleSelection(element);
multiselectArea = null;
}
});
$(document).on("multiselectarea.computed","#html-design-editor", function(event,area){
multiselectArea = area;
lastSelectedElement = null;
});
exports.deSelectAll = _handleDeselection;
exports.refreshSelection = _refreshSelection;
});