SimpleTextarea.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. define("dijit/form/SimpleTextarea", [
  2. "dojo/_base/declare", // declare
  3. "dojo/dom-class", // domClass.add
  4. "dojo/_base/sniff", // has("ie") has("opera")
  5. "dojo/_base/window", // win.doc.selection win.doc.selection.createRange
  6. "./TextBox"
  7. ], function(declare, domClass, has, win, TextBox){
  8. /*=====
  9. var TextBox = dijit.form.TextBox;
  10. =====*/
  11. // module:
  12. // dijit/form/SimpleTextarea
  13. // summary:
  14. // A simple textarea that degrades, and responds to
  15. // minimal LayoutContainer usage, and works with dijit.form.Form.
  16. // Doesn't automatically size according to input, like Textarea.
  17. return declare("dijit.form.SimpleTextarea", TextBox, {
  18. // summary:
  19. // A simple textarea that degrades, and responds to
  20. // minimal LayoutContainer usage, and works with dijit.form.Form.
  21. // Doesn't automatically size according to input, like Textarea.
  22. //
  23. // example:
  24. // | <textarea data-dojo-type="dijit.form.SimpleTextarea" name="foo" value="bar" rows=30 cols=40></textarea>
  25. //
  26. // example:
  27. // | new dijit.form.SimpleTextarea({ rows:20, cols:30 }, "foo");
  28. baseClass: "dijitTextBox dijitTextArea",
  29. // rows: Number
  30. // The number of rows of text.
  31. rows: "3",
  32. // rows: Number
  33. // The number of characters per line.
  34. cols: "20",
  35. templateString: "<textarea ${!nameAttrSetting} data-dojo-attach-point='focusNode,containerNode,textbox' autocomplete='off'></textarea>",
  36. postMixInProperties: function(){
  37. // Copy value from srcNodeRef, unless user specified a value explicitly (or there is no srcNodeRef)
  38. // TODO: parser will handle this in 2.0
  39. if(!this.value && this.srcNodeRef){
  40. this.value = this.srcNodeRef.value;
  41. }
  42. this.inherited(arguments);
  43. },
  44. buildRendering: function(){
  45. this.inherited(arguments);
  46. if(has("ie") && this.cols){ // attribute selectors is not supported in IE6
  47. domClass.add(this.textbox, "dijitTextAreaCols");
  48. }
  49. },
  50. filter: function(/*String*/ value){
  51. // Override TextBox.filter to deal with newlines... specifically (IIRC) this is for IE which writes newlines
  52. // as \r\n instead of just \n
  53. if(value){
  54. value = value.replace(/\r/g,"");
  55. }
  56. return this.inherited(arguments);
  57. },
  58. _onInput: function(/*Event?*/ e){
  59. // Override TextBox._onInput() to enforce maxLength restriction
  60. if(this.maxLength){
  61. var maxLength = parseInt(this.maxLength);
  62. var value = this.textbox.value.replace(/\r/g,'');
  63. var overflow = value.length - maxLength;
  64. if(overflow > 0){
  65. var textarea = this.textbox;
  66. if(textarea.selectionStart){
  67. var pos = textarea.selectionStart;
  68. var cr = 0;
  69. if(has("opera")){
  70. cr = (this.textbox.value.substring(0,pos).match(/\r/g) || []).length;
  71. }
  72. this.textbox.value = value.substring(0,pos-overflow-cr)+value.substring(pos-cr);
  73. textarea.setSelectionRange(pos-overflow, pos-overflow);
  74. }else if(win.doc.selection){ //IE
  75. textarea.focus();
  76. var range = win.doc.selection.createRange();
  77. // delete overflow characters
  78. range.moveStart("character", -overflow);
  79. range.text = '';
  80. // show cursor
  81. range.select();
  82. }
  83. }
  84. }
  85. this.inherited(arguments);
  86. }
  87. });
  88. });