//>>built
define("dojox/drawing/tools/TextBlock",["dojo","dijit/registry","../util/oo","../manager/_registry","../stencil/Text"],function(c,v,q,w,x){var k;c.addOnLoad(function(){(k=c.byId("conEdit"))&&k.parentNode.removeChild(k)});q=q.declare(x,function(a){if(a.data){a=a.data;var d=a.text?this.typesetter(a.text):a.text,b=a.width?"auto"==a.width?"auto":Math.max(a.width,this.style.text.minWidth):this.style.text.minWidth,e=this._lineHeight;d&&"auto"==b?(e=this.measureText(this.cleanText(d,!1),b),b=e.w,e=e.h):
this._text="";this.points=[{x:a.x,y:a.y},{x:a.x+b,y:a.y},{x:a.x+b,y:a.y+e},{x:a.x,y:a.y+e}];a.showEmpty||d?(this.editMode=!0,c.disconnect(this._postRenderCon),this._postRenderCon=null,this.connect(this,"render",this,"onRender",!0),a.showEmpty?(this._text=d||"",this.edit()):d&&a.editMode?(this._text="",this.edit()):d&&this.render(d),setTimeout(c.hitch(this,function(){this.editMode=!1}),100)):this.render()}else this.connectMouse(),this._postRenderCon=c.connect(this,"render",this,"_onPostRender")},{draws:!0,
baseRender:!1,type:"dojox.drawing.tools.TextBlock",_caretStart:0,_caretEnd:0,_blockExec:!1,selectOnExec:!0,showEmpty:!1,onDrag:function(a){this.parentNode||this.showParent(a);var d=this._startdrag;a=a.page;this._box.left=d.x<a.x?d.x:a.x;this._box.top=d.y;this._box.width=(d.x<a.x?a.x-d.x:d.x-a.x)+this.style.text.pad;c.style(this.parentNode,this._box.toPx())},onUp:function(a){if(this._downOnCanvas){this._downOnCanvas=!1;var d=c.connect(this,"render",this,function(){c.disconnect(d);this.onRender(this)});
this.editMode=!0;this.showParent(a);this.created=!0;this.createTextField();this.connectTextField()}},showParent:function(a){if(!this.parentNode){var d=a.pageX||10,b=a.pageY||10;this.parentNode=c.doc.createElement("div");this.parentNode.id=this.id;var e=this.style.textMode.create;this._box={left:d,top:b,width:a.width||1,height:a.height&&8<a.height?a.height:this._lineHeight,border:e.width+"px "+e.style+" "+e.color,position:"absolute",zIndex:500,toPx:function(){var f={},h;for(h in this)f[h]="number"==
typeof this[h]&&"zIndex"!=h?this[h]+"px":this[h];return f}};c.style(this.parentNode,this._box);document.body.appendChild(this.parentNode)}},createTextField:function(a){var d=this.style.textMode.edit;this._box.border=d.width+"px "+d.style+" "+d.color;this._box.height="auto";this._box.width=Math.max(this._box.width,this.style.text.minWidth*this.mouse.zoom);c.style(this.parentNode,this._box.toPx());this.parentNode.appendChild(k);c.style(k,{height:a?"auto":this._lineHeight+"px",fontSize:this.textSize/
this.mouse.zoom+"px",fontFamily:this.style.text.family});k.innerHTML=a||"";return k},connectTextField:function(){if(!this._textConnected){var a=v.byId("greekPalette"),d=void 0==a?!1:!0;d&&c.mixin(a,{_pushChangeTo:k,_textBlock:this});this._textConnected=!0;this._dropMode=!1;this.mouse.setEventMode("TEXT");this.keys.editMode(!0);var b=this,e=!1,f=function(){b._dropMode||(c.forEach([h,m,l,n],function(g){c.disconnect(g)}),b._textConnected=!1,b.keys.editMode(!1),b.mouse.setEventMode(),b.execText())};var h=
c.connect(k,"keyup",this,function(g){c.trim(k.innerHTML)&&!e?(c.style(k,"height","auto"),e=!0):2>c.trim(k.innerHTML).length&&e&&(c.style(k,"height",this._lineHeight+"px"),e=!1);if(this._blockExec)g.keyCode==c.keys.SPACE&&(c.stopEvent(g),d&&a.onCancel());else if(13==g.keyCode||27==g.keyCode)c.stopEvent(g),f()});var m=c.connect(k,"keydown",this,function(g){13!=g.keyCode&&27!=g.keyCode||c.stopEvent(g);if(220==g.keyCode){if(!d)return;c.stopEvent(g);this.getSelection(k);this.insertText(k,"\\");this._blockExec=
this._dropMode=!0;a.show({around:this.parentNode,orient:{BL:"TL"}})}if(this._dropMode)switch(g.keyCode){case c.keys.UP_ARROW:case c.keys.DOWN_ARROW:case c.keys.LEFT_ARROW:case c.keys.RIGHT_ARROW:c.stopEvent(g);a._navigateByArrow(g);break;case c.keys.ENTER:c.stopEvent(g);a._onCellClick(g);break;case c.keys.BACKSPACE:case c.keys.DELETE:c.stopEvent(g),a.onCancel()}else this._blockExec=!1});var l=c.connect(document,"mouseup",this,function(g){this._onAnchor||"conEdit"==g.target.id?"conEdit"==g.target.id&&
""==k.innerHTML&&(k.blur(),setTimeout(function(){k.focus()},200)):(c.stopEvent(g),f())});this.createAnchors();var n=c.connect(this.mouse,"setZoom",this,function(g){f()});k.focus();this.onDown=function(){};this.onDrag=function(){};setTimeout(c.hitch(this,function(){k.focus();this.onUp=function(){!b._onAnchor&&this.parentNode&&(b.disconnectMouse(),f(),b.onUp=function(){})}}),500)}},execText:function(){var a=c.marginBox(this.parentNode);a=Math.max(a.w,this.style.text.minWidth);var d=this.cleanText(k.innerHTML,
!0);k.innerHTML="";k.blur();this.destroyAnchors();d=this.typesetter(d);d=this.measureText(d,a);var b=this.mouse.scrollOffset(),e=this.mouse.origin,f=this._box.left+b.left-e.x;b=this._box.top+b.top-e.y;f*=this.mouse.zoom;b*=this.mouse.zoom;a*=this.mouse.zoom;d.h*=this.mouse.zoom;this.points=[{x:f,y:b},{x:f+a,y:b},{x:f+a,y:b+d.h},{x:f,y:b+d.h}];this.editMode=!1;d.text||(this._text="",this._textArray=[]);this.render(d.text);this.onChangeText(this.getText())},edit:function(){this.editMode=!0;var a=this.getText()||
"";if(!this.parentNode&&this.points){var d=this.pointsToData(),b=this.mouse.scrollOffset(),e=this.mouse.origin;d={pageX:d.x/this.mouse.zoom-b.left+e.x,pageY:d.y/this.mouse.zoom-b.top+e.y,width:d.width/this.mouse.zoom,height:d.height/this.mouse.zoom};this.remove(this.shape,this.hit);this.showParent(d);this.createTextField(a.replace("/n"," "));this.connectTextField();a&&this.setSelection(k,"end")}},cleanText:function(a,d){d&&c.forEach(["\x3cbr\x3e","\x3cbr/\x3e","\x3cbr /\x3e","\\n","\\r"],function(b){a=
a.replace(new RegExp(b,"gi")," ")});a=a.replace(/&nbsp;/g," ");a=function(b){var e={"\x26lt;":"\x3c","\x26gt;":"\x3e","\x26amp;":"\x26"},f;for(f in e)b=b.replace(new RegExp(f,"gi"),e[f]);return b}(a);a=c.trim(a);return a=a.replace(/\s{2,}/g," ")},measureText:function(a,d){this.showParent({width:d||"auto",height:"auto"});this.createTextField(a);var b="",e=k;e.innerHTML="X";b=c.marginBox(e).h;e.innerHTML=a;if(!d||RegExp("(\x3cbr\\s*/*\x3e)|(\\n)|(\\r)","gi").test(a))b=a.replace(RegExp("(\x3cbr\\s*/*\x3e)|(\\n)|(\\r)",
"gi"),"\n"),e.innerHTML=a.replace(RegExp("(\x3cbr\\s*/*\x3e)|(\\n)|(\\r)","gi"),"\x3cbr/\x3e");else if(c.marginBox(e).h==b)b=a;else{var f=a.split(" "),h=[[]],m=0;for(e.innerHTML="";f.length;){var l=f.shift();e.innerHTML+=l+" ";c.marginBox(e).h>b&&(m++,h[m]=[],e.innerHTML=l+" ");h[m].push(l)}c.forEach(h,function(n,g){h[g]=n.join(" ")});b=h.join("\n");e.innerHTML=b.replace("\n","\x3cbr/\x3e")}e=c.marginBox(e);k.parentNode.removeChild(k);c.destroy(this.parentNode);this.parentNode=null;return{h:e.h,w:e.w,
text:b}},_downOnCanvas:!1,onDown:function(a){this._startdrag={x:a.pageX,y:a.pageY};c.disconnect(this._postRenderCon);this._postRenderCon=null;this._downOnCanvas=!0},createAnchors:function(){this._anchors={};var a=this,d=this.style.anchors,b=d.width,e=d.size-2*b,f=d.size/2*-1+"px";d={position:"absolute",width:e+"px",height:d.size-2*b+"px",backgroundColor:d.fill,border:b+"px "+d.style+" "+d.color};c.isIE&&(d.paddingLeft=e+"px",d.fontSize=e+"px");e=[{top:f,left:f},{top:f,right:f},{bottom:f,right:f},
{bottom:f,left:f}];for(f=0;4>f;f++){var h=0==f||3==f;b=this.util.uid(h?"left_anchor":"right_anchor");var m=c.create("div",{id:b},this.parentNode);c.style(m,c.mixin(c.clone(d),e[f]));var l,n;var g=c.connect(m,"mousedown",this,function(r){h=-1<r.target.id.indexOf("left");a._onAnchor=!0;var t=r.pageX,u=this._box.width;c.stopEvent(r);l=c.connect(document,"mousemove",this,function(p){p=p.pageX;h?(this._box.left=p,this._box.width=u+t-p):this._box.width=p+u-t;c.style(this.parentNode,this._box.toPx())});
n=c.connect(document,"mouseup",this,function(p){t=this._box.left;u=this._box.width;c.disconnect(l);c.disconnect(n);a._onAnchor=!1;k.focus();c.stopEvent(p)})});this._anchors[b]={a:m,cons:[g]}}},destroyAnchors:function(){for(var a in this._anchors)c.forEach(this._anchors[a].con,c.disconnect,c),c.destroy(this._anchors[a].a)},setSavedCaret:function(a){this._caretStart=this._caretEnd=a},getSavedCaret:function(){return{start:this._caretStart,end:this._caretEnd}},insertText:function(a,d){var b=a.innerHTML;
var e=this.getSavedCaret();b=b.replace(/&nbsp;/g," ");b=b.substr(0,e.start)+d+b.substr(e.end);b=this.cleanText(b,!0);this.setSavedCaret(Math.min(b.length,e.end+d.length));a.innerHTML=b;this.setSelection(a,"stored")},getSelection:function(a){if(c.doc.selection){var d=c.doc.selection.createRange(),b=c.body().createTextRange();b.moveToElementText(a);a=b.duplicate();b.moveToBookmark(d.getBookmark());a.setEndPoint("EndToStart",b);this._caretStart=a.text.length;this._caretEnd=a.text.length+d.text.length}else this._caretStart=
c.global.getSelection().getRangeAt(a).startOffset,this._caretEnd=c.global.getSelection().getRangeAt(a).endOffset},setSelection:function(a,d){if(c.doc.selection){var b=c.body().createTextRange();b.moveToElementText(a);switch(d){case "end":b.collapse(!1);break;case "beg":b.collapse();break;case "all":b.collapse();b.moveStart("character",0);b.moveEnd("character",a.text.length);break;case "stored":b.collapse();var e=this._caretStart-this._caretEnd;b.moveStart("character",this._caretStart);b.moveEnd("character",
e)}b.select()}else{var f=function(m,l){l=l||[];for(var n=0;n<m.childNodes.length;n++){var g=m.childNodes[n];3==g.nodeType?l.push(g):g.tagName&&"img"==g.tagName.toLowerCase()&&l.push(g);g.childNodes&&g.childNodes.length&&f(g,l)}return l};a.focus();b=c.global.getSelection();b.removeAllRanges();e=c.doc.createRange();var h=f(a);switch(d){case "end":e.setStart(h[h.length-1],h[h.length-1].textContent.length);e.setEnd(h[h.length-1],h[h.length-1].textContent.length);break;case "beg":e.setStart(h[0],0);e.setEnd(h[0],
0);break;case "all":e.setStart(h[0],0);e.setEnd(h[h.length-1],h[h.length-1].textContent.length);break;case "stored":e.setStart(h[0],this._caretStart),e.setEnd(h[0],this._caretEnd)}b.addRange(e)}}});c.setObject("dojox.drawing.tools.TextBlock",q);q.setup={name:"dojox.drawing.tools.TextBlock",tooltip:"Text Tool",iconClass:"iconText"};w.register(q.setup,"tool");return q});