-
Notifications
You must be signed in to change notification settings - Fork 21
/
hulk.min.js
2 lines (2 loc) · 5.31 KB
/
hulk.min.js
1
2
/* Hulk v0.1 | (c) 2013 Kevin Burke | License: MIT */
!function(a){window.console&&console.log||(console={log:function(){},debug:function(){},info:function(){},warn:function(){},error:function(){}});var b="=>",c=function(){var a=document.createElement("button");return a.setAttribute("class","hulk-save"),a.innerHTML="Save",a},d=function(b,c){a(b).on("click",c)},e=function(b){a(b).on("click",function(c){c.preventDefault();var d=a(b).next();a(b).hasClass("collapsed")?(a(b).text("Collapse"),a(d).slideDown("slow",function(){a(b).removeClass("collapsed")})):a(d).slideUp("slow",function(){a(b).addClass("collapsed"),a(b).text("Expand")})})},f=function(b,c){a(b).on("click",function(d){d.preventDefault();var e=o("",c);a(b).before(e)})},g=function(b,c){a(b).on("click",function(d){d.preventDefault();var e=o({"":""},c);a(b).before(e)})},h=function(b,c){a(b).on("click",function(d){d.preventDefault();var e=m("","",c);a(b).before(e)})},i=function(a,b,c){return"undefined"==typeof a?c:"undefined"==typeof a[b]?c:a[b]},j=function(a){return!isNaN(parseFloat(a))&&isFinite(a)},k=function(a){return"object"==typeof a&&!(a instanceof Array)},l=function(a){try{return a.filter(k).length===a.length}catch(b){return!1}},m=function(c,d,f){var g=i(f,"separator",b),h=i(f,"depth",-1),j=a(document.createElement("div"));j.addClass("hulk-map-pair");var k=a(document.createElement("input"));k.addClass("hulk-map-key");var l=i(f,"permissions",[]);-1!==a.inArray("values-only",l)&&(k.prop("readonly",!0),k.css("border","0px")),k.attr("value",c),j.append(k);var m=a(document.createElement("p"));m.addClass("hulk-separator"),m.text(g),j.append(m);var n=jQuery.extend(!0,{},f);h>0&&(n.depth=h-1);var o=s(d,n,c),p=i(f,"permissions",[]);if(o.addClass("hulk-map-value-container"),o.children(".hulk-map-pair, .hulk-array-element").length>0){var q=a(document.createElement("button"));q.addClass("hulk-collapse-item"),0!==h?(q.text("Collapse"),e(q),-1===a.inArray("hide-toggle",p)&&j.append(q)):(q.addClass("collapsed"),q.text("Expand"),o.hide(),e(q),-1===a.inArray("hide-toggle",p)&&j.append(q))}return j.append(o),j},n=function(b,c,d){var e=a(document.createElement("div"));e.addClass("hulk-map");var f,g=[];for(f in b)b.hasOwnProperty(f)&&g.push(f);g.sort();for(var j=0;j<g.length;j++){f=g[j];var k=b[f],l=m(f,k,c);e.append(l)}var n=a(document.createElement("button"));n.addClass("hulk-map-add-pair");var o=p("key/value pair",d);n.text(o),h(n,c);var q=i(c,"permissions",[]);return-1!==a.inArray("no-append",q)?e:(e.append(n),e)},o=function(b,c){var d=a(document.createElement("div"));return d.addClass("hulk-array-element"),d.html(s(b,c)),d},p=function(a,b){var c=typeof b,d=30;return"undefined"===c||"string"!==c?["Add new ",a].join(""):b.length>d?["Add new ",a," to ",b.substring(0,d),"..."].join(""):["Add new ",a," to ",b].join("")},q=function(b,c,d){var e=a(document.createElement("div"));e.addClass("hulk-array");for(var h=0;h<b.length;h++){var i=o(b[h],c);e.append(i)}var j;if(l(b)){var k=a(document.createElement("button"));k.addClass("hulk-array-add-pair"),j=p("object",d),k.text(j),g(k,c),e.append(k)}else{var m=a(document.createElement("button"));m.addClass("hulk-array-add-row"),j=p("element",d),m.text(j),f(m,c),e.append(m)}return e},r=function(b,c){var d,e=80;if("string"===c&&b.length>e)d=a(document.createElement("textarea")),d.attr("rows",7),d.attr("cols",80),d.addClass("hulk-input-textarea");else if("boolean"===c){d=a(document.createElement("select"));var f=a(document.createElement("option"));f.attr("value","true").text("true");var g=a(document.createElement("option"));g.attr("value","false").text("false"),d.append(f),d.append(g),d.addClass("hulk-input-select")}else d=a(document.createElement("input")),d.addClass("hulk-input-text");return d.addClass("hulk-map-value"),"boolean"===c?d.val(b.toString()):d.val(b),d},s=function(a,b,c){var d=typeof a;return"string"===d||"number"===d||"boolean"===d||null===a?r(a,d):"[object Array]"===Object.prototype.toString.call(a)?q(a,b,c):n(a,b,c)},t=function(a,b){if(j(a))return parseFloat(a);if("true"===a)return!0;if("false"===a)return!1;if(null===a||"null"===a)return null;if(0===a.length){var c=i(b,"emptyString",!1);return c===!0?"":null}return a},u=function(b,c){var d=b.children(".hulk-map");if(d.length>0)return u(d,c);var e=b.children(".hulk-map-pair");if(e.length>0){var f={};return e.each(function(b,d){var e=a(d),g=e.children(".hulk-map-key");""!==g.val()&&(f[g.val()]=u(e.children(".hulk-map-value-container"),c))}),f}var g=b.children(".hulk-array");if(g.length>0)return u(g,c);if(b.hasClass("hulk-array")){var h=[];return b.children(".hulk-array-element").each(function(b,d){h.push(u(a(d),c))}),h}if(b.hasClass("hulk-map-value")){var j=i(c,"smartParsing",!0);return j?t(b.val(),c):b.val()}var k=b.children(".hulk-map-value");return k.length?u(k,c):b.hasClass("hulk-map-value-container")?u(b.children(".hulk-map-value"),c):{}};a.hulk=function(b,e,f,g){k(f)&&console.warn("Dictionary "+JSON.stringify(f)+" passed as the callback (3rd) argument, probably meant to pass it as the options (4th) argument");var h=a(b);if(h.addClass("hulk"),0===h.length)return void console.error("Attempting to hulk-ify element with selector "+b+" failed because the element does not exist. Quitting");var j=s(e,g);h.html(j);var l=i(g,"showSaveButton",!0);if(l){var m=c();d(m,function(a){var b=u(h.children(),g);f(b),a.preventDefault()}),h.append(m)}return h},a.hulkSmash=function(b,c){return u(a(b),c)}}(jQuery);