var initialClusters = []; var enrichedClusters = []; var clusters = []; var query = ""; displayOptions = { numberDisplayedItems: 4, showFilters: true, imageFilter: 'onlyImages' } function search(keyword, target) { query = keyword; $(document).prop('title', 'Searching for ' + query); $("#results").append(searchingHtml()); onDone = function(data){ $("#results").children().remove(); showFilters(); processJsonResults(data); createResultClusters(); $(document).prop('title', 'Results for ' + query); }; onFail = function(data, textStatus){ $("#results").children().remove(); $("#results").append(errorHtml(data, textStatus)); $(document).prop('title', 'Error on ' + query); }; //Get and process clusters if (typeof target == 'undefined') { $.getJSON("cluster_search_api", {query:query}) .done(onDone) .fail(onFail); } else { $.getJSON("cluster_search_api", {query:query, target:target}) .done(onDone) .fail(onFail); } } function searchingHtml(){ return $.el.div({'class':'row'}, $.el.div({'class':'col-lg-10 col-md-offset-1'}, $.el.h3('Searching for ', $.el.span({'class':'text-info'}, query)))); } function errorHtml(data, textStatus){ return $.el.div({'class':'row'}, $.el.div({'class':'col-lg-10 col-md-offset-1'}, $.el.h3('Unfortunately an ', $.el.span({'class':'text-danger'}, 'error'), ' has occured:')), $.el.div({'class':'row'}, $.el.div({'class':'well well-sm col-md-10 col-md-offset-1'}, data.responseText))); } function noResultsHtml(query) { return $.el.div({'class':'row'}, $.el.div({'class':'col-lg-10 col-md-offset-1'}, $.el.h3('No results found for ', $.el.span({'class':'text-danger'}, query)))); } function noFilterResultsHtml() { return $.el.h4('No results due to filter: ', $.el.span({'class':'text-danger'}, displayOptions.imageFilter)); } function showFilters() { console.log('Showing filters:', displayOptions.showFilters); if(displayOptions.showFilters) { $("#results").append(filterAndRankButtons()); } } function processJsonResults(data) { // Convert json to initialClusters array var sourceClusters = data.clusters; var numberOfClusters = sourceClusters.length; for (var i=0;i b.items.length) return -1; return 0; } function cluster(path, items) { this.path = path; this.items = items; } function item(uri, thumb, link, title) { this.uri = uri; this.thumb = thumb; this.link = link; this.title = title; } function clone(obj) { // Handle the 3 simple types, and null or undefined if (null == obj || "object" != typeof obj) return obj; // Handle Date if (obj instanceof Date) { var copy = new Date(); copy.setTime(obj.getTime()); return copy; } // Handle Array if (obj instanceof Array) { var copy = []; for (var i = 0, len = obj.length; i < len; i++) { copy[i] = clone(obj[i]); } return copy; } // Handle Object if (obj instanceof Object) { var copy = {}; for (var attr in obj) { if (obj.hasOwnProperty(attr)) copy[attr] = clone(obj[attr]); } return copy; } throw new Error("Unable to copy obj! Its type isn't supported."); } function truncate (string, limit) { var chars; var i; chars = string.split(''); if (chars.length > limit) { for (var i=chars.length - 1; i>-1; --i) { if (i>limit) { chars.length = i; } else if (' ' === chars[i]) { chars.length = i; break; } } chars.push('...'); } return chars.join(''); } function setGlobalQuery(keyword) { query = keyword; } //if ($("#search-field").length > 0) { //$("#search-field").typeahead({ // source: autoComplete, // items: 10, // updater: function(item) { // this.$element[0].value = item; // this.$element[0].form.submit(); // return item; // } //}); //} // //var autoComplete = function(query, process) { // //Construct the url (hardcoded the portnumber for now) // var autocompleteAPI = "http://" + document.domain + ":3737/api/ac_find_literal"; // maxResults = 10; // console.log("Url of the autocomplete api: " + autocompleteAPI); // console.log("Query length: " + query.length); // if(query.length>2){ // $.getJSON(autocompleteAPI, { // query:query, // maxResultsDisplayed:maxResults // }, // function(data){ // var results = processJsonResults(data, query, maxResults); // process(results); // }); // } //}; // // //var autoComplete = function(query, process) { // //Construct the url (hardcoded the portnumber for now) // var autocompleteAPI = "http://" + document.domain + ":3737/api/ac_find_literal"; // maxResults = 10; // console.log("Url of the autocomplete api: " + autocompleteAPI); // console.log("Query length: " + query.length); // if(query.length>2){ // $.getJSON(autocompleteAPI, { // query:query, // maxResultsDisplayed:maxResults // }, // function(data){ // var results = processJsonResults(data, query, maxResults); // process(results); // }); // } //}; // //if ($("#search-field").length > 0) { // $("#search-field").typeahead({ // source: autoComplete, // items: 10, // updater: function(item) { // this.$element[0].value = item; // this.$element[0].form.submit(); // return item; // } // }); //} //function processJsonResults(data, query, maxResults) { // var results = []; // var numberOfResults = data.query.count; // //Add query as first result (otherwise unwanted complex searches) // results[0] = query; // console.log("Json results: " + numberOfResults); // for (var i=0; i 25) { // label = label.substr(0,22) + "..."; // } // return label; //}