package("components.custom");
/**
* Construye caja de seleción de dirección por vivienda
*
* @namesa
* @extends Listener
* @author Pablo Viojo
* @version 1
*/
svweb.components.custom.AddressBox = Listener.extend({
lastDir:null,
data:{
region:"",
comuna:"",
calle:"",
nmro:""
},
initialize:function(options){
if (!options){
options = {};
}
this.options = jQuery.extend (
{
id:(new Date()).getTime(),
content:"",
name:"",
descName:"",
closeOnOk:false,
closeBtn:null,
buttons:null
},
options
);
this.obj = this.options.obj;
this.initializeUI();
},
initializeUI:function(){
this.wnd = new svweb.components.DialogBox({
content:this.options.content,
id:this.options.id,
buttons:this.options.buttons,
closeOnOk:this.options.closeOnOk,
offset:this.options.offset
});
this.wnd.suscribe("ok",this.selectVivienda,this);
this.wnd.suscribe("open",this.onOpenWnd,this);
this.wnd.suscribe("close",this.onCloseWnd,this);
var html = '' +
'' +
'' +
'' +
'' +
'' +
'';
$(this.obj).html(html);
if (this.options.idenVivienda){
$(".iden_vivienda",$(this.obj)).val(this.options.idenVivienda);
$(".lbl_vivienda",$(this.obj)).html(this.options.lblVivienda);
$(".desc_direccion",$(this.obj)).val(this.options.lblVivienda);
$(".nodir", $(this.obj)).hide(0);
$(".dir", $(this.obj)).show(0);
} else {
$(".nodir", $(this.obj)).show(0);
$(".dir", $(this.obj)).hide(0);
}
$(".loading", $(this.wnd.body)).hide(0);
$(".comuna, .region, .calle, .nmro", $(this.wnd.body)).change(this.checkDir.closure(this));
$(".calle, .nmro", $(this.wnd.body)).keyup(this.checkDir.closure(this));
$(".region", $(this.wnd.body)).change(this.onChangeRegion.closure(this));
$(".viviendas", $(this.wnd.body)).dblclick(this.selectVivienda.closure(this));
$(".btn_selectAddress, .btn_changeAddress", $(this.obj)).click(this.open.closure(this));
},
onOpenWnd:function(){
this.fire("open",null);
},
onCloseWnd:function(){
this.fire("close",null);
},
selectVivienda:function(){
var viviendasLst = $(".viviendas", $(this.wnd.body));
var data = null;
if (viviendasLst.val()!=""){
data = {
id:viviendasLst.val(),
value:$(".viviendas option[@value="+viviendasLst.val()+"]", $(this.wnd.body)).html()
};
}
if ($(".msgUnaVivienda",$(this.wnd.body)).css("display") != "none" && $(".msgUnaVivienda span",$(this.wnd.body)).attr("rel")!=""){
data = {
id:$(".msgUnaVivienda span",$(this.wnd.body)).attr("rel"),
value:$(".msgUnaVivienda span",$(this.wnd.body)).html()
};
}
if (data){
this.close();
$(".nodir", $(this.obj)).hide();
$(".dir", $(this.obj)).show();
$(".dir .iden_vivienda", $(this.obj)).val(data.id);
$(".dir .desc_direccion", $(this.obj)).val(data.value);
$(".dir .lbl_vivienda", $(this.obj)).html(data.value);
this.data = jQuery.extend(
this.lastDir,
{
id:data.id
}
);
this.dataViviendas = this.lastViviendas;
this.fire("viviendaSelected",data);
}
},
open:function(){
this.update();
this.wnd.open();
},
close:function(){
this.wnd.close();
},
checkDir:function(){
var comuna = null;
if ($(".comuna", $(this.wnd.body)).val()){
comuna = $(".comuna", $(this.wnd.body)).val();
} else {
if ($(".comuna", $(this.wnd.body)).attr("onLoadValue")!=""){
comuna = $(".comuna", $(this.wnd.body)).attr("onLoadValue");
}
}
var dir = {
comuna:comuna,
descCalle:$(".calle", $(this.wnd.body)).val(),
nmroCalle:$(".nmro", $(this.wnd.body)).val()
}
var isComplete = function(o){
if (!o){
return false;
}
for(i in o){
if (!o[i]){
return false;
}
}
return true;
}
if (isComplete(dir)){
if (!equals(this.lastDir,dir)){
$(".cntViviendas",$(this.wnd.body)).hide(0);
$(".msgUnaVivienda",$(this.wnd.body)).hide(0);
$(".msgSinVivienda",$(this.wnd.body)).hide(0);
$(".viviendas",$(this.wnd.body)).empty();
if (this.pId){
window.clearTimeout(this.pId);
this.pId = false;
}
window.fnLoadData = (function(){
this.lastDir = dir;
$(".loading",$(this.wnd.body)).show(0);
$.getJSON("direcciones.html",jQuery.extend(this.lastDir,{deptoCasa:""}),(function(data){
$(".loading",$(this.wnd.body)).hide(0);
if (data.length>0) {
this.loadViviendas(data);
} else {
$(".cntViviendas",$(this.wnd.body)).hide(0);
$(".msgUnaVivienda",$(this.wnd.body)).hide(0);
$(".msgSinVivienda",$(this.wnd.body)).show(0);
}
}).closure(this));
}).closure(this);
this.pId = window.setTimeout("window.fnLoadData()",500);
} else {
if($(".cntViviendas",$(this.wnd.body)).css("display")=="none"){
if ($(".cntViviendas",$(this.wnd.body)).find("option").length>0) {
$(".cntViviendas",$(this.wnd.body)).show(0);
}
if (this.pId){
window.clearTimeout(this.pId);
this.pId = false;
}
}
}
} else {
$(".cntViviendas",$(this.wnd.body)).hide(0);
$(".msgUnaVivienda",$(this.wnd.body)).hide(0);
$(".msgSinVivienda",$(this.wnd.body)).hide(0);
}
},
update:function(){
$(".cntViviendas",$(this.wnd.body)).hide()
$(".region", $(this.wnd.body)).val(this.data.region);
$(".comuna", $(this.wnd.body)).attr("onLoadValue",this.data.comuna);
$(".calle", $(this.wnd.body)).val(this.data.calle);
$(".nmro", $(this.wnd.body)).val(this.data.nmro);
$(".viviendas",$(this.wnd.body)).attr("onLoadValue",this.data.id);
if (this.dataViviendas){
this.loadViviendas(this.dataViviendas);
}
this.loadComunas($(".region", $(this.wnd.body)).val());
this.lastDir = null;
},
loadViviendas:function(data){
$(".msgUnaVivienda",$(this.wnd.body)).hide(0);
$(".msgSinVivienda",$(this.wnd.body)).hide(0);
$(".viviendas",$(this.wnd.body)).empty();
for (i in data){
if (data[i] && data[i].value){
data[i].value = data[i].value.toLowerCase();
}
}
this.lastViviendas = data;
if (data.length==1){
$(".msgUnaVivienda span",$(this.wnd.body)).html(data[0].value);
$(".msgUnaVivienda span",$(this.wnd.body)).attr("rel",data[0].id);
$(".msgUnaVivienda",$(this.wnd.body)).show(0);
} else {
$(".viviendas",$(this.wnd.body)).empty();
loadSelect($(".viviendas",$(this.wnd.body)), data);
$(".cntViviendas",$(this.wnd.body)).show(0);
}
},
onChangeRegion:function(e){
var target = e.target || e.srcElement;
if ($(target).val()!=""){
this.loadComunas($(target).val());
}
},
loadComunas:function(region){
$.getJSON("comunas.html?region="+region,
(function(data){
loadSelect($(".comuna", $(this.wnd.body)), data)
}).closure(this)
);
}
});