Created
February 17, 2016 01:53
-
-
Save mfyance/f68e790befa5d38664fa to your computer and use it in GitHub Desktop.
Ubigeo Peru
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
yOSON.AppCore.addModule("ubigeo_peru", function(Sb) { | |
var events, suscribeEvents, afterCathDom, beforeCathDom, catchDom, dom, initialize, st, ws, fn; | |
dom = {}; | |
ws = { | |
ubigeoPeru : PNSU.servicesPath + "ubigeo_peru", | |
paises : PNSU.servicesPath + "paises" | |
}; | |
st = { | |
paisNacimiento : "select[name='pPaisNacimiento']", | |
regNacimiento : "select[name='pRegNacimiento']", | |
provNacimiento : "select[name='pProvNacimiento']", | |
disNacimiento : "select[name='pDistNacimiento']", | |
regResidencia : "select[name='pRegResidencia']", | |
provResidencia : "select[name='pProvResidencia']", | |
disResidencia : "select[name='pDistResidencia']" | |
}; | |
beforeCathDom = function(){}; | |
catchDom = function(){ | |
dom.paisNacimiento = $(st.paisNacimiento) | |
dom.regNacimiento = $(st.regNacimiento) | |
dom.provNacimiento = $(st.provNacimiento) | |
dom.disNacimiento = $(st.disNacimiento) | |
dom.regResidencia = $(st.regResidencia) | |
dom.provResidencia = $(st.provResidencia) | |
dom.disResidencia = $(st.disResidencia) | |
}; | |
afterCathDom = function(){ | |
fn.cargarPaises() | |
fn.cargarRegion(dom.regResidencia) | |
}; | |
suscribeEvents = function () { | |
dom.paisNacimiento.on('change', events.detectarPaisNacimiento) | |
dom.regNacimiento.on('change', events.detectarRegionNacimiento) | |
dom.provNacimiento.on('change', events.detectarProvinciaNacimiento) | |
dom.regResidencia.on('change', events.detectarRegionResidencia) | |
dom.provResidencia.on('change', events.detectarProvinciaResidencia) | |
}; | |
events = { | |
detectarPaisNacimiento: function () { | |
pais = $(this).val() | |
if( pais === "PE" ){ | |
fn.cargarRegion(dom.regNacimiento) | |
} | |
}, | |
detectarRegionNacimiento: function () { | |
dom.provNacimiento.find('option:gt(0)').remove() | |
region = $(this).val() | |
fn.cargarProvincia(dom.provNacimiento, region) | |
}, | |
detectarProvinciaNacimiento: function () { | |
dom.disNacimiento.find('option:gt(0)').remove() | |
region = dom.regNacimiento.val() | |
provincia = $(this).val() | |
fn.cargarDistritos(dom.disNacimiento, region,provincia) | |
}, | |
detectarRegionResidencia: function () { | |
dom.provResidencia.find('option:gt(0)').remove() | |
region = $(this).val() | |
fn.cargarProvincia(dom.provResidencia, region) | |
}, | |
detectarProvinciaResidencia: function () { | |
dom.disResidencia.find('option:gt(0)').remove() | |
region = dom.regResidencia.val() | |
provincia = $(this).val() | |
fn.cargarDistritos(dom.disResidencia, region,provincia) | |
}, | |
}; | |
fn = { | |
cargarPaises: function () { | |
success = function(oResult) { | |
$.each(oResult.data, function (i,k) { | |
var html = '<option value="' + k.code + '">' + k.name + '</option>'; | |
dom.paisNacimiento.append(html); | |
}); | |
}; | |
Sb.trigger('controlLoadSelect', [dom.paisNacimiento , ws.paises , success]) | |
}, | |
cargarRegion: function (element) { | |
success = function(oResult) { | |
var regiones = $.grep(oResult.data, function (k,i) { | |
return (k.provincia === '00' && k.distrito === '00') | |
}) | |
$.each(regiones, function (i,k) { | |
var html = '<option value="' + k.departamento + '">' + k.nombre + '</option>'; | |
$(element).append(html); | |
}); | |
}; | |
Sb.trigger('controlLoadSelect', [$(element) , ws.ubigeoPeru , success]) | |
}, | |
cargarProvincia: function (element,region) { | |
success = function(oResult) { | |
var provincias = $.grep(oResult.data, function (k,i) { | |
return (k.departamento === region && k.provincia != 0 && k.distrito === '00') | |
}) | |
$.each(provincias, function (i,k) { | |
var html = '<option value="' + k.provincia + '">' + k.nombre + '</option>'; | |
$(element).append(html); | |
}); | |
}; | |
Sb.trigger('controlLoadSelect', [$(element) , ws.ubigeoPeru , success]) | |
}, | |
cargarDistritos: function (element, region,provincia) { | |
success = function(oResult) { | |
var distritos = $.grep(oResult.data, function (k,i) { | |
return (k.departamento === region && k.provincia === provincia && k.distrito !== 0) | |
}) | |
$.each(distritos, function (i,k) { | |
var html = '<option value="' + k.distrito + '">' + k.nombre + '</option>'; | |
$(element).append(html); | |
}); | |
}; | |
Sb.trigger('controlLoadSelect', [$(element) , ws.ubigeoPeru , success]) | |
} | |
}; | |
initialize = function(oP) { | |
$.extend(st, oP); | |
beforeCathDom(); | |
catchDom(); | |
afterCathDom(); | |
suscribeEvents(); | |
}; | |
return { | |
init: initialize | |
}; | |
}, [ | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment