ligerTextBox.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /**
  2. * jQuery ligerUI 1.1.9
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2012 [ gd_star@163.com ]
  7. *
  8. */
  9. (function ($)
  10. {
  11. $.fn.ligerTextBox = function ()
  12. {
  13. return $.ligerui.run.call(this, "ligerTextBox", arguments);
  14. };
  15. $.fn.ligerGetTextBoxManager = function ()
  16. {
  17. return $.ligerui.run.call(this, "ligerGetTextBoxManager", arguments);
  18. };
  19. $.ligerDefaults.TextBox = {
  20. onChangeValue: null,
  21. width: null,
  22. disabled: false,
  23. value: null, //初始化值
  24. nullText: null, //不能为空时的提示
  25. digits: false, //是否限定为数字输入框
  26. number: false //是否限定为浮点数格式输入框
  27. };
  28. $.ligerui.controls.TextBox = function (element, options)
  29. {
  30. $.ligerui.controls.TextBox.base.constructor.call(this, element, options);
  31. };
  32. $.ligerui.controls.TextBox.ligerExtend($.ligerui.controls.Input, {
  33. __getType: function ()
  34. {
  35. return 'TextBox'
  36. },
  37. __idPrev: function ()
  38. {
  39. return 'TextBox';
  40. },
  41. _init: function ()
  42. {
  43. $.ligerui.controls.TextBox.base._init.call(this);
  44. var g = this, p = this.options;
  45. if (!p.width)
  46. {
  47. p.width = $(g.element).width();
  48. }
  49. if ($(this.element).attr("readonly"))
  50. {
  51. p.disabled = true;
  52. }
  53. },
  54. _render: function ()
  55. {
  56. var g = this, p = this.options;
  57. g.inputText = $(this.element);
  58. //外层
  59. g.wrapper = g.inputText.wrap('<div class="l-text"></div>').parent();
  60. g.wrapper.append('<div class="l-text-l"></div><div class="l-text-r"></div>');
  61. if (!g.inputText.hasClass("l-text-field"))
  62. g.inputText.addClass("l-text-field");
  63. this._setEvent();
  64. g.set(p);
  65. g.checkValue();
  66. },
  67. _getValue: function ()
  68. {
  69. return this.inputText.val();
  70. },
  71. _setNullText: function ()
  72. {
  73. this.checkNotNull();
  74. },
  75. checkValue: function ()
  76. {
  77. var g = this, p = this.options;
  78. var v = g.inputText.val();
  79. if (p.number && !/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(v) || p.digits && !/^\d+$/.test(v))
  80. {
  81. g.inputText.val(g.value || 0);
  82. return;
  83. }
  84. g.value = v;
  85. },
  86. checkNotNull: function ()
  87. {
  88. var g = this, p = this.options;
  89. if (p.nullText && !p.disabled)
  90. {
  91. if (!g.inputText.val())
  92. {
  93. g.inputText.addClass("l-text-field-null").val(p.nullText);
  94. }
  95. }
  96. },
  97. _setEvent: function ()
  98. {
  99. var g = this, p = this.options;
  100. g.inputText.bind('blur.textBox', function ()
  101. {
  102. g.trigger('blur');
  103. g.checkNotNull();
  104. g.checkValue();
  105. g.wrapper.removeClass("l-text-focus");
  106. }).bind('focus.textBox', function ()
  107. {
  108. g.trigger('focus');
  109. if (p.nullText)
  110. {
  111. if ($(this).hasClass("l-text-field-null"))
  112. {
  113. $(this).removeClass("l-text-field-null").val("");
  114. }
  115. }
  116. g.wrapper.addClass("l-text-focus");
  117. })
  118. .change(function ()
  119. {
  120. g.trigger('changeValue', [this.value]);
  121. });
  122. g.wrapper.hover(function ()
  123. {
  124. g.trigger('mouseOver');
  125. g.wrapper.addClass("l-text-over");
  126. }, function ()
  127. {
  128. g.trigger('mouseOut');
  129. g.wrapper.removeClass("l-text-over");
  130. });
  131. },
  132. _setDisabled: function (value)
  133. {
  134. if (value)
  135. {
  136. this.inputText.attr("readonly", "readonly");
  137. this.wrapper.addClass("l-text-disabled");
  138. }
  139. else
  140. {
  141. this.inputText.removeAttr("readonly");
  142. this.wrapper.removeClass('l-text-disabled');
  143. }
  144. },
  145. _setWidth: function (value)
  146. {
  147. if (value > 20)
  148. {
  149. this.wrapper.css({ width: value });
  150. this.inputText.css({ width: value - 4 });
  151. }
  152. },
  153. _setHeight: function (value)
  154. {
  155. if (value > 10)
  156. {
  157. this.wrapper.height(value);
  158. this.inputText.height(value - 2);
  159. }
  160. },
  161. _setValue: function (value)
  162. {
  163. if (value != null)
  164. this.inputText.val(value);
  165. },
  166. _setLabel: function (value)
  167. {
  168. var g = this, p = this.options;
  169. if (!g.labelwrapper)
  170. {
  171. g.labelwrapper = g.wrapper.wrap('<div class="l-labeltext"></div>').parent();
  172. var lable = $('<div class="l-text-label" style="float:left;">' + value + ':&nbsp</div>');
  173. g.labelwrapper.prepend(lable);
  174. g.wrapper.css('float', 'left');
  175. if (!p.labelWidth)
  176. {
  177. p.labelWidth = lable.width();
  178. }
  179. else
  180. {
  181. g._setLabelWidth(p.labelWidth);
  182. }
  183. lable.height(g.wrapper.height());
  184. if (p.labelAlign)
  185. {
  186. g._setLabelAlign(p.labelAlign);
  187. }
  188. g.labelwrapper.append('<br style="clear:both;" />');
  189. g.labelwrapper.width(p.labelWidth + p.width + 2);
  190. }
  191. else
  192. {
  193. g.labelwrapper.find(".l-text-label").html(value + ':&nbsp');
  194. }
  195. },
  196. _setLabelWidth: function (value)
  197. {
  198. var g = this, p = this.options;
  199. if (!g.labelwrapper) return;
  200. g.labelwrapper.find(".l-text-label").width(value);
  201. },
  202. _setLabelAlign: function (value)
  203. {
  204. var g = this, p = this.options;
  205. if (!g.labelwrapper) return;
  206. g.labelwrapper.find(".l-text-label").css('text-align', value);
  207. },
  208. updateStyle: function ()
  209. {
  210. var g = this, p = this.options;
  211. if (g.inputText.attr('disabled') || g.inputText.attr('readonly'))
  212. {
  213. g.wrapper.addClass("l-text-disabled");
  214. g.options.disabled = true;
  215. }
  216. else
  217. {
  218. g.wrapper.removeClass("l-text-disabled");
  219. g.options.disabled = false;
  220. }
  221. if (g.inputText.hasClass("l-text-field-null") && g.inputText.val() != p.nullText)
  222. {
  223. g.inputText.removeClass("l-text-field-null");
  224. }
  225. g.checkValue();
  226. }
  227. });
  228. })(jQuery);