1
0
Fork 0
forked from OpenNeo/impress
impress/public/assets/closet_hangers/index.js

1 line
8 KiB
JavaScript
Raw Normal View History

(function(){function t(t){e[e.length]=t}function n(){for(var t=0;t<e.length;t++)e[t]()}function u(e,t){try{var n=$.parseJSON(e.responseText)}catch(r){var n={}}typeof n.errors!="undefined"?$.jGrowl("Error "+t+": "+n.errors.join(", ")):$.jGrowl("We had trouble "+t+" just now. Try again?")}function a(e){e.hide(250,$.proxy(e,"remove"))}function f(e,t){return e.find("span.name").text().localeCompare(t.find("span.name").text())}function l(e,t,n){return t?$("#closet-list-"+t):$("div.closet-hangers-group[data-owned="+e+"] div.closet-list.unlisted")}function c(e){e.attr("data-hangers-count",e.find("div.object").length)}function h(e,t,n){var r=l(t,n,e),i=e.closest("div.closet-list"),s=r.find("div.closet-list-hangers");e.insertIntoSortedList(s,f),c(i),c(r)}function p(e){if(e.data("loading"))return!1;var t=e.children("input[name=closet_hanger[quantity]]"),n=e.children("input[name=closet_hanger[owned]]"),r=e.children("input[name=closet_hanger[list_id]]"),i=n.hasChanged()||r.hasChanged();if(i||t.hasChanged()){var s=e.closest(".object").addClass("loading"),o=t.val(),f=s.find(".quantity span").text(o);s.attr("data-quantity",o);var c=e.serialize();s.disableForms(),e.data("loading",!0),i&&h(s,n.val(),r.val()),$.ajax({url:e.attr("action")+".json",type:"post",data:c,dataType:"json",complete:function(n){t.val()==0?a(s):s.removeClass("loading").enableForms(),e.data("loading",!1)},success:function(){var e=s.attr("data-item-id"),i=l(n.val(),r.val(),s).find("div[data-item-id="+e+"]").not(s);if(i.length){var u=parseInt(i.attr("data-quantity"),10),a=parseInt(o,10),c=u+a;f.text(c),t.val(c),s.attr("data-quantity",c),i.remove()}t.storeValue(),n.storeValue(),r.storeValue()},error:function(e){t.revertValue(),n.revertValue(),r.revertValue(),i&&h(s,n.val(),r.val()),f.text(t.val()),u(e,"updating the quantity")}})}}function d(){return $(i).find("input[name=closet_hanger[quantity]], input[name=closet_hanger[owned]], input[name=closet_hanger[list_id]]")}function T(){y.removeClass("editing")}function N(){var e=$(this).closest(".visibility-form").find("ul.visibility-descriptions");e.children("li.current").removeClass("current"),e.children("li[data-id="+$(this).val()+"]").addClass("current")}function C(){return $("form.visibility-form select")}var e=[],r=[];$("div.closet-hangers-group").each(function(){var e=$(this),t=[];e.find("div.closet-list").each(function(){var e=$(this),n=e.attr("data-id");n&&(t[t.length]={id:parseInt(n,10),label:e.find("h4").text()})}),r[r.length]={label:e.find("h3").text(),lists:t,owned:e.attr("data-owned")=="true"}}),$("div.closet-hangers-group span.toggle").live("click",function(){$(this).closest(".closet-hangers-group").toggleClass("hidden")});var i="#closet-hangers",s=$(i);$("#toggle-compare").click(function(){s.toggleClass("comparing")}),$.fn.liveDraggable=function(e){this.live("mouseover",function(){$(this).data("init")||$(this).data("init",!0).draggable(e)})};var o=$(document.body).addClass("js");if(!o.hasClass("current-user"))return!1;$.fn.disableForms=function(){return this.data("formsDisabled",!0).find("input").attr("disabled","disabled").end()},$.fn.enableForms=function(){return this.data("formsDisabled",!1).find("input").removeAttr("disabled").end()},$.fn.hasChanged=function(){return this.attr("data-previous-value")!=this.val()},$.fn.revertValue=function(){return this.each(function(){var e=$(this);e.val(e.attr("data-previous-value"))})},$.fn.storeValue=function(){return this.each(function(){var e=$(this);e.attr("data-previous-value",e.val())})},$.fn.insertIntoSortedList=function(e,t){var n=this,r=!1;return e.children().each(function(){if(t(n,$(this))<1)return n.insertBefore(this),r=!0,!1}),r||n.appendTo(e),this},$(i+" form.closet-hanger-update").live("submit",function(e){e.preventDefault(),p($(this))}),$(i+"input[name=closet_hanger[quantity]]").live("change",function(){p($(this).parent())}).storeValue(),t(function(){d().storeValue()}),$(i+" div.object").live("mouseleave",function(){p($(this).find("form.closet-hanger-update"))}).liveDraggable({appendTo:"#closet-hangers",distance:20,helper:"clone",revert:"invalid"}),$(i+" for