1matriz.utils = { 2 "date": { 3 /** 4 * Formato de data e hora 5 */ 6 DATE_TIME_FORMAT: "DD/MM/YYYY HH:mm", 7 /** 8 * Valida o intervalo de duas datas 9 * @param {object} $inputDateTimeStart Data inicial 10 * @param {object} $inputDateTimeEnd Data final 11 * @returns {Boolean} Retorna se intervalo é válido 12 */ 13 validateInterval: function ($inputDateTimeStart, $inputDateTimeEnd) { 14 if ($inputDateTimeStart.val() !== "" && $inputDateTimeEnd.val() !== "") { 15 var momentStart = moment($inputDateTimeStart.val(), matriz.utils.date.DATE_TIME_FORMAT); 16 var momentEnd = moment($inputDateTimeEnd.val(), matriz.utils.date.DATE_TIME_FORMAT); 17 return momentStart.isBefore(momentEnd) || momentStart.isSame(momentEnd); 18 } else { 19 return true; 20 } 21 }, 22 format: function (date) { 23 return moment(date).format(matriz.utils.date.DATE_TIME_FORMAT); 24 } 25 }, 26 "dom": { 27 "attributes": function (element, pattern, removePattern) { 28 var attributes = {}; 29 for (var i = 0; i < element.attributes.length; i++) { 30 if (!pattern || pattern.test(element.attributes.item(i).nodeName)) { 31 var key = element.attributes.item(i).nodeName; 32 if (removePattern) { 33 key = key.replace(pattern, ""); 34 } 35 attributes[key] = element.attributes.item(i).value || element.attributes.item(i).nodeValue; 36 } 37 } 38 return attributes; 39 } 40 }, 41 file: { 42 extensao: function (file) { 43 return ((file.name).split('.').pop()).toLowerCase(); 44 }, 45 toTamanhoExtenso: function (filesize) { 46 var sizes = ["Bytes", "KBytes", "MBytes", "GBytes", "TBytes", "PBytes", "EBytes", "ZBytes", "YBytes"]; 47 for (var i = 0; i < sizes.length && filesize > 1024; i++) { 48 filesize = filesize / 1024; 49 } 50 var size = String(Math.ceil(filesize * 100)); 51 return size.substr(0, size.length - 2) + "," + size.substr(size.length - 2, 2) + " " + sizes[i]; 52 }, 53 toNomeCurto: function(filename) { 54 var indexOfPoint = filename.lastIndexOf("."); 55 var name = filename.substr(0, indexOfPoint); 56 var extension = filename.substr(indexOfPoint); 57 return name.substr(0, 32) + extension; 58 }, 59 readImageFromFile: function (file) { 60 var deferred = $.Deferred(); 61 62 var image = new Image(); 63 image.onload = function () { 64 try { 65 deferred.resolve(image); 66 } catch (error) { 67 deferred.reject(error); 68 } 69 }; 70 71 var reader = new mOxie.FileReader(); 72 reader.onloadend = function () { 73 image.src = reader.result; 74 }; 75 76 reader.readAsDataURL(file); 77 78 return deferred.promise(); 79 }, 80 readFileAsArrayBuffer: function(file) { 81 var deferred = $.Deferred(); 82 var reader = new FileReader(); 83 reader.onloadend = function (e) { 84 deferred.resolve(reader.result); 85 }; 86 reader.onerror = function (errorEvent) { 87 var error = new Error(); 88 error.errorEvent = errorEvent; 89 deferred.reject(error); 90 }; 91 try { 92 reader.readAsArrayBuffer(file); 93 } catch (error) { 94 deferred.reject(error); 95 } 96 return deferred.promise(); 97 }, 98 arrayBufferToBase64: function(arrayBuffer) { 99 var binary = ''; 100 var bytes = new Uint8Array(arrayBuffer); 101 var len = bytes.byteLength; 102 for (var i = 0; i < len; i++) { 103 binary += String.fromCharCode(bytes[ i ]); 104 } 105 return btoa(binary); 106 } 107 }, 108 "string": { 109 "consts": { 110 "PAD_RIGHT": "PAD_RIGHT", 111 "PAD_LEFT": "PAD_LEFT" 112 }, 113 "isInt": function (value) { 114 return /^-?[0-9]+$/.test(value); 115 }, 116 "pad": function (val, minlen, char, type) { 117 val = '' + val; 118 var padstring = (new Array(minlen + 1)).join(char); 119 if (val.length < minlen) { 120 if (!type || type === matriz.utils.string.consts.PAD_LEFT) { 121 return (padstring + val).substring((padstring + val).length - minlen); 122 } else if (type && type === matriz.utils.string.consts.PAD_RIGHT) { 123 return (val + padstring).substring(0, minlen); 124 } 125 } else { 126 return val; 127 } 128 }, 129 "toNonAccentCase": function (string) { 130 var alphabet = { 131 from: 'ÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃÃà áäâãèéëêìÃïîòóöôõùúüûñç', 132 to: 'AAAAAEEEEIIIIOOOOOUUUUNCaaaaaeeeeiiiiooooouuuunc' 133 }; 134 135 for (var i = 0; i < alphabet.from.length; i++) {
136 string = string.replace(new RegExp(alphabet.from.charAt(i), 'g'), alphabet.to.charAt(i)); 137 } 138 139 return string; 140 }, 141 "toCodigoExterno": function (string) { 142 return matriz.utils.string.toNonAccentCase(string) 143 .toLowerCase() 144 .replace(/[^a-z0-9]+/g, ""); 145 }, 146 "truncarTexto": function (texto, separador, maxlength) { 147 var textoTruncado = ""; 148 var afrases = texto.split(separador); 149 if (texto.substr(0, maxlength).indexOf(separador) !== -1) { 150 for (var i = 0; i < afrases.length; i++) { 151 if ((textoTruncado + afrases[i] + separador).length <= maxlength) { 152 textoTruncado = textoTruncado + afrases[i] + separador; 153 } else { 154 break; 155 } 156 } 157 return textoTruncado; 158 } else { 159 return null; 160 } 161 } 162 }, 163 "form": { 164 "serialize": function (form) { 165 var formData = $(form).serializeArray(); 166 167 for (var i in formData) { 168 if (formData[i].name.toLowerCase() === "conteudo[texto]") { 169 formData[i].value = btoa(unescape(encodeURIComponent(formData[i].value))); 170 break; 171 } 172 } 173 174 var csrfToken = this.csrfToken(); 175 if (csrfToken) { 176 formData.push({ 177 "name": "csrf-token", 178 "value": btoa(unescape(encodeURIComponent(csrfToken))) 179 }); 180 } 181 182 return formData; 183 }, 184 "serializeForm": function (form) { 185 var data = {}; 186 var formData = $(form).serializeArray(); 187 188 for (var key in formData) { 189 data[formData[key].name] = formData[key].value; 190 } 191 192 var csrfToken = this.csrfToken(); 193 if (csrfToken) { 194 data['csrf-token'] = csrfToken; 195 } 196 197 return data; 198 }, 199 "csrfToken" : function() { 200 if ("csrf" in matriz) { 201 return matriz.csrf.value; 202 } 203 return $("meta[name='csrf-token']").attr("content"); 204 }, 205 post: function (options) { 206 var deferred = $.Deferred(); 207 208 matriz.ui.loading.modal.show(); 209 210 var xhr = new mOxie.XMLHttpRequest(); 211 xhr.onload = function (e) { 212 if (parseInt(xhr.status) >= 200 && parseInt(xhr.status) < 300) { 213 deferred.resolve(xhr); 214 } else { 215 var error = new Error(xhr.responseText); 216 error.xhr = xhr; 217 deferred.reject(error); 218 } 219 }; 220 221 var formData = new mOxie.FormData(); 222 for (var key in options.data) { 223 formData.append(key, options.data[key]); 224 } 225 226 var data = matriz.utils.form.serializeForm(options.form); 227 for (var key in data) { 228 formData.append(key, data[key]); 229 } 230 231 xhr.open("POST", $(options.form).attr("action")); 232 xhr.setRequestHeader(matriz.csrf.key, matriz.csrf.value); 233 xhr.send(formData); 234 235 return deferred.promise(); 236 } 237 }, 238 ajax: function (options) { 239 if (options.modal !== true) { 240 matriz.ui.loading.modal.show(); 241 } 242 243 if (!options.headers) { 244 options.headers = {'X-Requested-With': 'XMLHttpRequest'}; 245 } 246 247 var promise = $.ajax(options).fail(function (xhr) { 248 matriz.ajax.messages.fail.default(xhr, xhr.statusText); 249 matriz.ui.loading.modal.hide(); 250 }); 251 252 // para fins de compatibilidade foi implementado um decorador no metodo done da promise do ajax do jquery 253 // o decorador executa a funcao de callback desejada pelo cliente e apos executa a a inicializacao do ui do matriz 'matriz.ui.init.common' 254 var super_done = promise.done; 255 promise.done = function(callback_done) { 256 super_done(function() { 257 callback_done.apply(this, arguments); 258 matriz.ui.init.common(); 259 }); 260 261 return promise; 262 }; 263 264 promise.success = promise.done; 265 266 return promise; 267 }, 268 history: { 269 isIllegalPopState: null, 270 location: null, 271 change: function (href, modal) { 272 if (modal !== true) { 273 if (!href.endsWith(location.pathname.concat(location.search))) { 274 this.pushState({}, href); 275 } 276 } 277 }, 278 pushState: function (state, href) { 279 state = state || {}; 280 281 if (history.pushState) { 282 state.referrer = location.href; 283 history.pushState(state, null, href); 284 } 285 286 matriz.utils.history.onPushState({ state: state }); 287 }, 288 replace: function(href) { 289 this.replaceState({}, href); 290 }, 291 replaceState: function(state, href) { 292 state = state || {}; 293 294 if (history.replaceState) { 295 state.referrer = location.href; 296 history.replaceState(state, null, href); 297 } 298 }, 299 onPopState: function () { 300 if (matriz.utils.history.isIllegalPopState) { 301 matriz.utils.history.isIllegalPopState = false; 302 } else { 303 if (matriz.utils.history.location !== matriz.utils.history.getCurrentLocation()) { 304 location.reload(); 305 } 306 307 matriz.utils.history.changeCurrentLocation(); 308 } 309 }, 310 onPushState: function() { 311 matriz.utils.history.changeCurrentLocation(); 312 }, 313 onHashChange: function() {}, 314 getCurrentLocation: function() { 315 return location.pathname.concat(location.search); 316 }, 317 changeCurrentLocation: function() { 318 matriz.utils.history.location = matriz.utils.history.getCurrentLocation(); 319 }, 320 onLoad: function () { 321 matriz.utils.history.isIllegalPopState = matriz.utils.navigator.isSafari(); 322 matriz.utils.history.changeCurrentLocation(); 323 } 324 }, 325 regex: { 326 escape: function (s) { 327 return s.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&'); 328 } 329 }, 330 url: { 331 param: function (name, url) { 332 if (!url) { 333 url = window.location.href; 334 } 335 var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(url); 336 if (!results) { 337 return undefined; 338 } 339 return results[1] || undefined; 340 }, 341 params: function () { 342 if (location.search.length > 1) { 343 var params = {}; 344 var searchFields = location.search.substr(1).split("&"); 345 for (var key in searchFields) { 346 var searchFieldsValue = searchFields[key].split("="); 347 var fieldName = decodeURIComponent(searchFieldsValue[0]); 348 if (fieldName.indexOf("[]") !== -1) { 349 fieldName = fieldName.split("[]")[0]; 350 if (typeof params[fieldName] === "undefined") { 351 params[fieldName] = []; 352 } 353 params[fieldName].push(decodeURIComponent(searchFieldsValue[1])); 354 } else { 355 params[fieldName] = decodeURIComponent(searchFieldsValue[1]); 356 } 357 } 358 return params; 359 } 360 }, 361 paramsToForm: function ($form) { 362 var params = matriz.utils.url.params(); 363 if (params) { 364 for (var paramName in params) { 365 if (Array.isArray(params[paramName])) { 366 var $input = $form.find(":input[name='" + paramName + "[]']"); 367 } else { 368 var $input = $form.find(":input[name='" + paramName + "']"); 369 } 370 if ($input) { 371 var paramValue = params[paramName]; 372 paramValue = paramValue.replace(/\+/g, " "); 373 $input.val(paramValue); 374 if ($input.hasClass("matriz-ui-combobox")) { 375 $input.selectpicker("refresh"); 376 } 377 } 378 } 379 } 380 }, 381 serializePagedListFilter: function($form) { 382 var $data = $form.find(":input").filter(function () { 383 if (($(this).is("select")) && !($(this).prop("multiple"))) { 384 if ($(this).find("option:selected").filter("[selected]").length) { 385 return false; 386 } 387 388 if (($(this).find("option:eq(0):selected").length) && !($(this).find("option[selected]").length)) { 389 return false; 390 } 391 392 return true; 393 } 394 return $(this).val() !== ''; 395 });
396 return $data.serialize(); 397 }, 398 search: function ($form) { 399 var locationHref = location.href.replace(location.search, "").replace("?", ""); 400 var locationSearch = this.serializePagedListFilter($form); 401 402 if (locationSearch) { 403 locationHref = locationHref.concat("?").concat(locationSearch); 404 } 405 406 matriz.utils.history.change(locationHref, $form.parents().filter(".modal").length > 0); 407 } 408 }, 409 "storage": { 410 "session": { 411 getItem: function (key) { 412 var item = sessionStorage.getItem(key); 413 if (item) { 414 item = JSON.parse(item); 415 } 416 return item; 417 }, 418 setItem: function (key, value) { 419 sessionStorage.setItem(key, JSON.stringify(value)); 420 }, 421 removeItem: function(key) { 422 sessionStorage.removeItem(key); 423 }, 424 getAttribute: function (key, attributeName) { 425 var item = matriz.utils.storage.session.getItem(key); 426 if (item !== null && item[attributeName]) { 427 item = item[attributeName]; 428 } 429 return item; 430 }, 431 setAttribute: function (key, attributeName, value) { 432 var item = matriz.utils.storage.session.getItem(key); 433 if (item === null) { 434 item = {}; 435 } 436 item[attributeName] = value; 437 matriz.utils.storage.session.setItem(key, item); 438 } 439 }, 440 "local": { 441 getItem: function (key) { 442 var item = localStorage.getItem(key); 443 if (item) { 444 item = JSON.parse(item); 445 } 446 return item; 447 }, 448 setItem: function (key, value) { 449 localStorage.setItem(key, JSON.stringify(value)); 450 }, 451 removeItem: function(key) { 452 localStorage.removeItem(key); 453 } 454 } 455 }, 456 navigator: { 457 isMobile: function () { 458 if (navigator.userAgent.match(/Android/i) 459 || navigator.userAgent.match(/webOS/i) 460 || navigator.userAgent.match(/iPhone/i) 461 || navigator.userAgent.match(/iPad/i) 462 || navigator.userAgent.match(/iPod/i) 463 || navigator.userAgent.match(/BlackBerry/i) 464 || navigator.userAgent.match(/Windows Phone/i)) { 465 return true; 466 } else { 467 return false; 468 } 469 }, 470 isAndroid: function () { 471 return navigator.userAgent.match(/Android/i); 472 }, 473 isSafari: function () { 474 return navigator.userAgent.match(/Safari/i) && !navigator.userAgent.match(/Chrome/i); 475 } 476 }, 477 script: { 478 load: function(url, callback) { 479 var script = document.createElement("script"); 480 script.type = "text/javascript"; 481 script.onload = callback; 482 document.getElementsByTagName("head")[0].appendChild(script); 483 script.src = url; 484 }, 485 append: function(url, document) { 486 if (document === undefined) { 487 document = window.document; 488 } 489 var script = document.createElement("script"); 490 script.async = false; 491 script.src = url; 492 document.querySelector("body").appendChild(script); 493 var deferred = $.Deferred(); 494 script.onload = function() { 495 deferred.resolve(); 496 }; 497 return deferred.promise(); 498 } 499 }, 500 hash : { 501 hexStringToSHA256String: function(string) { 502 return CryptoJS.SHA256(string).toString(CryptoJS.enc.Hex); 503 }, 504 arrayBufferToSHA256String: function(arrayBuffer) { 505 var wordArray = CryptoJS.lib.WordArray.create(arrayBuffer); 506 return CryptoJS.SHA256(wordArray).toString(CryptoJS.enc.Hex); 507 } 508 }, 509 internationalization: { 510 translate: function(term, context) { 511 context = context === undefined ? "default" : context; 512 if (this.dictionary[context] !== undefined && this.dictionary[context][term] !== undefined) { 513 return this.dictionary[context][term]; 514 } else { 515 return term; 516 } 517 }, 518 dictionary: {}
518 /*carregado na master.tpl*/ 519 } 520}; 521 522window.onload = matriz.utils.history.onLoad; 523window.onpopstate = matriz.utils.history.onPopState; 524window.onhashchange = matriz.utils.history.onHashChange;
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.