1function query_string (objeto) { 2 var qs = []; 3 for (var chave in objeto) { 4 qs.push(chave + '=' + encodeURIComponent(objeto[chave])); 5 } 6 return qs.join('&'); 7} 8 9function ajax (metodo, url, campos, callback, progresso) { 10 11 function query_string_elementos (elementos) { 12 var dic = {}; 13 for (var i = 0; i < elementos.length; i++) { 14 if (elementos[i].type != 'checkbox' && elementos[i].type != 'radio' || elementos[i].checked) { 15 dic[elementos[i].name || elementos[i].id] = elementos[i].value; 16 } 17 } 18 return query_string(dic); 19 } 20 21 var map_met = { 22 'GET': 'GET', 23 'get': 'GET', 24 'G': 'GET', 25 'g': 'GET', 26 'POST': 'POST', 27 'post': 'POST', 28 'P': 'POST', 29 'p': 'POST', 30 }; 31 if (typeof map_met[metodo] != 'undefined') { 32 metodo = map_met[metodo]; 33 } else { 34 metodo = 'GET'; 35 } 36 if (campos instanceof HTMLElement) { 37 if (campos.tagName == 'FORM') { 38 campos = query_string_elementos(campos.elements); 39 } else { 40 campos = query_string_elementos(campos.querySelectorAll('input, select, textarea')); 41 } 42 } else if (campos instanceof FormData) { 43 metodo = 'POST'; 44 } else if (typeof campos == 'string') { 45 //não faça nada, query_string supostamente está pronta. Exemplo: a=1&b=2. 46 } else if (campos instanceof Object) { 47 campos = query_string(campos); 48 } 49 50 var xhr = new XMLHttpRequest(); 51 xhr['qrystring'] = campos; 52 if (metodo == 'POST') { 53 xhr.open(metodo, url, true); 54 if (!(campos instanceof FormData)) { 55 xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded; charset=utf-8'); 56 } 57 } else { 58 if (campos) { 59 url = url + '?' + campos; 60 } 61 xhr.open(metodo, url, true); 62 xhr.setRequestHeader('content-type', 'text/html; charset=utf-8'); 63 } 64 xhr.setRequestHeader('Cache-Control', 'no-store, no-cache, must-revalidate'); 65 xhr.onload = function() { 66 if (callback) { 67 callback(this.responseText, this.status); 68 } 69 }; 70 if (progresso) { 71 if (xhr.upload) { 72 xhr.upload.onprogress = function(e) { 73 progresso(e); 74 }; 75 } 76 } 77 xhr.send(campos); 78 return xhr; 79} 80 81function delrow (linha) { 82 linha.parentNode.parentNode.deleteRow(linha.rowIndex); 83} 84 85function xy (elemento) { 86 var y = 0, x = 0; 87 if (elemento.offsetParent) { 88 do { 89 y += elemento.offsetTop; 90 x += elemento.offsetLeft; 91 } while (elemento = elemento.offsetParent); 92 } 93 return { 'x': x, 'y': y }; 94} 95 96function foreach (nodeList, fun) { 97 Array.prototype.forEach.call(nodeList, fun); 98} 99 100var historico = {}; 101historico.disabled = false; 102historico.hash = function () { 103 var hash = location.hash.replace('#', ''); 104 var arr_hash = hash.split('&'); 105 var obj_hash = {}; 106 var campo; 107 for (var i = 0; i < arr_hash.length; i++) { 108 if (arr_hash[i]) { 109 campo = arr_hash[i].split('='); 110 obj_hash[campo[0]] = decodeURIComponent(campo[1] || ''); 111 } 112 } 113 return obj_hash; 114}; 115historico.load = function (funcao) { 116 window.addEventListener('load', function () { 117 historico.onload = true; 118 historico.disabled = true; 119 funcao(historico.hash()); 120 historico.disabled = false; 121 }); 122 window.addEventListener('popstate', function () { 123 historico.onload = false; 124 historico.disabled = true; 125 funcao(historico.hash()); 126 historico.disabled = false; 127 }); 128}; 129historico.push = function (dados) { 130 var hash; 131 if (!this.disabled) { 132 var arr_hash = []; 133 for (var k in dados) { 134 arr_hash.push(k + '=' + encodeURIComponent(dados[k])); 135 } 136 if (arr_hash.length) { 137 hash = '#' + arr_hash.join('&'); 138 } else { 139 hash = ''; 140 } 141 if (hash != location.hash) { 142 history.pushState(null, null, location.pathname + location.search + hash); 143 } 144 } 145}; 146 147function is_touch () { 148 return 'ontouchstart' in window // works on most browsers 149 || navigator.maxTouchPoints; // works on IE10/11 and Surface 150} 151 152function is_visible (el) { 153 return (el.offsetWidth > 0 && el.offsetHeight > 0); 154} 155 156function is_inviewport (el) { 157 var rect = el.getBoundingClientRect(); 158 var A = rect.width * rect.height; 159 return A && rect.top >= 0 && rect.bottom <= window.innerHeight && rect.left >= 0 && rect.right <= window.innerWidth; 160} 161 162function is_scrollable (el) { 163 return el.scrollHeight > el.offsetHeight || el.scrollWidth > el.offsetWidth; 164} 165 166function is_data (strdata) { 167 168 function e_data (dia, mes, ano) { 169 dia = parseFloat(dia); 170 mes = parseFloat(mes) - 1; 171 ano = parseFloat(ano); 172 var data = new Date(ano, mes, dia, 12, 0, 0); 173 var d = data.getDate(); 174 var m = data.getMonth(); 175 var a = data.getFullYear(); 176 return (d == dia && m == mes && a == ano); 177 } 178 179 var partes = strdata.split('/'); 180 if (partes.length == 3) { 181 return e_data(partes[0], partes[1], partes[2]); 182 } 183 return false; 184} 185 186function cria_data (strdata) { 187 if (!is_data(strdata)) { 188 throw 'Formato de data inválido para criação.'; 189 } 190 var d = new Date(); 191 var partes = strdata.split('/'); 192 d.setDate(parseFloat(partes[0])); 193 d.setMonth(parseFloat(partes[1]) - 1); 194 d.setFullYear(parseFloat(partes[2])); 195 d.setHours(0); 196 d.setMinutes(0); 197 d.setSeconds(0); 198 d.setMilliseconds(0); 199 return d; 200} 201 202function date_diff (d1, d2) { 203 var diferenca = d1.getTime() - d2.getTime(); 204 return Math.floor(diferenca / 86400000); 205} 206 207function gera_token () { 208 return Math.random().toString(16).substr(2); 209} 210 211var QUERY_SELECTOR_CAMPOS = 'input[type=text], input[type=hidden], input[type=email], input[type=password], ' + 212 'input[type=checkbox], input[type=radio], select, textarea, input[type=tel], input[type=range]'; 213 214var api = {}; 215api.cep = function (cep, retorno) { 216 ajax(SITE + 'core/api/cep.php', { 'cep': cep }, function (json) { 217 retorno(JSON.parse(json)); 218 }); 219}; 220api.unidade_negocio = {}; 221api.unidade_negocio.pesquisa = function (t, retorno, opcoes) { 222 var g = { 223 t: t, 224 apenas_superiores: opcoes.apenas_superiores ? '1' : '0', 225 }; 226 if (opcoes) { 227 if (opcoes.tipos) { 228 g.tipos = opcoes.tipos; 229 } 230 } 231 ajax(SITE + 'core/api/unidade_negocio/pesquisa.php', g, function (json) { 232 retorno(JSON.parse(json)); 233 }); 234}; 235 236api.unidade_negocio.pesquisa_mcu = function (mcu, retorno) { 237 ajax(SITE + 'core/api/unidade_negocio/pesquisa_mcu.php', { 'mcu': mcu }, function (json) { 238 retorno(JSON.parse(json)); 239 }); 240}; 241var DataHora = { 242 hoje: function () { 243 var d = new Date(); 244 var dia = d.getDate(); 245 var mes = d.getMonth() + 1; 246 var ano = d.getFullYear(); 247 if (dia < 10) { 248 dia = '0' + dia; 249 } 250 if (mes < 10) { 251 mes = '0' + mes; 252 } 253 return dia + '/' + mes + '/' + ano; 254 }, 255}; 256var Numeros = { 257 formata: function (numero, casas, separador) { 258 var retorno, negativo = false; 259 if (numero === '' || numero === null) { 260 return null; 261 } 262 numero = parseFloat(numero); 263 if (numero < 0) { 264 negativo = true; 265 numero = Math.abs(numero); 266 } 267 if (isNaN(numero)) { 268 throw 'Número precisa ser real para ser formatado.'; 269 } 270 if (casas === undefined) { 271 casas = 2; 272 } 273 var partes = numero.toFixed(casas).split('.'); 274 var parteInteira = partes[0]; 275 var parteDecimal = partes[1]; 276 if (separador) { 277 var trincas = []; 278 while (parteInteira.length > 3) { 279 trincas.push(parteInteira.substr(parteInteira.length - 3)); 280 parteInteira = parteInteira.substring(0, parteInteira.length - 3); 281 } 282 if (parteInteira.length) { 283 trincas.push(parteInteira); 284 } 285 trincas.reverse(); 286 retorno = trincas.join('.'); 287 } else { 288 retorno = parteInteira; 289 } 290 if (casas) { 291 retorno += ',' + parteDecimal; 292 } 293 if (negativo) { 294 retorno = '-' + retorno; 295 } 296 return retorno; 297 }, 298 moeda: function (numero) { 299 return Numeros.formata(numero, 2, true); 300 }, 301 real: function (strnumero) { 302 strnumero = strnumero.toString(); 303 if (strnumero.indexOf('.') != -1 && strnumero.indexOf(',') != -1) { 304 strnumero = strnumero.replace(/\./g, '').replace(',', '.'); 305 } else if (strnumero.indexOf(',') != -1) { 306 strnumero = strnumero.replace(',', '.'); 307 } 308 if (isNaN(strnumero)) { 309 throw 'Número com formato incorreto para ser convertido em real.'; 310 } 311 return parseFloat(strnumero); 312 }, 313 eInteiro: function (numero) { 314 return !isNaN(numero) && 315 parseInt(Number(numero)) == numero && !isNaN(parseInt(numero, 10)); 316 }, 317 novesFora: function (numero) { 318 if (numero < 10) { 319 return numero; 320 } 321 var texto = numero.toString(); 322 var soma = 0; 323 for (var i = 0; i < texto.length; i++) { 324 soma += parseInt(texto.charAt(i), 10); 325 } 326 return Numeros.novesFora(soma);
327 }, 328}; 329 330var APICORREIOS = { 331 unidadeNegocio: { 332 orgaos: function (texto, callback) { 333 var campos = { 334 'o': texto, 335 }; 336 ajax('http://apps2.correiosnet.int/mservice/unidadenegocio/orgaos', campos, function (res) { 337 var r = JSON.parse(res); 338 if (!r[0]) { 339 return; 340 } 341 callback(r[0].Resultado); 342 }); 343 }, 344 }, 345}; 346 347function encodeValues(objeto) { 348 var qs = []; 349 for (let chave in objeto) { 350 qs.push(chave + '=' + encodeURIComponent(objeto[chave])); 351 } 352 return qs.join('&'); 353} 354 355//todo remover getget de todos os lugares e substituir por parseQuery 356function parseQuery (query) { 357 if (!query) { 358 query = location.search.substr(1); 359 if (!query) { 360 return {}; 361 } 362 } 363 var pares = query.split('&'), 364 par; 365 var values = {}; 366 for (var i = 0; i < pares.length; i++) { 367 par = pares[i].split('='); 368 values[par[0]] = decodeURIComponent(par[1]); 369 } 370 return values; 371}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.