/** * Sortable feature for RightJS * See http://rightjs.org/ui/sortable * * NOTE: requires the dnd-plugin * * Copyright (C) 2009-2010 Nikolay Nemshilov */ var Sortable=RightJS.Sortable=function(l,f){var j=f.$,m=f.$w,n=f.isString,o=f.isArray,k=f.Object,h=new (function(a,b){if(!b){b=a;a="DIV"}var d=new f.Wrapper(f.Element.Wrappers[a]||f.Element,{initialize:function(e,c){this.key=e;var g=[{"class":"rui-"+e}];this instanceof f.Input||this instanceof f.Form||g.unshift(a);this.$super.apply(this,g);if(f.isString(c))c=f.$(c);if(c instanceof f.Element){this._=c._;if("$listeners"in c)c.$listeners=c.$listeners;c={}}this.setOptions(c,this);return this},setOptions:function(e, c){c=c||this;f.Options.setOptions.call(this,f.Object.merge(e,eval("("+(c.get("data-"+this.key)||"{}")+")")));return this}});d=new f.Wrapper(d,b);f.Observer.createShortcuts(d.prototype,d.EVENTS||[]);return d})("UL",{extend:{version:"2.0.0",EVENTS:m("change"),Options:{url:null,method:"put",Xhr:{},idParam:"id",posParam:"position",parseId:true,dragClass:"dragging",accept:null,minLength:1,cssRule:"*[data-sortable]"},current:false},initialize:function(a,b){this.$super("sortable",a).setOptions(b).addClass("rui-sortable").on("change", this._tryXhr).on("selectstart","stopEvent")},setOptions:function(a,b){this.$super(a,b);a=this.options;var d=a.accept||[];o(d)||(d=[d]);a.accept=f([this].concat(d)).map(j).uniq();return this},startDrag:function(a){if(!(this.children().length<=this.options.minLength)){var b=a.target;b=f([b].concat(b.parents()));b=b[b.indexOf(this)-1];a=a.position();if(b){this._initDrag(b,a);h.current=this}}},moveItem:function(a){a=a.position();var b=this.itemClone._.style,d=a.y-this.yRDiff,e=a.x-this.xRDiff,c=e+this.cloneWidth, g=d+this.cloneHeight;b.top=a.y-this.yDiff+"px";b.left=a.x-this.xDiff+"px";if(a=this.suspects.first(function(i){return(d>i.top&&di.topHalf)&&(e>i.left&&ei.leftHalf)})){b=a.item;b.insert(this.item,b.prevSiblings().include(this.item)?"after":"before");this._findSuspects();a=b.parent();a instanceof h||(a=new h(a));this.fire("change",{list:a,item:this.item,index:a.children().indexOf(this.item)})}},finishDrag:function(){if(this.itemClone){this.itemClone.remove(); this.item.setStyle("visibility:visible")}h.current=false},_initDrag:function(a,b){this.dimensions();var d=a.dimensions(),e=a.clone().setStyle({margin:0,zIndex:9999,position:"absolute",top:"0px",left:"0px"}).addClass(this.options.dragClass).insertTo(this).setHeight(this.cloneHeight=d.height).setWidth(this.cloneWidth=d.width),c=e.position(),g=d.left-c.x;d=d.top-c.y;e.moveTo(g,d);this.item=a.setStyle("visibility:hidden");this.itemClone=e;this.xDiff=b.x-g;this.yDiff=b.y-d;this.xRDiff=b.x-e.position().x; this.yRDiff=b.y-e.position().y;this._findSuspects()},_findSuspects:function(){var a=this.suspects=f([]),b=this.item,d=this.itemClone;this.options.accept.each(function(e){e.children().each(function(c){if(c!==b&&c!==d){var g=c.dimensions();a.push({item:c,top:g.top,left:g.left,right:g.left+g.width,bottom:g.top+g.height,topHalf:g.top+g.height/2,leftHalf:g.left+g.width/2})}})})},_tryXhr:function(a){if(this.options.url){var b=f(this.options.url),d={},e=a.item;a=a.index+1;var c=k.merge({method:this.options.method, params:{}},this.options.Xhr);e=e.get("id")||"";if(this.options.parseId&&e)e=(e.match(/\d+/)||[""])[0];if(b.include("%{id}"))b=b.replace("%{id}",e);else d[this.options.idParam]=e;d[this.options.posParam]=a;if(n(c.params))c.params+="&"+k.toQueryString(d);else c.params=k.merge(c.params,d);f.Xhr.load(b,c)}}});j(l).on({mousedown:function(a){var b=a.find(h.Options.cssRule+",*.rui-sortable");if(b){b instanceof h||(b=new h(b));b.startDrag(a)}},mousemove:function(a){h.current&&h.current.moveItem(a)},mouseup:function(){h.current&& h.current.finishDrag()}});j(window).onBlur(function(){h.current&&h.current.finishDrag()});l.write('');return h}(document,RightJS);