PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/Carcheck/CarCheckx/Scripts/asb_includes/AutoSuggestBox.js

http://github.com/royriojas/Clave-Projects
JavaScript | 652 lines | 427 code | 168 blank | 57 comment | 57 complexity | a099c56f9b624dd00d3d42eb5c8d2fae MD5 | raw file
  1. //============================
  2. //AutoSuggestBox version 1.5.3
  3. //============================
  4. //Global variable and methods that keeps track of all ASB
  5. //javascript objects on the page
  6. var g_ASBObjects;
  7. function asbAddObj(sTextBoxID, oJSAutoSuggestBox)
  8. {
  9. if (typeof(g_ASBObjects) == "undefined")
  10. g_ASBObjects=new Array();
  11. g_ASBObjects[sTextBoxID]=oJSAutoSuggestBox;
  12. }
  13. function asbGetObj(sTextBoxID)
  14. {
  15. //JSAutoSuggestBox.TRACE("asbGetObj : " + g_ASBObjects[sTextBoxID].msTextBoxID);
  16. return g_ASBObjects[sTextBoxID];
  17. }
  18. ///////////////////////////////////////////////////
  19. //Class that stores all auto suggest box properties
  20. ///////////////////////////////////////////////////
  21. function JSAutoSuggestBox()
  22. {
  23. //Class properties
  24. var msTextBoxID;
  25. var msMenuDivID;
  26. var msDataType;
  27. var mnMaxSuggestChars;
  28. var mnKeyPressDelay;
  29. var mnNumMenuItems;
  30. var mbIncludeMoreMenuItem;
  31. var msMoreMenuItemLabel;
  32. var msMenuCSSClass;
  33. var msMenuItemCSSClass;
  34. var msSelMenuItemCSSClass;
  35. var mbUseIFrame;
  36. //RRRM
  37. var msFilters;
  38. var msResourceDir;
  39. var mbHasFocus;
  40. //Internal attributes
  41. var mnSelMenuItem = 0;
  42. var mbCancelSubmit;
  43. var msOldTextBoxValue="";
  44. //Class methods
  45. this.GetKey =GetKey;
  46. this.GetTextBoxCtrl =GetTextBoxCtrl;
  47. this.GetMenuDiv =GetMenuDiv;
  48. this.GetXmlHttp =GetXmlHttp;
  49. this.GetDataFromServer =GetDataFromServer;
  50. this.SetSelectedValue =SetSelectedValue;
  51. this.SetTextBoxValue =SetTextBoxValue;
  52. this.GetTextBoxValue =GetTextBoxValue;
  53. this.OnMouseClick =OnMouseClick;
  54. this.OnMouseOver =OnMouseOver;
  55. this.OnKeyDown =OnKeyDown;
  56. this.OnKeyPress =OnKeyPress;
  57. this.OnKeyUp =OnKeyUp;
  58. this.OnBlur =OnBlur;
  59. this.GetSelMenuItemDiv =GetSelMenuItemDiv;
  60. this.GetMenuItemDivID =GetMenuItemDivID;
  61. this.GetMenuItemDiv =GetMenuItemDiv;
  62. this.MoveUp =MoveUp;
  63. this.MoveDown =MoveDown;
  64. this.SelectMenuItem =SelectMenuItem;
  65. this.UnselectMenuItem =UnselectMenuItem;
  66. this.IsVisibleMenuDiv =IsVisibleMenuDiv;
  67. this.MoveMenuDivIfAbsolutePos =MoveMenuDivIfAbsolutePos;
  68. this.ShowMenuDiv =ShowMenuDiv;
  69. this.HideMenuDiv =HideMenuDiv;
  70. this.GetSelectedValue = GetSelectedValue;
  71. //Detects what key was pressed
  72. function GetKey(evt)
  73. {
  74. evt = (evt) ? evt : (window.event) ? event : null;
  75. if (evt)
  76. {
  77. var cCode = (evt.charCode) ? evt.charCode :
  78. ((evt.keyCode) ? evt.keyCode :
  79. ((evt.which) ? evt.which : 0));
  80. return cCode;
  81. }
  82. }
  83. function GetTextBoxCtrl()
  84. {
  85. return document.getElementById(this.msTextBoxID);
  86. }
  87. function GetMenuDiv()
  88. {
  89. return document.getElementById(this.msMenuDivID);
  90. }
  91. //Create and return XmlHttp object
  92. function GetXmlHttp()
  93. {
  94. var oXmlHttp=false;
  95. // -----> This method was provided from Jim Ley's website
  96. /*@cc_on @*/
  97. /*@if (@_jscript_version >= 5)
  98. // JScript gives us Conditional compilation, we can cope with old IE versions.
  99. // and security blocked creation of the objects.
  100. try {
  101. oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
  102. } catch (e) {
  103. try {
  104. oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  105. } catch (E) {
  106. oXmlHttp = false;
  107. }
  108. }
  109. /*@end @*/
  110. if (!oXmlHttp && typeof XMLHttpRequest!='undefined')
  111. {
  112. oXmlHttp = new XMLHttpRequest();
  113. }
  114. return oXmlHttp;
  115. }
  116. // Make a call back to our server side page and return the results from our query
  117. // to a DIV tag sitting under the text box
  118. function GetDataFromServer(sValue)
  119. {
  120. var sUrl;
  121. sUrl=this.msResourcesDir +
  122. "/GetAutoSuggestData.aspx?TextBoxID=" + encodeURIComponent(this.msTextBoxID) +
  123. "&MenuDivID=" + encodeURIComponent(this.msMenuDivID) +
  124. "&DataType=" + encodeURIComponent(this.msDataType) +
  125. "&NumMenuItems=" + this.mnNumMenuItems +
  126. "&IncludeMoreMenuItem=" + this.mbIncludeMoreMenuItem +
  127. "&MoreMenuItemLabel=" + encodeURIComponent(this.msMoreMenuItemLabel) +
  128. "&MenuItemCSSClass=" + encodeURIComponent(this.msMenuItemCSSClass) +
  129. "&Keyword=" + encodeURIComponent(sValue) +
  130. "&Filters=" + encodeURIComponent(this.msFilters) +
  131. "&WarnNoValueSelected=" + encodeURIComponent(this.msWarnNoValueSelected) +
  132. "&NoValueSelectedCSSClass=" + encodeURIComponent(this.msNoValueSelectedCSSClass) +
  133. "&OnFocusShowAll=" + encodeURIComponent(this.msOnFocusShowAll);
  134. //RRRM 13-08-2006
  135. TRACE("GetDataFromServer: " + sUrl);
  136. var oXmlHttp=GetXmlHttp();
  137. oXmlHttp.open("GET", sUrl, true);
  138. var me=this; //Use it to be able to access ShowMenuDiv and HideMenuDiv in the function()
  139. oXmlHttp.onreadystatechange=function()
  140. {
  141. if (oXmlHttp.readyState==4)
  142. {
  143. if (oXmlHttp.responseText!="")
  144. {
  145. if (me.mbHasFocus)
  146. {
  147. me.ShowMenuDiv(oXmlHttp.responseText);
  148. }
  149. }
  150. else
  151. {
  152. me.HideMenuDiv()
  153. }
  154. }
  155. }
  156. oXmlHttp.send(null)
  157. }
  158. function SetSelectedValue(sValue)
  159. {
  160. TRACE("SetSelectedValue: " + sValue);
  161. var hdnSelectedValue=document.getElementById(this.msTextBoxID + "_SelectedValue");
  162. hdnSelectedValue.value=sValue;
  163. }
  164. function GetSelectedValue() {
  165. var hdnSelectedValue=document.getElementById(this.msTextBoxID + "_SelectedValue");
  166. return hdnSelectedValue.value;
  167. }
  168. function SetTextBoxValue()
  169. {
  170. var divMenuItem=this.GetSelMenuItemDiv();
  171. if(divMenuItem)
  172. {
  173. var sValue=divMenuItem.getAttribute('value');
  174. TRACE("SetTextBoxValue : Set selected item to " + sValue);
  175. //Set selected value of control to the value of selected menu item
  176. this.SetSelectedValue(sValue);
  177. var txtCtrl=this.GetTextBoxCtrl();
  178. txtCtrl.value = GetInnerHtml(divMenuItem);
  179. }
  180. }
  181. function GetTextBoxValue()
  182. {
  183. var txtCtrl=this.GetTextBoxCtrl();
  184. return(txtCtrl.value);
  185. }
  186. function OnMouseClick(nMenuIndex)
  187. {
  188. this.mnSelMenuItem=nMenuIndex;
  189. this.SetTextBoxValue();
  190. this.HideMenuDiv();
  191. }
  192. function OnMouseOver(nMenuIndex)
  193. {
  194. this.SelectMenuItem(nMenuIndex);
  195. }
  196. function OnKeyDown(evt)
  197. {
  198. TRACE("OnKeyDown : " + this.GetKey(evt) + ", " + this.msTextBoxID);
  199. //Indicate that control has focus
  200. this.mbHasFocus=true;
  201. //Save current text box value before key press takes affect
  202. this.msOldTextBoxValue=this.GetTextBoxValue();
  203. TRACE("OnKeyDown : old text box value='" + this.msOldTextBoxValue + "'");
  204. var nKey;
  205. nKey=this.GetKey(evt);
  206. TRACE("OnKeyDown : Key is " + nKey);
  207. //Detect if the user is using the down button
  208. if(nKey==38) //Up arrow
  209. {
  210. this.MoveDown()
  211. }
  212. else if(nKey==40) //Down arrow
  213. {
  214. this.MoveUp()
  215. }
  216. else if(nKey==13) //Enter
  217. {
  218. TRACE("OnKeyDown : IsVisibleMenuDiv - " + this.IsVisibleMenuDiv());
  219. if (this.IsVisibleMenuDiv())
  220. {
  221. this.HideMenuDiv();
  222. evt.cancelBubble = true;
  223. if (evt.returnValue) evt.returnValue = false;
  224. if (evt.stopPropagation) evt.stopPropagation();
  225. this.mbCancelSubmit=true;
  226. }
  227. else
  228. {
  229. this.mbCancelSubmit=false;
  230. }
  231. }
  232. else
  233. {
  234. this.HideMenuDiv();
  235. }
  236. return true;
  237. }
  238. function OnKeyPress(evt)
  239. {
  240. TRACE("OnKeyPress : " + this.GetKey(evt));
  241. if ((this.GetKey(evt)==13) && (this.mbCancelSubmit))
  242. {
  243. return false;
  244. }
  245. return true;
  246. }
  247. this.doRequest = function(sNewValue) {
  248. if (sNewValue == "")
  249. {
  250. TRACE("DoRequest : Getting data for '" + sNewValue + "'");
  251. var divMenu = this.GetMenuDiv();
  252. if (divMenu.timer) window.clearTimeout(divMenu.timer);
  253. //Add escape char to single quote
  254. sNewValue=sNewValue.replace(/\'/, "\\\'");
  255. //Set timer to update div. If user types quickly return suggestions when he stops.
  256. var sFunc="asbGetObj('" + this.msTextBoxID + "').GetDataFromServer('" + sNewValue + "')";
  257. TRACE("DoRequest : " + sFunc);
  258. divMenu.timer = window.setTimeout(sFunc, this.mnKeyPressDelay);
  259. }
  260. }
  261. //RRRM
  262. this.OnFocus = function() {
  263. if (this.msOnFocusShowAll) {
  264. this.mbHasFocus = true;
  265. var sNewValue;
  266. sNewValue=this.GetTextBoxValue();
  267. //alert(sNewValue == "");
  268. //si ha recibido el foco y el texto es ""
  269. this.doRequest(sNewValue);
  270. }
  271. }
  272. function OnKeyUp(evt)
  273. {
  274. var nKey;
  275. nKey=this.GetKey(evt);
  276. TRACE("OnKeyUp : " + nKey);
  277. //Skip up/down/enter
  278. if ((nKey!=38) && (nKey!=40) && (nKey!=13))
  279. {
  280. var sNewValue;
  281. sNewValue=this.GetTextBoxValue();
  282. if ((this.msOnFocusShowAll) && (sNewValue == '')) {
  283. this.doRequest(sNewValue);
  284. }
  285. else {
  286. //Limit num of characters to display suggestions
  287. if ((sNewValue.length <= this.mnMaxSuggestChars) && (sNewValue.length > 0))
  288. {
  289. TRACE("OnKeyUp : Getting data for '" + sNewValue + "'");
  290. var divMenu = this.GetMenuDiv();
  291. if (divMenu.timer) window.clearTimeout(divMenu.timer);
  292. //Add escape char to single quote
  293. sNewValue=sNewValue.replace(/\'/, "\\\'");
  294. //Set timer to update div. If user types quickly return suggestions when he stops.
  295. var sFunc="asbGetObj('" + this.msTextBoxID + "').GetDataFromServer('" + sNewValue + "')";
  296. TRACE("OnKeyUp : " + sFunc);
  297. divMenu.timer = window.setTimeout(sFunc, this.mnKeyPressDelay);
  298. }
  299. if (this.msOldTextBoxValue!=sNewValue)
  300. {
  301. this.SetSelectedValue("");
  302. }
  303. }
  304. }
  305. }
  306. function OnBlur()
  307. {
  308. TRACE("OnBlur");
  309. this.HideMenuDiv();
  310. this.mbHasFocus=false;
  311. if (this.msWarnNoValueSelected) {
  312. this.OnNoValueSelected();
  313. }
  314. //if (this.msOldTextBoxValue != this.GetTextBoxValue()) alert('value has changed');
  315. }
  316. this.OnNoValueSelected = function() {
  317. if ((this.GetTextBoxValue() !='') && (this.GetSelectedValue() == '')) {
  318. this.GetTextBoxCtrl().className = this.replaceAll(this.GetTextBoxCtrl().className,[[' '+this.msNoValueSelectedCSSClass,'']]);
  319. this.GetTextBoxCtrl().className += ' '+this.msNoValueSelectedCSSClass; ;
  320. }
  321. else {
  322. this.GetTextBoxCtrl().className = this.replaceAll(this.GetTextBoxCtrl().className,[[' '+this.msNoValueSelectedCSSClass,'']]);
  323. }
  324. }
  325. this.replaceAll = function ( str, replacements ) {
  326. for ( i = 0; i < replacements.length; i++ ) {
  327. var idx = str.indexOf( replacements[i][0] );
  328. while ( idx > -1 ) {
  329. str = str.replace( replacements[i][0], replacements[i][1] );
  330. idx = str.indexOf( replacements[i][0] );
  331. }
  332. }
  333. return str;
  334. }
  335. function GetSelMenuItemDiv()
  336. {
  337. return this.GetMenuItemDiv(this.mnSelMenuItem);
  338. }
  339. function GetMenuItemDivID(nMenuItem)
  340. {
  341. return (this.msTextBoxID + "_mi_" + nMenuItem);
  342. }
  343. function GetMenuItemDiv(nMenuItem)
  344. {
  345. var sDivMenuItemID=this.GetMenuItemDivID(nMenuItem);
  346. return document.getElementById(sDivMenuItemID)
  347. }
  348. function MoveUp()
  349. {
  350. var nMenuItem;
  351. nMenuItem=this.mnSelMenuItem+1;
  352. //Check if menu item exists
  353. if(this.GetMenuItemDiv(nMenuItem))
  354. {
  355. this.SelectMenuItem(nMenuItem)
  356. }
  357. }
  358. function MoveDown()
  359. {
  360. var nMenuItem;
  361. nMenuItem=this.mnSelMenuItem-1;
  362. if(nMenuItem!=0)
  363. {
  364. this.SelectMenuItem(nMenuItem)
  365. }
  366. }
  367. //Highlights a div
  368. function SelectMenuItem(nMenuItem)
  369. {
  370. var divMenuItem=this.GetMenuItemDiv(nMenuItem)
  371. if(divMenuItem)
  372. {
  373. if (nMenuItem!=this.mnSelMenuItem)
  374. {
  375. this.UnselectMenuItem();
  376. this.mnSelMenuItem=nMenuItem;
  377. this.SetTextBoxValue();
  378. divMenuItem.className=this.msSelMenuItemCSSClass;
  379. }
  380. }
  381. }
  382. //unhighlights a div
  383. function UnselectMenuItem()
  384. {
  385. var divMenuItem=this.GetSelMenuItemDiv()
  386. if(divMenuItem)
  387. {
  388. divMenuItem.className=this.msMenuItemCSSClass;
  389. }
  390. }
  391. function IsVisibleMenuDiv()
  392. {
  393. if (this.GetMenuDiv().style.visibility == 'hidden')
  394. {
  395. return false;
  396. }
  397. else
  398. {
  399. return true;
  400. }
  401. }
  402. function MoveMenuDivIfAbsolutePos()
  403. {
  404. var txtCtrl=this.GetTextBoxCtrl();
  405. var divMenu=this.GetMenuDiv();
  406. if (txtCtrl.style.position!="absolute")
  407. return;
  408. TRACE("MoveMenuDivIfAbsolutePos Moving absolute");
  409. //Move menu right under text box
  410. divMenu.style.left =txtCtrl.offsetLeft;
  411. divMenu.style.top =txtCtrl.offsetTop + txtCtrl.offsetHeight;
  412. }
  413. function ShowMenuDiv(sDivContent)
  414. {
  415. this.MoveMenuDivIfAbsolutePos();
  416. TRACE("ShowMenuDiv : " + this.msTextBoxID);
  417. var divMenu=this.GetMenuDiv();
  418. var sInnerHtml;
  419. //Use IFrame of the same size as div
  420. if (IsIE() && this.mbUseIFrame)
  421. {
  422. sInnerHtml = "<div id='" + this.msMenuDivID + "_content'>";
  423. sInnerHtml += sDivContent;
  424. sInnerHtml += "</div>";
  425. var sBlankPage=this.msResourcesDir + "/Blank.html"; //Use blank page to hide 'nonsecure items' message in IE when using HTTPS
  426. sInnerHtml += "<iframe id='" + this.msMenuDivID + "_iframe' src='" + sBlankPage + "' frameborder='1' scrolling='no'></iframe>";
  427. }
  428. else
  429. {
  430. sInnerHtml=sDivContent;
  431. }
  432. divMenu.innerHTML = sInnerHtml;
  433. if (IsIE() && this.mbUseIFrame)
  434. {
  435. var divContent;
  436. divContent=document.getElementById(this.msMenuDivID + "_content");
  437. var divIframe;
  438. divIframe=document.getElementById(this.msMenuDivID + "_iframe");
  439. //Remember display type
  440. divContent.className=this.msMenuCSSClass;
  441. divMenu.className="asbMenuBase";
  442. divIframe.style.width = divContent.offsetWidth + 'px';
  443. divIframe.style.height = divContent.offsetHeight + 'px';
  444. divIframe.marginTop = "-" + divContent.offsetHeight + 'px';
  445. }
  446. divMenu.style.visibility = 'visible';
  447. //alert(divMenu.outerHTML);
  448. }
  449. function HideMenuDiv()
  450. {
  451. this.GetMenuDiv().style.visibility = 'hidden';
  452. this.mnSelMenuItem=0;
  453. //
  454. //alert(this.msOldTextBoxValue != this.GetTextBoxValue());
  455. }
  456. //Utility functions (don't need this.)
  457. function IsIE()
  458. {
  459. return ( navigator.appName=="Microsoft Internet Explorer" );
  460. }
  461. function IsNav()
  462. {
  463. return ( navigator.appName=="Netscape" );
  464. }
  465. function GetInnerHtml(oItem)
  466. {
  467. var sOut;
  468. if (oItem.innerText)
  469. {
  470. sOut=oItem.innerText; // IE
  471. }
  472. else if (oItem.textContent)
  473. {
  474. sOut=oItem.textContent; // Mozilla
  475. }
  476. return (sOut);
  477. }
  478. function TRACE(sText)
  479. {
  480. var txtTrace=document.getElementById("txtASBTrace");
  481. if (txtTrace!=null)
  482. txtTrace.value = txtTrace.value + sText + "\n";
  483. }
  484. }