Advertisement
qsadfasdgfgads

Untitled

Feb 9th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*!
  2.  * Tabledit v1.2.3 (https://github.com/markcell/jQuery-Tabledit)
  3.  * Copyright (c) 2015 Celso Marques
  4.  * Licensed under MIT (https://github.com/markcell/jQuery-Tabledit/blob/master/LICENSE)
  5.  */
  6. if ("undefined" == typeof jQuery) throw new Error("Tabledit requires jQuery library.");
  7. ! function(t) {
  8.     "use strict";
  9.     t.fn.Tabledit = function(e) {
  10.         function n(e) {
  11.             var n = i.find(".tabledit-input").serialize() + "&action=" + e,
  12.                 a = d.onAjax(e, n);
  13.             if (a === !1) return !1;
  14.             var l = t.post(d.url, n, function(t, n, a) {
  15.                 e === d.buttons.edit.action && (s.removeClass(d.dangerClass).addClass(d.warningClass), setTimeout(function() {
  16.                     i.find("tr." + d.warningClass).removeClass(d.warningClass)
  17.                 }, 1400)), d.onSuccess(t, n, a)
  18.             }, "json");
  19.             return l.fail(function(t, n, i) {
  20.                 e === d.buttons["delete"].action ? (o.removeClass(d.mutedClass).addClass(d.dangerClass), o.find(".tabledit-toolbar button").attr("disabled", !1), o.find(".tabledit-toolbar .tabledit-restore-button").hide()) : e === d.buttons.edit.action && s.addClass(d.dangerClass), d.onFail(t, n, i)
  21.             }), l.always(function() {
  22.                 d.onAlways()
  23.             }), l
  24.         }
  25.         if (!this.is("table")) throw new Error("Tabledit only works when applied to a table.");
  26.         var i = this,
  27.             a = {
  28.                 url: window.location.href,
  29.                 inputClass: "form-control input-sm",
  30.                 toolbarClass: "btn-toolbar",
  31.                 groupClass: "btn-group btn-group-sm",
  32.                 dangerClass: "danger",
  33.                 warningClass: "warning",
  34.                 mutedClass: "text-muted",
  35.                 eventType: "click",
  36.                 rowIdentifier: "id",
  37.                 hideIdentifier: !1,
  38.                 autoFocus: !0,
  39.                 editButton: !0,
  40.                 deleteButton: !0,
  41.                 saveButton: !0,
  42.                 restoreButton: !0,
  43.                 buttons: {
  44.                     edit: {
  45.                         "class": "btn btn-sm btn-default",
  46.                         html: '<span class="glyphicon glyphicon-pencil"></span>',
  47.                         action: "edit"
  48.                     },
  49.                     "delete": {
  50.                         "class": "btn btn-sm btn-default",
  51.                         html: '<span class="glyphicon glyphicon-trash"></span>',
  52.                         action: "delete"
  53.                     },
  54.                     save: {
  55.                         "class": "btn btn-sm btn-success",
  56.                         html: "Save"
  57.                     },
  58.                     restore: {
  59.                         "class": "btn btn-sm btn-warning",
  60.                         html: "Restore",
  61.                         action: "restore"
  62.                     },
  63.                     confirm: {
  64.                         "class": "btn btn-sm btn-danger",
  65.                         html: "Confirm"
  66.                     }
  67.                 },
  68.                 onDraw: function() {},
  69.                 onSuccess: function() {},
  70.                 onFail: function() {},
  71.                 onAlways: function() {},
  72.                 onAjax: function() {}
  73.             },
  74.             d = t.extend(!0, a, e),
  75.             s = "undefined",
  76.             o = "undefined",
  77.             l = "undefined",
  78.             r = {
  79.                 columns: {
  80.                     identifier: function() {
  81.                         d.hideIdentifier && i.find("th:nth-child(" + parseInt(d.columns.identifier[0]) + "1), tbody td:nth-child(" + parseInt(d.columns.identifier[0]) + "1)").hide();
  82.                         var e = i.find("tbody td:nth-child(" + (parseInt(d.columns.identifier[0]) + 1) + ")");
  83.                         e.each(function() {
  84.                             var e = '<span class="tabledit-span tabledit-identifier">' + t(this).text() + "</span>",
  85.                                 n = '<input class="tabledit-input tabledit-identifier" type="hidden" name="' + d.columns.identifier[1] + '" value="' + t(this).text() + '" disabled>';
  86.                             t(this).html(e + n), t(this).parent("tr").attr(d.rowIdentifier, t(this).text())
  87.                         })
  88.                     },
  89.                     editable: function() {
  90.                         for (var e = 0; e < d.columns.editable.length; e++) {
  91.                             var n = i.find("tbody td:nth-child(" + (parseInt(d.columns.editable[e][0]) + 1) + ")");
  92.                             n.each(function() {
  93.                                 var n = t(this).text();
  94.                                 d.editButton || t(this).css("cursor", "pointer");
  95.                                 var i = '<span class="tabledit-span">' + n + "</span>";
  96.                                 if ("undefined" != typeof d.columns.editable[e][2]) {
  97.                                     var a = '<select class="tabledit-input ' + d.inputClass + '" name="' + d.columns.editable[e][1] + '" style="display: none;" disabled>';
  98.                                     t.each(jQuery.parseJSON(d.columns.editable[e][2]), function(t, e) {
  99.                                         a += n === e ? '<option value="' + t + '" selected>' + e + "</option>" : '<option value="' + t + '">' + e + "</option>"
  100.                                     }), a += "</select>"
  101.                                 } else var a = '<input class="tabledit-input ' + d.inputClass + '" type="text" name="' + d.columns.editable[e][1] + '" value="' + t(this).text() + '" style="display: none;" disabled>';
  102.                                 t(this).html(i + a), t(this).addClass("tabledit-view-mode")
  103.                             })
  104.                         }
  105.                     },
  106.                     toolbar: function() {
  107.                         if (d.editButton || d.deleteButton) {
  108.                             var t = "",
  109.                                 e = "",
  110.                                 n = "",
  111.                                 a = "",
  112.                                 s = "";
  113.                             0 === i.find("th.tabledit-toolbar-column").length && i.find("tr:first").append('<th class="tabledit-toolbar-column"></th>'), d.editButton && (t = '<button type="button" class="tabledit-edit-button ' + d.buttons.edit["class"] + '" style="float: none;">' + d.buttons.edit.html + "</button>"), d.deleteButton && (e = '<button type="button" class="tabledit-delete-button ' + d.buttons["delete"]["class"] + '" style="float: none;">' + d.buttons["delete"].html + "</button>", s = '<button type="button" class="tabledit-confirm-button ' + d.buttons.confirm["class"] + '" style="display: none; float: none;">' + d.buttons.confirm.html + "</button>"), d.editButton && d.saveButton && (n = '<button type="button" class="tabledit-save-button ' + d.buttons.save["class"] + '" style="display: none; float: none;">' + d.buttons.save.html + "</button>"), d.deleteButton && d.restoreButton && (a = '<button type="button" class="tabledit-restore-button ' + d.buttons.restore["class"] + '" style="display: none; float: none;">' + d.buttons.restore.html + "</button>");
  114.                             var o = '<div class="tabledit-toolbar ' + d.toolbarClass + '" style="text-align: left;">\n                                           <div class="' + d.groupClass + '" style="float: none;">' + t + e + "</div>\n                                           " + n + "\n                                           " + s + "\n                                           " + a + "\n                                       </div></div>";
  115.                             i.find("tr:gt(0)").append('<td style="white-space: nowrap; width: 1%;">' + o + "</td>")
  116.                         }
  117.                     }
  118.                 }
  119.             },
  120.             u = {
  121.                 view: function(e) {
  122.                     var n = t(e).parent("tr");
  123.                     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(".tabledit-span").show(), t(e).addClass("tabledit-view-mode").removeClass("tabledit-edit-mode"), d.editButton && (n.find("button.tabledit-save-button").hide(), n.find("button.tabledit-edit-button").removeClass("active").blur())
  124.                 },
  125.                 edit: function(e) {
  126.                     c.reset(e);
  127.                     var n = t(e).parent("tr");
  128.                     n.find(".tabledit-input.tabledit-identifier").prop("disabled", !1), t(e).find(".tabledit-span").hide();
  129.                     var i = t(e).find(".tabledit-input");
  130.                     i.prop("disabled", !1).show(), d.autoFocus && i.focus(), t(e).addClass("tabledit-edit-mode").removeClass("tabledit-view-mode"), d.editButton && (n.find("button.tabledit-edit-button").addClass("active"), n.find("button.tabledit-save-button").show())
  131.                 }
  132.             },
  133.             b = {
  134.                 reset: function(e) {
  135.                     t(e).each(function() {
  136.                         var e = t(this).find(".tabledit-input"),
  137.                             n = t(this).find(".tabledit-span").text();
  138.                         e.is("select") ? e.find("option").filter(function() {
  139.                             return t.trim(t(this).text()) === n
  140.                         }).attr("selected", !0) : e.val(n), u.view(this)
  141.                     })
  142.                 },
  143.                 submit: function(e) {
  144.                     var i = n(d.buttons.edit.action);
  145.                     i !== !1 && (t(e).each(function() {
  146.                         var e = t(this).find(".tabledit-input");
  147.                         t(this).find(".tabledit-span").text(e.is("select") ? e.find("option:selected").text() : e.val()), u.view(this)
  148.                     }), s = t(e).parent("tr"))
  149.                 }
  150.             },
  151.             c = {
  152.                 reset: function(t) {
  153.                     i.find(".tabledit-confirm-button").hide(), i.find(".tabledit-delete-button").removeClass("active").blur()
  154.                 },
  155.                 submit: function(e) {
  156.                     c.reset(e), t(e).parent("tr").find("input.tabledit-identifier").attr("disabled", !1);
  157.                     var i = n(d.buttons["delete"].action);
  158.                     t(e).parents("tr").find("input.tabledit-identifier").attr("disabled", !0), i !== !1 && (t(e).parent("tr").addClass("tabledit-deleted-row"), t(e).parent("tr").addClass(d.mutedClass).find(".tabledit-toolbar button:not(.tabledit-restore-button)").attr("disabled", !0), t(e).find(".tabledit-restore-button").show(), o = t(e).parent("tr"))
  159.                 },
  160.                 confirm: function(e) {
  161.                     i.find("td.tabledit-edit-mode").each(function() {
  162.                         b.reset(this)
  163.                     }), t(e).find(".tabledit-delete-button").addClass("active"), t(e).find(".tabledit-confirm-button").show()
  164.                 },
  165.                 restore: function(e) {
  166.                     t(e).parent("tr").find("input.tabledit-identifier").attr("disabled", !1);
  167.                     var i = n(d.buttons.restore.action);
  168.                     t(e).parents("tr").find("input.tabledit-identifier").attr("disabled", !0), i !== !1 && (t(e).parent("tr").removeClass("tabledit-deleted-row"), t(e).parent("tr").removeClass(d.mutedClass).find(".tabledit-toolbar button").attr("disabled", !1), t(e).find(".tabledit-restore-button").hide(), l = t(e).parent("tr"))
  169.                 }
  170.             };
  171.         return r.columns.identifier(), r.columns.editable(), r.columns.toolbar(), d.onDraw(), d.deleteButton && (i.on("click", "button.tabledit-delete-button", function(e) {
  172.             if (e.handled !== !0) {
  173.                 e.preventDefault();
  174.                 var n = t(this).hasClass("active"),
  175.                     i = t(this).parents("td");
  176.                 c.reset(i), n || c.confirm(i), e.handled = !0
  177.             }
  178.         }), i.on("click", "button.tabledit-confirm-button", function(e) {
  179.             if (e.handled !== !0) {
  180.                 e.preventDefault();
  181.                 var n = t(this).parents("td");
  182.                 c.submit(n), e.handled = !0
  183.             }
  184.         })), d.restoreButton && i.on("click", "button.tabledit-restore-button", function(e) {
  185.             e.handled !== !0 && (e.preventDefault(), c.restore(t(this).parents("td")), e.handled = !0)
  186.         }), d.editButton ? (i.on("click", "button.tabledit-edit-button", function(e) {
  187.             if (e.handled !== !0) {
  188.                 e.preventDefault();
  189.                 var n = t(this),
  190.                     a = n.hasClass("active");
  191.                 b.reset(i.find("td.tabledit-edit-mode")), a || t(n.parents("tr").find("td.tabledit-view-mode").get().reverse()).each(function() {
  192.                     u.edit(this)
  193.                 }), e.handled = !0
  194.             }
  195.         }), i.on("click", "button.tabledit-save-button", function(e) {
  196.             e.handled !== !0 && (e.preventDefault(), b.submit(t(this).parents("tr").find("td.tabledit-edit-mode")), e.handled = !0)
  197.         })) : (i.on(d.eventType, "tr:not(.tabledit-deleted-row) td.tabledit-view-mode", function(t) {
  198.             t.handled !== !0 && (t.preventDefault(), b.reset(i.find("td.tabledit-edit-mode")), u.edit(this), t.handled = !0)
  199.         }), i.on("change", "select.tabledit-input:visible", function() {
  200.             event.handled !== !0 && (b.submit(t(this).parent("td")), event.handled = !0)
  201.         }), t(document).on("click", function(t) {
  202.             var e = i.find(".tabledit-edit-mode");
  203.             e.is(t.target) || 0 !== e.has(t.target).length || b.reset(i.find(".tabledit-input:visible").parent("td"))
  204.         })), t(document).on("keyup", function(t) {
  205.             var e = i.find(".tabledit-input:visible"),
  206.                 n = i.find(".tabledit-confirm-button");
  207.             if (e.length > 0) var a = e.parents("td");
  208.             else {
  209.                 if (!(n.length > 0)) return;
  210.                 var a = n.parents("td")
  211.             }
  212.             switch (t.keyCode) {
  213.                 case 9:
  214.                     d.editButton || (b.submit(a), u.edit(a.closest("td").next()));
  215.                     break;
  216.                 case 13:
  217.                     b.submit(a);
  218.                     break;
  219.                 case 27:
  220.                     b.reset(a), c.reset(a)
  221.             }
  222.         }), this
  223.     }
  224. }(jQuery);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement