Ext.ns("Ext.ux");Ext.ux.StarComment=Ext.extend(Ext.util.Observable,{tpl:'<div class="ux-comment-star"><div style="display: none;" class="hide-cnt"><div class="close"></div><div class="red-r"><div class="cn tl"></div><div class="cn tr"></div><div class="middle"><div class="items">{middle}</div><div class="spacer5"></div><textarea></textarea><div class="spacer5"></div><div class="remove"><a href="#" class="local remove">Удалить</a></div><button style="float: right;" name="send">Добавить</button><div class="clear"></div></div><div class="cn bl"></div><div class="cn br"></div></div></div></div>',tplRender:"",commentTpl:'<li id="comment-{id}" style="display: none;"><div class="ts">{date}</div><p>{comment}</p></li>',commentTplRender:"",hideCntWidth:220,left:0,top:0,x:0,y:0,imageId:0,constructor:function(a){Ext.apply(this,a);Ext.ux.StarComment.superclass.constructor.call(this);this.tplRender=new Ext.Template(this.tpl);if(this.renderTo){this.applyToMarkup(Ext.get(this.renderTo));this.applySendTool()}this.init()},init:function(){var a=this;if(this.el){this.el.on({click:this.onStarClick,scope:this,mouseenter:function(){this.el.addClass("hover")},mouseleave:function(){this.el.removeClass("hover")}});this.left=this.left||this.el.getLeft(true);this.top=this.top||this.el.getTop(true)}},onStarClick:function(c,a){this.clearSelection();c.stopEvent();if(this.ddInProgress===true){this.ddInProgress=false;c.stopEvent();return}var b=Ext.get(a);if(!b.hasClass("close")&&!b.hasClass("ux-comment-star")&&!b.hasClass("hide-cnt")&&!b.hasClass("cn")){return}if(this.hideCntEl.isVisible()){this.hideComment()}else{if(Ext.ux.StarComment.activeComment){Ext.ux.StarComment.activeComment.hideComment()}this.showComment()}},hideComment:function(a){a=(Ext.isEmpty(a)?true:a);Ext.ux.StarComment.activeComment="";this.el.removeClass("active");this.el.setStyle({zIndex:"9999"});this.hideCntEl.stopFx().fadeOut({duration:(a?0.2:0.001),scope:this,useDisplay:true,callback:function(b){this.el.removeClass("active");if(!this.ddInProgress){this.el.setStyle({zIndex:"auto"})}}})},showComment:function(a){a=(Ext.isEmpty(a)?true:a);Ext.ux.StarComment.activeComment=this;this.el.setStyle({zIndex:"9999"});this.hideCntEl.stopFx().fadeIn({duration:(a?0.2:0.001),scope:this,useDisplay:true,callback:function(b){this.el.setStyle({zIndex:"auto"});this.el.addClass("active");this.textarea.focus(200);this.cleanMessageError()}})},clearSelection:function(){if(Ext.isGecko3){if(window.getSelection){window.getSelection().removeAllRanges()}else{if(document.selection&&document.selection.clear){document.selection.clear()}}}},setLeftTop:function(b,a){this.el.setLeft(b);this.el.setTop(a)},setXY:function(a,b){this.el.setXY([a,b])},setImageId:function(a){this.imageId=a;return this},add:function(a){this.el=this.tplRender.append(a,{},true);if(this.left&&this.top){this.setLeftTop(this.left,this.top)}else{if(this.x&&this.y){this.setXY(this.x,this.y)}}this.hideCntEl=this.el.child(".hide-cnt");this.hideCntEl.setWidth(this.hideCntWidth);this.init();return this},applyToMarkup:function(a){this.el=this.tplRender.insertBefore(a,{middle:a.dom.innerHTML},true);this.setLeftTop(this.left||a.getLeft(true)||0,this.top||a.getTop(true)||0);a.remove();this.hideCntEl=this.el.child(".hide-cnt");this.hideCntEl.setWidth(this.hideCntWidth)},setDD:function(){this.dd=new Ext.dd.DD(this.el);this.dd.me=this;this.dd.startDrag=function(a,b){this.constrainTo(Ext.get("comments"));if(Ext.ux.StarComment.activeComment){Ext.ux.StarComment.activeComment.hideComment(false)}this.me.ddInProgress=true;this.me.el.addClass("drag");this.me.el.applyStyles({zIndex:99999,color:"red"})};this.dd.endDrag=function(a){this.me.el.applyStyles({zIndex:"auto"});this.me.el.removeClass("drag");this.me.left=this.me.el.getLeft(true);this.me.top=this.me.el.getTop(true)};return this},setDraft:function(){this.el.addClass("draft");this.setDD();return this},unsetDraft:function(){this.el.removeClass("draft");if(this.dd){this.dd.unreg()}return this},isDraft:function(){return this.el.hasClass("draft")},applyRemoveTool:function(){this.removeTool=this.el.child("a.remove");if(this.removeTool){if(!this.isDraft()){this.removeTool.remove()}else{this.removeTool.on("click",function(b,a){b.stopEvent();this.destroy()},this)}}return this},applySendTool:function(){this.sendTool=this.el.child("button[name=send]");this.textarea=this.el.child("textarea");if(this.sendTool&&this.textarea){this.sendTool.on("click",function(b,a){if(b){b.stopEvent()}var c=this.textarea.getValue();this.textarea.dom.setAttribute("disabled","disabled");this.textarea.addClass("spinner");this.sendTool.dom.setAttribute("disabled","disabled");this.cleanMessageError();Ext.php.Comment.add({comment:c,image_id:this.imageId,left:this.el.getLeft(true),top:this.el.getTop(true)},function(d){if(!Ext.isEmpty(d)){try{var h=this.el.child(".items ul");if(!h){var f=document.createElement("ul");var h=this.el.child(".items").appendChild(f)}if(Ext.isEmpty(this.commentTplRender)){this.commentTplRender=new Ext.Template(this.commentTpl);this.commentTplRender.compile()}d.date=(Date.parseDate(d.ts,"U")).format("j M Y (H:i)");var g=this.commentTplRender.append(h,d,true).slideIn("t",{scope:this,duration:0.2,callback:function(){var j=g.parent(".items");itemsScroll=j.getScroll();offset=g.getOffsetsTo(j);var e=offset[1]-j.getHeight()+g.getHeight();if(e){g.parent(".items").scroll("b",e,true)}}});if(this.removeTool){this.removeTool.remove()}this.unsetDraft();this.textarea.dom.value=""}catch(i){}}this.textarea.removeClass("spinner");this.textarea.dom.removeAttribute("disabled");this.textarea.focus(200);this.sendTool.dom.removeAttribute("disabled")},this)},this);this.textarea.on("keydown",function(){this.cleanMessageError()},this);this.textarea.on("focus",function(){if(T&&T.keyMapArrows){T.keyMapArrows.disable()}},this);this.textarea.on("click",function(){},this);this.textarea.on("blur",function(){if(T&&T.keyMapArrows){T.keyMapArrows.enable()}},this);this.textarea.addKeyListener({key:Ext.EventObject.ENTER,ctrl:true},function(){this.sendTool.dom.click()},this)}return this},setMessageError:function(){if(this.textarea){this.textarea.addClass("error")}},cleanMessageError:function(){if(this.textarea){this.textarea.removeClass("error")}},render:function(a){a=a||this.applyTo||"";if(!a){throw new UserException("Empty renderTo element")}this.applyToMarkup(a)},destroy:function(){Ext.ux.StarComment.activeComment="";this.el.remove()}});
