Sencha (ExtJS) Map Control

En esta semana se me metió la idea a la cabeza de hacer un control de ExtJS para manejar mapas. Por ejemplo, supongamos que tienes que hacer una aplicación donde te dan una imagen de un fraccionamiento y tienes que identificar todas las propiedades en dicha imagen y luego mostrar el mapa con una imagencilla que indica el estatus (si ya esta vendida, en renta, o lo que sea).

Google maps nos saca del apuro muchas veces, pero a veces necesitamos usar nuestras propias imagenes (mapas), es por eso que comenzé a desarrollar este controlillo.

El control en acción:



Antes de comenzar es necesario hacer pruebas de CSS para ver que atributos necesitamos para que el mapa aparezc adentro de un cuadrito (DIV), para esto es necesario poner un div (al que llamaré wrapper) y es necesario que tenga posición "relative" y overflow: hidden. Esto es para que la imagen no se aplaste dentro del wrapper. Despues, a la imagen le ponemos posición "absolute" y listo, ya tenemos una imagen encerrada dentro de un div igual que google maps. Lo que sigue es agregarle la funcionalidad drag a la imagen interna para poder moverla dentro del div. Lo que todos sabemos es que si ponemos un elemento del DOM en posicion absoluta, este ignorará la jerarquia del DOM y "flotará" por encima de los demás, sin embargo, si su padre tiene posición relativa (como el wrapper), entonces este quedará atrapado dentro de su padre.

Esta algo confuso, pero asi funciona y eso no lo sabia jojo.

Apenas llevo la primera etapa, ya funciona el "Drag" del mapa para moverlo y cuando das click pone una marker, el siguiente paso es guardar toda la info de las markers y serializarlas para poder enviarlas al server, tambien falta el setValues, para que puedas regresar las markers desde el server y mostrarlas en el mapa, pero bueno, esto me lo eche en una noche, espero terminarlo pronto y subirlo completo.

Pero bueno, el codigo del control es el siguiente:


Ext.ns('Ext.ux.ImageMap');

Ext.ux.ImageMap = Ext.extend(Ext.form.Field, {
initComponent : function(){
/*Apply modified Properties, Listeners dont work here*/
Ext.apply(this,{
autoCreate : {tag:'div', style:'position:relative; overflow:hidden; border:1px solid black;'}
});
this.markers = [];
/*Change Normal Events*/
Ext.ux.ImageMap.superclass.initComponent.apply(this, arguments);
}
,onRender : function (){
Ext.ux.ImageMap.superclass.onRender.apply(this, arguments);
var me = this;
/*Main Overflown wrapper*/
this.wrapper = this.el;
/*Create inner wrapper with image inside */
this.innerwrapper = this.wrapper.createChild({
tag : 'div'
,style : 'position:absolute;'
,children : [{
id : this.mapId = Ext.id() + 'map'
,tag : 'img'
,src : this.bgimage
}]
});


/*Add click event*/
this.innerwrapper.on({
click: function(evt){
if(!me.endDrag){

me.addMarker(evt.xy);
}else{
//Reste drag flag
me.endDrag = false;
}
}
})
/*Set Draggable Actions*/
this.dd = new Ext.dd.DD(this.innerwrapper, 'group');
Ext.apply(this.dd,{
endDrag : function(e){
/*Flag to avoid firing the click event when its a drag*/
me.endDrag = e.getTarget().id == me.mapId ? true : false;
}
});

}
,addMarker : function(p){
var m = this.innerwrapper.createChild({
tag : 'div'
,html :' '
,cls : this.markerImgClass
});
this.markers.push(m);
var ctXY = this.wrapper.getXY();
var imgXY = this.innerwrapper.getXY(); //absolute to frame

//Calculate position
var pos = [];
pos[0] = ctXY[0] - imgXY[0];
pos[1] = ctXY[1] - imgXY[1];
p[0] = pos[0] + (p[0] - ctXY[0])-8;
p[1] = pos[1] + (p[1] - ctXY[1])-8;
m.setLeftTop(p[0], p[1]);

console.log(this.markers);
}
});



Ext.onReady(function(){

/*To use the control just add it to a form or render it to a div*/
var form = new Ext.form.FormPanel({
renderTo :'container'
,title : 'Image Control'
,items : [

new Ext.ux.ImageMap({
height :200
,width :300
,hideLabel : true
,bgimage : '../assets/images/map.jpg' //map image you want to use
,markerImgClass : 'markerImgClass' //marker image
})
]

});
});



Jerry ;)

Dynamically loading any JS File on Demand (Including Google Maps api)

Developing with ExtJS is a pleasure, the best engineered js framework out there.
At work we decided to start using it to develop a BIG project and its all made with ExtJS and CodeIgniter for the server side code.

The main requirement is to make the application as fast and reliable as possible and even though we are no even at the middle of it, we already have like 25+ JS files containing a variety of extended components. We also need to use Googlemaps/map24/M$maps depending on the user configuration and so on.

We've tried compressing all of the source code and putting into one single js file, but the file size is about 500kb + Maps API + ExtJS Api + anything else that we will need later.

So, what to do now? Lazy Load to the rescue =)

For all of you who don't know what that means, is "Loading js files on demand" duh!.

I started looking everywhere and found many different options but any of those satisfied my needs.
Requirements:
  1. Avoid at all cost storing JS code in the database.
  2. Eval Function is Evil!
  3. Need to know when the js code has been loaded.
From all of the options reviewed, only the first 2 requirements where met.

So, I grab the best from all of the approaches reviewed and this is the result:


Ext.ux.lazyLoad is a Singleton, you can copy and paste this code into a js file and start using it as follows:


Ext.ux.lazyLoad.get('gmap', function(obj){
Alert("Google Maps Has Been Loaded");
//Your code goes here and will be executed after loading the js file
},this);


This means that every time we need to call a function that is tied to a class that has been dynamically loaded, we need to do it by proxy-ing using the get function from the lazyLoad object. This object keeps track of what js files has been loaded and if it is loaded already it only calls the callback function supplied by you using the scope that you send.


The lazyLoad Singleton

The function inside the singleton uses the defer function from extjs to check every 2 seconds if the js has been loaded by evaluating the "checkLoad" variable from the "objects" variable. In the mean time, you can put a mask on your components while waiting for the code to load, since this will only happend the first time.

This way, we are sure that the classes, functions and everything we need have been loaded before using em'.

This component is working like a charm on our system, so I hope it can be useful for anyone of my fellow ExtJS developers.

Any questions, critics or suggestions are very welcome =)

Happy Coding , Jerry

/**
* Ext.ux.lazyLoad
*
* @author Jerry Sosa (sosamv) Published with the permission of Near It Services (nearitservices.com)
* @version Ext.ux.lazyLoad.js
* @date 10. February 2001
*
* @license Ext.ux.lazyLoad is licensed under the terms of
* the Open Source LGPL 3.0 license. Commercial use is permitted to the extent
* that the code/component(s) do NOT become part of another Open Source or Commercially
* licensed development library or toolkit without explicit permission.
*
* License details: http://www.gnu.org/licenses/lgpl.html
*/


Ext.namespace('Ext.ux.lazyLoad');
/********************************************************************
*@name Ext.ux {Singleton}
* Load javascript files onDemand
**/
Ext.ux.lazyLoad = function() {

var debug = false;
var objects = {
gmap:{
loaded:false
,checkLoad : 'GMap2' /*Variable that must exists to confirm load*/
,url:'http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAZtXebGuEUyPUsb16TunzchQH1vzzQPdXCsLMkqVnPRtWvfSdoRToJrguncXU9Z0FgvbgReXeej9BDQ&sensor=false&async=2'
}
,ctrlPropertiesGrid:{
loaded:false
,checkLoad : 'Ext.ux.customControl'
,url:'/app/properties/Ext.ux.customControl.js'
}
//.....ADD HERE MORE FILES
}
return{
/*
* @name Ext.ux.lazyLoad.get
* Retrieve js files on demand if not loaded
* @params
* -jsKey {String}: Js File name to load
* -callback {function}: function to be executed after
* -scope {Object}: Object scope to run the callback function
**/
get : function(jsKey, callback, scope){
/*Return if is not a valid object*/
if(!objects[jsKey]) return;

/*Run thread*/
var waitCounter = 0;
this.thread(jsKey, callback, scope, waitCounter, 'loadScript' + Ext.id());
}
/*
*@name Ext.ux.lazyLoad.thread
*Reusable thread to check if js file has been loaded
*@params
* -jsKey{String}: js key from the objects array
* -callback{Function}: function to execute when file is loaded
* -waitCointer{Integer}: Current cycle number
* -id{String}: Id used to identify the script tag
**/
,thread : function(jsKey, callback, scope, waitCounter, id){

/*If its loaded*/
if(this.isLoaded(jsKey)){
callback.call(scope);
}else{
/*If script object hasnt been included, do it*/
if(!Ext.get(id)){
/*If hasnt been loaded*/
/*Prepare url*/
var url = objects[jsKey].url;

this.head = document.getElementsByTagName('head').item(0);
script = document.createElement('script');
script.src = url;
script.type = 'text/javascript';
script.id = id;
this.head.appendChild(script);
}
if(debug)Ext.get('testdiv').insertHtml('beforeEnd',jsKey + ' wait counter:'+ waitCounter+'
');
try{
var checkVar = eval(objects[jsKey].checkLoad);
objects[jsKey].loaded = true;
callback.call(scope);
this.clean(id);
return;
}catch(e){
if(waitCounter++ <10){
/*Will check 10 times every 2 seconds if the js has been loaded*/
this.thread.defer(2000, this, [jsKey, callback, scope, waitCounter,id]);
}else{
this.clean(id);
alert("Problem loading libraries");
}
}

}
}
/*
* @name Ext.ux.lazyLoad.clean
* Removes recently used script tag from dom
**/
,clean : function(id){
var scriptTag = document.getElementById(id);
if(scriptTag) this.head.removeChild(scriptTag);
}
/*
* @name Ext.ux.lazyLoad.isLoaded
* Getter function to retrieve status
**/
,isLoaded : function(jsKey){
return objects[jsKey].loaded;
}
}
}();


/*USAGE EXAMPLE*/
Ext.onReady(function() {

Ext.ux.lazyLoad.get('gmap', function(obj){
Alert("Google Maps Has Been Loaded");
//Your code goes here and will be executed after loading the js file
},this);

});