SimpleTextarea.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Copyright (c) 2004-2012, 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.form.SimpleTextarea"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
  7. dojo._hasResource["dijit.form.SimpleTextarea"] = true;
  8. dojo.provide("dijit.form.SimpleTextarea");
  9. dojo.require("dijit.form.TextBox");
  10. dojo.declare("dijit.form.SimpleTextarea",
  11. dijit.form.TextBox,
  12. {
  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. //
  18. // example:
  19. // | <textarea dojoType="dijit.form.SimpleTextarea" name="foo" value="bar" rows=30 cols=40></textarea>
  20. //
  21. // example:
  22. // | new dijit.form.SimpleTextarea({ rows:20, cols:30 }, "foo");
  23. baseClass: "dijitTextBox dijitTextArea",
  24. attributeMap: dojo.delegate(dijit.form._FormValueWidget.prototype.attributeMap, {
  25. rows:"textbox", cols: "textbox"
  26. }),
  27. // rows: Number
  28. // The number of rows of text.
  29. rows: "3",
  30. // rows: Number
  31. // The number of characters per line.
  32. cols: "20",
  33. templateString: "<textarea ${!nameAttrSetting} dojoAttachPoint='focusNode,containerNode,textbox' autocomplete='off'></textarea>",
  34. postMixInProperties: function(){
  35. // Copy value from srcNodeRef, unless user specified a value explicitly (or there is no srcNodeRef)
  36. // TODO: parser will handle this in 2.0
  37. if(!this.value && this.srcNodeRef){
  38. this.value = this.srcNodeRef.value;
  39. }
  40. this.inherited(arguments);
  41. },
  42. buildRendering: function(){
  43. this.inherited(arguments);
  44. if(dojo.isIE && this.cols){ // attribute selectors is not supported in IE6
  45. dojo.addClass(this.textbox, "dijitTextAreaCols");
  46. }
  47. },
  48. filter: function(/*String*/ value){
  49. // Override TextBox.filter to deal with newlines... specifically (IIRC) this is for IE which writes newlines
  50. // as \r\n instead of just \n
  51. if(value){
  52. value = value.replace(/\r/g,"");
  53. }
  54. return this.inherited(arguments);
  55. },
  56. _previousValue: "",
  57. _onInput: function(/*Event?*/ e){
  58. // Override TextBox._onInput() to enforce maxLength restriction
  59. if(this.maxLength){
  60. var maxLength = parseInt(this.maxLength);
  61. var value = this.textbox.value.replace(/\r/g,'');
  62. var overflow = value.length - maxLength;
  63. if(overflow > 0){
  64. if(e){ dojo.stopEvent(e); }
  65. var textarea = this.textbox;
  66. if(textarea.selectionStart){
  67. var pos = textarea.selectionStart;
  68. var cr = 0;
  69. if(dojo.isOpera){
  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(dojo.doc.selection){ //IE
  75. textarea.focus();
  76. var range = dojo.doc.selection.createRange();
  77. // delete overflow characters
  78. range.moveStart("character", -overflow);
  79. range.text = '';
  80. // show cursor
  81. range.select();
  82. }
  83. }
  84. this._previousValue = this.textbox.value;
  85. }
  86. this.inherited(arguments);
  87. }
  88. });
  89. }