VAFocus.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. VAFocus = function (w, changed, color, linkColor, bgColor) {
  2. this.window = w ? w : window;
  3. this.changed = changed;
  4. this.color = color ? color : "";
  5. this.linkColor = linkColor ? linkColor : "";
  6. this.bgColor = bgColor ? bgColor : "";
  7. this.active = jQuery.proxy(this.active, this);
  8. this.reset = jQuery.proxy(this.reset, this);
  9. this.paint = jQuery.proxy(this.paint, this);
  10. this.mouseOver = jQuery.proxy(this.mouseOver, this);
  11. this.mouseOut = jQuery.proxy(this.mouseOut, this);
  12. this.focusNext = jQuery.proxy(this.focusNext, this);
  13. this.clearFocusNext = jQuery.proxy(this.clearFocusNext, this);
  14. this.focus = jQuery.proxy(this.focus, this);
  15. this.blur = jQuery.proxy(this.blur, this);
  16. this.getText = jQuery.proxy(this.getText, this);
  17. this.elements = [];
  18. this.current = null;
  19. this.useFocusNext = false;
  20. this.nextFocus = null;
  21. }
  22. VAFocus.prototype = {
  23. active: function () {
  24. this.elements = [];
  25. this.current = null;
  26. this.elements = jQuery("a, span, img, input, button, select, textarea, li, b, i, strong, h1, h2, h3, h4, h5, h6, font, p, div, td", this.window.document)
  27. .not("a *")
  28. .bind("mouseover", this.mouseOver)
  29. .bind("mouseout", this.mouseOut);
  30. },
  31. reset: function () {
  32. this.color = "";
  33. this.linkColor = "";
  34. this.bgColor = "";
  35. },
  36. paint: function (color, linkColor, bgColor) {
  37. this.color = color;
  38. this.linkColor = linkColor;
  39. this.bgColor = bgColor;
  40. },
  41. mouseOver: function (e) {
  42. e.stopPropagation();
  43. e.stopImmediatePropagation();
  44. var t = e.currentTarget;
  45. if (!this.useFocusNext) {
  46. this.focus(t);
  47. }
  48. else {
  49. if (jQuery.inArray(t.tagName, ["A", "SPAN", "B", "H1", "H2", "H3", "H4", "H5", "H6", "FONT", "STRONG", "I", "LI", "P"]) > -1) {
  50. this.nextFocus = t;
  51. }
  52. else {
  53. this.nextFocus = null;
  54. }
  55. }
  56. },
  57. mouseOut: function (e) {
  58. e.stopPropagation();
  59. e.stopImmediatePropagation();
  60. var t = e.currentTarget;
  61. if (!this.useFocusNext) {
  62. this.blur(t);
  63. }
  64. else {
  65. this.nextFocus = null;
  66. }
  67. },
  68. focusNext: function () {
  69. if (this.nextFocus && this.nextFocus != this.current) {
  70. var t = this.nextFocus;
  71. this.nextFocus = null;
  72. this.focus(t);
  73. return;
  74. }
  75. var index = this.elements.index(this.current);
  76. var old = this.current;
  77. while (index != 1 && index > -1 && ++index < this.elements.length) {
  78. this.current = this.elements[index];
  79. if (this.getText() != "") {
  80. this.useFocusNext = true;
  81. this.blur(old);
  82. this.nextFocus = null;
  83. this.focus(this.current);
  84. return;
  85. }
  86. }
  87. this.useFocusNext = false;
  88. },
  89. clearFocusNext: function () {
  90. this.useFocusNext = false;
  91. this.nextFocus = null;
  92. },
  93. focus: function (t) {
  94. if (this.current) {
  95. this.blur(this.current);
  96. }
  97. this.current = t;
  98. var color = this.color;
  99. var linkColor = this.linkColor;
  100. var bgColor = this.bgColor;
  101. if (!color) {
  102. color = "Blue";
  103. }
  104. if (!linkColor) {
  105. linkColor = "Black";
  106. }
  107. if (!bgColor) {
  108. bgColor = "Yellow";
  109. }
  110. switch (t.tagName) {
  111. case "A":
  112. jQuery(t).css("background", linkColor).css("color", bgColor).focus();
  113. break;
  114. case "SPAN":
  115. case "IMG":
  116. case "INPUT":
  117. case "BUTTON":
  118. case "SELECT":
  119. case "TEXTAREA":
  120. case "B":
  121. case "H1":
  122. case "H2":
  123. case "H3":
  124. case "H4":
  125. case "H5":
  126. case "H6":
  127. case "FONT":
  128. case "STRONG":
  129. case "I":
  130. case "LI":
  131. case "P":
  132. jQuery(t).css("background", color).css("color", bgColor).focus();
  133. break;
  134. }
  135. if (jQuery.isFunction(this.changed)) {
  136. this.changed(this);
  137. }
  138. },
  139. blur: function (t) {
  140. switch (t.tagName) {
  141. case "A":
  142. jQuery(t).css("background", this.bgColor).css("color", this.linkColor).blur();
  143. break;
  144. case "SPAN":
  145. case "IMG":
  146. case "INPUT":
  147. case "BUTTON":
  148. case "SELECT":
  149. case "TEXTAREA":
  150. case "B":
  151. case "H1":
  152. case "H2":
  153. case "H3":
  154. case "H4":
  155. case "H5":
  156. case "H6":
  157. case "FONT":
  158. case "STRONG":
  159. case "I":
  160. case "LI":
  161. case "P":
  162. jQuery(t).css("background", this.bgColor).css("color", this.color).blur();
  163. break;
  164. }
  165. },
  166. getText: function () {
  167. var node = this.current;
  168. var t = "";
  169. switch (node.tagName) {
  170. case "DIV":
  171. case "TD":
  172. case "P":
  173. case "SPAN":
  174. case "LI":
  175. for (var i = 0; i < node.childNodes.length; i++) {
  176. if (node.childNodes[i].nodeType == 3) {
  177. t += node.childNodes[i].data;
  178. }
  179. else if (node.childNodes[i].tagName == "OBJECT" || node.childNodes[i].tagName == "EMBED") {
  180. t = "媒体";
  181. }
  182. }
  183. break;
  184. case "IMG":
  185. if (!!jQuery(node).attr("title")) {
  186. t = "图片:" + jQuery(node).attr("title");
  187. }
  188. else if (!!jQuery(node).attr("alt")) {
  189. t = "图片:" + jQuery(node).attr("alt");
  190. }
  191. else {
  192. t = "图片";
  193. }
  194. break;
  195. case "A":
  196. if (!!jQuery(node).attr("title")) {
  197. t = jQuery(node).attr("title");
  198. }
  199. else {
  200. t = jQuery(node).text();
  201. }
  202. t = jQuery.trim(t);
  203. if (t) {
  204. t = "链接:" + t;
  205. }
  206. break;
  207. case "OBJECT":
  208. case "EMBED":
  209. t = "媒体";
  210. break;
  211. case "TEXTAREA":
  212. t = "文本框:" + jQuery(node).val();
  213. break;
  214. case "INPUT":
  215. var type = jQuery(node).attr("type");
  216. switch (type) {
  217. case "text":
  218. t = "文本框:" + jQuery(node).val();
  219. break;
  220. case "password":
  221. t = "密码框:" + jQuery(node).val();
  222. break;
  223. case "button":
  224. t = "按钮" + jQuery(node).val();
  225. break;
  226. case "radio":
  227. t = "单选框:" + jQuery(node).attr("checked") ? "选中" : "未选中";
  228. break;
  229. case "checkbox":
  230. t = "复选框:" + jQuery(node).attr("checked") ? "选中" : "未选中";
  231. break;
  232. }
  233. break;
  234. case "SELECT":
  235. for (var i = 0; i < node.childNodes.length; i++) {
  236. if (node.childNodes[i].value == node.value) {
  237. t = "选项:" + jQuery(node.childNodes[i]).text();
  238. break;
  239. }
  240. }
  241. break;
  242. default:
  243. t = jQuery(node).text();
  244. }
  245. return jQuery.trim(t);
  246. }
  247. }