I am a newcomer to knockout JavaScript and am currently utilizing Knockout drag and drop functionality in my project. Initially, I have two divisions - one is visible while the other has a display property set to none. During the drag enter function, I want to hide the first division and display the second division. However, upon dragging leaving, I need to hide the second division and show the first division again. The issue arises when the functionality of the second division changes during drag enter, but I do not wish to hide the second division until the drop event occurs. Any assistance in resolving this matter would be greatly appreciated.
It is important that no HTML is altered within the ".typeTextareaSection" block in the code.
You can view my fiddle Knockout Drag and Drop Fiddle
/** JavaScript ViewModel Code **/
/* ViewModel Function */
function ViewModel(){
var self = this;
this.dropZones = ko.observableArray([{
'elements' : ko.observableArray([]) // just for showcasing purposes
}]);
this.dragoverTextarea = function(e){
console.log('dragOver');
e.stopPropagation();
e.preventDefault();
}
this.dropTextarea = function(e, data){
console.log('drop');
e.stopPropagation();
e.preventDefault();
var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
data.elements.push(f.name);
}
$('.typeTextarea').children('.typeTextareaSection').css('display', 'block');
$('.typeTextarea').children('#dragtimeTextarea').css('display', 'none');
}
this.dragenterTextarea = function(e, index){
console.log('dragEnter');
$('.typeTextarea').eq(index).children('.typeTextareaSection').css('display', 'none');
$('.typeTextarea').children('#dragtimeTextarea').css('display', 'block');
}
this.dragleaveTextarea = function(e, index){
console.log('end');
$('.typeTextarea').children('.typeTextareaSection').css('display', 'block');
$('.typeTextarea').children('#dragtimeTextarea').css('display', 'none');
}
}
// Applying Knockout bindings to the ViewModel
ko.applyBindings(new ViewModel());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/2.3.0/knockout-min.js"></script>
/*HTML Markup*/
<div class="col-md-12" data-bind="foreach: dropZones">
<div class="typeTextarea" style="margin-top: 20px; height: 120px; border: 2px dashed #bbb; padding: 10px;" data-bind="event: {
dragover: function (data, e) { $root.dragoverTextarea(e); },
drop: function (data, e) { $root.dropTextarea(e, $data); },
dragenter: function (data, e) { $root.dragenterTextarea(e, $index()); },
dragleave: function (data, e) { $root.dragleaveTextarea(e, $index()); }
}">
/* Content of Type Textarea Division */
</div>
</div>