PageRenderTime 51ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/Web/asb_includes/AutoSuggestBox.js

http://github.com/royriojas/RGEN2
JavaScript | 657 lines | 437 code | 167 blank | 53 comment | 59 complexity | 8d1a779025df7d33b9ebb49c765fca15 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. function replaceAll( str, replacements ) {
  117. for ( i = 0; i < replacements.length; i++ ) {
  118. var idx = str.indexOf( replacements[i][0] );
  119. while ( idx > -1 ) {
  120. str = str.replace( replacements[i][0], replacements[i][1] );
  121. idx = str.indexOf( replacements[i][0] );
  122. }
  123. }
  124. return str;
  125. }
  126. // Make a call back to our server side page and return the results from our query
  127. // to a DIV tag sitting under the text box
  128. function GetDataFromServer(sValue)
  129. {
  130. var sUrl;
  131. sUrl=this.msResourcesDir +
  132. "/GetAutoSuggestData.aspx?TextBoxID=" + encodeURIComponent(this.msTextBoxID) +
  133. "&MenuDivID=" + encodeURIComponent(this.msMenuDivID) +
  134. "&DataType=" + encodeURIComponent(this.msDataType) +
  135. "&NumMenuItems=" + this.mnNumMenuItems +
  136. "&IncludeMoreMenuItem=" + this.mbIncludeMoreMenuItem +
  137. "&MoreMenuItemLabel=" + encodeURIComponent(this.msMoreMenuItemLabel) +
  138. "&MenuItemCSSClass=" + encodeURIComponent(this.msMenuItemCSSClass) +
  139. "&Keyword=" + encodeURIComponent(sValue) +
  140. "&Filters=" + encodeURIComponent(this.msFilters) +
  141. "&WarnNoValueSelected=" + encodeURIComponent(this.msWarnNoValueSelected) +
  142. "&NoValueSelectedCSSClass=" + encodeURIComponent(this.msNoValueSelectedCSSClass) +
  143. "&OnFocusShowAll=" + encodeURIComponent(this.msOnFocusShowAll);
  144. //RRRM 13-08-2006
  145. TRACE("GetDataFromServer: " + sUrl);
  146. var oXmlHttp=GetXmlHttp();
  147. oXmlHttp.open("GET", sUrl, true);
  148. var me=this; //Use it to be able to access ShowMenuDiv and HideMenuDiv in the function()
  149. oXmlHttp.onreadystatechange=function()
  150. {
  151. if (oXmlHttp.readyState==4)
  152. {
  153. if (oXmlHttp.responseText!="")
  154. {
  155. if (me.mbHasFocus)
  156. {
  157. me.ShowMenuDiv(oXmlHttp.responseText);
  158. }
  159. }
  160. else
  161. {
  162. me.HideMenuDiv()
  163. }
  164. }
  165. }
  166. oXmlHttp.send(null)
  167. }
  168. function SetSelectedValue(sValue)
  169. {
  170. TRACE("SetSelectedValue: " + sValue);
  171. var hdnSelectedValue=document.getElementById(this.msTextBoxID + "_SelectedValue");
  172. hdnSelectedValue.value=sValue;
  173. }
  174. function GetSelectedValue() {
  175. var hdnSelectedValue=document.getElementById(this.msTextBoxID + "_SelectedValue");
  176. return hdnSelectedValue.value;
  177. }
  178. function SetTextBoxValue()
  179. {
  180. var divMenuItem=this.GetSelMenuItemDiv();
  181. if(divMenuItem)
  182. {
  183. var sValue=divMenuItem.getAttribute('value');
  184. TRACE("SetTextBoxValue : Set selected item to " + sValue);
  185. //Set selected value of control to the value of selected menu item
  186. this.SetSelectedValue(sValue);
  187. var txtCtrl=this.GetTextBoxCtrl();
  188. txtCtrl.value = GetInnerHtml(divMenuItem);
  189. }
  190. }
  191. function GetTextBoxValue()
  192. {
  193. var txtCtrl=this.GetTextBoxCtrl();
  194. return(txtCtrl.value);
  195. }
  196. function OnMouseClick(nMenuIndex)
  197. {
  198. this.mnSelMenuItem=nMenuIndex;
  199. this.SetTextBoxValue();
  200. this.HideMenuDiv();
  201. }
  202. function OnMouseOver(nMenuIndex)
  203. {
  204. this.SelectMenuItem(nMenuIndex);
  205. }
  206. function OnKeyDown(evt)
  207. {
  208. TRACE("OnKeyDown : " + this.GetKey(evt) + ", " + this.msTextBoxID);
  209. //Indicate that control has focus
  210. this.mbHasFocus=true;
  211. //Save current text box value before key press takes affect
  212. this.msOldTextBoxValue=this.GetTextBoxValue();
  213. TRACE("OnKeyDown : old text box value='" + this.msOldTextBoxValue + "'");
  214. var nKey;
  215. nKey=this.GetKey(evt);
  216. TRACE("OnKeyDown : Key is " + nKey);
  217. //Detect if the user is using the down button
  218. if(nKey==38) //Up arrow
  219. {
  220. this.MoveDown()
  221. }
  222. else if(nKey==40) //Down arrow
  223. {
  224. this.MoveUp()
  225. }
  226. else if(nKey==13) //Enter
  227. {
  228. TRACE("OnKeyDown : IsVisibleMenuDiv - " + this.IsVisibleMenuDiv());
  229. if (this.IsVisibleMenuDiv())
  230. {
  231. this.HideMenuDiv();
  232. evt.cancelBubble = true;
  233. if (evt.returnValue) evt.returnValue = false;
  234. if (evt.stopPropagation) evt.stopPropagation();
  235. this.mbCancelSubmit=true;
  236. }
  237. else
  238. {
  239. this.mbCancelSubmit=false;
  240. }
  241. }
  242. else
  243. {
  244. this.HideMenuDiv();
  245. }
  246. return true;
  247. }
  248. function OnKeyPress(evt)
  249. {
  250. TRACE("OnKeyPress : " + this.GetKey(evt));
  251. if ((this.GetKey(evt)==13) && (this.mbCancelSubmit))
  252. {
  253. return false;
  254. }
  255. return true;
  256. }
  257. this.doRequest = function(sNewValue) {
  258. if (sNewValue == "")
  259. {
  260. TRACE("DoRequest : Getting data for '" + sNewValue + "'");
  261. var divMenu = this.GetMenuDiv();
  262. if (divMenu.timer) window.clearTimeout(divMenu.timer);
  263. //Add escape char to single quote
  264. sNewValue=sNewValue.replace(/\'/, "\\\'");
  265. //Set timer to update div. If user types quickly return suggestions when he stops.
  266. var sFunc="asbGetObj('" + this.msTextBoxID + "').GetDataFromServer('" + sNewValue + "')";
  267. TRACE("DoRequest : " + sFunc);
  268. divMenu.timer = window.setTimeout(sFunc, this.mnKeyPressDelay);
  269. }
  270. }
  271. //RRRM
  272. this.OnFocus = function() {
  273. if (this.msOnFocusShowAll) {
  274. this.mbHasFocus = true;
  275. var sNewValue;
  276. sNewValue=this.GetTextBoxValue();
  277. //alert(sNewValue == "");
  278. //si ha recibido el foco y el texto es ""
  279. this.doRequest(sNewValue);
  280. }
  281. }
  282. function OnKeyUp(evt)
  283. {
  284. var nKey;
  285. nKey=this.GetKey(evt);
  286. TRACE("OnKeyUp : " + nKey);
  287. //Skip up/down/enter
  288. if ((nKey!=38) && (nKey!=40) && (nKey!=13))
  289. {
  290. var sNewValue;
  291. sNewValue=this.GetTextBoxValue();
  292. if ((this.msOnFocusShowAll) && (sNewValue == '')) {
  293. this.doRequest(sNewValue);
  294. }
  295. else {
  296. //Limit num of characters to display suggestions
  297. if ((sNewValue.length <= this.mnMaxSuggestChars) && (sNewValue.length > 0))
  298. {
  299. TRACE("OnKeyUp : Getting data for '" + sNewValue + "'");
  300. var divMenu = this.GetMenuDiv();
  301. if (divMenu.timer) window.clearTimeout(divMenu.timer);
  302. //Add escape char to single quote
  303. sNewValue=sNewValue.replace(/\'/, "\\\'");
  304. //Set timer to update div. If user types quickly return suggestions when he stops.
  305. var sFunc="asbGetObj('" + this.msTextBoxID + "').GetDataFromServer('" + sNewValue + "')";
  306. TRACE("OnKeyUp : " + sFunc);
  307. divMenu.timer = window.setTimeout(sFunc, this.mnKeyPressDelay);
  308. }
  309. if (this.msOldTextBoxValue!=sNewValue)
  310. {
  311. this.SetSelectedValue("");
  312. }
  313. }
  314. }
  315. }
  316. function OnBlur()
  317. {
  318. TRACE("OnBlur");
  319. this.HideMenuDiv();
  320. this.mbHasFocus=false;
  321. if (this.msWarnNoValueSelected) {
  322. this.OnNoValueSelected();
  323. }
  324. }
  325. this.OnNoValueSelected = function() {
  326. if ((this.GetTextBoxValue() !='') && (this.GetSelectedValue() == '')) {
  327. this.GetTextBoxCtrl().className = this.replaceAll(this.GetTextBoxCtrl().className,[[' '+this.msNoValueSelectedCSSClass,'']]);
  328. this.GetTextBoxCtrl().className += ' '+this.msNoValueSelectedCSSClass; ;
  329. }
  330. else {
  331. this.GetTextBoxCtrl().className = this.replaceAll(this.GetTextBoxCtrl().className,[[' '+this.msNoValueSelectedCSSClass,'']]);
  332. }
  333. }
  334. this.replaceAll = function ( str, replacements ) {
  335. for ( i = 0; i < replacements.length; i++ ) {
  336. var idx = str.indexOf( replacements[i][0] );
  337. while ( idx > -1 ) {
  338. str = str.replace( replacements[i][0], replacements[i][1] );
  339. idx = str.indexOf( replacements[i][0] );
  340. }
  341. }
  342. return str;
  343. }
  344. function GetSelMenuItemDiv()
  345. {
  346. return this.GetMenuItemDiv(this.mnSelMenuItem);
  347. }
  348. function GetMenuItemDivID(nMenuItem)
  349. {
  350. return (this.msTextBoxID + "_mi_" + nMenuItem);
  351. }
  352. function GetMenuItemDiv(nMenuItem)
  353. {
  354. var sDivMenuItemID=this.GetMenuItemDivID(nMenuItem);
  355. return document.getElementById(sDivMenuItemID)
  356. }
  357. function MoveUp()
  358. {
  359. var nMenuItem;
  360. nMenuItem=this.mnSelMenuItem+1;
  361. //Check if menu item exists
  362. if(this.GetMenuItemDiv(nMenuItem))
  363. {
  364. this.SelectMenuItem(nMenuItem)
  365. }
  366. }
  367. function MoveDown()
  368. {
  369. var nMenuItem;
  370. nMenuItem=this.mnSelMenuItem-1;
  371. if(nMenuItem!=0)
  372. {
  373. this.SelectMenuItem(nMenuItem)
  374. }
  375. }
  376. //Highlights a div
  377. function SelectMenuItem(nMenuItem)
  378. {
  379. var divMenuItem=this.GetMenuItemDiv(nMenuItem)
  380. if(divMenuItem)
  381. {
  382. if (nMenuItem!=this.mnSelMenuItem)
  383. {
  384. this.UnselectMenuItem();
  385. this.mnSelMenuItem=nMenuItem;
  386. this.SetTextBoxValue();
  387. divMenuItem.className=this.msSelMenuItemCSSClass;
  388. }
  389. }
  390. }
  391. //unhighlights a div
  392. function UnselectMenuItem()
  393. {
  394. var divMenuItem=this.GetSelMenuItemDiv()
  395. if(divMenuItem)
  396. {
  397. divMenuItem.className=this.msMenuItemCSSClass;
  398. }
  399. }
  400. function IsVisibleMenuDiv()
  401. {
  402. if (this.GetMenuDiv().style.visibility == 'hidden')
  403. {
  404. return false;
  405. }
  406. else
  407. {
  408. return true;
  409. }
  410. }
  411. function MoveMenuDivIfAbsolutePos()
  412. {
  413. var txtCtrl=this.GetTextBoxCtrl();
  414. var divMenu=this.GetMenuDiv();
  415. if (txtCtrl.style.position!="absolute")
  416. return;
  417. TRACE("MoveMenuDivIfAbsolutePos Moving absolute");
  418. //Move menu right under text box
  419. divMenu.style.left =txtCtrl.offsetLeft;
  420. divMenu.style.top =txtCtrl.offsetTop + txtCtrl.offsetHeight;
  421. }
  422. function ShowMenuDiv(sDivContent)
  423. {
  424. this.MoveMenuDivIfAbsolutePos();
  425. TRACE("ShowMenuDiv : " + this.msTextBoxID);
  426. var divMenu=this.GetMenuDiv();
  427. var sInnerHtml;
  428. //Use IFrame of the same size as div
  429. if (IsIE() && this.mbUseIFrame)
  430. {
  431. sInnerHtml = "<div id='" + this.msMenuDivID + "_content'>";
  432. sInnerHtml += sDivContent;
  433. sInnerHtml += "</div>";
  434. var sBlankPage=this.msResourcesDir + "/Blank.html"; //Use blank page to hide 'nonsecure items' message in IE when using HTTPS
  435. sInnerHtml += "<iframe id='" + this.msMenuDivID + "_iframe' src='" + sBlankPage + "' frameborder='1' scrolling='no'></iframe>";
  436. }
  437. else
  438. {
  439. sInnerHtml=sDivContent;
  440. }
  441. divMenu.innerHTML = sInnerHtml;
  442. if (IsIE() && this.mbUseIFrame)
  443. {
  444. var divContent;
  445. divContent=document.getElementById(this.msMenuDivID + "_content");
  446. var divIframe;
  447. divIframe=document.getElementById(this.msMenuDivID + "_iframe");
  448. //Remember display type
  449. divContent.className=this.msMenuCSSClass;
  450. divMenu.className="asbMenuBase";
  451. divIframe.style.width = divContent.offsetWidth + 'px';
  452. divIframe.style.height = divContent.offsetHeight + 'px';
  453. divIframe.marginTop = "-" + divContent.offsetHeight + 'px';
  454. }
  455. divMenu.style.visibility = 'visible';
  456. }
  457. function HideMenuDiv()
  458. {
  459. this.GetMenuDiv().style.visibility = 'hidden';
  460. this.mnSelMenuItem=0;
  461. }
  462. //Utility functions (don't need this.)
  463. function IsIE()
  464. {
  465. return ( navigator.appName=="Microsoft Internet Explorer" );
  466. }
  467. function IsNav()
  468. {
  469. return ( navigator.appName=="Netscape" );
  470. }
  471. function GetInnerHtml(oItem)
  472. {
  473. var sOut;
  474. if (oItem.innerText)
  475. {
  476. sOut=oItem.innerText; // IE
  477. }
  478. else if (oItem.textContent)
  479. {
  480. sOut=oItem.textContent; // Mozilla
  481. }
  482. return (sOut);
  483. }
  484. function TRACE(sText)
  485. {
  486. var txtTrace=document.getElementById("txtASBTrace");
  487. if (txtTrace!=null)
  488. txtTrace.value = txtTrace.value + sText + "\n";
  489. }
  490. }