TextBox.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. require({cache:{
  2. 'url:dijit/form/templates/TextBox.html':"<div class=\"dijit dijitReset dijitInline dijitLeft\" id=\"widget_${id}\" role=\"presentation\"\n\t><div class=\"dijitReset dijitInputField dijitInputContainer\"\n\t\t><input class=\"dijitReset dijitInputInner\" data-dojo-attach-point='textbox,focusNode' autocomplete=\"off\"\n\t\t\t${!nameAttrSetting} type='${type}'\n\t/></div\n></div>\n"}});
  3. define("dijit/form/TextBox", [
  4. "dojo/_base/declare", // declare
  5. "dojo/dom-construct", // domConstruct.create
  6. "dojo/dom-style", // domStyle.getComputedStyle
  7. "dojo/_base/kernel", // kernel.deprecated
  8. "dojo/_base/lang", // lang.hitch
  9. "dojo/_base/sniff", // has("ie") has("mozilla")
  10. "dojo/_base/window", // win.doc.selection.createRange
  11. "./_FormValueWidget",
  12. "./_TextBoxMixin",
  13. "dojo/text!./templates/TextBox.html",
  14. ".." // to export dijit._setSelectionRange, remove in 2.0
  15. ], function(declare, domConstruct, domStyle, kernel, lang, has, win,
  16. _FormValueWidget, _TextBoxMixin, template, dijit){
  17. /*=====
  18. var _FormValueWidget = dijit.form._FormValueWidget;
  19. var _TextBoxMixin = dijit.form._TextBoxMixin;
  20. =====*/
  21. // module:
  22. // dijit/form/TextBox
  23. // summary:
  24. // A base class for textbox form inputs
  25. var TextBox = declare(/*====="dijit.form.TextBox", =====*/ [_FormValueWidget, _TextBoxMixin], {
  26. // summary:
  27. // A base class for textbox form inputs
  28. templateString: template,
  29. _singleNodeTemplate: '<input class="dijit dijitReset dijitLeft dijitInputField" data-dojo-attach-point="textbox,focusNode" autocomplete="off" type="${type}" ${!nameAttrSetting} />',
  30. _buttonInputDisabled: has("ie") ? "disabled" : "", // allows IE to disallow focus, but Firefox cannot be disabled for mousedown events
  31. baseClass: "dijitTextBox",
  32. postMixInProperties: function(){
  33. var type = this.type.toLowerCase();
  34. if(this.templateString && this.templateString.toLowerCase() == "input" || ((type == "hidden" || type == "file") && this.templateString == this.constructor.prototype.templateString)){
  35. this.templateString = this._singleNodeTemplate;
  36. }
  37. this.inherited(arguments);
  38. },
  39. _onInput: function(e){
  40. this.inherited(arguments);
  41. if(this.intermediateChanges){ // _TextBoxMixin uses onInput
  42. var _this = this;
  43. // the setTimeout allows the key to post to the widget input box
  44. setTimeout(function(){ _this._handleOnChange(_this.get('value'), false); }, 0);
  45. }
  46. },
  47. _setPlaceHolderAttr: function(v){
  48. this._set("placeHolder", v);
  49. if(!this._phspan){
  50. this._attachPoints.push('_phspan');
  51. // dijitInputField class gives placeHolder same padding as the input field
  52. // parent node already has dijitInputField class but it doesn't affect this <span>
  53. // since it's position: absolute.
  54. this._phspan = domConstruct.create('span',{ onmousedown:function(e){ e.preventDefault(); }, className:'dijitPlaceHolder dijitInputField'},this.textbox,'after');
  55. }
  56. this._phspan.innerHTML="";
  57. this._phspan.appendChild(document.createTextNode(v));
  58. this._updatePlaceHolder();
  59. },
  60. _updatePlaceHolder: function(){
  61. if(this._phspan){
  62. this._phspan.style.display=(this.placeHolder&&!this.focused&&!this.textbox.value)?"":"none";
  63. }
  64. },
  65. _setValueAttr: function(value, /*Boolean?*/ priorityChange, /*String?*/ formattedValue){
  66. this.inherited(arguments);
  67. this._updatePlaceHolder();
  68. },
  69. getDisplayedValue: function(){
  70. // summary:
  71. // Deprecated. Use get('displayedValue') instead.
  72. // tags:
  73. // deprecated
  74. kernel.deprecated(this.declaredClass+"::getDisplayedValue() is deprecated. Use set('displayedValue') instead.", "", "2.0");
  75. return this.get('displayedValue');
  76. },
  77. setDisplayedValue: function(/*String*/ value){
  78. // summary:
  79. // Deprecated. Use set('displayedValue', ...) instead.
  80. // tags:
  81. // deprecated
  82. kernel.deprecated(this.declaredClass+"::setDisplayedValue() is deprecated. Use set('displayedValue', ...) instead.", "", "2.0");
  83. this.set('displayedValue', value);
  84. },
  85. _onBlur: function(e){
  86. if(this.disabled){ return; }
  87. this.inherited(arguments);
  88. this._updatePlaceHolder();
  89. },
  90. _onFocus: function(/*String*/ by){
  91. if(this.disabled || this.readOnly){ return; }
  92. this.inherited(arguments);
  93. this._updatePlaceHolder();
  94. }
  95. });
  96. if(has("ie") < 9){
  97. TextBox = declare(/*===== "dijit.form.TextBox.IEMixin", =====*/ TextBox, {
  98. declaredClass: "dijit.form.TextBox", // for user code referencing declaredClass
  99. _isTextSelected: function(){
  100. var range = win.doc.selection.createRange();
  101. var parent = range.parentElement();
  102. return parent == this.textbox && range.text.length == 0;
  103. },
  104. postCreate: function(){
  105. this.inherited(arguments);
  106. // IE INPUT tag fontFamily has to be set directly using STYLE
  107. // the setTimeout gives IE a chance to render the TextBox and to deal with font inheritance
  108. setTimeout(lang.hitch(this, function(){
  109. try{
  110. var s = domStyle.getComputedStyle(this.domNode); // can throw an exception if widget is immediately destroyed
  111. if(s){
  112. var ff = s.fontFamily;
  113. if(ff){
  114. var inputs = this.domNode.getElementsByTagName("INPUT");
  115. if(inputs){
  116. for(var i=0; i < inputs.length; i++){
  117. inputs[i].style.fontFamily = ff;
  118. }
  119. }
  120. }
  121. }
  122. }catch(e){/*when used in a Dialog, and this is called before the dialog is
  123. shown, s.fontFamily would trigger "Invalid Argument" error.*/}
  124. }), 0);
  125. }
  126. });
  127. // Overrides definition of _setSelectionRange from _TextBoxMixin (TODO: move to _TextBoxMixin.js?)
  128. dijit._setSelectionRange = _TextBoxMixin._setSelectionRange = function(/*DomNode*/ element, /*Number?*/ start, /*Number?*/ stop){
  129. if(element.createTextRange){
  130. var r = element.createTextRange();
  131. r.collapse(true);
  132. r.moveStart("character", -99999); // move to 0
  133. r.moveStart("character", start); // delta from 0 is the correct position
  134. r.moveEnd("character", stop-start);
  135. r.select();
  136. }
  137. }
  138. }else if(has("mozilla")){
  139. TextBox = declare(/*===== "dijit.form.TextBox.MozMixin", =====*/TextBox, {
  140. declaredClass: "dijit.form.TextBox", // for user code referencing declaredClass
  141. _onBlur: function(e){
  142. this.inherited(arguments);
  143. if(this.selectOnClick){
  144. // clear selection so that the next mouse click doesn't reselect
  145. this.textbox.selectionStart = this.textbox.selectionEnd = undefined;
  146. }
  147. }
  148. });
  149. }else{
  150. TextBox.prototype.declaredClass = "dijit.form.TextBox";
  151. }
  152. lang.setObject("dijit.form.TextBox", TextBox); // don't do direct assignment, it confuses API doc parser
  153. return TextBox;
  154. });