Showing posts with label salako ahmed. Show all posts
Showing posts with label salako ahmed. Show all posts

Friday, January 14, 2011

Demystifying Drag and Drop in ASP.NET (Part 1)

This is not particularly and fully related to ASP.NET but the main project which I extracted this concept from is an asp.net project, so I have decided to present an asp.net example.

As a requirement for one of my pet projects, I am required to create drag and drop event handler on an scheduler/events calendar. This threw me into the dark side of javascripting, ASP.NET ICallBackEvent interface, post back/viewstate dodging.

I searched the web for an existing solutions that will enable me drag events memo from a date to another date using JavaScript. Most of the approaches that I encountered are either too complex or overly complicated for the simple scenario

Then I stumbled on this particular script at Web Tool kit which blew my mind away, this JavaScript particularly simplifies the cross browsers headache when trying to handle mouse events across browsers. And its simple object oriented approach to handling and delegating event for drag and drop is just fantastic.

Then something happened
I was looking for a solution that will enable me to drag an event from one data point to another. The solution i am talking about above though perfect but it has not fulfilled the entire drag and drop functionality. I can drag and drop, but when droping, the element being dragged should be attached to the object in the drop zones.

For clarity, try the drag drop sample below. Please note that the two drop zones can allow drag gable elements to be dropped upon them, and that you cannot drop items outside of the drop zones. To give a clear comparison then try the example given in the original script, located here , then you can understand the concepts of drop zones and drop targeting.
















Drop Zone 1 Drop Zone 2 Collection Area




Draggable 1




Draggable 2





The above drag and drop simulates the drop zone functionality which allows drag able elements to be dropped into the zones without hassles.

The missing points in original JavaScript, is the fact that drop zones are not recognized. I will take you bit by bit into how I have improved and added new functionality to the original code :

Identifying the container

The container element for this page example is a table with id = 'main'. A container is an HTML element which will contain the drop zones, note, any html which can contain other elements (p, div, table li etc.)element within the body section of an HTML document can be used as a container. This example makes use of a table as the container.


<table id="main" cellpadding="6" cellspacing="6" border="1">
<thead>
<tr class="style1">
<th class="style1" valign="top"> Drop Zone 1 </th>
<th class="style1" valign="top"> Drop Zone 2 </th>
<th class="style1" valign="top"> Collection Area </th>
</tr>
</thead>
<tbody>
<tr>
<td class="zone">

</td>
<td class="zone">


</td>
<td class="zone">
<div id="dragable1" class="element-class">
<h3 class="box-head">Draggable 1 </h3>
</div>

<div id="dragable2" class="element-class">
<h3 class="box-head">Draggable 2 </h3>
</div>
<span id="writtable">

</span>
</td>
</tr>
</tbody>

</table>


Some table cells within the table main's table row have the class name zone identifying them as the drop zones. Any element with the class name = "zone" can contain draggables.

Any element can also be dragged (except td, th etc. ), so far they are registered using the following javascript snippet.


DragHandler.attach(document.getElementById('dragable element'));


Registering/Preparing the Container and Drag able objects
We needed to look up the DOM and hook events on the drag gable's and retain drop zones into a Position object created below.


var zonesArray = new Array();
var index = 0;

window.onload = function () {

var dragable1 = DragHandler.attach(document.getElementById('dragable1'));
var dragable2 = DragHandler.attach(document.getElementById('dragable2'));

var main = document.getElementById('main');

for (var j = 0; j < main.childNodes.length; j++) {

PrepareDropZones(main.childNodes[j], zonesArray, "zone");
}
}


function Position(element) {
this.X = findPosX(element);
this.Y = findPosY(element);
this.Element = element;
this.Width = (this.X + element.offsetWidth);
this.Height = (this.Y + element.offsetHeight);

this.IsInCordinate = function (XCord, YCord) {
if (XCord > this.X && XCord < (this.Width) && YCord > this.Y && YCord < (this.Height)) {
return true;
}

return false;
}
}


The above code is the missing point in the original code, and the part where elements are dropped. As soon as an element is dropped, the mouse coordinates X and Y are checked against the drop zones X + width and Y + height , if the mouse is within coordinate, then the element is dropped. Shown below is the code for checking drop zones when an element is dropped.


// private method. Stop drag process.
_dragEnd: function (e) {
var oElem = DragHandler._oElem;

var x = parseInt(oElem.style.left);
var y = parseInt(oElem.style.top);

oElem.dragEnd(oElem, x, y);

var evt = e || window.event;
var evtTarget = evt.target || evt.srcElement;

for (var i = 0; i < zonesArray.length; i++) {

var dZone = zonesArray[i];
var cursor = getMousePosition(e);

if (dZone.IsInCordinate(cursor.x, cursor.y)) {
dZone.Element.appendChild(oElem);
oElem.style.left = 0;
oElem.style.top = 0;
break;
}
else {
oElem.style.left = DragHandler._beginX;
oElem.style.top = DragHandler._beginY;
}
}

document.onmousemove = null;
document.onmouseup = null;
DragHandler._oElem = null;
oElem = null;
}


Download the source code (VS 2010):

Wednesday, November 11, 2009

Google is Ready to "GO"

There is a new addition to the Object Object Oriented programming platform , codenamed GO (A new addition and descendant of the C family) by Google. It is said to take advantages of new trends in the software versus hardware community. Go will take advantage of dynamic languages like python, it is said to have an efficient Garbage collection, runtime speed close to C language and full support for multi-core processor from ground up.

Go is blessed with true closures and reflection (Programmers delight to extending the framework). The reasons behind the introduction of GO is that the older languages have failed to include current computing trends like fast programming, expressiveness (Functional), multi-processors, true closures . I personally love the idea of methods/functions returning multiple values. Although i struggle with the return type after the method parameter.

Is Go the future that we all want?

The problems claimed to be solved by Go is already taken care of by mature object oriented programming languages. Go should not be seen as a replacement for C# or Java etc but a language introduced specifically to solve specific computing problems. C# is still in its infants and we have experienced more powerful programming concepts and constructs that Go claims to be a silver bullet for.
Dynamic language integration is already a number one citizen of todays languages, Java has the Rhino (Integrates with JavaScript), and C# 4.0 is coming with the dynamic keyword to solve Com inter op problems and integration with more dynamic language. .NET is a platform that cannot just be thrown away, because it already blends with todays problems and it is constantly being updated to support features required to develop todays application.

What does programmers want?
We want a unified programming platform, we would like to see all object oriented programming languages to have very easy integration and it is not wise to introduce new language to address specific problems. If you are a system integrator and an advance developer, you would realize that bringing platforms together is more fury. We would love to see a bridge across platforms, enough of this chaos.

Will Go Make it?
well, it is still experimental and we are all allowed to contribute to it because its completely free. Go still have a lot of hurdles to pass through like every other new language, it will survive most of this hurdle but if it is focused on the right direction. I personally does not see it as a replacement for any of the more matured and advanced languages.
Share your views. and Go for Go here.

Command architecture and dependency injection

On a recent project, we were mapping commands to user intentions, covering all aspects pertaining to the usage of the application from the p...