logo

searx

My custom branche(s) on searx, a meta-search engine git clone https://hacktivis.me/git/searx.git

autocomplete.js (19786B)


  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.AutoComplete = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. /*
  3. * @license MIT
  4. *
  5. * Autocomplete.js v2.6.3
  6. * Developed by Baptiste Donaux
  7. * http://autocomplete-js.com
  8. *
  9. * (c) 2017, Baptiste Donaux
  10. */
  11. "use strict";
  12. var ConditionOperator;
  13. (function (ConditionOperator) {
  14. ConditionOperator[ConditionOperator["AND"] = 0] = "AND";
  15. ConditionOperator[ConditionOperator["OR"] = 1] = "OR";
  16. })(ConditionOperator || (ConditionOperator = {}));
  17. var EventType;
  18. (function (EventType) {
  19. EventType[EventType["KEYDOWN"] = 0] = "KEYDOWN";
  20. EventType[EventType["KEYUP"] = 1] = "KEYUP";
  21. })(EventType || (EventType = {}));
  22. /**
  23. * Core
  24. *
  25. * @class
  26. * @author Baptiste Donaux <baptiste.donaux@gmail.com> @baptistedonaux
  27. */
  28. var AutoComplete = (function () {
  29. // Constructor
  30. function AutoComplete(params, selector) {
  31. if (params === void 0) { params = {}; }
  32. if (selector === void 0) { selector = "[data-autocomplete]"; }
  33. if (Array.isArray(selector)) {
  34. selector.forEach(function (s) {
  35. new AutoComplete(params, s);
  36. });
  37. }
  38. else if (typeof selector == "string") {
  39. var elements = document.querySelectorAll(selector);
  40. Array.prototype.forEach.call(elements, function (input) {
  41. new AutoComplete(params, input);
  42. });
  43. }
  44. else {
  45. var specificParams = AutoComplete.merge(AutoComplete.defaults, params, {
  46. DOMResults: document.createElement("div")
  47. });
  48. AutoComplete.prototype.create(specificParams, selector);
  49. return specificParams;
  50. }
  51. }
  52. AutoComplete.prototype.create = function (params, element) {
  53. params.Input = element;
  54. if (params.Input.nodeName.match(/^INPUT$/i) && (params.Input.hasAttribute("type") === false || params.Input.getAttribute("type").match(/^TEXT|SEARCH$/i))) {
  55. params.Input.setAttribute("autocomplete", "off");
  56. params._Position(params);
  57. params.Input.parentNode.appendChild(params.DOMResults);
  58. params.$Listeners = {
  59. blur: params._Blur.bind(params),
  60. destroy: AutoComplete.prototype.destroy.bind(null, params),
  61. focus: params._Focus.bind(params),
  62. keyup: AutoComplete.prototype.event.bind(null, params, EventType.KEYUP),
  63. keydown: AutoComplete.prototype.event.bind(null, params, EventType.KEYDOWN),
  64. position: params._Position.bind(params)
  65. };
  66. for (var event in params.$Listeners) {
  67. params.Input.addEventListener(event, params.$Listeners[event]);
  68. }
  69. }
  70. };
  71. AutoComplete.prototype.getEventsByType = function (params, type) {
  72. var mappings = {};
  73. for (var key in params.KeyboardMappings) {
  74. var event = EventType.KEYUP;
  75. if (params.KeyboardMappings[key].Event !== undefined) {
  76. event = params.KeyboardMappings[key].Event;
  77. }
  78. if (event == type) {
  79. mappings[key] = params.KeyboardMappings[key];
  80. }
  81. }
  82. return mappings;
  83. };
  84. AutoComplete.prototype.event = function (params, type, event) {
  85. var eventIdentifier = function (condition) {
  86. if ((match === true && mapping.Operator == ConditionOperator.AND) || (match === false && mapping.Operator == ConditionOperator.OR)) {
  87. condition = AutoComplete.merge({
  88. Not: false
  89. }, condition);
  90. if (condition.hasOwnProperty("Is")) {
  91. if (condition.Is == event.keyCode) {
  92. match = !condition.Not;
  93. }
  94. else {
  95. match = condition.Not;
  96. }
  97. }
  98. else if (condition.hasOwnProperty("From") && condition.hasOwnProperty("To")) {
  99. if (event.keyCode >= condition.From && event.keyCode <= condition.To) {
  100. match = !condition.Not;
  101. }
  102. else {
  103. match = condition.Not;
  104. }
  105. }
  106. }
  107. };
  108. for (var name in AutoComplete.prototype.getEventsByType(params, type)) {
  109. var mapping = AutoComplete.merge({
  110. Operator: ConditionOperator.AND
  111. }, params.KeyboardMappings[name]), match = ConditionOperator.AND == mapping.Operator;
  112. mapping.Conditions.forEach(eventIdentifier);
  113. if (match === true) {
  114. mapping.Callback.call(params, event);
  115. }
  116. }
  117. };
  118. AutoComplete.prototype.makeRequest = function (params, callback) {
  119. var propertyHttpHeaders = Object.getOwnPropertyNames(params.HttpHeaders), request = new XMLHttpRequest(), method = params._HttpMethod(), url = params._Url(), queryParams = params._Pre(), queryParamsStringify = encodeURIComponent(params._QueryArg()) + "=" + encodeURIComponent(queryParams);
  120. if (method.match(/^GET$/i)) {
  121. if (url.indexOf("?") !== -1) {
  122. url += "&" + queryParamsStringify;
  123. }
  124. else {
  125. url += "?" + queryParamsStringify;
  126. }
  127. }
  128. request.open(method, url, true);
  129. for (var i = propertyHttpHeaders.length - 1; i >= 0; i--) {
  130. request.setRequestHeader(propertyHttpHeaders[i], params.HttpHeaders[propertyHttpHeaders[i]]);
  131. }
  132. request.onreadystatechange = function () {
  133. if (request.readyState == 4 && request.status == 200) {
  134. params.$Cache[queryParams] = request.response;
  135. callback(request.response);
  136. }
  137. };
  138. return request;
  139. };
  140. AutoComplete.prototype.ajax = function (params, request, timeout) {
  141. if (timeout === void 0) { timeout = true; }
  142. if (params.$AjaxTimer) {
  143. window.clearTimeout(params.$AjaxTimer);
  144. }
  145. if (timeout === true) {
  146. params.$AjaxTimer = window.setTimeout(AutoComplete.prototype.ajax.bind(null, params, request, false), params.Delay);
  147. }
  148. else {
  149. if (params.Request) {
  150. params.Request.abort();
  151. }
  152. params.Request = request;
  153. params.Request.send(params._QueryArg() + "=" + params._Pre());
  154. }
  155. };
  156. AutoComplete.prototype.cache = function (params, callback) {
  157. var response = params._Cache(params._Pre());
  158. if (response === undefined) {
  159. var request = AutoComplete.prototype.makeRequest(params, callback);
  160. AutoComplete.prototype.ajax(params, request);
  161. }
  162. else {
  163. callback(response);
  164. }
  165. };
  166. AutoComplete.prototype.destroy = function (params) {
  167. for (var event in params.$Listeners) {
  168. params.Input.removeEventListener(event, params.$Listeners[event]);
  169. }
  170. params.DOMResults.parentNode.removeChild(params.DOMResults);
  171. };
  172. return AutoComplete;
  173. }());
  174. AutoComplete.merge = function () {
  175. var merge = {}, tmp;
  176. for (var i = 0; i < arguments.length; i++) {
  177. for (tmp in arguments[i]) {
  178. merge[tmp] = arguments[i][tmp];
  179. }
  180. }
  181. return merge;
  182. };
  183. AutoComplete.defaults = {
  184. Delay: 150,
  185. EmptyMessage: "No result here",
  186. Highlight: {
  187. getRegex: function (value) {
  188. return new RegExp(value, "ig");
  189. },
  190. transform: function (value) {
  191. return "<strong>" + value + "</strong>";
  192. }
  193. },
  194. HttpHeaders: {
  195. "Content-type": "application/x-www-form-urlencoded"
  196. },
  197. Limit: 0,
  198. MinChars: 0,
  199. HttpMethod: "GET",
  200. QueryArg: "q",
  201. Url: null,
  202. KeyboardMappings: {
  203. "Enter": {
  204. Conditions: [{
  205. Is: 13,
  206. Not: false
  207. }],
  208. Callback: function (event) {
  209. if (this.DOMResults.getAttribute("class").indexOf("open") != -1) {
  210. var liActive = this.DOMResults.querySelector("li.active");
  211. if (liActive !== null) {
  212. event.preventDefault();
  213. this._Select(liActive);
  214. this.DOMResults.setAttribute("class", "autocomplete");
  215. }
  216. }
  217. },
  218. Operator: ConditionOperator.AND,
  219. Event: EventType.KEYDOWN
  220. },
  221. "KeyUpAndDown_down": {
  222. Conditions: [{
  223. Is: 38,
  224. Not: false
  225. },
  226. {
  227. Is: 40,
  228. Not: false
  229. }],
  230. Callback: function (event) {
  231. event.preventDefault();
  232. },
  233. Operator: ConditionOperator.OR,
  234. Event: EventType.KEYDOWN
  235. },
  236. "KeyUpAndDown_up": {
  237. Conditions: [{
  238. Is: 38,
  239. Not: false
  240. },
  241. {
  242. Is: 40,
  243. Not: false
  244. }],
  245. Callback: function (event) {
  246. event.preventDefault();
  247. var first = this.DOMResults.querySelector("li:first-child:not(.locked)"), last = this.DOMResults.querySelector("li:last-child:not(.locked)"), active = this.DOMResults.querySelector("li.active");
  248. if (active) {
  249. var currentIndex = Array.prototype.indexOf.call(active.parentNode.children, active), position = currentIndex + (event.keyCode - 39), lisCount = this.DOMResults.getElementsByTagName("li").length;
  250. if (position < 0) {
  251. position = lisCount - 1;
  252. }
  253. else if (position >= lisCount) {
  254. position = 0;
  255. }
  256. active.classList.remove("active");
  257. active.parentElement.children.item(position).classList.add("active");
  258. }
  259. else if (last && event.keyCode == 38) {
  260. last.classList.add("active");
  261. }
  262. else if (first) {
  263. first.classList.add("active");
  264. }
  265. },
  266. Operator: ConditionOperator.OR,
  267. Event: EventType.KEYUP
  268. },
  269. "AlphaNum": {
  270. Conditions: [{
  271. Is: 13,
  272. Not: true
  273. }, {
  274. From: 35,
  275. To: 40,
  276. Not: true
  277. }],
  278. Callback: function () {
  279. var oldValue = this.Input.getAttribute("data-autocomplete-old-value"), currentValue = this._Pre();
  280. if (currentValue !== "" && currentValue.length >= this._MinChars()) {
  281. if (!oldValue || currentValue != oldValue) {
  282. this.DOMResults.setAttribute("class", "autocomplete open");
  283. }
  284. AutoComplete.prototype.cache(this, function (response) {
  285. this._Render(this._Post(response));
  286. this._Open();
  287. }.bind(this));
  288. }
  289. },
  290. Operator: ConditionOperator.AND,
  291. Event: EventType.KEYUP
  292. }
  293. },
  294. DOMResults: null,
  295. Request: null,
  296. Input: null,
  297. /**
  298. * Return the message when no result returns
  299. */
  300. _EmptyMessage: function () {
  301. var emptyMessage = "";
  302. if (this.Input.hasAttribute("data-autocomplete-empty-message")) {
  303. emptyMessage = this.Input.getAttribute("data-autocomplete-empty-message");
  304. }
  305. else if (this.EmptyMessage !== false) {
  306. emptyMessage = this.EmptyMessage;
  307. }
  308. else {
  309. emptyMessage = "";
  310. }
  311. return emptyMessage;
  312. },
  313. /**
  314. * Returns the maximum number of results
  315. */
  316. _Limit: function () {
  317. var limit = this.Input.getAttribute("data-autocomplete-limit");
  318. if (isNaN(limit) || limit === null) {
  319. return this.Limit;
  320. }
  321. return parseInt(limit, 10);
  322. },
  323. /**
  324. * Returns the minimum number of characters entered before firing ajax
  325. */
  326. _MinChars: function () {
  327. var minchars = this.Input.getAttribute("data-autocomplete-minchars");
  328. if (isNaN(minchars) || minchars === null) {
  329. return this.MinChars;
  330. }
  331. return parseInt(minchars, 10);
  332. },
  333. /**
  334. * Apply transformation on labels response
  335. */
  336. _Highlight: function (label) {
  337. return label.replace(this.Highlight.getRegex(this._Pre()), this.Highlight.transform);
  338. },
  339. /**
  340. * Returns the HHTP method to use
  341. */
  342. _HttpMethod: function () {
  343. if (this.Input.hasAttribute("data-autocomplete-method")) {
  344. return this.Input.getAttribute("data-autocomplete-method");
  345. }
  346. return this.HttpMethod;
  347. },
  348. /**
  349. * Returns the query param to use
  350. */
  351. _QueryArg: function () {
  352. if (this.Input.hasAttribute("data-autocomplete-param-name")) {
  353. return this.Input.getAttribute("data-autocomplete-param-name");
  354. }
  355. return this.QueryArg;
  356. },
  357. /**
  358. * Returns the URL to use for AJAX request
  359. */
  360. _Url: function () {
  361. if (this.Input.hasAttribute("data-autocomplete")) {
  362. return this.Input.getAttribute("data-autocomplete");
  363. }
  364. return this.Url;
  365. },
  366. /**
  367. * Manage the close
  368. */
  369. _Blur: function (now) {
  370. if (now === true) {
  371. this.DOMResults.setAttribute("class", "autocomplete");
  372. this.Input.setAttribute("data-autocomplete-old-value", this.Input.value);
  373. }
  374. else {
  375. var params = this;
  376. setTimeout(function () {
  377. params._Blur(true);
  378. }, 150);
  379. }
  380. },
  381. /**
  382. * Manage the cache
  383. */
  384. _Cache: function (value) {
  385. return this.$Cache[value];
  386. },
  387. /**
  388. * Manage the open
  389. */
  390. _Focus: function () {
  391. var oldValue = this.Input.getAttribute("data-autocomplete-old-value");
  392. if ((!oldValue || this.Input.value != oldValue) && this._MinChars() <= this.Input.value.length) {
  393. this.DOMResults.setAttribute("class", "autocomplete open");
  394. }
  395. },
  396. /**
  397. * Bind all results item if one result is opened
  398. */
  399. _Open: function () {
  400. var params = this;
  401. Array.prototype.forEach.call(this.DOMResults.getElementsByTagName("li"), function (li) {
  402. if (li.getAttribute("class") != "locked") {
  403. li.onclick = function (event) {
  404. params._Select(li);
  405. };
  406. li.onmouseenter = function () {
  407. var active = params.DOMResults.querySelector("li.active");
  408. if (active !== li) {
  409. if (active !== null) {
  410. active.classList.remove("active");
  411. }
  412. li.classList.add("active");
  413. }
  414. };
  415. }
  416. });
  417. },
  418. /**
  419. * Position the results HTML element
  420. */
  421. _Position: function () {
  422. this.DOMResults.setAttribute("class", "autocomplete");
  423. this.DOMResults.setAttribute("style", "top:" + (this.Input.offsetTop + this.Input.offsetHeight) + "px;left:" + this.Input.offsetLeft + "px;width:" + this.Input.clientWidth + "px;");
  424. },
  425. /**
  426. * Execute the render of results DOM element
  427. */
  428. _Render: function (response) {
  429. var ul;
  430. if (typeof response == "string") {
  431. ul = this._RenderRaw(response);
  432. }
  433. else {
  434. ul = this._RenderResponseItems(response);
  435. }
  436. if (this.DOMResults.hasChildNodes()) {
  437. this.DOMResults.removeChild(this.DOMResults.childNodes[0]);
  438. }
  439. this.DOMResults.appendChild(ul);
  440. },
  441. /**
  442. * ResponseItems[] rendering
  443. */
  444. _RenderResponseItems: function (response) {
  445. var ul = document.createElement("ul"), li = document.createElement("li"), limit = this._Limit();
  446. // Order
  447. if (limit < 0) {
  448. response = response.reverse();
  449. }
  450. else if (limit === 0) {
  451. limit = response.length;
  452. }
  453. for (var item = 0; item < Math.min(Math.abs(limit), response.length); item++) {
  454. li.innerHTML = response[item].Label;
  455. li.setAttribute("data-autocomplete-value", response[item].Value);
  456. ul.appendChild(li);
  457. li = document.createElement("li");
  458. }
  459. return ul;
  460. },
  461. /**
  462. * string response rendering (RAW HTML)
  463. */
  464. _RenderRaw: function (response) {
  465. var ul = document.createElement("ul"), li = document.createElement("li");
  466. if (response.length > 0) {
  467. this.DOMResults.innerHTML = response;
  468. }
  469. else {
  470. var emptyMessage = this._EmptyMessage();
  471. if (emptyMessage !== "") {
  472. li.innerHTML = emptyMessage;
  473. li.setAttribute("class", "locked");
  474. ul.appendChild(li);
  475. }
  476. }
  477. return ul;
  478. },
  479. /**
  480. * Deal with request response
  481. */
  482. _Post: function (response) {
  483. try {
  484. var returnResponse = [];
  485. //JSON return
  486. var json = JSON.parse(response);
  487. if (Object.keys(json).length === 0) {
  488. return "";
  489. }
  490. if (Array.isArray(json)) {
  491. for (var i = 0; i < Object.keys(json).length; i++) {
  492. returnResponse[returnResponse.length] = { "Value": json[i], "Label": this._Highlight(json[i]) };
  493. }
  494. }
  495. else {
  496. for (var value in json) {
  497. returnResponse.push({
  498. "Value": value,
  499. "Label": this._Highlight(json[value])
  500. });
  501. }
  502. }
  503. return returnResponse;
  504. }
  505. catch (event) {
  506. //HTML return
  507. return response;
  508. }
  509. },
  510. /**
  511. * Return the autocomplete value to send (before request)
  512. */
  513. _Pre: function () {
  514. return this.Input.value;
  515. },
  516. /**
  517. * Choice one result item
  518. */
  519. _Select: function (item) {
  520. console.log('test test test');
  521. if (item.hasAttribute("data-autocomplete-value")) {
  522. this.Input.value = item.getAttribute("data-autocomplete-value");
  523. }
  524. else {
  525. this.Input.value = item.innerHTML;
  526. }
  527. this.Input.setAttribute("data-autocomplete-old-value", this.Input.value);
  528. },
  529. $AjaxTimer: null,
  530. $Cache: {},
  531. $Listeners: {}
  532. };
  533. module.exports = AutoComplete;
  534. },{}]},{},[1])(1)
  535. });