UU跑腿标准版

ligerTextBox.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /**
  2. * jQuery ligerUI 1.2.4
  3. *
  4. * http://ligerui.com
  5. *
  6. * Author daomi 2014 [ 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. onMouseOver: null,
  22. onMouseOut: null,
  23. onBlur: null,
  24. onFocus: null,
  25. width: null,
  26. disabled: false,
  27. value: null, //初始化值
  28. nullText: null, //不能为空时的提示
  29. digits: false, //是否限定为数字输入框
  30. number: false, //是否限定为浮点数格式输入框
  31. currency: false, //是否显示为货币形式
  32. readonly: false //是否只读
  33. };
  34. $.ligerui.controls.TextBox = function (element, options)
  35. {
  36. $.ligerui.controls.TextBox.base.constructor.call(this, element, options);
  37. };
  38. $.ligerui.controls.TextBox.ligerExtend($.ligerui.controls.Input, {
  39. __getType: function ()
  40. {
  41. return 'TextBox'
  42. },
  43. __idPrev: function ()
  44. {
  45. return 'TextBox';
  46. },
  47. _init: function ()
  48. {
  49. $.ligerui.controls.TextBox.base._init.call(this);
  50. var g = this, p = this.options;
  51. if (!p.width)
  52. {
  53. p.width = $(g.element).width();
  54. }
  55. if ($(this.element).attr("readonly"))
  56. {
  57. p.readonly = true;
  58. } else if (p.readonly)
  59. {
  60. $(this.element).attr("readonly", true);
  61. }
  62. },
  63. _render: function ()
  64. {
  65. var g = this, p = this.options;
  66. g.inputText = $(this.element);
  67. //外层
  68. g.wrapper = g.inputText.wrap('<div class="l-text"></div>').parent();
  69. g.wrapper.append('<div class="l-text-l"></div><div class="l-text-r"></div>');
  70. if (!g.inputText.hasClass("l-text-field"))
  71. g.inputText.addClass("l-text-field");
  72. this._setEvent();
  73. if (p.digits || p.number || p.currency)
  74. {
  75. g.inputText.addClass("l-text-field-number");
  76. }
  77. g.set(p);
  78. g.checkValue();
  79. },
  80. destroy: function ()
  81. {
  82. var g = this;
  83. if (g.wrapper)
  84. {
  85. g.wrapper.remove();
  86. }
  87. g.options = null;
  88. liger.remove(this);
  89. },
  90. _getValue: function ()
  91. {
  92. return this.inputText.val();
  93. },
  94. _setNullText: function ()
  95. {
  96. this.checkNotNull();
  97. },
  98. checkValue: function ()
  99. {
  100. var g = this, p = this.options;
  101. var v = g.inputText.val() || "";
  102. if (p.currency) v = v.replace(/\$|\,/g, '');
  103. var isFloat = p.number || p.currency, isDigits = p.digits;
  104. if (v != "" && isFloat && !/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/.test(v) || isDigits && !/^\d+$/.test(v))
  105. {
  106. if (g.value != null)
  107. {
  108. //不符合,恢复到原来的值
  109. g.inputText.val(g.value);
  110. }
  111. else
  112. {
  113. g.inputText.val('');
  114. }
  115. p.currency && g.inputText.val(currencyFormatter(g.value));
  116. return;
  117. }
  118. g.value = v;
  119. p.currency && g.inputText.val(currencyFormatter(g.value));
  120. },
  121. checkNotNull: function ()
  122. {
  123. var g = this, p = this.options;
  124. if (p.nullText && !p.disabled)
  125. {
  126. if (!g.inputText.val())
  127. {
  128. g.inputText.addClass("l-text-field-null").val(p.nullText);
  129. }
  130. }
  131. },
  132. _setEvent: function ()
  133. {
  134. var g = this, p = this.options;
  135. g.inputText.bind('blur.textBox', function ()
  136. {
  137. g.trigger('blur');
  138. g.checkNotNull();
  139. g.checkValue();
  140. g.wrapper.removeClass("l-text-focus");
  141. }).bind('focus.textBox', function ()
  142. {
  143. g.trigger('focus');
  144. if (p.nullText)
  145. {
  146. if ($(this).hasClass("l-text-field-null"))
  147. {
  148. $(this).removeClass("l-text-field-null").val("");
  149. }
  150. }
  151. g.wrapper.addClass("l-text-focus");
  152. })
  153. .change(function ()
  154. {
  155. g.trigger('changeValue', [this.value]);
  156. });
  157. g.wrapper.hover(function ()
  158. {
  159. g.trigger('mouseOver');
  160. g.wrapper.addClass("l-text-over");
  161. }, function ()
  162. {
  163. g.trigger('mouseOut');
  164. g.wrapper.removeClass("l-text-over");
  165. });
  166. },
  167. _setDisabled: function (value)
  168. {
  169. var g = this, p = this.options;
  170. if (value)
  171. {
  172. this.inputText.attr("readonly", "readonly");
  173. this.wrapper.addClass("l-text-disabled");
  174. }
  175. else if (!p.readonly)
  176. {
  177. this.inputText.removeAttr("readonly");
  178. this.wrapper.removeClass('l-text-disabled');
  179. }
  180. },
  181. _setWidth: function (value)
  182. {
  183. if (value > 20)
  184. {
  185. this.wrapper.css({ width: value });
  186. this.inputText.css({ width: value - 4 });
  187. }
  188. },
  189. _setHeight: function (value)
  190. {
  191. if (value > 10)
  192. {
  193. this.wrapper.height(value);
  194. this.inputText.height(value - 2);
  195. }
  196. },
  197. _setValue: function (value)
  198. {
  199. if (value != null)
  200. this.inputText.val(value);
  201. },
  202. _setLabel: function (value)
  203. {
  204. var g = this, p = this.options;
  205. if (!g.labelwrapper)
  206. {
  207. g.labelwrapper = g.wrapper.wrap('<div class="l-labeltext"></div>').parent();
  208. var lable = $('<div class="l-text-label" style="float:left;">' + value + ':&nbsp</div>');
  209. g.labelwrapper.prepend(lable);
  210. g.wrapper.css('float', 'left');
  211. if (!p.labelWidth)
  212. {
  213. p.labelWidth = lable.width();
  214. }
  215. else
  216. {
  217. g._setLabelWidth(p.labelWidth);
  218. }
  219. lable.height(g.wrapper.height());
  220. if (p.labelAlign)
  221. {
  222. g._setLabelAlign(p.labelAlign);
  223. }
  224. g.labelwrapper.append('<br style="clear:both;" />');
  225. g.labelwrapper.width(p.labelWidth + p.width + 2);
  226. }
  227. else
  228. {
  229. g.labelwrapper.find(".l-text-label").html(value + ':&nbsp');
  230. }
  231. },
  232. _setLabelWidth: function (value)
  233. {
  234. var g = this, p = this.options;
  235. if (!g.labelwrapper) return;
  236. g.labelwrapper.find(".l-text-label").width(value);
  237. },
  238. _setLabelAlign: function (value)
  239. {
  240. var g = this, p = this.options;
  241. if (!g.labelwrapper) return;
  242. g.labelwrapper.find(".l-text-label").css('text-align', value);
  243. },
  244. updateStyle: function ()
  245. {
  246. var g = this, p = this.options;
  247. if (g.inputText.attr('readonly'))
  248. {
  249. g.wrapper.addClass("l-text-readonly");
  250. p.disabled = true;
  251. }
  252. else
  253. {
  254. g.wrapper.removeClass("l-text-readonly");
  255. p.disabled = false;
  256. }
  257. if (g.inputText.attr('disabled'))
  258. {
  259. g.wrapper.addClass("l-text-disabled");
  260. p.disabled = true;
  261. }
  262. else
  263. {
  264. g.wrapper.removeClass("l-text-disabled");
  265. p.disabled = false;
  266. }
  267. if (g.inputText.hasClass("l-text-field-null") && g.inputText.val() != p.nullText)
  268. {
  269. g.inputText.removeClass("l-text-field-null");
  270. }
  271. g.checkValue();
  272. }
  273. });
  274. function currencyFormatter(num)
  275. {
  276. if (!num) return "0.00";
  277. num = num.toString().replace(/\$|\,/g, '');
  278. if (isNaN(num))
  279. num = "0.00";
  280. sign = (num == (num = Math.abs(num)));
  281. num = Math.floor(num * 100 + 0.50000000001);
  282. cents = num % 100;
  283. num = Math.floor(num / 100).toString();
  284. if (cents < 10)
  285. cents = "0" + cents;
  286. for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3) ; i++)
  287. num = num.substring(0, num.length - (4 * i + 3)) + ',' +
  288. num.substring(num.length - (4 * i + 3));
  289. return "" + (((sign) ? '' : '-') + '' + num + '.' + cents);
  290. }
  291. })(jQuery);