Advertisement
Guest User

Untitled

a guest
Apr 8th, 2014
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.43 KB | None | 0 0
  1. if (typeof(bsn) == "undefined")
  2. _b = bsn = {};
  3. if (typeof(_b.Autosuggest) == "undefined")
  4. _b.Autosuggest = {};
  5. else
  6. alert("Autosuggest is already set!");
  7. _b.AutoSuggest = function (id, param)
  8. {
  9. // no DOM - give up!
  10. //
  11. if (!document.getElementById)
  12. return 0;
  13. // get field via DOM
  14. //
  15. this.fld = _b.DOM.gE(id);
  16. if (!this.fld)
  17. return 0;
  18. // init variables
  19. //
  20. this.sInp = "";
  21. this.nInpC = 0;
  22. this.aSug = [];
  23. this.iHigh = 0;
  24. // parameters object
  25. //
  26. this.oP = param ? param : {};
  27. // defaults
  28. //
  29. var k, def = {minchars:1, meth:"get", varname:"input", className:"autosuggest", timeout:50000, delay:0, offsety:-5, shownoresults: true, noresults: "No albums or tracks have been found.", maxheight: 250, cache: false, maxentries: 25};
  30. for (k in def)
  31. {
  32. if (typeof(this.oP[k]) != typeof(def[k]))
  33. this.oP[k] = def[k];
  34. }
  35. // set keyup handler for field
  36. // and prevent autocomplete from client
  37. //
  38. var p = this;
  39. // NOTE: not using addEventListener because UpArrow fired twice in Safari
  40. //_b.DOM.addEvent( this.fld, 'keyup', function(ev){ return pointer.onKeyPress(ev); } );
  41. this.fld.onkeypress = function(ev){ return p.onKeyPress(ev); };
  42. this.fld.onkeyup = function(ev){ return p.onKeyUp(ev); };
  43. this.fld.setAttribute("autocomplete","off");
  44. };
  45. _b.AutoSuggest.prototype.onKeyPress = function(ev)
  46. {
  47. var key = (window.event) ? window.event.keyCode : ev.keyCode;
  48. // set responses to keydown events in the field
  49. // this allows the user to use the arrow keys to scroll through the results
  50. // ESCAPE clears the list
  51. // TAB sets the current highlighted value
  52. //
  53. var RETURN = 13;
  54. var TAB = 9;
  55. var ESC = 27;
  56. var bubble = 1;
  57. switch(key)
  58. {
  59. case RETURN:
  60. this.setHighlightedValue();
  61. bubble = 0;
  62. break;
  63. case ESC:
  64. this.clearSuggestions();
  65. break;
  66. }
  67. return bubble;
  68. };
  69. _b.AutoSuggest.prototype.onKeyUp = function(ev)
  70. {
  71. var key = (window.event) ? window.event.keyCode : ev.keyCode;
  72. // set responses to keydown events in the field
  73. // this allows the user to use the arrow keys to scroll through the results
  74. // ESCAPE clears the list
  75. // TAB sets the current highlighted value
  76. //
  77. var ARRUP = 38;
  78. var ARRDN = 40;
  79. var bubble = 1;
  80. switch(key)
  81. {
  82. case ARRUP:
  83. this.changeHighlight(key);
  84. bubble = 0;
  85. break;
  86. case ARRDN:
  87. this.changeHighlight(key);
  88. bubble = 0;
  89. break;
  90.  
  91.  
  92. default:
  93. this.getSuggestions(this.fld.value);
  94. }
  95. return bubble;
  96.  
  97. };_b.AutoSuggest.prototype.getSuggestions = function (val)
  98. {
  99. // if input stays the same, do nothing
  100. //
  101. if (val == this.sInp)
  102. return 0;
  103. // kill list
  104. //
  105. _b.DOM.remE(this.idAs);
  106. this.sInp = val;
  107. // input length is less than the min required to trigger a request
  108. // do nothing
  109. //
  110. if (val.length < this.oP.minchars)
  111. {
  112. this.aSug = [];
  113. this.nInpC = val.length;
  114. return 0;
  115. }
  116. var ol = this.nInpC; // old length
  117. this.nInpC = val.length ? val.length : 0;
  118. // if caching enabled, and user is typing (ie. length of input is increasing)
  119. // filter results out of aSuggestions from last request
  120. //
  121. var l = this.aSug.length;
  122. if (this.nInpC > ol && l && l<this.oP.maxentries && this.oP.cache)
  123. {
  124. var arr = [];
  125. for (var i=0;i<l;i++)
  126. {
  127. if (this.aSug[i].value.substr(0,val.length).toLowerCase() == val.toLowerCase())
  128. arr.push( this.aSug[i] );
  129. }
  130. this.aSug = arr;
  131.  
  132. this.createList(this.aSug);
  133.  
  134.  
  135.  
  136. return false;
  137. }
  138. else
  139. // do new request
  140. //
  141. {
  142. var pointer = this;
  143. var input = this.sInp;
  144. clearTimeout(this.ajID);
  145. this.ajID = setTimeout( function() { pointer.doAjaxRequest(input) }, this.oP.delay );
  146. }
  147. return false;
  148. };_b.AutoSuggest.prototype.doAjaxRequest = function (input)
  149. {
  150. // check that saved input is still the value of the field
  151. //
  152. if (input != this.fld.value)
  153. return false;
  154. var pointer = this;
  155. // create ajax request
  156. //
  157. if (typeof(this.oP.script) == "function")
  158. var url = this.oP.script(encodeURIComponent(this.sInp));
  159. else
  160. var url = this.oP.script+this.oP.varname+"="+encodeURIComponent(this.sInp);
  161. if (!url)
  162. return false;
  163. var meth = this.oP.meth;
  164. var input = this.sInp;
  165. var onSuccessFunc = function (req) { pointer.setSuggestions(req, input) };
  166. var onErrorFunc = function (status) { alert("AJAX error: "+status); };
  167. var myAjax = new _b.Ajax();
  168. myAjax.makeRequest( url, meth, onSuccessFunc, onErrorFunc );
  169. };_b.AutoSuggest.prototype.setSuggestions = function (req, input)
  170. {
  171. // if field input no longer matches what was passed to the request
  172. // don't show the suggestions
  173. //
  174. if (input != this.fld.value)
  175. return false;
  176. this.aSug = [];
  177. if (this.oP.json)
  178. {
  179. var jsondata = eval('(' + req.responseText + ')');
  180.  
  181. for (var i=0;i<jsondata.results.length;i++)
  182. {
  183. this.aSug.push( { 'id':jsondata.results[i].id, 'value':jsondata.results[i].value, 'info':jsondata.results[i].info } );
  184. }
  185. }
  186. else
  187. {
  188. var xml = req.responseXML;
  189. // traverse xml
  190. //
  191. var results = xml.getElementsByTagName('results')[0].childNodes;
  192. for (var i=0;i<results.length;i++)
  193. {
  194. if (results[i].hasChildNodes())
  195. this.aSug.push( { 'id':results[i].getAttribute('id'), 'value':results[i].childNodes[0].nodeValue, 'info':results[i].getAttribute('info') } );
  196. }
  197. }
  198. this.idAs = "as_"+this.fld.id;
  199. this.createList(this.aSug);
  200. };_b.AutoSuggest.prototype.createList = function(arr)
  201. {
  202. var pointer = this;
  203. // get rid of old list
  204. // and clear the list removal timeout
  205. //
  206. _b.DOM.remE(this.idAs);
  207. this.killTimeout();
  208. // if no results, and shownoresults is false, do nothing
  209. //
  210. if (arr.length == 0 && !this.oP.shownoresults)
  211. return false;
  212. // create holding div
  213. //
  214. var div = _b.DOM.cE("div", {id:this.idAs, className:this.oP.className});
  215. var hcorner = _b.DOM.cE("div", {className:"as_corner"});
  216. var hbar = _b.DOM.cE("div", {className:"as_bar"});
  217. var header = _b.DOM.cE("div", {className:"as_header"});
  218. header.appendChild(hcorner);
  219. header.appendChild(hbar);
  220. div.appendChild(header);
  221. // create and populate ul
  222. //
  223. var ul = _b.DOM.cE("ul", {id:"as_ul"});
  224. // loop throught arr of suggestions
  225. // creating an LI element for each suggestion
  226. //
  227. for (var i=0;i<arr.length;i++)
  228. {
  229. // format output with the input enclosed in a EM element
  230. // (as HTML, not DOM)
  231. //
  232. var val = arr[i].value;
  233. var st = val.toLowerCase().indexOf( this.sInp.toLowerCase() );
  234. var output = val.substring(0,st) + "<em>" + val.substring(st, st+this.sInp.length) + "</em>" + val.substring(st+this.sInp.length);
  235.  
  236.  
  237. var span = _b.DOM.cE("span", {}, output, true);
  238. if (arr[i].info != "")
  239. {
  240. var br = _b.DOM.cE("br", {});
  241. span.appendChild(br);
  242. var small = _b.DOM.cE("small", {}, arr[i].info);
  243. span.appendChild(small);
  244. }
  245.  
  246. var a = _b.DOM.cE("a", { href:"#" });
  247.  
  248. var tl = _b.DOM.cE("span", {className:"tl"}, " ");
  249. var tr = _b.DOM.cE("span", {className:"tr"}, " ");
  250. var pic = _b.DOM.cE("img",{src:"AutoSuggest/"+arr[i].id+".jpg",className:"float"}," ");
  251. a.appendChild(tl);
  252. a.appendChild(tr);
  253. a.appendChild(pic);
  254. a.appendChild(span);
  255.  
  256. a.name = i+1;
  257. a.onclick = function () { pointer.setHighlightedValue(); return false; };
  258. a.onmouseover = function () { pointer.setHighlight(this.name); };
  259.  
  260. var li = _b.DOM.cE( "li", {}, a );
  261.  
  262. ul.appendChild( li );
  263. }
  264. // no results
  265. //
  266. if (arr.length == 0 && this.oP.shownoresults)
  267. {
  268. var li = _b.DOM.cE( "li", {className:"as_warning"}, this.oP.noresults );
  269. ul.appendChild( li );
  270. }
  271. div.appendChild( ul );
  272. var fcorner = _b.DOM.cE("div", {className:"as_corner"});
  273. var fbar = _b.DOM.cE("div", {className:"as_bar"});
  274. var footer = _b.DOM.cE("div", {className:"as_footer"});
  275. footer.appendChild(fcorner);
  276. footer.appendChild(fbar);
  277. div.appendChild(footer);
  278. // get position of target textfield
  279. // position holding div below it
  280. // set width of holding div to width of field
  281. //
  282. var pos = _b.DOM.getPos(this.fld);
  283. div.style.left = pos.x + "px";
  284. div.style.top = ( pos.y + this.fld.offsetHeight + this.oP.offsety ) + "px";
  285. div.style.width = this.fld.offsetWidth + "px";
  286. // set mouseover functions for div
  287. // when mouse pointer leaves div, set a timeout to remove the list after an interval
  288. // when mouse enters div, kill the timeout so the list won't be removed
  289. //
  290. div.onmouseover = function(){ pointer.killTimeout() };
  291. div.onmouseout = function(){ pointer.resetTimeout() };
  292. // add DIV to document
  293. //
  294. document.getElementsByTagName("body")[0].appendChild(div);
  295. // currently no item is highlighted
  296. //
  297. this.iHigh = 0;
  298. // remove list after an interval
  299. //
  300. var pointer = this;
  301. this.toID = setTimeout(function () { pointer.clearSuggestions() }, this.oP.timeout);
  302. };_b.AutoSuggest.prototype.changeHighlight = function(key)
  303. {
  304. var list = _b.DOM.gE("as_ul");
  305. if (!list)
  306. return false;
  307. var n;
  308. if (key == 40)
  309. n = this.iHigh + 1;
  310. else if (key == 38)
  311. n = this.iHigh - 1;
  312. if (n > list.childNodes.length)
  313. n = list.childNodes.length;
  314. if (n < 1)
  315. n = 1;
  316. this.setHighlight(n);
  317. };
  318. _b.AutoSuggest.prototype.setHighlight = function(n)
  319. {
  320. var list = _b.DOM.gE("as_ul");
  321. if (!list)
  322. return false;
  323. if (this.iHigh > 0)
  324. this.clearHighlight();
  325. this.iHigh = Number(n);
  326. list.childNodes[this.iHigh-1].className = "as_highlight";
  327. this.killTimeout();
  328. };
  329. _b.AutoSuggest.prototype.clearHighlight = function()
  330. {
  331. var list = _b.DOM.gE("as_ul");
  332. if (!list)
  333. return false;
  334. if (this.iHigh > 0)
  335. {
  336. list.childNodes[this.iHigh-1].className = "";
  337. this.iHigh = 0;
  338. }
  339. };
  340. _b.AutoSuggest.prototype.setHighlightedValue = function ()
  341. {
  342. if (this.iHigh)
  343. {
  344. this.sInp = this.fld.value = this.aSug[ this.iHigh-1 ].value;
  345.  
  346. // move cursor to end of input (safari)
  347. //
  348. this.fld.focus();
  349. if (this.fld.selectionStart)
  350. this.fld.setSelectionRange(this.sInp.length, this.sInp.length);
  351.  
  352. this.clearSuggestions();
  353.  
  354. // pass selected object to callback function, if exists
  355. //
  356. if (typeof(this.oP.callback) == "function")
  357. this.oP.callback( this.aSug[this.iHigh-1] );
  358. }
  359. };
  360. _b.AutoSuggest.prototype.killTimeout = function()
  361. {
  362. clearTimeout(this.toID);
  363. };
  364. _b.AutoSuggest.prototype.resetTimeout = function()
  365. {
  366. clearTimeout(this.toID);
  367. var pointer = this;
  368. this.toID = setTimeout(function () { pointer.clearSuggestions() }, 1000);
  369. };
  370. _b.AutoSuggest.prototype.clearSuggestions = function ()
  371. {
  372. this.killTimeout();
  373. var ele = _b.DOM.gE(this.idAs);
  374. var pointer = this;
  375. if (ele)
  376. {
  377. var fade = new _b.Fader(ele,1,0,250,function () { _b.DOM.remE(pointer.idAs) });
  378. }
  379. };
  380. // AJAX PROTOTYPE _____________________________________________
  381. if (typeof(_b.Ajax) == "undefined")
  382. _b.Ajax = {};
  383. _b.Ajax = function ()
  384. {
  385. this.req = {};
  386. this.isIE = false;
  387. };
  388. _b.Ajax.prototype.makeRequest = function (url, meth, onComp, onErr)
  389. {
  390. if (meth != "POST")
  391. meth = "GET";
  392. this.onComplete = onComp;
  393. this.onError = onErr;
  394. var pointer = this;
  395. // branch for native XMLHttpRequest object
  396. if (window.XMLHttpRequest)
  397. {
  398. this.req = new XMLHttpRequest();
  399. this.req.onreadystatechange = function () { pointer.processReqChange() };
  400. this.req.open("GET", url, true); //
  401. this.req.send(null);
  402. // branch for IE/Windows ActiveX version
  403. }
  404. else if (window.ActiveXObject)
  405. {
  406. this.req = new ActiveXObject("Microsoft.XMLHTTP");
  407. if (this.req)
  408. {
  409. this.req.onreadystatechange = function () { pointer.processReqChange() };
  410. this.req.open(meth, url, true);
  411. this.req.send();
  412. }
  413. }
  414. };
  415. _b.Ajax.prototype.processReqChange = function()
  416. {
  417. // only if req shows "loaded"
  418. if (this.req.readyState == 4) {
  419. // only if "OK"
  420. if (this.req.status == 200)
  421. {
  422. this.onComplete( this.req );
  423. } else {
  424. this.onError( this.req.status );
  425. }
  426. }
  427. };
  428. // DOM PROTOTYPE _____________________________________________
  429. if (typeof(_b.DOM) == "undefined")
  430. _b.DOM = {};
  431. /* create element */
  432. _b.DOM.cE = function ( type, attr, cont, html )
  433. {
  434. var ne = document.createElement( type );
  435. if (!ne)
  436. return 0;
  437.  
  438. for (var a in attr)
  439. ne[a] = attr[a];
  440. var t = typeof(cont);
  441. if (t == "string" && !html)
  442. ne.appendChild( document.createTextNode(cont) );
  443. else if (t == "string" && html)
  444. ne.innerHTML = cont;
  445. else if (t == "object")
  446. ne.appendChild( cont );
  447. return ne;
  448. };
  449. /* get element */
  450. _b.DOM.gE = function ( e )
  451. {
  452. var t=typeof(e);
  453. if (t == "undefined")
  454. return 0;
  455. else if (t == "string")
  456. {
  457. var re = document.getElementById( e );
  458. if (!re)
  459. return 0;
  460. else if (typeof(re.appendChild) != "undefined" )
  461. return re;
  462. else
  463. return 0;
  464. }
  465. else if (typeof(e.appendChild) != "undefined")
  466. return e;
  467. else
  468. return 0;
  469. };
  470. /* remove element */
  471. _b.DOM.remE = function ( ele )
  472. {
  473. var e = this.gE(ele);
  474. if (!e)
  475. return 0;
  476. else if (e.parentNode.removeChild(e))
  477. return true;
  478. else
  479. return 0;
  480. };
  481. /* get position */
  482. _b.DOM.getPos = function ( e )
  483. {
  484. var e = this.gE(e);
  485. var obj = e;
  486. var curleft = 0;
  487. if (obj.offsetParent)
  488. {
  489. while (obj.offsetParent)
  490. {
  491. curleft += obj.offsetLeft;
  492. obj = obj.offsetParent;
  493. }
  494. }
  495. else if (obj.x)
  496. curleft += obj.x;
  497. var obj = e;
  498. var curtop = 0;
  499. if (obj.offsetParent)
  500. {
  501. while (obj.offsetParent)
  502. {
  503. curtop += obj.offsetTop;
  504. obj = obj.offsetParent;
  505. }
  506. }
  507. else if (obj.y)
  508. curtop += obj.y;
  509. return {x:curleft, y:curtop};
  510. };
  511. // FADER PROTOTYPE _____________________________________________
  512. if (typeof(_b.Fader) == "undefined")
  513. _b.Fader = {};_b.Fader = function (ele, from, to, fadetime, callback)
  514. {
  515. if (!ele)
  516. return 0;
  517. this.e = ele;
  518. this.from = from;
  519. this.to = to;
  520. this.cb = callback;
  521. this.nDur = fadetime;
  522.  
  523. this.nInt = 50;
  524. this.nTime = 0;
  525. var p = this;
  526. this.nID = setInterval(function() { p._fade() }, this.nInt);
  527. };_b.Fader.prototype._fade = function()
  528. {
  529. this.nTime += this.nInt;
  530. var ieop = Math.round( this._tween(this.nTime, this.from, this.to, this.nDur) * 100 );
  531. var op = ieop / 100;
  532. if (this.e.filters) // internet explorer
  533. {
  534. try
  535. {
  536. this.e.filters.item("DXImageTransform.Microsoft.Alpha").opacity = ieop;
  537. } catch (e) {
  538. // If it is not set initially, the browser will throw an error. This will set it if it is not set yet.
  539. this.e.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity='+ieop+')';
  540. }
  541. }
  542. else // other browsers
  543. {
  544. this.e.style.opacity = op;
  545. }
  546. if (this.nTime == this.nDur)
  547. {
  548. clearInterval( this.nID );
  549. if (this.cb != undefined)
  550. this.cb();
  551. }
  552. };
  553. _b.Fader.prototype._tween = function(t,b,c,d)
  554. {
  555. return b + ( (c-b) * (t/d) );
  556. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement