I tried many things but none of them are working. Hence, posting the same.
Here is the background:
I have this HTML
element which will create a rectangular box based on ng-repeat
dynamically. Within each of this Rectangular
box there are various HTML elements such as HTML select and input text
etc and these fields can be edited by the user. I want to make this rectangular box draggable
so the user can move it around.
Hence, I have written an angularjs directive
to do it and its working fine But if I add the directive then the select
field and all the fields within the rectangular
box are no more editable as it applies the draggable directive
to the whole rectangular
. If I remove this directive then it would work fine. How can I make the rectangular
box draggable
, as well as fields within it, are selectable or editable
?
HTML CODE
:
<div ng-repeat="NewField in NewFieldValues">
<div style="width:250px;height:120px;border:1px solid #000;" draggable draggable="true">
<select ng-model="Dynamic.BusinessStep[NewField.ID]" ng-change="BusinessStepChange(NewField.ID,'BusinessStep')" class="form-control">
<option class="dropdown-item" value="" selected> Choose Business Step</option>
<option class="dropdown-item" ng-repeat="businessStep in businessSteps" value="{{ businessStep.value }}"> {{ businessStep.text }} </option>
</select>
<br/>
<input type="text" ng-model="Dynamic.ObjectCount[NewField.ID]" ng-blur="BusinessStepChange(NewField.ID,'EventCount')" placeholder="number of objects" class="form-control"></input>
</div>
<br/>
</div>
Angularjs Directive:
app.directive('draggable', function($document) {
return function(scope, element, attr) {
var startX = 0, startY = 0, x = 0, y = 0;
element.css({
position: 'relative',
border: '1px solid red',
backgroundColor: 'lightgrey',
cursor: 'pointer',
display: 'block'
});
element.on('mousedown', function(event) {
console.log("MOUSE DOWN");
// Prevent default dragging of selected content
event.preventDefault();
startX = event.screenX - x;
startY = event.screenY - y;
$document.on('mousemove', mousemove);
$document.on('mouseup', mouseup);
return true;
});
function mousemove(event) {
console.log("MOUSE MOVE");
y = event.screenY - startY;
x = event.screenX - startX;
element.css({
top : y + 'px',
left : x + 'px'
});
return true;
}
function mouseup() {
$document.off('mousemove', mousemove);
$document.off('mouseup', mouseup);
return true;
}
};
});
I tried to change a few things but none of them are working. As I trying to add the draggable
to the rectangular box the fields within that by default get the draggable directive
and hence their default
behavior is overridden by the angularjs directive
. I would appreciate it if someone can provide me some guidance here.
The solution is simply to implement $event.stopPropagation();
, what this does is prevent the related $event
from bubbling up and triggering other similar events.