(function($) { /* * 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); console.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; } console.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) { console.log('data '+data); return data;//{ "id" : node.id, "nodo": node }; }, "beforeSend": function(node) { console.log('beforeSend '+node); }, "error": function(node) { console.log('error '+node); }, "success": function (node) { console.log('success '+node); if ($(this).isEmptyJSON(node)) { console.log("NO DATA!"); $(this).showMessage('gvhlang_NoData','alert'); } }, "search": { "case_insensitive": true, "ajax" : { "url": "phrame.php?action=TreeSelector__treeAjax&value=search", 'dataType': 'json', 'type': 'POST', "data": function (node) { console.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 * */ /*function isEmptyJSON(obj) { for(var i in obj) { return false; } return true; }*/ $('button[data-gvhFunction=gvhTreeSelector]').filter('[data-gvhTreeAjax=false]').on({ click: function() { console.log('gvh_tree.js - button gvhTreeSelector ajax false'); panel = $(this).attr('data-gvhPanelOn'); estado = $('input[id*=est_'+panel+']').val(); estado = $('div[id=P_'+panel+']').attr('data-gvhestadopanel'); idPanel = 'P_'+panel; claseManejadora = $(this).attr('data-gvhClaseManejadora'); if ($(this).attr('data-gvhMultiple') == "true") multiple = true; else multiple = false; idForm = $(this).closest('form').attr('id'); campoDestino = $(this).attr('data-gvhActualizaA'); iterActual = $(this).attr('data-gvhIterActual'); sufijo = panel+'_'+iterActual; if (panel == 'fil') { campoDestinoValue = campoDestino; campoDestinoID = 'tree_'+campoDestinoValue; } else { if (estado == 'I') { campoDestinoValue = 'ins___'+campoDestino+'___'+sufijo; campoDestinoID = 'ins___tree_'+campoDestino+'___'+sufijo; } else { campoDestinoValue = 'cam___'+campoDestino+'___'+sufijo; campoDestinoID = 'cam___tree_'+campoDestino+'___'+sufijo; } } id = $(this).attr('id'); vId = id.split('_'); // Comprobar si existe la variable datosJSON raizJSON = datosJSON[claseManejadora][panel]; var data = []; destino = $(this).attr('data-gvhDestino'); if (raizJSON['gvhJSON'] == 'false') { data = JSON.stringify($("#"+idForm).serialize()); url = "phrame.php?action="+claseManejadora+"__"+destino; } else { data = datosJSON; url = "phrame.php?action="+claseManejadora+"__"+destino+"&gvhcache=true&claseManejadora="+claseManejadora+"&gvhpanel="+panel; } console.log(data); var json = null; $.ajax({ 'async': false, 'type': "POST", 'url': url, 'data': data, 'dataType': "json", 'fail': function(json) { console.log('fail'); }, 'success': function(json) { console.log(json); if ($(this).isEmptyJSON(json)) { console.log("NO DATA!"); $(this).showMessage('gvhlang_NoData','alert'); $('#'+vId[1]).filter('[data-gvhTree=true]').treeview('remove'); } else { $("#treeDialog_"+vId[1]).dialog("open"); //console.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)); console.log('vincular'); idTree = $(this).attr('id'); //$(this).attr('data-gvhIDTree'); //panel = $('#'+idTree).attr('data-gvhPanelOn'); idPanel = 'P_'+panel; panelJSON = panel; if (panel == 'lisDetalle') panelJSON = 'lis'; else if (panel == 'ediDetalle') panelJSON = 'edi'; campoDestino = $('#'+idTree).attr('data-gvhActualizaA'); iterActual = $('#'+idTree).attr('data-gvhIterActual'); sufijo = panel+'_'+iterActual; if (panel == 'fil') { campoDestinoValue = campoDestino; campoDestinoID = 'tree_'+campoDestinoValue; } else { estadoPanel = $('#'+idPanel).attr('data-gvhestadopanel'); console.log('estadoPanel: '+estadoPanel); if (estadoPanel == 'I') { campoDestinoValue = 'ins___'+campoDestino+'___'+sufijo; campoDestinoID = 'ins___tree_'+campoDestino+'___'+sufijo; } else { campoDestinoValue = 'cam___'+campoDestino+'___'+sufijo; campoDestinoID = 'cam___tree_'+campoDestino+'___'+sufijo; } } actualizaA = $('#'+campoDestinoValue).attr('data-gvhActualizaA'); formulario = $('#'+campoDestinoValue).closest('form'); // checkedNodes = $('#'+idTree).jstree("get_checked",null,true); //console.log('chequeados '+checkedNodes); var allText = []; /*$.each(checkedNodes, function (i, nodeId) { var node = $('#'+idTree).jstree("get_node", nodeId); allText.push(node.text); }); console.log('allText '+allText);*/ if ($('#'+campoDestinoValue).attr('data-gvhActualizaA')) { console.log(' PARAMS: '); console.log(' id - '+campoDestinoValue); console.log(' actualizaA - '+actualizaA); console.log(' panel - '+panel); console.log(' claseM - '+claseManejadora); console.log(' formulario - '+formulario); $('#'+campoDestinoValue).updateEvent({ id: campoDestinoValue, actualizaA: actualizaA, panel: panel, claseM: claseManejadora, formulario: formulario }); } }, search: function(event,node) { $('#input-check-node').val(), { ignoreCase: false, exactMatch: false } }, }); } } }); } }); $('button[data-gvhFunction=gvhTreeSelector]').filter('[data-gvhTreeAjax=true]').on({ click: function() { panel = $(this).attr('data-gvhPanelOn'); idPanel = 'P_'+panel; claseManejadora = $(this).attr('data-gvhClaseManejadora'); destino = $(this).attr('data-gvhDestino'); url = "phrame.php?action="+claseManejadora+"__"+destino; campoDestino = $(this).attr('data-gvhActualizaA'); multiple = $(this).attr('data-gvhMultiple'); idForm = $(this).closest('form').attr('id'); id = $(this).attr('id'); vId = id.split('_'); var 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 }); } }); $('div[data-gvhTree=true]').on("changed.jstree", function (e, data) { panel = $(this).attr('data-gvhPanelOn'); idPanel = 'P_'+panel; panelJSON = panel; if (panel == 'lisDetalle') panelJSON = 'lis'; else if (panel == 'ediDetalle') panelJSON = 'edi'; claseManejadora = $(this).attr('data-gvhClaseManejadora'); action = $(this).closest('form').attr('action'); idForm = $(this).closest('form').attr('id'); campoDestino = $(this).attr('data-gvhActualizaA'); iterActual = $(this).attr('data-gvhIterActual'); 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; } } console.log("Nodo seleccionado:"); selectedNodes = data.selected; console.log(data.selected); if (selectedNodes.length > 0) { $.each(selectedNodes, function( index, value ) { console.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); //console.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 { console.log('no hay seleccionados'); } }); var to = false; $('#search_treeDialog_arbolAjax').keyup(function () { var v = $(this).val(); console.log(v); $('#arbolAjax').jstree(true).search(v);//, true, false, false, '#'); console.log('post'); //if(to) { clearTimeout(to); } /*to = setTimeout(function () { var v = $(this).val(); $('#arbolAjax').jstree(true).search(v); }, 250);*/ });