/* * ntg_frames_reformat * written by Hrodrigu * http://www.newtenberg.com * GPL (GPL-LICENSE.txt) licenses. * Built for jQuery library * http://jquery.com * $Id: ntg_frames_reformat.js 1667 2023-01-11 21:25:05Z hrodrigu $ * carga elementos desde moldes externos ocupando como insumo recuadros de articulo * extrae los datos relevantes y luego los formatea usando templates mustache que toman * conjuntos de cardinalidad variable definida en el propio template, reescribiendo completamente * el dom de los recuadros * https://engine.dt.gob.cl/pressroom/boxConfig.cgi?action=tabEditBox&cid=912&bid=17247 */ (function($){ $.fn.ntg_frames_reformat = function(options){ var params = []; for(var i=1; i{{titulo.cont}}{{/eidox.0}}"+ "{{#eidox.1}}
{{titulo.cont}}
{{/eidox.1}}" } ], "debug":false }; if (typeof(options) ==='object' || typeof(options) ==='undefined' ){ return this.each(function(){ var obj = {}; obj.dom = this; obj.opts = $.extend(defaults, options); obj.order = []; obj.sideMenu = []; obj.sideMenuCurrent; obj.sideMenuElements = {}; obj.current = obj.opts.select; obj.busy = false; obj.init = function(force){ if (typeof($(obj.dom).data("NTG_FRAMES_REFORMAT_INSTANCE")) != "undefined" && typeof(force) == "undefined"){ return; } //init plugin debug("init"); obj.data = []; obj.mes = { "01": { "short": "Ene", "long": "Enero" }, "02":{ "short": "Feb", "long": "Febrero" }, "03":{ "short": "Mar", "long": "Marzo" }, "04":{ "short": "Abr", "long": "Abril" }, "05":{ "short": "May", "long": "Mayo" }, "06":{ "short": "Jun", "long": "Junio" }, "07":{ "short": "Jul", "long": "Julio" }, "08":{ "short": "Ago", "long": "Agosto" }, "09":{ "short": "Sep", "long": "Septiembre" }, "10":{ "short": "Oct", "long": "Octubre" }, "11":{ "short": "Nov", "long": "Noviembre" }, "12":{ "short": "Dic", "long": "Diciembre" } }; var grupo = {eidox:[]}; var curr_template = 0; var num_eidox; if( obj.opts.templates.constructor == Array){ // se comienza el primer grupo con los items del primer template num_eidox = obj.opts.templates[0].items; }else if(obj.opts.templates.hasOwnProperty("items")){ // se fija el tamaño del grupo con los items del único template num_eidox = obj.opts.templates.items; }else{ // si no hay definidos items par ael template se ocupa 1 solo grupo num_eidox = $(obj.opts.frameSelector,obj.dom).length; } obj.backup = $(obj.opts.frameSelector,obj.dom).each(function(i, recuadro){ // se van agregando de a uno los frames a cada grupo var info = get_info(recuadro, i); debug("frame i="+i+" info="+JSON.stringify(info)); grupo.eidox.push(info); num_eidox--; if(num_eidox == 0 ){ obj.data.push(grupo); debug("curr_template ="+curr_template+"grupo "+ JSON.stringify(grupo)); grupo = {eidox:[]}; curr_template++; curr_template %= obj.opts.templates.length; // se define el siguiente grupo con los items requeridos por el nuevo template num_eidox = obj.opts.templates[curr_template % obj.opts.templates.length].items; } }).detach(); if(grupo.eidox.length > 0){ obj.data.push(grupo); } buildFrames(); //se guarda la instancia el propio nodo DOM $(obj.dom).data("NTG_FRAMES_REFORMAT_INSTANCE",obj); return obj.dom; }; obj.destroy = function(){ $(obj.newFrames).remove(); $(obj.dom).append(obj.backup); $(obj.dom).removeData("NTG_FRAMES_REFORMAT_INSTANCE"); return obj.dom; } function buildFrames(){ var frames = ""; if( obj.opts.templates.constructor == Array){ $.each(obj.data, function(i,grupo){ var template; if( obj.opts.templates[i % obj.opts.templates.length].hasOwnProperty("template") && obj.opts.templates[i % obj.opts.templates.length].template.length > 0 ){ template = get_template(obj.opts.templates[i % obj.opts.templates.length].template); }else{ template = get_template(obj.opts.templates[i % obj.opts.templates.length].templateId); } var output = Mustache.render(template,grupo); frames += output.replace(/^\s*/,""); }); }else{ // template único var template; if(typeof(obj.opts.templates) == "string"){ template = get_template(obj.opts.templates); var output = Mustache.render(template,obj.data[0]); frames += output.replace(/^\s*/,""); }else{ $.each(obj.data, function(i,grupo){ template = get_template( obj.opts.templates.template ); var output = Mustache.render(template,grupo); frames += output.replace(/^\s*/,""); }); } } obj.newFrames = $(frames); if(obj.opts.mode == "append"){ $(obj.dom).append(obj.newFrames); }else if(obj.opts.mode == "replaceWith"){ $(obj.dom).replaceWith(obj.newFrames); } } function get_template(t){ var template; if($("#"+t).length ==1 ){ if($("#"+t).get(0).nodeName.toLowerCase() == "textarea"){ template = $("#"+t).val() .replace(/[\r\n\f]/gm,"") .replace(/^\s*\s*$/,""); }else{ template = $("#"+t).html(); } }else{ template = t; } return template; } function get_info(recuadro,n){ var info = {}; var numero = n; info.n = n; $(recuadro).children().each(function(i, nodoHijo){ var estilo = $(nodoHijo).attr("class"); var a = $("a",nodoHijo); var test_pvalues = $("*[class*='pvid-']",nodoHijo).length > 0; if($(a).length == 0 && $(nodoHijo).get(0).nodeName.toLowerCase() == 'a'){ a = $(nodoHijo).clone(); } var img = $("img",nodoHijo); var sizekb = $(".sizekb", nodoHijo); if("undefined" != typeof(estilo) && estilo.length > 0){ estilo = estilo.trim(); var nuevo = {}; var nombre = estilo.split(" ").shift(); var cont = $("
").append($(nodoHijo).clone()); var test_fecha = estilo.match(/iso8601-(\d\d\d\d)([01]\d)([0123]\d)T(\d\d)(\d\d)(\d\d)(\d\d\d\d)/); if(! info.hasOwnProperty(nombre)){ info[nombre] = nuevo; }else{ if( info[nombre].constructor != Array ){ info[nombre] = Array(info[nombre] ); } info[nombre].push(nuevo); } if($(a).length > 0){ nuevo.url = $(a).attr("href"); } if($(img).length == 1){ nuevo.img_url = $(img).attr("src"); nuevo.img_alt = $(img).attr("alt"); nuevo.img_title = $(img).attr("title"); } if($(sizekb).length > 0){ nuevo.sizekb = $(sizekb).text(); } var idsEstilo = estilo + " " +$("[class]",nodoHijo).map(function(){ return $(this).attr("class"); }).get().join(" ").trim(); $.each(idsEstilo.split(/\ +/), function(i,val){ var test = val.match(/(aid|pvid|pv-pid|pnid|cid|binary|format)-([0-9]+|[a-z][a-z0-9_]*)/i); if(test != null){ nuevo[test[1]] = test[2]; if(test[1] == "format"){ nuevo["is_"+test[2]] = true; } } }); if(test_fecha != null){ nuevo.iso8601 = test_fecha[1]+ test_fecha[2]+ test_fecha[3]+ "T"+ test_fecha[4]+ test_fecha[5]+ test_fecha[6]+ test_fecha[7]; nuevo.year = test_fecha[1]; nuevo.month = test_fecha[2]; nuevo.shortMonth = obj.mes[test_fecha[2]].short; nuevo.longMonth = obj.mes[test_fecha[2]].long; nuevo.day = test_fecha[3]; nuevo.hour = test_fecha[4]; nuevo.min = test_fecha[5]; nuevo.sec = test_fecha[6]; nuevo.tz = test_fecha[7]; } nuevo.innerHtml = $(nodoHijo).html(); nuevo.cont = $(nodoHijo).text(); nuevo.html = $(cont).html(); info["i"] = i; if(test_pvalues){ nuevo["branch"] = []; $(".pv-branch", nodoHijo).each(function(j, branch){ var nuevo_branch = { "pvalues":[]}; var estilo_branch = $(branch).attr("class").split(/\ +/); nuevo_branch.primero = j == 0; $.each(estilo_branch, function(k,cl){ if(cl != "pv-branch"){ nuevo_branch[cl.split("-").shift()] = cl.split("-").pop(); } }); $("*[class*='pvid-']",branch).each(function(l, pval){ var nuevo_pval = {}; var a_pval = $("a", pval); var estilo_pval = $(pval).attr("class").trim().split(/\ +/); nuevo_pval.primero = l == 0; $.each(estilo_pval, function(n, cla){ var test_pval = cla.match(/(pvid|pv-pid|pnid|cid)-([0-9]+)/); if(test_pval != null){ nuevo_pval[test_pval[1]] = test_pval[2]; } }); if($(a).length > 0){ nuevo_pval.url = $(a).attr("href"); } nuevo_pval.cont = $(pval).text(); nuevo_pval.html = $(pval).get(0).outerHTML; nuevo_pval.innerHtml = $(pval).html(); nuevo_branch.pvalues.push(nuevo_pval); }); nuevo_branch.pvalues_rev = nuevo_branch.pvalues.concat().reverse(); nuevo_branch.pvalues_rev.map(function(el){ el.primero = !el.primero; }); nuevo["branch"].push(nuevo_branch); }); } if(numero == 0){ info.primero = true; } }else{ if($(a).length > 1){ info.a = []; $(a).each(function(j,link){ info.a.push({ url: $(link).attr("href"), text: $(link).text() }); }); }else if($(a).length ==1){ info.a = { url: $(a).attr("href"), text: $(a).text() } } if($(img).length > 1){ info.img = []; $(img).each(function(j,pic){ info.img.push({ url: $(pic).attr("src"), alt:$(pic).attr("alt"), title:$(pic).attr("title") }); }); }else if($(img).length == 1){ info.img = { url: $(img).attr("src"), alt:$(img).attr("alt"), title:$(img).attr("title") }; } if($(sizekb).length > 0){ info.sizekb = $(sizekb).text(); } } }); if("function" == typeof(obj.opts.postProcessFunc)){ info = obj.opts.postProcessFunc.call(obj,info, recuadro); } return info; } function debug(msg){ if(obj.opts.debug && window.console){ window.console.log("ntg_frames_reformat:"+ msg); } } obj.init(); }); } else if(typeof(options)==='string'){ var ret = this; this.each(function(){ var instancia = $(this).data("NTG_FRAMES_REFORMAT_INSTANCE"); if('undefined' != typeof(instancia) && 'function' === typeof(instancia[options])){ ret = instancia[options].apply(instancia,params); return false; } }); return ret; } else { return this; } }; })(jQuery);