search.js 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407
  1. /*!
  2. * # Semantic UI 2.2.6 - Search
  3. * http://github.com/semantic-org/semantic-ui/
  4. *
  5. *
  6. * Released under the MIT license
  7. * http://opensource.org/licenses/MIT
  8. *
  9. */
  10. ;(function ($, window, document, undefined) {
  11. "use strict";
  12. window = (typeof window != 'undefined' && window.Math == Math)
  13. ? window
  14. : (typeof self != 'undefined' && self.Math == Math)
  15. ? self
  16. : Function('return this')()
  17. ;
  18. $.fn.search = function(parameters) {
  19. var
  20. $allModules = $(this),
  21. moduleSelector = $allModules.selector || '',
  22. time = new Date().getTime(),
  23. performance = [],
  24. query = arguments[0],
  25. methodInvoked = (typeof query == 'string'),
  26. queryArguments = [].slice.call(arguments, 1),
  27. returnedValue
  28. ;
  29. $(this)
  30. .each(function() {
  31. var
  32. settings = ( $.isPlainObject(parameters) )
  33. ? $.extend(true, {}, $.fn.search.settings, parameters)
  34. : $.extend({}, $.fn.search.settings),
  35. className = settings.className,
  36. metadata = settings.metadata,
  37. regExp = settings.regExp,
  38. fields = settings.fields,
  39. selector = settings.selector,
  40. error = settings.error,
  41. namespace = settings.namespace,
  42. eventNamespace = '.' + namespace,
  43. moduleNamespace = namespace + '-module',
  44. $module = $(this),
  45. $prompt = $module.find(selector.prompt),
  46. $searchButton = $module.find(selector.searchButton),
  47. $results = $module.find(selector.results),
  48. $result = $module.find(selector.result),
  49. $category = $module.find(selector.category),
  50. element = this,
  51. instance = $module.data(moduleNamespace),
  52. disabledBubbled = false,
  53. module
  54. ;
  55. module = {
  56. initialize: function() {
  57. module.verbose('Initializing module');
  58. module.determine.searchFields();
  59. module.bind.events();
  60. module.set.type();
  61. module.create.results();
  62. module.instantiate();
  63. },
  64. instantiate: function() {
  65. module.verbose('Storing instance of module', module);
  66. instance = module;
  67. $module
  68. .data(moduleNamespace, module)
  69. ;
  70. },
  71. destroy: function() {
  72. module.verbose('Destroying instance');
  73. $module
  74. .off(eventNamespace)
  75. .removeData(moduleNamespace)
  76. ;
  77. },
  78. refresh: function() {
  79. module.debug('Refreshing selector cache');
  80. $prompt = $module.find(selector.prompt);
  81. $searchButton = $module.find(selector.searchButton);
  82. $category = $module.find(selector.category);
  83. $results = $module.find(selector.results);
  84. $result = $module.find(selector.result);
  85. },
  86. refreshResults: function() {
  87. $results = $module.find(selector.results);
  88. $result = $module.find(selector.result);
  89. },
  90. bind: {
  91. events: function() {
  92. module.verbose('Binding events to search');
  93. if(settings.automatic) {
  94. $module
  95. .on(module.get.inputEvent() + eventNamespace, selector.prompt, module.event.input)
  96. ;
  97. $prompt
  98. .attr('autocomplete', 'off')
  99. ;
  100. }
  101. $module
  102. // prompt
  103. .on('focus' + eventNamespace, selector.prompt, module.event.focus)
  104. .on('blur' + eventNamespace, selector.prompt, module.event.blur)
  105. .on('keydown' + eventNamespace, selector.prompt, module.handleKeyboard)
  106. // search button
  107. .on('click' + eventNamespace, selector.searchButton, module.query)
  108. // results
  109. .on('mousedown' + eventNamespace, selector.results, module.event.result.mousedown)
  110. .on('mouseup' + eventNamespace, selector.results, module.event.result.mouseup)
  111. .on('click' + eventNamespace, selector.result, module.event.result.click)
  112. ;
  113. }
  114. },
  115. determine: {
  116. searchFields: function() {
  117. // this makes sure $.extend does not add specified search fields to default fields
  118. // this is the only setting which should not extend defaults
  119. if(parameters && parameters.searchFields !== undefined) {
  120. settings.searchFields = parameters.searchFields;
  121. }
  122. }
  123. },
  124. event: {
  125. input: function() {
  126. clearTimeout(module.timer);
  127. module.timer = setTimeout(module.query, settings.searchDelay);
  128. },
  129. focus: function() {
  130. module.set.focus();
  131. if( module.has.minimumCharacters() ) {
  132. module.query();
  133. if( module.can.show() ) {
  134. module.showResults();
  135. }
  136. }
  137. },
  138. blur: function(event) {
  139. var
  140. pageLostFocus = (document.activeElement === this),
  141. callback = function() {
  142. module.cancel.query();
  143. module.remove.focus();
  144. module.timer = setTimeout(module.hideResults, settings.hideDelay);
  145. }
  146. ;
  147. if(pageLostFocus) {
  148. return;
  149. }
  150. if(module.resultsClicked) {
  151. module.debug('Determining if user action caused search to close');
  152. $module
  153. .one('click.close' + eventNamespace, selector.results, function(event) {
  154. if(module.is.inMessage(event) || disabledBubbled) {
  155. $prompt.focus();
  156. return;
  157. }
  158. disabledBubbled = false;
  159. if( !module.is.animating() && !module.is.hidden()) {
  160. callback();
  161. }
  162. })
  163. ;
  164. }
  165. else {
  166. module.debug('Input blurred without user action, closing results');
  167. callback();
  168. }
  169. },
  170. result: {
  171. mousedown: function() {
  172. module.resultsClicked = true;
  173. },
  174. mouseup: function() {
  175. module.resultsClicked = false;
  176. },
  177. click: function(event) {
  178. module.debug('Search result selected');
  179. var
  180. $result = $(this),
  181. $title = $result.find(selector.title).eq(0),
  182. $link = $result.is('a[href]')
  183. ? $result
  184. : $result.find('a[href]').eq(0),
  185. href = $link.attr('href') || false,
  186. target = $link.attr('target') || false,
  187. title = $title.html(),
  188. // title is used for result lookup
  189. value = ($title.length > 0)
  190. ? $title.text()
  191. : false,
  192. results = module.get.results(),
  193. result = $result.data(metadata.result) || module.get.result(value, results),
  194. returnedValue
  195. ;
  196. if( $.isFunction(settings.onSelect) ) {
  197. if(settings.onSelect.call(element, result, results) === false) {
  198. module.debug('Custom onSelect callback cancelled default select action');
  199. disabledBubbled = true;
  200. return;
  201. }
  202. }
  203. module.hideResults();
  204. if(value) {
  205. module.set.value(value);
  206. }
  207. if(href) {
  208. module.verbose('Opening search link found in result', $link);
  209. if(target == '_blank' || event.ctrlKey) {
  210. window.open(href);
  211. }
  212. else {
  213. window.location.href = (href);
  214. }
  215. }
  216. }
  217. }
  218. },
  219. handleKeyboard: function(event) {
  220. var
  221. // force selector refresh
  222. $result = $module.find(selector.result),
  223. $category = $module.find(selector.category),
  224. $activeResult = $result.filter('.' + className.active),
  225. currentIndex = $result.index( $activeResult ),
  226. resultSize = $result.length,
  227. hasActiveResult = $activeResult.length > 0,
  228. keyCode = event.which,
  229. keys = {
  230. backspace : 8,
  231. enter : 13,
  232. escape : 27,
  233. upArrow : 38,
  234. downArrow : 40
  235. },
  236. newIndex
  237. ;
  238. // search shortcuts
  239. if(keyCode == keys.escape) {
  240. module.verbose('Escape key pressed, blurring search field');
  241. module.trigger.blur();
  242. }
  243. if( module.is.visible() ) {
  244. if(keyCode == keys.enter) {
  245. module.verbose('Enter key pressed, selecting active result');
  246. if( $result.filter('.' + className.active).length > 0 ) {
  247. module.event.result.click.call($result.filter('.' + className.active), event);
  248. event.preventDefault();
  249. return false;
  250. }
  251. }
  252. else if(keyCode == keys.upArrow && hasActiveResult) {
  253. module.verbose('Up key pressed, changing active result');
  254. newIndex = (currentIndex - 1 < 0)
  255. ? currentIndex
  256. : currentIndex - 1
  257. ;
  258. $category
  259. .removeClass(className.active)
  260. ;
  261. $result
  262. .removeClass(className.active)
  263. .eq(newIndex)
  264. .addClass(className.active)
  265. .closest($category)
  266. .addClass(className.active)
  267. ;
  268. event.preventDefault();
  269. }
  270. else if(keyCode == keys.downArrow) {
  271. module.verbose('Down key pressed, changing active result');
  272. newIndex = (currentIndex + 1 >= resultSize)
  273. ? currentIndex
  274. : currentIndex + 1
  275. ;
  276. $category
  277. .removeClass(className.active)
  278. ;
  279. $result
  280. .removeClass(className.active)
  281. .eq(newIndex)
  282. .addClass(className.active)
  283. .closest($category)
  284. .addClass(className.active)
  285. ;
  286. event.preventDefault();
  287. }
  288. }
  289. else {
  290. // query shortcuts
  291. if(keyCode == keys.enter) {
  292. module.verbose('Enter key pressed, executing query');
  293. module.query();
  294. module.set.buttonPressed();
  295. $prompt.one('keyup', module.remove.buttonFocus);
  296. }
  297. }
  298. },
  299. setup: {
  300. api: function(searchTerm) {
  301. var
  302. apiSettings = {
  303. debug : settings.debug,
  304. on : false,
  305. cache : true,
  306. action : 'search',
  307. urlData : {
  308. query : searchTerm
  309. },
  310. onSuccess : function(response) {
  311. module.parse.response.call(element, response, searchTerm);
  312. },
  313. onAbort : function(response) {
  314. },
  315. onFailure : function() {
  316. module.displayMessage(error.serverError);
  317. },
  318. onError : module.error
  319. },
  320. searchHTML
  321. ;
  322. $.extend(true, apiSettings, settings.apiSettings);
  323. module.verbose('Setting up API request', apiSettings);
  324. $module.api(apiSettings);
  325. }
  326. },
  327. can: {
  328. useAPI: function() {
  329. return $.fn.api !== undefined;
  330. },
  331. show: function() {
  332. return module.is.focused() && !module.is.visible() && !module.is.empty();
  333. },
  334. transition: function() {
  335. return settings.transition && $.fn.transition !== undefined && $module.transition('is supported');
  336. }
  337. },
  338. is: {
  339. animating: function() {
  340. return $results.hasClass(className.animating);
  341. },
  342. hidden: function() {
  343. return $results.hasClass(className.hidden);
  344. },
  345. inMessage: function(event) {
  346. if(!event.target) {
  347. return;
  348. }
  349. var
  350. $target = $(event.target),
  351. isInDOM = $.contains(document.documentElement, event.target)
  352. ;
  353. return (isInDOM && $target.closest(selector.message).length > 0);
  354. },
  355. empty: function() {
  356. return ($results.html() === '');
  357. },
  358. visible: function() {
  359. return ($results.filter(':visible').length > 0);
  360. },
  361. focused: function() {
  362. return ($prompt.filter(':focus').length > 0);
  363. }
  364. },
  365. trigger: {
  366. blur: function() {
  367. var
  368. events = document.createEvent('HTMLEvents'),
  369. promptElement = $prompt[0]
  370. ;
  371. if(promptElement) {
  372. module.verbose('Triggering native blur event');
  373. events.initEvent('blur', false, false);
  374. promptElement.dispatchEvent(events);
  375. }
  376. }
  377. },
  378. get: {
  379. inputEvent: function() {
  380. var
  381. prompt = $prompt[0],
  382. inputEvent = (prompt !== undefined && prompt.oninput !== undefined)
  383. ? 'input'
  384. : (prompt !== undefined && prompt.onpropertychange !== undefined)
  385. ? 'propertychange'
  386. : 'keyup'
  387. ;
  388. return inputEvent;
  389. },
  390. value: function() {
  391. return $prompt.val();
  392. },
  393. results: function() {
  394. var
  395. results = $module.data(metadata.results)
  396. ;
  397. return results;
  398. },
  399. result: function(value, results) {
  400. var
  401. lookupFields = ['title', 'id'],
  402. result = false
  403. ;
  404. value = (value !== undefined)
  405. ? value
  406. : module.get.value()
  407. ;
  408. results = (results !== undefined)
  409. ? results
  410. : module.get.results()
  411. ;
  412. if(settings.type === 'category') {
  413. module.debug('Finding result that matches', value);
  414. $.each(results, function(index, category) {
  415. if($.isArray(category.results)) {
  416. result = module.search.object(value, category.results, lookupFields)[0];
  417. // don't continue searching if a result is found
  418. if(result) {
  419. return false;
  420. }
  421. }
  422. });
  423. }
  424. else {
  425. module.debug('Finding result in results object', value);
  426. result = module.search.object(value, results, lookupFields)[0];
  427. }
  428. return result || false;
  429. },
  430. },
  431. select: {
  432. firstResult: function() {
  433. module.verbose('Selecting first result');
  434. $result.first().addClass(className.active);
  435. }
  436. },
  437. set: {
  438. focus: function() {
  439. $module.addClass(className.focus);
  440. },
  441. loading: function() {
  442. $module.addClass(className.loading);
  443. },
  444. value: function(value) {
  445. module.verbose('Setting search input value', value);
  446. $prompt
  447. .val(value)
  448. ;
  449. },
  450. type: function(type) {
  451. type = type || settings.type;
  452. if(settings.type == 'category') {
  453. $module.addClass(settings.type);
  454. }
  455. },
  456. buttonPressed: function() {
  457. $searchButton.addClass(className.pressed);
  458. }
  459. },
  460. remove: {
  461. loading: function() {
  462. $module.removeClass(className.loading);
  463. },
  464. focus: function() {
  465. $module.removeClass(className.focus);
  466. },
  467. buttonPressed: function() {
  468. $searchButton.removeClass(className.pressed);
  469. }
  470. },
  471. query: function() {
  472. var
  473. searchTerm = module.get.value(),
  474. cache = module.read.cache(searchTerm)
  475. ;
  476. if( module.has.minimumCharacters() ) {
  477. if(cache) {
  478. module.debug('Reading result from cache', searchTerm);
  479. module.save.results(cache.results);
  480. module.addResults(cache.html);
  481. module.inject.id(cache.results);
  482. }
  483. else {
  484. module.debug('Querying for', searchTerm);
  485. if($.isPlainObject(settings.source) || $.isArray(settings.source)) {
  486. module.search.local(searchTerm);
  487. }
  488. else if( module.can.useAPI() ) {
  489. module.search.remote(searchTerm);
  490. }
  491. else {
  492. module.error(error.source);
  493. }
  494. }
  495. settings.onSearchQuery.call(element, searchTerm);
  496. }
  497. else {
  498. module.hideResults();
  499. }
  500. },
  501. search: {
  502. local: function(searchTerm) {
  503. var
  504. results = module.search.object(searchTerm, settings.content),
  505. searchHTML
  506. ;
  507. module.set.loading();
  508. module.save.results(results);
  509. module.debug('Returned local search results', results);
  510. searchHTML = module.generateResults({
  511. results: results
  512. });
  513. module.remove.loading();
  514. module.addResults(searchHTML);
  515. module.inject.id(results);
  516. module.write.cache(searchTerm, {
  517. html : searchHTML,
  518. results : results
  519. });
  520. },
  521. remote: function(searchTerm) {
  522. if($module.api('is loading')) {
  523. $module.api('abort');
  524. }
  525. module.setup.api(searchTerm);
  526. $module
  527. .api('query')
  528. ;
  529. },
  530. object: function(searchTerm, source, searchFields) {
  531. var
  532. results = [],
  533. fuzzyResults = [],
  534. searchExp = searchTerm.toString().replace(regExp.escape, '\\$&'),
  535. matchRegExp = new RegExp(regExp.beginsWith + searchExp, 'i'),
  536. // avoid duplicates when pushing results
  537. addResult = function(array, result) {
  538. var
  539. notResult = ($.inArray(result, results) == -1),
  540. notFuzzyResult = ($.inArray(result, fuzzyResults) == -1)
  541. ;
  542. if(notResult && notFuzzyResult) {
  543. array.push(result);
  544. }
  545. }
  546. ;
  547. source = source || settings.source;
  548. searchFields = (searchFields !== undefined)
  549. ? searchFields
  550. : settings.searchFields
  551. ;
  552. // search fields should be array to loop correctly
  553. if(!$.isArray(searchFields)) {
  554. searchFields = [searchFields];
  555. }
  556. // exit conditions if no source
  557. if(source === undefined || source === false) {
  558. module.error(error.source);
  559. return [];
  560. }
  561. // iterate through search fields looking for matches
  562. $.each(searchFields, function(index, field) {
  563. $.each(source, function(label, content) {
  564. var
  565. fieldExists = (typeof content[field] == 'string')
  566. ;
  567. if(fieldExists) {
  568. if( content[field].search(matchRegExp) !== -1) {
  569. // content starts with value (first in results)
  570. addResult(results, content);
  571. }
  572. else if(settings.searchFullText && module.fuzzySearch(searchTerm, content[field]) ) {
  573. // content fuzzy matches (last in results)
  574. addResult(fuzzyResults, content);
  575. }
  576. }
  577. });
  578. });
  579. return $.merge(results, fuzzyResults);
  580. }
  581. },
  582. fuzzySearch: function(query, term) {
  583. var
  584. termLength = term.length,
  585. queryLength = query.length
  586. ;
  587. if(typeof query !== 'string') {
  588. return false;
  589. }
  590. query = query.toLowerCase();
  591. term = term.toLowerCase();
  592. if(queryLength > termLength) {
  593. return false;
  594. }
  595. if(queryLength === termLength) {
  596. return (query === term);
  597. }
  598. search: for (var characterIndex = 0, nextCharacterIndex = 0; characterIndex < queryLength; characterIndex++) {
  599. var
  600. queryCharacter = query.charCodeAt(characterIndex)
  601. ;
  602. while(nextCharacterIndex < termLength) {
  603. if(term.charCodeAt(nextCharacterIndex++) === queryCharacter) {
  604. continue search;
  605. }
  606. }
  607. return false;
  608. }
  609. return true;
  610. },
  611. parse: {
  612. response: function(response, searchTerm) {
  613. var
  614. searchHTML = module.generateResults(response)
  615. ;
  616. module.verbose('Parsing server response', response);
  617. if(response !== undefined) {
  618. if(searchTerm !== undefined && response[fields.results] !== undefined) {
  619. module.addResults(searchHTML);
  620. module.inject.id(response[fields.results]);
  621. module.write.cache(searchTerm, {
  622. html : searchHTML,
  623. results : response[fields.results]
  624. });
  625. module.save.results(response[fields.results]);
  626. }
  627. }
  628. }
  629. },
  630. cancel: {
  631. query: function() {
  632. if( module.can.useAPI() ) {
  633. $module.api('abort');
  634. }
  635. }
  636. },
  637. has: {
  638. minimumCharacters: function() {
  639. var
  640. searchTerm = module.get.value(),
  641. numCharacters = searchTerm.length
  642. ;
  643. return (numCharacters >= settings.minCharacters);
  644. }
  645. },
  646. clear: {
  647. cache: function(value) {
  648. var
  649. cache = $module.data(metadata.cache)
  650. ;
  651. if(!value) {
  652. module.debug('Clearing cache', value);
  653. $module.removeData(metadata.cache);
  654. }
  655. else if(value && cache && cache[value]) {
  656. module.debug('Removing value from cache', value);
  657. delete cache[value];
  658. $module.data(metadata.cache, cache);
  659. }
  660. }
  661. },
  662. read: {
  663. cache: function(name) {
  664. var
  665. cache = $module.data(metadata.cache)
  666. ;
  667. if(settings.cache) {
  668. module.verbose('Checking cache for generated html for query', name);
  669. return (typeof cache == 'object') && (cache[name] !== undefined)
  670. ? cache[name]
  671. : false
  672. ;
  673. }
  674. return false;
  675. }
  676. },
  677. create: {
  678. id: function(resultIndex, categoryIndex) {
  679. var
  680. resultID = (resultIndex + 1), // not zero indexed
  681. categoryID = (categoryIndex + 1),
  682. firstCharCode,
  683. letterID,
  684. id
  685. ;
  686. if(categoryIndex !== undefined) {
  687. // start char code for "A"
  688. letterID = String.fromCharCode(97 + categoryIndex);
  689. id = letterID + resultID;
  690. module.verbose('Creating category result id', id);
  691. }
  692. else {
  693. id = resultID;
  694. module.verbose('Creating result id', id);
  695. }
  696. return id;
  697. },
  698. results: function() {
  699. if($results.length === 0) {
  700. $results = $('<div />')
  701. .addClass(className.results)
  702. .appendTo($module)
  703. ;
  704. }
  705. }
  706. },
  707. inject: {
  708. result: function(result, resultIndex, categoryIndex) {
  709. module.verbose('Injecting result into results');
  710. var
  711. $selectedResult = (categoryIndex !== undefined)
  712. ? $results
  713. .children().eq(categoryIndex)
  714. .children(selector.result).eq(resultIndex)
  715. : $results
  716. .children(selector.result).eq(resultIndex)
  717. ;
  718. module.verbose('Injecting results metadata', $selectedResult);
  719. $selectedResult
  720. .data(metadata.result, result)
  721. ;
  722. },
  723. id: function(results) {
  724. module.debug('Injecting unique ids into results');
  725. var
  726. // since results may be object, we must use counters
  727. categoryIndex = 0,
  728. resultIndex = 0
  729. ;
  730. if(settings.type === 'category') {
  731. // iterate through each category result
  732. $.each(results, function(index, category) {
  733. resultIndex = 0;
  734. $.each(category.results, function(index, value) {
  735. var
  736. result = category.results[index]
  737. ;
  738. if(result.id === undefined) {
  739. result.id = module.create.id(resultIndex, categoryIndex);
  740. }
  741. module.inject.result(result, resultIndex, categoryIndex);
  742. resultIndex++;
  743. });
  744. categoryIndex++;
  745. });
  746. }
  747. else {
  748. // top level
  749. $.each(results, function(index, value) {
  750. var
  751. result = results[index]
  752. ;
  753. if(result.id === undefined) {
  754. result.id = module.create.id(resultIndex);
  755. }
  756. module.inject.result(result, resultIndex);
  757. resultIndex++;
  758. });
  759. }
  760. return results;
  761. }
  762. },
  763. save: {
  764. results: function(results) {
  765. module.verbose('Saving current search results to metadata', results);
  766. $module.data(metadata.results, results);
  767. }
  768. },
  769. write: {
  770. cache: function(name, value) {
  771. var
  772. cache = ($module.data(metadata.cache) !== undefined)
  773. ? $module.data(metadata.cache)
  774. : {}
  775. ;
  776. if(settings.cache) {
  777. module.verbose('Writing generated html to cache', name, value);
  778. cache[name] = value;
  779. $module
  780. .data(metadata.cache, cache)
  781. ;
  782. }
  783. }
  784. },
  785. addResults: function(html) {
  786. if( $.isFunction(settings.onResultsAdd) ) {
  787. if( settings.onResultsAdd.call($results, html) === false ) {
  788. module.debug('onResultsAdd callback cancelled default action');
  789. return false;
  790. }
  791. }
  792. if(html) {
  793. $results
  794. .html(html)
  795. ;
  796. module.refreshResults();
  797. if(settings.selectFirstResult) {
  798. module.select.firstResult();
  799. }
  800. module.showResults();
  801. }
  802. else {
  803. module.hideResults();
  804. }
  805. },
  806. showResults: function() {
  807. if(!module.is.visible()) {
  808. if( module.can.transition() ) {
  809. module.debug('Showing results with css animations');
  810. $results
  811. .transition({
  812. animation : settings.transition + ' in',
  813. debug : settings.debug,
  814. verbose : settings.verbose,
  815. duration : settings.duration,
  816. queue : true
  817. })
  818. ;
  819. }
  820. else {
  821. module.debug('Showing results with javascript');
  822. $results
  823. .stop()
  824. .fadeIn(settings.duration, settings.easing)
  825. ;
  826. }
  827. settings.onResultsOpen.call($results);
  828. }
  829. },
  830. hideResults: function() {
  831. if( module.is.visible() ) {
  832. if( module.can.transition() ) {
  833. module.debug('Hiding results with css animations');
  834. $results
  835. .transition({
  836. animation : settings.transition + ' out',
  837. debug : settings.debug,
  838. verbose : settings.verbose,
  839. duration : settings.duration,
  840. queue : true
  841. })
  842. ;
  843. }
  844. else {
  845. module.debug('Hiding results with javascript');
  846. $results
  847. .stop()
  848. .fadeOut(settings.duration, settings.easing)
  849. ;
  850. }
  851. settings.onResultsClose.call($results);
  852. }
  853. },
  854. generateResults: function(response) {
  855. module.debug('Generating html from response', response);
  856. var
  857. template = settings.templates[settings.type],
  858. isProperObject = ($.isPlainObject(response[fields.results]) && !$.isEmptyObject(response[fields.results])),
  859. isProperArray = ($.isArray(response[fields.results]) && response[fields.results].length > 0),
  860. html = ''
  861. ;
  862. if(isProperObject || isProperArray ) {
  863. if(settings.maxResults > 0) {
  864. if(isProperObject) {
  865. if(settings.type == 'standard') {
  866. module.error(error.maxResults);
  867. }
  868. }
  869. else {
  870. response[fields.results] = response[fields.results].slice(0, settings.maxResults);
  871. }
  872. }
  873. if($.isFunction(template)) {
  874. html = template(response, fields);
  875. }
  876. else {
  877. module.error(error.noTemplate, false);
  878. }
  879. }
  880. else if(settings.showNoResults) {
  881. html = module.displayMessage(error.noResults, 'empty');
  882. }
  883. settings.onResults.call(element, response);
  884. return html;
  885. },
  886. displayMessage: function(text, type) {
  887. type = type || 'standard';
  888. module.debug('Displaying message', text, type);
  889. module.addResults( settings.templates.message(text, type) );
  890. return settings.templates.message(text, type);
  891. },
  892. setting: function(name, value) {
  893. if( $.isPlainObject(name) ) {
  894. $.extend(true, settings, name);
  895. }
  896. else if(value !== undefined) {
  897. settings[name] = value;
  898. }
  899. else {
  900. return settings[name];
  901. }
  902. },
  903. internal: function(name, value) {
  904. if( $.isPlainObject(name) ) {
  905. $.extend(true, module, name);
  906. }
  907. else if(value !== undefined) {
  908. module[name] = value;
  909. }
  910. else {
  911. return module[name];
  912. }
  913. },
  914. debug: function() {
  915. if(!settings.silent && settings.debug) {
  916. if(settings.performance) {
  917. module.performance.log(arguments);
  918. }
  919. else {
  920. module.debug = Function.prototype.bind.call(console.info, console, settings.name + ':');
  921. module.debug.apply(console, arguments);
  922. }
  923. }
  924. },
  925. verbose: function() {
  926. if(!settings.silent && settings.verbose && settings.debug) {
  927. if(settings.performance) {
  928. module.performance.log(arguments);
  929. }
  930. else {
  931. module.verbose = Function.prototype.bind.call(console.info, console, settings.name + ':');
  932. module.verbose.apply(console, arguments);
  933. }
  934. }
  935. },
  936. error: function() {
  937. if(!settings.silent) {
  938. module.error = Function.prototype.bind.call(console.error, console, settings.name + ':');
  939. module.error.apply(console, arguments);
  940. }
  941. },
  942. performance: {
  943. log: function(message) {
  944. var
  945. currentTime,
  946. executionTime,
  947. previousTime
  948. ;
  949. if(settings.performance) {
  950. currentTime = new Date().getTime();
  951. previousTime = time || currentTime;
  952. executionTime = currentTime - previousTime;
  953. time = currentTime;
  954. performance.push({
  955. 'Name' : message[0],
  956. 'Arguments' : [].slice.call(message, 1) || '',
  957. 'Element' : element,
  958. 'Execution Time' : executionTime
  959. });
  960. }
  961. clearTimeout(module.performance.timer);
  962. module.performance.timer = setTimeout(module.performance.display, 500);
  963. },
  964. display: function() {
  965. var
  966. title = settings.name + ':',
  967. totalTime = 0
  968. ;
  969. time = false;
  970. clearTimeout(module.performance.timer);
  971. $.each(performance, function(index, data) {
  972. totalTime += data['Execution Time'];
  973. });
  974. title += ' ' + totalTime + 'ms';
  975. if(moduleSelector) {
  976. title += ' \'' + moduleSelector + '\'';
  977. }
  978. if($allModules.length > 1) {
  979. title += ' ' + '(' + $allModules.length + ')';
  980. }
  981. if( (console.group !== undefined || console.table !== undefined) && performance.length > 0) {
  982. console.groupCollapsed(title);
  983. if(console.table) {
  984. console.table(performance);
  985. }
  986. else {
  987. $.each(performance, function(index, data) {
  988. console.log(data['Name'] + ': ' + data['Execution Time']+'ms');
  989. });
  990. }
  991. console.groupEnd();
  992. }
  993. performance = [];
  994. }
  995. },
  996. invoke: function(query, passedArguments, context) {
  997. var
  998. object = instance,
  999. maxDepth,
  1000. found,
  1001. response
  1002. ;
  1003. passedArguments = passedArguments || queryArguments;
  1004. context = element || context;
  1005. if(typeof query == 'string' && object !== undefined) {
  1006. query = query.split(/[\. ]/);
  1007. maxDepth = query.length - 1;
  1008. $.each(query, function(depth, value) {
  1009. var camelCaseValue = (depth != maxDepth)
  1010. ? value + query[depth + 1].charAt(0).toUpperCase() + query[depth + 1].slice(1)
  1011. : query
  1012. ;
  1013. if( $.isPlainObject( object[camelCaseValue] ) && (depth != maxDepth) ) {
  1014. object = object[camelCaseValue];
  1015. }
  1016. else if( object[camelCaseValue] !== undefined ) {
  1017. found = object[camelCaseValue];
  1018. return false;
  1019. }
  1020. else if( $.isPlainObject( object[value] ) && (depth != maxDepth) ) {
  1021. object = object[value];
  1022. }
  1023. else if( object[value] !== undefined ) {
  1024. found = object[value];
  1025. return false;
  1026. }
  1027. else {
  1028. return false;
  1029. }
  1030. });
  1031. }
  1032. if( $.isFunction( found ) ) {
  1033. response = found.apply(context, passedArguments);
  1034. }
  1035. else if(found !== undefined) {
  1036. response = found;
  1037. }
  1038. if($.isArray(returnedValue)) {
  1039. returnedValue.push(response);
  1040. }
  1041. else if(returnedValue !== undefined) {
  1042. returnedValue = [returnedValue, response];
  1043. }
  1044. else if(response !== undefined) {
  1045. returnedValue = response;
  1046. }
  1047. return found;
  1048. }
  1049. };
  1050. if(methodInvoked) {
  1051. if(instance === undefined) {
  1052. module.initialize();
  1053. }
  1054. module.invoke(query);
  1055. }
  1056. else {
  1057. if(instance !== undefined) {
  1058. instance.invoke('destroy');
  1059. }
  1060. module.initialize();
  1061. }
  1062. })
  1063. ;
  1064. return (returnedValue !== undefined)
  1065. ? returnedValue
  1066. : this
  1067. ;
  1068. };
  1069. $.fn.search.settings = {
  1070. name : 'Search',
  1071. namespace : 'search',
  1072. silent : false,
  1073. debug : false,
  1074. verbose : false,
  1075. performance : true,
  1076. // template to use (specified in settings.templates)
  1077. type : 'standard',
  1078. // minimum characters required to search
  1079. minCharacters : 1,
  1080. // whether to select first result after searching automatically
  1081. selectFirstResult : false,
  1082. // API config
  1083. apiSettings : false,
  1084. // object to search
  1085. source : false,
  1086. // fields to search
  1087. searchFields : [
  1088. 'title',
  1089. 'description'
  1090. ],
  1091. // field to display in standard results template
  1092. displayField : '',
  1093. // whether to include fuzzy results in local search
  1094. searchFullText : true,
  1095. // whether to add events to prompt automatically
  1096. automatic : true,
  1097. // delay before hiding menu after blur
  1098. hideDelay : 0,
  1099. // delay before searching
  1100. searchDelay : 200,
  1101. // maximum results returned from local
  1102. maxResults : 7,
  1103. // whether to store lookups in local cache
  1104. cache : true,
  1105. // whether no results errors should be shown
  1106. showNoResults : true,
  1107. // transition settings
  1108. transition : 'scale',
  1109. duration : 200,
  1110. easing : 'easeOutExpo',
  1111. // callbacks
  1112. onSelect : false,
  1113. onResultsAdd : false,
  1114. onSearchQuery : function(query){},
  1115. onResults : function(response){},
  1116. onResultsOpen : function(){},
  1117. onResultsClose : function(){},
  1118. className: {
  1119. animating : 'animating',
  1120. active : 'active',
  1121. empty : 'empty',
  1122. focus : 'focus',
  1123. hidden : 'hidden',
  1124. loading : 'loading',
  1125. results : 'results',
  1126. pressed : 'down'
  1127. },
  1128. error : {
  1129. source : 'Cannot search. No source used, and Semantic API module was not included',
  1130. noResults : 'Your search returned no results',
  1131. logging : 'Error in debug logging, exiting.',
  1132. noEndpoint : 'No search endpoint was specified',
  1133. noTemplate : 'A valid template name was not specified.',
  1134. serverError : 'There was an issue querying the server.',
  1135. maxResults : 'Results must be an array to use maxResults setting',
  1136. method : 'The method you called is not defined.'
  1137. },
  1138. metadata: {
  1139. cache : 'cache',
  1140. results : 'results',
  1141. result : 'result'
  1142. },
  1143. regExp: {
  1144. escape : /[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g,
  1145. beginsWith : '(?:\s|^)'
  1146. },
  1147. // maps api response attributes to internal representation
  1148. fields: {
  1149. categories : 'results', // array of categories (category view)
  1150. categoryName : 'name', // name of category (category view)
  1151. categoryResults : 'results', // array of results (category view)
  1152. description : 'description', // result description
  1153. image : 'image', // result image
  1154. price : 'price', // result price
  1155. results : 'results', // array of results (standard)
  1156. title : 'title', // result title
  1157. url : 'url', // result url
  1158. action : 'action', // "view more" object name
  1159. actionText : 'text', // "view more" text
  1160. actionURL : 'url' // "view more" url
  1161. },
  1162. selector : {
  1163. prompt : '.prompt',
  1164. searchButton : '.search.button',
  1165. results : '.results',
  1166. message : '.results > .message',
  1167. category : '.category',
  1168. result : '.result',
  1169. title : '.title, .name'
  1170. },
  1171. templates: {
  1172. escape: function(string) {
  1173. var
  1174. badChars = /[&<>"'`]/g,
  1175. shouldEscape = /[&<>"'`]/,
  1176. escape = {
  1177. "&": "&amp;",
  1178. "<": "&lt;",
  1179. ">": "&gt;",
  1180. '"': "&quot;",
  1181. "'": "&#x27;",
  1182. "`": "&#x60;"
  1183. },
  1184. escapedChar = function(chr) {
  1185. return escape[chr];
  1186. }
  1187. ;
  1188. if(shouldEscape.test(string)) {
  1189. return string.replace(badChars, escapedChar);
  1190. }
  1191. return string;
  1192. },
  1193. message: function(message, type) {
  1194. var
  1195. html = ''
  1196. ;
  1197. if(message !== undefined && type !== undefined) {
  1198. html += ''
  1199. + '<div class="message ' + type + '">'
  1200. ;
  1201. // message type
  1202. if(type == 'empty') {
  1203. html += ''
  1204. + '<div class="header">No Results</div class="header">'
  1205. + '<div class="description">' + message + '</div class="description">'
  1206. ;
  1207. }
  1208. else {
  1209. html += ' <div class="description">' + message + '</div>';
  1210. }
  1211. html += '</div>';
  1212. }
  1213. return html;
  1214. },
  1215. category: function(response, fields) {
  1216. var
  1217. html = '',
  1218. escape = $.fn.search.settings.templates.escape
  1219. ;
  1220. if(response[fields.categoryResults] !== undefined) {
  1221. // each category
  1222. $.each(response[fields.categoryResults], function(index, category) {
  1223. if(category[fields.results] !== undefined && category.results.length > 0) {
  1224. html += '<div class="category">';
  1225. if(category[fields.categoryName] !== undefined) {
  1226. html += '<div class="name">' + category[fields.categoryName] + '</div>';
  1227. }
  1228. // each item inside category
  1229. $.each(category.results, function(index, result) {
  1230. if(result[fields.url]) {
  1231. html += '<a class="result" href="' + result[fields.url] + '">';
  1232. }
  1233. else {
  1234. html += '<a class="result">';
  1235. }
  1236. if(result[fields.image] !== undefined) {
  1237. html += ''
  1238. + '<div class="image">'
  1239. + ' <img src="' + result[fields.image] + '">'
  1240. + '</div>'
  1241. ;
  1242. }
  1243. html += '<div class="content">';
  1244. if(result[fields.price] !== undefined) {
  1245. html += '<div class="price">' + result[fields.price] + '</div>';
  1246. }
  1247. if(result[fields.title] !== undefined) {
  1248. html += '<div class="title">' + result[fields.title] + '</div>';
  1249. }
  1250. if(result[fields.description] !== undefined) {
  1251. html += '<div class="description">' + result[fields.description] + '</div>';
  1252. }
  1253. html += ''
  1254. + '</div>'
  1255. ;
  1256. html += '</a>';
  1257. });
  1258. html += ''
  1259. + '</div>'
  1260. ;
  1261. }
  1262. });
  1263. if(response[fields.action]) {
  1264. html += ''
  1265. + '<a href="' + response[fields.action][fields.actionURL] + '" class="action">'
  1266. + response[fields.action][fields.actionText]
  1267. + '</a>';
  1268. }
  1269. return html;
  1270. }
  1271. return false;
  1272. },
  1273. standard: function(response, fields) {
  1274. var
  1275. html = ''
  1276. ;
  1277. if(response[fields.results] !== undefined) {
  1278. // each result
  1279. $.each(response[fields.results], function(index, result) {
  1280. if(result[fields.url]) {
  1281. html += '<a class="result" href="' + result[fields.url] + '">';
  1282. }
  1283. else {
  1284. html += '<a class="result">';
  1285. }
  1286. if(result[fields.image] !== undefined) {
  1287. html += ''
  1288. + '<div class="image">'
  1289. + ' <img src="' + result[fields.image] + '">'
  1290. + '</div>'
  1291. ;
  1292. }
  1293. html += '<div class="content">';
  1294. if(result[fields.price] !== undefined) {
  1295. html += '<div class="price">' + result[fields.price] + '</div>';
  1296. }
  1297. if(result[fields.title] !== undefined) {
  1298. html += '<div class="title">' + result[fields.title] + '</div>';
  1299. }
  1300. if(result[fields.description] !== undefined) {
  1301. html += '<div class="description">' + result[fields.description] + '</div>';
  1302. }
  1303. html += ''
  1304. + '</div>'
  1305. ;
  1306. html += '</a>';
  1307. });
  1308. if(response[fields.action]) {
  1309. html += ''
  1310. + '<a href="' + response[fields.action][fields.actionURL] + '" class="action">'
  1311. + response[fields.action][fields.actionText]
  1312. + '</a>';
  1313. }
  1314. return html;
  1315. }
  1316. return false;
  1317. }
  1318. }
  1319. };
  1320. })( jQuery, window, document );