-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountable.min.js
2 lines (2 loc) · 3.21 KB
/
mountable.min.js
1
2
/* MounTable.js | Developed by Guilherme Augusto Madaleno <guimadaleno@me.com> | www.guimadaleno.com | (C) 2014 All rights reserved | Licensed under GNU GPL */
$.fn.mounTable=function(e,t){var n={debug:t&&t.noDebug?false:true,fn:{element:"",model:{"class":t&&t.model?t.model:".mountable-model",content:"",length:Object.keys($(this).find(t&&t.model?t.model:".mountable-model")).length,newLine:t&&t.addLine&&t.addLine.button?t.addLine.button:".mountable-new-line",origin:"",saved:""},table:this}};var r={init:function(){if(e&&e.length){if(!$(n.fn.table).length){if(n.debug){console.log("MounTable: Table "+n.fn.table.selector+" not found!")}}else{$.each(e,function(e,t){if(t){$.each(t,function(e,t){if($.type(t)==="string"||$.type(t)==="number"){n.fn.element=$(n.fn.table).find(n.fn.model.class).find('input[name="'+e+'[]"], input[name="'+e+'"], select[name="'+e+'[]"], select[name="'+e+'"]');if(n.fn.element.prop("tagName")=="INPUT"){if(n.fn.element.attr("type")=="text"){n.fn.element.attr("value",t)}else if(n.fn.element.attr("type")=="checkbox"){if(n.fn.element.length>1){$.each(n.fn.element,function(e,n){if(n.value==t){n.setAttribute("checked","checked")}else{n.removeAttribute("checked")}})}}}else if(n.fn.element.prop("tagName")=="SELECT"){n.fn.element.val(t);n.fn.element.children("option").attr("selected",false);n.fn.element.children('option[value="'+t+'"]').attr("selected","selected")}}else if($.type(t)==="array"){n.fn.element=$(n.fn.table).find(n.fn.model.class).find('select[name="'+e+'[]"], select[name="'+e+'"]');if(n.fn.element.prop("tagName")=="SELECT"){selectOpts=false;$.each(t,function(e,t){selectOpts+='<option value="'+t+'">'+t+"</option>"});n.fn.element.html(selectOpts);n.fn.element.attr("data-mountable-filled-up-selectbox","yes")}}});n.fn.model.dom=$(n.fn.table).find(n.fn.model.class);n.fn.model.content=$(n.fn.table).find(n.fn.model.class).html();$(n.fn.table).find(n.fn.model.class).parent().append("<tr>"+n.fn.model.content+"</tr>");$(n.fn.table).find(n.fn.model.class+" input").each(function(){if($(this).prop("type")=="text"){$(this).attr("value","")}else if($(this).prop("type")=="checkbox"){$(this).each(function(){$(this).attr("checked",false)})}});$(n.fn.table).find(n.fn.model.class+" select").each(function(){$(this).find("option").each(function(){$(this).removeAttr("selected")});$(this).attr("value","");if($(this).attr("data-mountable-filled-up-selectbox")=="yes"){$(this).html(" ")}})}})}n.fn.model.saved="<tr>"+$(n.fn.table).find(n.fn.model.class).html()+"</tr>";n.fn.model.origin=$(n.fn.table).find(n.fn.model.class).parent();if(n.debug){console.log("MounTable: content successfully mounted on "+n.fn.table.selector)}$(n.fn.model.newLine).off("click").on("click",function(){if(t.addLine.onClick&&$.type(t.addLine.onClick)==="function"){if(t.addLine.onClick($(n.fn.table).find(n.fn.model.class))===true){n.fn.model.origin.append(n.fn.model.saved);r.deleteLine()}}});r.deleteLine();$(n.fn.table).find(n.fn.model.class).remove()}},deleteLine:function(){if(t&&t.deleteLine&&t.deleteLine.button){$(t.deleteLine.button).off("click").on("click",function(){if(t.deleteLine.onClick&&$.type(t.deleteLine.onClick)==="function"){if(t.deleteLine.onClick()===true){$(this).parent().parent().remove()}}else{$(this).parent().parent().remove()}})}}};r.init()}