//>>built
define("dojox/drawing/tools/TextBlock",["dojo","dijit/registry","../util/oo","../manager/_registry","../stencil/Text"],function(c,q,n,r,t){var h;c.addOnLoad(function(){(h=c.byId("conEdit"))?h.parentNode.removeChild(h):console.error("A contenteditable div is missing from the main document. See 'dojox.drawing.tools.TextBlock'")});n=n.declare(t,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 a={},c;for(c in this)a[c]="number"==typeof this[c]&&"zIndex"!=c?this[c]+"px":this[c];return a}};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(h);c.style(h,
{height:a?"auto":this._lineHeight+"px",fontSize:this.textSize/this.mouse.zoom+"px",fontFamily:this.style.text.family});h.innerHTML=a||"";return h},connectTextField:function(){if(!this._textConnected){var a=q.byId("greekPalette"),d=void 0==a?!1:!0;d&&c.mixin(a,{_pushChangeTo:h,_textBlock:this});this._textConnected=!0;this._dropMode=!1;this.mouse.setEventMode("TEXT");this.keys.editMode(!0);var b,e,g,f,k=this,l=!1,m=function(){k._dropMode||(c.forEach([b,e,g,f],function(a){c.disconnect(a)}),k._textConnected=
!1,k.keys.editMode(!1),k.mouse.setEventMode(),k.execText())};b=c.connect(h,"keyup",this,function(b){c.trim(h.innerHTML)&&!l?(c.style(h,"height","auto"),l=!0):2>c.trim(h.innerHTML).length&&l&&(c.style(h,"height",this._lineHeight+"px"),l=!1);if(this._blockExec)b.keyCode==c.keys.SPACE&&(c.stopEvent(b),d&&a.onCancel());else if(13==b.keyCode||27==b.keyCode)c.stopEvent(b),m()});e=c.connect(h,"keydown",this,function(b){13!=b.keyCode&&27!=b.keyCode||c.stopEvent(b);if(220==b.keyCode){if(!d){console.info("For greek letter assistance instantiate: dojox.drawing.plugins.drawing.GreekPalette");
return}c.stopEvent(b);this.getSelection(h);this.insertText(h,"\\");this._blockExec=this._dropMode=!0;a.show({around:this.parentNode,orient:{BL:"TL"}})}if(this._dropMode)switch(b.keyCode){case c.keys.UP_ARROW:case c.keys.DOWN_ARROW:case c.keys.LEFT_ARROW:case c.keys.RIGHT_ARROW:c.stopEvent(b);a._navigateByArrow(b);break;case c.keys.ENTER:c.stopEvent(b);a._onCellClick(b);break;case c.keys.BACKSPACE:case c.keys.DELETE:c.stopEvent(b),a.onCancel()}else this._blockExec=!1});g=c.connect(document,"mouseup",
this,function(a){this._onAnchor||"conEdit"==a.target.id?"conEdit"==a.target.id&&""==h.innerHTML&&(h.blur(),setTimeout(function(){h.focus()},200)):(c.stopEvent(a),m())});this.createAnchors();f=c.connect(this.mouse,"setZoom",this,function(a){m()});h.focus();this.onDown=function(){};this.onDrag=function(){};setTimeout(c.hitch(this,function(){h.focus();this.onUp=function(){!k._onAnchor&&this.parentNode&&(k.disconnectMouse(),m(),k.onUp=function(){})}}),500)}},execText:function(){var a=c.marginBox(this.parentNode),
a=Math.max(a.w,this.style.text.minWidth),d=this.cleanText(h.innerHTML,!0);h.innerHTML="";h.blur();this.destroyAnchors();var d=this.typesetter(d),d=this.measureText(d,a),b=this.mouse.scrollOffset(),e=this.mouse.origin,g=this._box.left+b.left-e.x,b=this._box.top+b.top-e.y,g=g*this.mouse.zoom,b=b*this.mouse.zoom,a=a*this.mouse.zoom;d.h*=this.mouse.zoom;this.points=[{x:g,y:b},{x:g+a,y:b},{x:g+a,y:b+d.h},{x:g,y:b+d.h}];this.editMode=!1;console.log("EXEC TEXT::::",this._postRenderCon);d.text||(this._text=
"",this._textArray=[]);this.render(d.text);this.onChangeText(this.getText())},edit:function(){this.editMode=!0;var a=this.getText()||"";console.log("EDIT TEXT:",a," ",a.replace("/n"," "));if(!this.parentNode&&this.points){var c=this.pointsToData(),b=this.mouse.scrollOffset(),e=this.mouse.origin,c={pageX:c.x/this.mouse.zoom-b.left+e.x,pageY:c.y/this.mouse.zoom-b.top+e.y,width:c.width/this.mouse.zoom,height:c.height/this.mouse.zoom};this.remove(this.shape,this.hit);this.showParent(c);this.createTextField(a.replace("/n",
" "));this.connectTextField();a&&this.setSelection(h,"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(a){var c={"\x26lt;":"\x3c","\x26gt;":"\x3e","\x26amp;":"\x26"},b;for(b in c)a=a.replace(new RegExp(b,"gi"),c[b]);return a}(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=h;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 g=a.split(" "),f=[[]],k=0;for(e.innerHTML="";g.length;){var l=g.shift();e.innerHTML+=l+" ";c.marginBox(e).h>b&&(k++,f[k]=[],e.innerHTML=l+" ");f[k].push(l)}c.forEach(f,function(a,c){f[c]=a.join(" ")});
b=f.join("\n");e.innerHTML=b.replace("\n","\x3cbr/\x3e")}e=c.marginBox(e);h.parentNode.removeChild(h);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,g=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:g,left:g},{top:g,right:g},{bottom:g,right:g},{bottom:g,left:g}];for(g=0;4>g;g++){var f=0==g||3==g,b=this.util.uid(f?"left_anchor":"right_anchor"),k=c.create("div",{id:b},this.parentNode);c.style(k,c.mixin(c.clone(d),e[g]));var l,m,p;l=c.connect(k,"mousedown",this,function(b){f=-1<b.target.id.indexOf("left");a._onAnchor=!0;var d=b.pageX,e=this._box.width;c.stopEvent(b);m=c.connect(document,
"mousemove",this,function(a){a=a.pageX;f?(this._box.left=a,this._box.width=e+d-a):this._box.width=a+e-d;c.style(this.parentNode,this._box.toPx())});p=c.connect(document,"mouseup",this,function(b){d=this._box.left;e=this._box.width;c.disconnect(m);c.disconnect(p);a._onAnchor=!1;h.focus();c.stopEvent(b)})});this._anchors[b]={a:k,cons:[l]}}},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,c){var b;b=a.innerHTML;var d=this.getSavedCaret();b=b.replace(/&nbsp;/g," ");b=b.substr(0,d.start)+c+b.substr(d.end);b=this.cleanText(b,!0);this.setSavedCaret(Math.min(b.length,d.end+c.length));a.innerHTML=b;this.setSelection(a,"stored")},getSelection:function(a){var d,b;c.doc.selection?(b=c.doc.selection.createRange(),d=c.body().createTextRange(),d.moveToElementText(a),a=d.duplicate(),
d.moveToBookmark(b.getBookmark()),a.setEndPoint("EndToStart",d),d=this._caretStart=a.text.length,b=this._caretEnd=a.text.length+b.text.length,console.warn("Caret start: ",d," end: ",b," length: ",a.text.length," text: ",a.text)):(this._caretStart=c.global.getSelection().getRangeAt(a).startOffset,this._caretEnd=c.global.getSelection().getRangeAt(a).endOffset,console.log("Caret start: ",this._caretStart," end: ",this._caretEnd))},setSelection:function(a,d){console.warn("setSelection:");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 g=function(a,b){b=b||[];for(var c=0;c<a.childNodes.length;c++){var d=a.childNodes[c];3==d.nodeType?b.push(d):d.tagName&&"img"==d.tagName.toLowerCase()&&
b.push(d);d.childNodes&&d.childNodes.length&&g(d,b)}return b};console.log("ff node:",a);a.focus();b=c.global.getSelection();b.removeAllRanges();var e=c.doc.createRange(),f=g(a);switch(d){case "end":console.log("len:",f[f.length-1].textContent.length);e.setStart(f[f.length-1],f[f.length-1].textContent.length);e.setEnd(f[f.length-1],f[f.length-1].textContent.length);break;case "beg":e.setStart(f[0],0);e.setEnd(f[0],0);break;case "all":e.setStart(f[0],0);e.setEnd(f[f.length-1],f[f.length-1].textContent.length);
break;case "stored":console.log("Caret start: ",this._caretStart," caret end: ",this._caretEnd),e.setStart(f[0],this._caretStart),e.setEnd(f[0],this._caretEnd)}b.addRange(e);console.log("sel ",d," on ",a)}}});c.setObject("dojox.drawing.tools.TextBlock",n);n.setup={name:"dojox.drawing.tools.TextBlock",tooltip:"Text Tool",iconClass:"iconText"};r.register(n.setup,"tool");return n});