/dojo-release-1.2.0/dijit/InlineEditBox.js

https://github.com/intrinsyx/dojox-diagram · JavaScript · 210 lines · 203 code · 2 blank · 5 comment · 10 complexity · 2640b18c3831f98dce1a40e2f2b06884 MD5 · raw file

  1. /*
  2. Copyright (c) 2004-2008, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. if(!dojo._hasResource["dijit.InlineEditBox"]){
  7. dojo._hasResource["dijit.InlineEditBox"]=true;
  8. dojo.provide("dijit.InlineEditBox");
  9. dojo.require("dojo.i18n");
  10. dojo.require("dijit._Widget");
  11. dojo.require("dijit._Container");
  12. dojo.require("dijit.form.Button");
  13. dojo.require("dijit.form.TextBox");
  14. dojo.requireLocalization("dijit","common",null,"zh,ca,pt,da,tr,ru,de,sv,ja,he,fi,nb,el,ar,ROOT,pt-pt,cs,fr,es,ko,nl,zh-tw,pl,th,it,hu,sk,sl");
  15. dojo.declare("dijit.InlineEditBox",dijit._Widget,{editing:false,autoSave:true,buttonSave:"",buttonCancel:"",renderAsHtml:false,editor:"dijit.form.TextBox",editorParams:{},onChange:function(_1){
  16. },onCancel:function(){
  17. },width:"100%",value:"",noValueIndicator:"<span style='font-family: wingdings; text-decoration: underline;'>&nbsp;&nbsp;&nbsp;&nbsp;&#x270d;&nbsp;&nbsp;&nbsp;&nbsp;</span>",constructor:function(){
  18. this.editorParams={};
  19. },postMixInProperties:function(){
  20. this.inherited(arguments);
  21. this.displayNode=this.srcNodeRef;
  22. var _2={ondijitclick:"_onClick",onmouseover:"_onMouseOver",onmouseout:"_onMouseOut",onfocus:"_onMouseOver",onblur:"_onMouseOut"};
  23. for(var _3 in _2){
  24. this.connect(this.displayNode,_3,_2[_3]);
  25. }
  26. dijit.setWaiRole(this.displayNode,"button");
  27. if(!this.displayNode.getAttribute("tabIndex")){
  28. this.displayNode.setAttribute("tabIndex",0);
  29. }
  30. this.attr("value",this.value||this.displayNode.innerHTML);
  31. },setDisabled:function(_4){
  32. dojo.deprecated("dijit.InlineEditBox.setDisabled() is deprecated. Use attr('disabled', bool) instead.","","2.0");
  33. this.attr("disabled",_4);
  34. },_setDisabledAttr:function(_5){
  35. this.disabled=_5;
  36. dijit.setWaiState(this.domNode,"disabled",_5);
  37. },_onMouseOver:function(){
  38. dojo.addClass(this.displayNode,this.disabled?"dijitDisabledClickableRegion":"dijitClickableRegion");
  39. },_onMouseOut:function(){
  40. dojo.removeClass(this.displayNode,this.disabled?"dijitDisabledClickableRegion":"dijitClickableRegion");
  41. },_onClick:function(e){
  42. if(this.disabled){
  43. return;
  44. }
  45. if(e){
  46. dojo.stopEvent(e);
  47. }
  48. this._onMouseOut();
  49. setTimeout(dojo.hitch(this,"edit"),0);
  50. },edit:function(){
  51. if(this.disabled||this.editing){
  52. return;
  53. }
  54. this.editing=true;
  55. var _7=(this.renderAsHtml?this.value:this.value.replace(/\s*\r?\n\s*/g,"").replace(/<br\/?>/gi,"\n").replace(/&gt;/g,">").replace(/&lt;/g,"<").replace(/&amp;/g,"&").replace(/&quot;/g,"\""));
  56. var _8=dojo.doc.createElement("span");
  57. dojo.place(_8,this.domNode,"before");
  58. var ew=this.editWidget=new dijit._InlineEditor({value:dojo.trim(_7),autoSave:this.autoSave,buttonSave:this.buttonSave,buttonCancel:this.buttonCancel,renderAsHtml:this.renderAsHtml,editor:this.editor,editorParams:this.editorParams,style:dojo.getComputedStyle(this.displayNode),save:dojo.hitch(this,"save"),cancel:dojo.hitch(this,"cancel"),width:this.width},_8);
  59. var _a=ew.domNode.style;
  60. this.displayNode.style.display="none";
  61. _a.position="static";
  62. _a.visibility="visible";
  63. this.domNode=ew.domNode;
  64. setTimeout(function(){
  65. if(ew.editWidget._resetValue===undefined){
  66. ew.editWidget._resetValue=ew.getValue();
  67. }
  68. ew.focus();
  69. },100);
  70. },_showText:function(_b){
  71. this.displayNode.style.display="";
  72. var ew=this.editWidget;
  73. var _d=ew.domNode.style;
  74. _d.position="absolute";
  75. _d.visibility="hidden";
  76. this.domNode=this.displayNode;
  77. if(_b){
  78. dijit.focus(this.displayNode);
  79. }
  80. _d.display="none";
  81. setTimeout(function(){
  82. ew.destroy();
  83. delete ew;
  84. if(dojo.isIE){
  85. dijit.focus(dijit.getFocus());
  86. }
  87. },1000);
  88. },save:function(_e){
  89. if(this.disabled||!this.editing){
  90. return;
  91. }
  92. this.editing=false;
  93. var _f=this.editWidget.getValue()+"";
  94. this.attr("value",this.renderAsHtml?_f:_f.replace(/&/gm,"&amp;").replace(/</gm,"&lt;").replace(/>/gm,"&gt;").replace(/"/gm,"&quot;").replace(/\n/g,"<br>"));
  95. this.onChange(_f);
  96. this._showText(_e);
  97. },setValue:function(val){
  98. dojo.deprecated("dijit.InlineEditBox.setValue() is deprecated. Use attr('value', ...) instead.","","2.0");
  99. return this.attr("value",val);
  100. },_setValueAttr:function(val){
  101. this.value=val;
  102. this.displayNode.innerHTML=dojo.trim(val)||this.noValueIndicator;
  103. },getValue:function(){
  104. dojo.deprecated("dijit.InlineEditBox.getValue() is deprecated. Use attr('value') instead.","","2.0");
  105. return this.attr("value");
  106. },cancel:function(_12){
  107. this.editing=false;
  108. this.onCancel();
  109. this._showText(_12);
  110. }});
  111. dojo.declare("dijit._InlineEditor",[dijit._Widget,dijit._Templated],{templateString:"<span dojoAttachPoint=\"editNode\" waiRole=\"presentation\" style=\"position: absolute; visibility:hidden\" class=\"dijitReset dijitInline\"\n\tdojoAttachEvent=\"onkeypress: _onKeyPress\" \n\t><span dojoAttachPoint=\"editorPlaceholder\"></span\n\t><span dojoAttachPoint=\"buttonContainer\"\n\t\t><button class='saveButton' dojoAttachPoint=\"saveButton\" dojoType=\"dijit.form.Button\" dojoAttachEvent=\"onClick:save\" disabled=\"true\" label=\"${buttonSave}\"></button\n\t\t><button class='cancelButton' dojoAttachPoint=\"cancelButton\" dojoType=\"dijit.form.Button\" dojoAttachEvent=\"onClick:cancel\" label=\"${buttonCancel}\"></button\n\t></span\n></span>\n",widgetsInTemplate:true,postMixInProperties:function(){
  112. this.inherited(arguments);
  113. this.messages=dojo.i18n.getLocalization("dijit","common",this.lang);
  114. dojo.forEach(["buttonSave","buttonCancel"],function(_13){
  115. if(!this[_13]){
  116. this[_13]=this.messages[_13];
  117. }
  118. },this);
  119. },postCreate:function(){
  120. var cls=dojo.getObject(this.editor);
  121. var _15=this.style;
  122. var _16="line-height:"+_15.lineHeight+";";
  123. dojo.forEach(["Weight","Family","Size","Style"],function(_17){
  124. _16+="font-"+_17+":"+_15["font"+_17]+";";
  125. },this);
  126. dojo.forEach(["marginTop","marginBottom","marginLeft","marginRight"],function(_18){
  127. this.domNode.style[_18]=_15[_18];
  128. },this);
  129. if(this.width=="100%"){
  130. _16+="width:100%;";
  131. this.domNode.style.display="block";
  132. }else{
  133. _16+="width:"+(this.width+(Number(this.width)==this.width?"px":""))+";";
  134. }
  135. this.editorParams.style=_16;
  136. this.editorParams["displayedValue" in cls.prototype?"displayedValue":"value"]=this.value;
  137. var ew=this.editWidget=new cls(this.editorParams,this.editorPlaceholder);
  138. this.connect(ew,"onChange","_onChange");
  139. this.connect(ew,"onKeyPress","_onKeyPress");
  140. if(this.autoSave){
  141. this.buttonContainer.style.display="none";
  142. }
  143. },destroy:function(){
  144. this.editWidget.destroy();
  145. this.inherited(arguments);
  146. },getValue:function(){
  147. var ew=this.editWidget;
  148. return ew.attr("displayedValue" in ew?"displayedValue":"value");
  149. },_onKeyPress:function(e){
  150. if(this._exitInProgress){
  151. return;
  152. }
  153. if(this.autoSave){
  154. if(e.altKey||e.ctrlKey){
  155. return;
  156. }
  157. if(e.charOrCode==dojo.keys.ESCAPE){
  158. dojo.stopEvent(e);
  159. this._exitInProgress=true;
  160. this.cancel(true);
  161. }else{
  162. if(e.charOrCode==dojo.keys.ENTER){
  163. dojo.stopEvent(e);
  164. this._exitInProgress=true;
  165. this.save(true);
  166. }else{
  167. if(e.charOrCode===dojo.keys.TAB){
  168. this._exitInProgress=true;
  169. setTimeout(dojo.hitch(this,"save",false),0);
  170. }
  171. }
  172. }
  173. }else{
  174. var _1c=this;
  175. setTimeout(function(){
  176. _1c.saveButton.attr("disabled",_1c.getValue()==_1c.editWidget._resetValue);
  177. },100);
  178. }
  179. },_onBlur:function(){
  180. this.inherited(arguments);
  181. if(this._exitInProgress){
  182. return;
  183. }
  184. if(this.autoSave){
  185. this._exitInProgress=true;
  186. if(this.getValue()==this.editWidget._resetValue){
  187. this.cancel(false);
  188. }else{
  189. this.save(false);
  190. }
  191. }
  192. },_onChange:function(){
  193. if(this._exitInProgress){
  194. return;
  195. }
  196. if(this.autoSave){
  197. this._exitInProgress=true;
  198. this.save(true);
  199. }else{
  200. this.saveButton.attr("disabled",(this.getValue()==this.editWidget._resetValue)||!this.enableSave());
  201. }
  202. },enableSave:function(){
  203. return this.editWidget.isValid?this.editWidget.isValid():true;
  204. },focus:function(){
  205. this.editWidget.focus();
  206. dijit.selectInputText(this.editWidget.focusNode);
  207. }});
  208. }