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
/
DragAndDropCreationHandler.js
138 lines (116 loc) · 4.74 KB
/
DragAndDropCreationHandler.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
/**
* @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 AppInit = brackets.getModule("utils/AppInit");
var currentContext = { type:'pointer', profile:'html', template: null};
var newElement = null;
var newElementID = null;
var targetContainer = null;
var ProfileSelector = require("widgetprofiles/WidgetProfileSelector");
function _initDrag(){
$("#element-drawing-plane").show();
}
$(document).on("bind-dragsources","#widget-toolbox",function(event){
$( ".widgetDragSource" ).draggable({
helper: _createWidgetLook,
start: _initDrag,
cursorAt: { top: 10, left: 10 }
});
});
$(document).on('mousedown',".widgetDragSource",function(event){
currentContext.profile = this.dataset.profile;
currentContext.type=this.id.replace('ad-element-','');
if(currentContext.type != 'pointer'){
$("#element-drawing-plane").show();
} else {
$("#element-drawing-plane").empty();
$("#element-drawing-plane").hide();
}
});
function _endDrag(event,ui){
$("#element-drawing-plane").hide();
_sendNewElementToTargetDOM(event,ui);
}
//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 _getContainerAtPoint(doc,point){
var toBeReverted = [];
var isContainerFound = false;
var targetElement = doc.elementFromPoint(point.x,point.y);
while(!isContainerFound && targetElement){
if(targetElement.tagName === 'HTML'){
targetElement = null;
}else if(targetElement.tagName === 'DIV' || targetElement.tagName === 'BODY'){
isContainerFound = true;
} else {
$(targetElement).css("pointer-events",'none');
toBeReverted.push(targetElement);
targetElement = doc.elementFromPoint(point.x,point.y);
}
}
//revert pointer event opacity of modified elements
_makePointerEventOpaque(toBeReverted);
return targetElement;
}
function _create(event,ui){
var shadowedDoc = document.getElementById('htmldesignerIframe').contentWindow.document;
var targetX = event.clientX - $("#element-drawing-plane").offset().left - event.offsetX;
var targetY = event.clientY - $("#element-drawing-plane").offset().top - event.offsetY;
targetContainer = _getContainerAtPoint(shadowedDoc,{x:targetX,y:targetY});
if(!targetContainer){
targetContainer = shadowedDoc.body;
}
return {x:targetX,y:targetY};
}
function _createWidgetLook(){
var template = ProfileSelector.getProfile(currentContext.profile).getTemplate(currentContext.type);
newElement = $(template.template).clone();
newElementID = template.uid;
return newElement;
}
function _sendNewElementToTargetDOM(event,ui){
var targetPoint = _create(event,ui);
if(newElement[0]){
$(newElement).css('left',targetPoint.x+'px').css('top',targetPoint.y+'px').removeAttr("class");
var newWidgettemplate = newElement[0].outerHTML;
var parentElement = $(targetContainer || document.getElementById('htmldesignerIframe').contentWindow.document.body);
$("#html-design-editor")
.trigger(
"design.editor.event"
,[
'create.new.element'
,{ template : newWidgettemplate, containerElement:parentElement,uid:newElementID}
,currentContext.profile
,currentContext.type
]
);
newElement.remove();
$("#element-drawing-plane").hide();
newElement = null;
}
}
AppInit.appReady(function () {
$( ".widgetDragSource" ).draggable({
helper: _createWidgetLook,
start: _initDrag,
cursorAt: { top: 10, left: 10 }
});
$("#element-drawing-plane").droppable({
accept: "*",
drop: _sendNewElementToTargetDOM
});
$("#element-drawing-plane").on('dragenter dragleave dragover',function(event){
event.preventDefault();
});
});
});