(function($) { var gvh = gvh || {}; /* * isEmptyJSON() * FUNCIÓN: Indicará si un objeto JSON es vacío o no */ $.fn.isEmptyJSON = function(obj) { for(var i in obj) { return false; } return true; } /* * createTree() * FUNCIÓN: Crea un árbol jquery con el plugin jstree * PARAMS: idTree - del BOTÓN para el que se van a activar los campos * node - */ $.fn.createTree = function(options) { var defaults = $.extend({ idTree: '', node: '', multiple: 'false', url: '', claseM: '', destino: '', actualizaA: '', idForm: '' },options); gvh.showConsoleMsg('log',defaults.idTree); if (defaults.multiple == "true") multiple = true; else multiple = false; // Comprobar si existe la variable datosJSON raizJSON = datosJSON[defaults.claseM][defaults.panel]; var data = []; if (raizJSON['gvhJSON'] == 'false') { data = JSON.stringify($("#"+defaults.idForm).serialize()); url = defaults.url; } else { data = datosJSON; url = defaults.url+"&gvhcache=true&claseManejadora="+defaults.claseM+"&gvhpanel="+defaults.panel; } gvh.showConsoleMsg('log',data); $('#'+defaults.idTree).jstree({ "types" : { "default" : { "icon" : "glyphicon glyphicon-flash" }, "root" : { "icon" : "fa fa-chevron-down" }, "users" : { "icon" : "fa fa-users" }, "volley" : { "icon" : "fa fa-anchor" }, "ciclismo" : { "icon" : "fa fa-bicycle" }, "child" : { "icon" : "fa fa-child" } }, "core" : { "multiple": multiple, "checkbox": true, "ajax": true, "worker": true, "data": { "url": url, //"url": "phrame.php?action=TreeSelector__treeAjax", "data": function(data) { gvh.showConsoleMsg('log','data '+data); return data;//{ "id" : node.id, "nodo": node }; }, "beforeSend": function(node) { gvh.showConsoleMsg('log','beforeSend '+node); }, "error": function(node) { gvh.showConsoleMsg('log','error '+node); }, "success": function (node) { gvh.showConsoleMsg('log','success '+node); if ($(this).isEmptyJSON(node)) { gvh.showConsoleMsg('log',"NO DATA!"); gvh.showMessage('gvhlang_NoData','alert'); } }, "search": { "case_insensitive": true, "ajax" : { "url": "phrame.php?action=TreeSelector__treeAjax&value=search", 'dataType': 'json', 'type': 'POST', "data": function (node) { gvh.showConsoleMsg('log','search '+node.id) return { "id": node.id }; } } }, }, "themes": { 'name': 'proton', 'responsive': true }, }, "plugins" : [ "types", "wholerow", "search", "themes", "html_data", "checkbox", "sort", "ui" ] }); } })(jQuery); /************************************************************************************************************************ * BOTÓN TOOLTIP TREE SELECTOR *************************************************************************************************************************/ /* * BOTÓN TOOLTIP TREE SELECTOR * */ $('#gvhPage').offon( 'click', 'button[data-gvhFunction=gvhTreeSelector][data-gvhTreeAjax=false]', function(e) { gvh.showConsoleMsg('log','gvh_tree.js - button gvhTreeSelector ajax false'); let panel = $(this).attr('data-gvhPanelOn'); let estado = $('div[id=P_'+panel+']').attr('data-gvhestadopanel'); let idPanel = 'P_'+panel; let claseManejadora = $(this).attr('data-gvhClaseManejadora'); let multiple = false; if ($(this).attr('data-gvhMultiple') == "true") { multiple = true; } let idForm = $(this).closest('form').attr('id'); let campoDestino = $(this).attr('data-gvhActualizaA'); let iterActual = $(this).attr('data-gvhIterActual'); let sufijo = panel+'_'+iterActual; if (panel == 'fil') { let campoDestinoValue = campoDestino; let campoDestinoID = 'tree_'+campoDestinoValue; } else { if (estado == 'I') { let campoDestinoValue = 'ins___'+campoDestino+'___'+sufijo; let campoDestinoID = 'ins___tree_'+campoDestino+'___'+sufijo; } else { let campoDestinoValue = 'cam___'+campoDestino+'___'+sufijo; let campoDestinoID = 'cam___tree_'+campoDestino+'___'+sufijo; } } let id = $(this).attr('id'); let vId = id.split('_'); // Comprobar si existe la variable datosJSON let raizJSON = datosJSON[claseManejadora][panel]; let data = []; let destino = $(this).attr('data-gvhDestino'); if (raizJSON['gvhJSON'] == 'false') { let data = JSON.stringify($("#"+idForm).serialize()); let url = "phrame.php?action="+claseManejadora+"__"+destino; } else { let data = datosJSON; let url = "phrame.php?action="+claseManejadora+"__"+destino+"&gvhcache=true&claseManejadora="+claseManejadora+"&gvhpanel="+panel; } gvh.showConsoleMsg('log',data); let json = null; $.ajax({ 'async': false, 'type': "POST", 'url': url, 'data': data, 'dataType': "json", 'fail': function(json) { gvh.showConsoleMsg('log','fail'); }, 'success': function(json) { gvh.showConsoleMsg('log',json); if ($(this).isEmptyJSON(json)) { gvh.showConsoleMsg('log',"NO DATA!"); gvh.showMessage('gvhlang_NoData','alert'); $('#'+vId[1]).filter('[data-gvhTree=true]').treeview('remove'); } else { $("#treeDialog_"+vId[1]).dialog("open"); //gvh.showConsoleMsg('log','abrir el árbol '+vId[1]); $('#'+vId[1]).filter('[data-gvhTree=true]').treeview( { data: json, multiSelect: multiple, /*multiple*/ showCheckBox: true, showTags: true, highlightSelected: true, levels: 1, // árbol cerrado //nodeIcon: 'glyphicon glyphicon-user', onNodeUnselected: function (event, node) { $(':input[id='+campoDestinoValue+']').filter('[data-gvhpanelon='+panel+']').map(function() { $('input[id='+campoDestinoValue+']').val(''); $('input[id='+campoDestinoID+']').val(''); }); $("select[id="+campoDestinoValue+"] option[value="+node.id+"]").remove(); }, onNodeSelected: function(event, node) { $(':input[id='+campoDestinoValue+']').filter('[data-gvhpanelon='+panel+']').map(function() { $('input[id='+campoDestinoValue+']').val(node.text); $('input[id='+campoDestinoID+']').val(node.id); }); $('select[id='+campoDestinoValue+']').filter('[data-gvhpanelon='+panel+']') .append($("") .attr("value", node.id ) .attr("selected", true ) .text(node.text)); gvh.showConsoleMsg('log','vincular'); let idTree = $(this).attr('id'); //$(this).attr('data-gvhIDTree'); //panel = $('#'+idTree).attr('data-gvhPanelOn'); let idPanel = 'P_'+panel; let panelJSON = panel; if (panel == 'lisDetalle') panelJSON = 'lis'; else if (panel == 'ediDetalle') panelJSON = 'edi'; let campoDestino = $('#'+idTree).attr('data-gvhActualizaA'); let iterActual = $('#'+idTree).attr('data-gvhIterActual'); let sufijo = panel+'_'+iterActual; if (panel == 'fil') { let campoDestinoValue = campoDestino; let campoDestinoID = 'tree_'+campoDestinoValue; } else { let estadoPanel = $('#'+idPanel).attr('data-gvhestadopanel'); gvh.showConsoleMsg('log','estadoPanel: '+estadoPanel); if (estadoPanel == 'I') { let campoDestinoValue = 'ins___'+campoDestino+'___'+sufijo; let campoDestinoID = 'ins___tree_'+campoDestino+'___'+sufijo; } else { let campoDestinoValue = 'cam___'+campoDestino+'___'+sufijo; let campoDestinoID = 'cam___tree_'+campoDestino+'___'+sufijo; } } let actualizaA = $('#'+campoDestinoValue).attr('data-gvhActualizaA'); let formulario = $('#'+campoDestinoValue).closest('form'); // checkedNodes = $('#'+idTree).jstree("get_checked",null,true); //gvh.showConsoleMsg('log','chequeados '+checkedNodes); let allText = []; /*$.each(checkedNodes, function (i, nodeId) { var node = $('#'+idTree).jstree("get_node", nodeId); allText.push(node.text); }); gvh.showConsoleMsg('log','allText '+allText);*/ if ($('#'+campoDestinoValue).attr('data-gvhActualizaA')) { gvh.showConsoleMsg('log',' PARAMS: '); gvh.showConsoleMsg('log',' id - '+campoDestinoValue); gvh.showConsoleMsg('log',' actualizaA - '+actualizaA); gvh.showConsoleMsg('log',' panel - '+panel); gvh.showConsoleMsg('log',' claseM - '+claseManejadora); gvh.showConsoleMsg('log',' formulario - '+formulario); gvh.updateEvent($('#'+campoDestinoValue)); } }, search: function(event,node) { $('#input-check-node').val(), { ignoreCase: false, exactMatch: false } }, }); } } }); }); $('#gvhPage').offon( 'click', 'button[data-gvhFunction=gvhTreeSelector][data-gvhTreeAjax=true]', function(e) { let panel = $(this).attr('data-gvhPanelOn'); let idPanel = 'P_'+panel; let claseManejadora = $(this).attr('data-gvhClaseManejadora'); let destino = $(this).attr('data-gvhDestino'); let url = "phrame.php?action="+claseManejadora+"__"+destino; let campoDestino = $(this).attr('data-gvhActualizaA'); let multiple = $(this).attr('data-gvhMultiple'); let idForm = $(this).closest('form').attr('id'); let id = $(this).attr('id'); let vId = id.split('_'); let json = null; $("#treeDialog_"+vId[1]).dialog("open"); $(this).createTree({ idTree: vId[1], node: '#', multiple: multiple, url: url, claseM: claseManejadora, destino: destino, actualizaA: campoDestino, panel: panel, idForm: idForm }); }); $('#gvhPage').offon( 'changed.jstree', 'div[data-gvhTree=true]', function(e) { let panel = $(this).attr('data-gvhPanelOn'); let idPanel = 'P_'+panel; let panelJSON = panel; if (panel == 'lisDetalle') panelJSON = 'lis'; else if (panel == 'ediDetalle') panelJSON = 'edi'; let claseManejadora = $(this).attr('data-gvhClaseManejadora'); let action = $(this).closest('form').attr('action'); let idForm = $(this).closest('form').attr('id'); let campoDestino = $(this).attr('data-gvhActualizaA'); let iterActual = $(this).attr('data-gvhIterActual'); let sufijo = panel+'_'+iterActual; if (panel == 'fil') { campoDestinoValue = campoDestino; campoDestinoID = 'tree_'+campoDestinoValue; } else { estadoPanel = $('#'+idPanel).attr('data-gvhestadopanel'); if (estadoPanel == 'I') { campoDestinoValue = 'ins___'+campoDestino+'___'+sufijo; campoDestinoID = 'ins___tree_'+campoDestino+'___'+sufijo; } else { campoDestinoValue = 'cam___'+campoDestino+'___'+sufijo; campoDestinoID = 'cam___tree_'+campoDestino+'___'+sufijo; } } gvh.showConsoleMsg('log',"Nodo seleccionado:"); selectedNodes = data.selected; gvh.showConsoleMsg('log',data.selected); if (selectedNodes.length > 0) { $.each(selectedNodes, function( index, value ) { gvh.showConsoleMsg('log','1 - '+data.instance.get_selected(true)[index].text); $('#'+campoDestinoValue).val(data.instance.get_selected(true)[index].text); $('#'+campoDestinoID).val(data.instance.get_selected(true)[index].id); //gvh.showConsoleMsg('log','2 - '+data.instance.get_node(data.selected[index]).text); }); /*$.ajax({ data: datosJSON, type: "POST", datatype: "json", url: action+"&gvhcache=true&claseManejadora="+claseManejadora+"&gvhpanel="+panelJSON, beforeSend: function() { $(this).loading(); }, success: function(response) { window.location.href = decodeURIComponent(jQuery.parseJSON(response)); }, fail: function() { alert('fail'); }, complete: function( jqXHR, textStatus, errorThrown ) { $('div[id=loading]').hide(); } })*/ } else { gvh.showConsoleMsg('log','no hay seleccionados'); } }); var to = false; //$('#search_treeDialog_arbolAjax').keyup(function () { $('#gvhPage').offon( 'keyup', '#search_treeDialog_arbolAjax', function(e) { var v = $(this).val(); gvh.showConsoleMsg('log',v); $('#arbolAjax').jstree(true).search(v);//, true, false, false, '#'); gvh.showConsoleMsg('log','post'); //if(to) { clearTimeout(to); } /*to = setTimeout(function () { var v = $(this).val(); $('#arbolAjax').jstree(true).search(v); }, 250);*/ });