-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.tablemanager.min.js
22 lines (20 loc) · 15.4 KB
/
jquery.tablemanager.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*!
* Tabledit v1.2.3 ()
* Copyright (c) 2015 Celso Marques
* @author Celso Marques
* @link https://github.com/markcell/jQuery-Tabledit
* Licensed under MIT (https://github.com/markcell/jQuery-Tabledit/blob/master/LICENSE)
*/
/**
* The modified version
* @version 1.2.6
* @author BluesatKV
* @link https://github.com/BluesatKV/jquery-tabledit
*/
/**
* jTableManager
* @version 1.0
* @author eafarooqi
* @link https://github.com/eafarooqi/jTableManager
*/
if("undefined"==typeof jQuery)throw new Error("jTableManager requires jQuery library.");!function(t){"use strict";var e="_TableditData",n={init:function(n){function a(t){var e={"&":"&","<":"<",">":">",'"':"","'":"","/":"/","`":"`","=":"="};return String(t).replace(/[&<>"'`=\/]/g,function(t){return e[t]})}function i(e){function n(t){var e=null;if(document.cookie&&""!=document.cookie)for(var n=document.cookie.split(";"),a=0;a<n.length;a++){var i=jQuery.trim(n[a]);if(i.substring(0,t.length+1)==t+"="){e=decodeURIComponent(i.substring(t.length+1));break}}return e}function a(t){return/^(GET|HEAD|OPTIONS|TRACE)$/.test(t)}var i,o,u=s.find(".tabledit-input").serialize();return u?(u+="&action="+e,o=l.onAjax(e,u),o===!1?!1:(l.method=l[e+"method"],t.ajaxSetup({beforeSend:function(t,e){a(e.type)||this.crossDomain||t.setRequestHeader("X-CSRFToken",n("csrftoken"))}}),i=t.ajax({url:l.url,type:l.method,data:u,dataType:"json"}),i.done(function(t,n,a){"success"==n?(e===l.buttons.edit.action&&(d.removeClass(l.dangerClass).addClass(l.successClass),setTimeout(function(){s.find("tr."+l.successClass).removeClass(l.successClass)},1400)),l.onSuccess(t,n,a)):l.onFail(n)}),i.fail(function(t,n,a){e===l.buttons["delete"].action?(r.removeClass(l.mutedClass).addClass(l.dangerClass),r.find(".tabledit-toolbar button").attr("disabled",!1),r.find(".tabledit-toolbar .tabledit-restore-button").hide()):e===l.buttons.edit.action&&d.addClass(l.dangerClass),l.onFail(t,n,a),console.log("jTableManager Ajax fail => "+n+" : "+a)}),i.always(function(){l.onAlways()}),i)):!1}if(!this.is("table"))throw new Error("jTableManager only works when applied to a table.");var s=this,o=function(t){return void 0!==t||null!==t||""!==t};if(!o(n.columns))return console.log("jTableManager Jquery Plugin not initialize. Set required parameters."),this;o(n.lang)&&n.lang in s.jTableManager.langs?n.lang=n.lang.toLowerCase():n.lang=s.jTableManager.defaults.lang;var l=t.extend({},s.jTableManager.defaults,n);t(this).data(e,t.extend({},s.jTableManager.defaults,n||{}));var d="undefined",r="undefined",u="undefined";if(l.editButton&&l.buttons.edit.html)var b=l.buttons.edit.html;else var b=s.jTableManager.langs[l.lang].btn_edit;if(l.deleteButton&&l.buttons["delete"].html)var c=l.buttons["delete"].html;else var c=s.jTableManager.langs[l.lang].btn_delete;if(l.confirmButton&&l.buttons.confirm.html)var f=l.buttons.confirm.html;else var f=s.jTableManager.langs[l.lang].btn_confirm;if(l.saveButton&&l.buttons.save.html)var p=l.buttons.save.html;else var p=s.jTableManager.langs[l.lang].btn_save;if(l.restoreButton&&l.buttons.restore.html)var h=l.buttons.restore.html;else var h=s.jTableManager.langs[l.lang].btn_restore;n.debug&&console.log("jTableManager Init -> Element:",s),n.debug&&console.log("jTableManager Init -> dataPrefix:",e),n.debug&&console.log("jTableManager Init -> Settings: ",l),n.debug&&console.log("jTableManager Init: -----------------------------------");var m={columns:{identifier:function(){l.hideIdentifier&&s.find("th:nth-child("+parseInt(l.columns.identifier[0])+"1), tbody td:nth-child("+parseInt(l.columns.identifier[0])+"1)").hide();var e=s.find('tbody tr:not(".'+l.noEditClass+'") td:not(".'+l.noEditClass+'")').filter(":nth-child("+(parseInt(l.columns.identifier[0])+1)+")");e.each(function(){var e=t.trim(t(this).text().replace(/^\s+|\s+$/g,"")),e=l.escapehtml?a(e):e,n='<span class="tabledit-span tabledit-identifier">'+e+"</span>",i='<input class="tabledit-input tabledit-identifier" type="hidden" name="'+l.columns.identifier[1]+'" value="'+e+'" disabled>';t(this).html(n+i),t(this).parent("tr").attr(l.rowIdentifier,t.trim(t(this).text()))})},editable:function(){for(var e=0;e<l.columns.editable.length;e++){var n=s.find('tbody tr:not(".'+l.noEditClass+'") td:not(".'+l.noEditClass+'")').filter(":nth-child("+(parseInt(l.columns.editable[e][0])+1)+")");n.each(function(){var n=t.trim(t(this).text().replace(/(^\s+|\s+$)/g,"")),n=l.escapehtml?a(n):n;l.editButton||t(this).css("cursor","pointer");var i,o='<span class="tabledit-span">'+n+"</span>",d=["input","hidden","number","select","textarea"],r=["rows","cols","maxlength","wrap"],u=l.columns.editable[e][2];switch(-1==t.inArray(u,d)&&(u="input"),u){case"input":i='<input class="tabledit-input '+l.inputClass+'" type="text" name="'+l.columns.editable[e][1]+'" value="'+n+'" style="display: none;" disabled>';break;case"hidden":i='<input class="tabledit-input '+l.inputClass+'" type="hidden" name="'+l.columns.editable[e][1]+'" value="'+n+'" style="display: none;" disabled>';break;case"number":i='<input class="tabledit-input '+l.inputClass+'" type="number" name="'+l.columns.editable[e][1]+'" value="'+n+'" style="display: none;" disabled>';break;case"select":"undefined"!=typeof l.columns.editable[e][3]&&(i='<select class="tabledit-input '+l.inputClass+'" name="'+l.columns.editable[e][1]+'" style="display: none;" disabled>',t.each(t.parseJSON(l.columns.editable[e][3]),function(e,a){a=t.trim(a),i+=n===a?'<option value="'+e+'" selected>'+a+"</option>":'<option value="'+e+'">'+a+"</option>"}),i+="</select>");break;case"textarea":i="<textarea ",t.each(t.parseJSON(l.columns.editable[e][3]),function(e,n){i+=-1!=t.inArray(e,r)?e+'="'+n+'" ':""}),i+=' class="tabledit-input '+l.inputClass+'" name="'+l.columns.editable[e][1]+'" style="display: none;" disabled>'+n+'</textarea><span class="count_" style="display: none;"><span class="countno_"></span> '+s.jTableManager.langs[l.lang].txt_remain+"</span>"}t(this).html(o+i),t(this).addClass("tabledit-view-mode")})}},toolbar:function(){if(l.editButton||l.deleteButton){var t="",e="",n="",a="",i="";0===s.find("th.tabledit-toolbar-column").length&&s.find("tr:first").append('<th class="tabledit-toolbar-column">'+s.jTableManager.langs[l.lang].txt_action+"</th>"),l.editButton&&(t='<button type="button" class="tabledit-edit-button '+l.buttons.edit["class"]+'" style="float: none;">'+b+"</button>"),l.deleteButton&&(e='<button type="button" class="tabledit-delete-button '+l.buttons["delete"]["class"]+'" style="float: none;">'+c+"</button>",i='<button type="button" class="tabledit-confirm-button '+l.buttons.confirm["class"]+'" style="display: none; float: none;">'+f+"</button>"),l.editButton&&l.saveButton&&(n='<button type="button" class="tabledit-save-button '+l.buttons.save["class"]+'" style="display: none; float: none;">'+p+"</button>"),l.deleteButton&&l.restoreButton&&(a='<button type="button" class="tabledit-restore-button '+l.buttons.restore["class"]+'" style="display: none; float: none;">'+h+"</button>");var o='<div class="tabledit-toolbar '+l.toolbarClass+'" style="text-align: left;">\n <div class="'+l.groupClass+'" style="float: none;">'+t+e+"</div>\n "+n+"\n "+i+"\n "+a+"\n </div></div>";s.find('tbody > tr:not(".'+l.noEditClass+'")').append('<td class="toolbar" style="white-space: nowrap; width: 1%;">'+o+"</td>"),s.find("tbody > tr."+l.noEditClass).append('<td class="toolbar" style="white-space: nowrap; width: 1%;"></td>')}}}},v={view:function(e){var n=t(e).parent("tr");t(e).parent("tr").find(".tabledit-input.tabledit-identifier").prop("disabled",!0),t(e).find(".tabledit-input").blur().hide().prop("disabled",!0),t(e).find(".count_").hide(),t(e).find(".tabledit-span").show(),t(e).addClass("tabledit-view-mode").removeClass("tabledit-edit-mode"),l.editButton&&(n.find("button.tabledit-save-button").hide(),n.find("button.tabledit-edit-button").removeClass("active").blur())},edit:function(e){y.reset(e);var n=t(e).parent("tr");n.find(".tabledit-input.tabledit-identifier").prop("disabled",!1),t(e).find(".tabledit-span").hide();var a=t(e).find(".tabledit-input");a.prop("disabled",!1).show(),t(e).find("textarea").css({resize:"none"}),t(e).find(".count_").css({color:"#CCC",display:"block","float":"right"}).show(),t(e).find(".countno_").html("..."),l.autoFocus&&(a.is("select")&&l.disableSelectAutoFocus||(a.focus(),1==l.textSelection&&a.select(),"end"==l.cursorPosition&&0==l.textSelection&&a.putCursorAtEnd().on("focus",function(){a.putCursorAtEnd()}))),t(e).addClass("tabledit-edit-mode").removeClass("tabledit-view-mode"),l.editButton&&(n.find("button.tabledit-edit-button").addClass("active"),n.find("button.tabledit-save-button").show())}},g={reset:function(e){t(e).each(function(){var e=t(this).find(".tabledit-input"),n=t.trim(t(this).find(".tabledit-span").text());e.is("select")?e.find("option").filter(function(){return t.trim(t(this).text())===n}).attr("selected",!0):e.val(n),v.view(this)})},submit:function(e){var n=i(l.buttons.edit.action);n!==!1&&(t(e).each(function(){var e=t(this).find(".tabledit-input");e.is("select")?t(this).find(".tabledit-span").text(t.trim(e.find("option:selected").text())):t(this).find(".tabledit-span").text(e.val()),v.view(this)}),d=t(e).parent("tr"))}},y={reset:function(t){s.find(".tabledit-confirm-button").hide(),s.find(".tabledit-delete-button").removeClass("active").blur()},submit:function(e){y.reset(e),t(e).parent("tr").find("input.tabledit-identifier").attr("disabled",!1);var n=i(l.buttons["delete"].action);t(e).parents("tr").find("input.tabledit-identifier").attr("disabled",!0),n!==!1&&(t(e).parent("tr").addClass("tabledit-deleted-row"),t(e).parent("tr").addClass(l.mutedClass).find(".tabledit-toolbar button:not(.tabledit-restore-button)").attr("disabled",!0),t(e).find(".tabledit-restore-button").show(),r=t(e).parent("tr"),1==l.hideDeletedRow&&0==l.restoreButton&&r.hide("slow"))},confirm:function(e){s.find("td.tabledit-edit-mode").each(function(){g.reset(this)}),t(e).find(".tabledit-delete-button").addClass("active"),t(e).find(".tabledit-confirm-button").show()},restore:function(e){t(e).parent("tr").find("input.tabledit-identifier").attr("disabled",!1);var n=i(l.buttons.restore.action);t(e).parents("tr").find("input.tabledit-identifier").attr("disabled",!0),n!==!1&&(t(e).parent("tr").removeClass("tabledit-deleted-row"),t(e).parent("tr").removeClass(l.mutedClass).find(".tabledit-toolbar button").attr("disabled",!1),t(e).find(".tabledit-restore-button").hide(),u=t(e).parent("tr"))}};return m.columns.identifier(),m.columns.editable(),m.columns.toolbar(),l.onDraw(),l.deleteButton&&(s.on("click","button.tabledit-delete-button",function(e){if(e.handled!==!0){e.preventDefault();var n=t(this).hasClass("active"),a=t(this).parents("td");y.reset(a),n||y.confirm(a),e.handled=!0}}),s.on("click","button.tabledit-confirm-button",function(e){if(e.handled!==!0){e.preventDefault();var n=t(this).parents("td");y.submit(n),e.handled=!0}})),l.restoreButton&&s.on("click","button.tabledit-restore-button",function(e){e.handled!==!0&&(e.preventDefault(),y.restore(t(this).parents("td")),e.handled=!0)}),l.editButton?(s.on("click","button.tabledit-edit-button",function(e){if(e.handled!==!0){e.preventDefault();var n=t(this),a=n.hasClass("active");g.reset(s.find("td.tabledit-edit-mode")),a||t(n.parents("tr").find("td.tabledit-view-mode").get().reverse()).each(function(){v.edit(this)}),e.handled=!0}}),s.on("click","button.tabledit-save-button",function(e){e.handled!==!0&&(e.preventDefault(),g.submit(t(this).parents("tr").find("td.tabledit-edit-mode")),e.handled=!0)})):(s.on(l.eventType,"tr:not(.tabledit-deleted-row) td.tabledit-view-mode",function(t){t.handled!==!0&&(t.preventDefault(),g.reset(s.find("td.tabledit-edit-mode")),v.edit(this),t.handled=!0)}),s.on("change","select.tabledit-input:visible",function(e){e.handled!==!0&&(g.submit(t(this).parent("td")),e.handled=!0)}),t(document).on("click",function(t){var e=s.find(".tabledit-edit-mode");e.is(t.target)||0!==e.has(t.target).length||g.reset(s.find(".tabledit-input:visible").parent("td"))})),s.on("keypress, keydown",function(t){var e=s.find(".tabledit-input:visible"),n=s.find(".tabledit-confirm-button");if(e.length>0)var a=e.parents("td");else{if(!(n.length>0))return;var a=n.parents("td")}switch(t.keyCode){case 9:l.editButton||(g.submit(a),v.edit(a.closest("td").next()));break;case 13:t.preventDefault(),g.submit(a);break;case 27:g.reset(a),y.reset(a);break;case 40:if(!l.editButton&&1==l.allowArrowKeyMovement){var i=a.index(),o=a.parent("tr").next(),d=o.find("td").eq(i);g.submit(a),v.edit(d)}break;case 38:if(!l.editButton&&1==l.allowArrowKeyMovement){var i=a.index(),r=a.parent("tr").prev(),u=r.find("td").eq(i);g.submit(a),v.edit(u)}}}),t("textarea").on("change keyup keydown",function(){var e=t(this).val().length,n=t(this).attr("maxlength"),e=n-e;t(this).parent().find(".countno_").text(e)}),this},destroy:function(e){var n=this;e.debug&&console.log("jTableManager Destroy -> Element:",t(this)),n.find(".tabledit-toolbar-column").remove(),n.find(".toolbar").remove()},update:function(a){var i=t(this).data(e);if(!i)return void console.log("jTableManager Update -> Data: jTableManager must be initialized before setting options");var a=t.extend(!0,i,a);a.debug&&console.log("jTableManager Update -> Element:",t(this)),a.debug&&console.log("jTableManager Update -> Settings: ",a);var s=t.parseJSON(JSON.stringify(a,["debug"]));n.destroy.apply(this,[s]),n.init.apply(this,[a])}};jQuery.fn.putCursorAtEnd=function(){return this.each(function(){var e=t(this),n=this;if(e.is(":focus")||e.focus(),n.setSelectionRange){var a=2*e.val().length;setTimeout(function(){n.setSelectionRange(a,a)},1)}else e.val(e.val());this.scrollTop=999999})},t.fn.jTableManager=function(e){return n[e]?n[e].apply(this,Array.prototype.slice.call(arguments,1)):"object"!=typeof e&&e?void t.error("Method "+e+" does not exist on jQuery.jTableManager"):n.init.apply(this,arguments)},t.fn.jTableManager.defaults={url:window.location.href,editmethod:"post",deletemethod:"post",inputClass:"form-control input-sm",toolbarClass:"btn-toolbar",groupClass:"btn-group btn-group-sm",dangerClass:"danger",successClass:"success",mutedClass:"text-muted",noEditClass:"noedit",eventType:"click",rowIdentifier:"id",hideIdentifier:!1,autoFocus:!0,disableSelectAutoFocus:!1,cursorPosition:"start",hideDeletedRow:!1,textSelection:!1,allowArrowKeyMovement:!0,lang:"en",debug:!1,escapehtml:!0,editButton:!0,deleteButton:!0,saveButton:!0,restoreButton:!0,buttons:{edit:{"class":"btn btn-sm btn-default",html:'<span class="glyphicon glyphicon-pencil"></span>',action:"edit"},"delete":{"class":"btn btn-sm btn-default",html:'<span class="glyphicon glyphicon-trash"></span>',action:"delete"},save:{"class":"btn btn-sm btn-success",html:""},restore:{"class":"btn btn-sm btn-warning",html:"Restore",action:"restore"},confirm:{"class":"btn btn-sm btn-danger",html:"Confirm"}},onDraw:function(){},onSuccess:function(){},onFail:function(){},onAlways:function(){},onAjax:function(){}},t.fn.jTableManager.langs={en:{btn_edit:"Edit",btn_delete:"Delete",btn_confirm:"Confirm",btn_save:"Save",btn_restore:"Restore",txt_action:"Actions",txt_remain:"characters remaining"},de:{btn_edit:"Editieren",btn_delete:"Löschen",btn_confirm:"Bestätigen",btn_save:"Speichern",btn_restore:"Inhalt",txt_action:"Aktion",txt_remain:""},fr:{btn_edit:"Éditer",btn_delete:"Supprimer",btn_confirm:"Confirmer",btn_save:"Sauver",btn_restore:"Restaurer",txt_action:"Mode",txt_remain:""},cz:{btn_edit:"Editovat",btn_delete:"Odstranit",btn_confirm:"Potvrdit",btn_save:"Uložit",btn_restore:"Obnovit",txt_action:"Akce",txt_remain:"znak(ů) zbývá"}}}(jQuery);