From 177ed4b6aa9c6c4e93a4a5a95ea24fb6db66077a Mon Sep 17 00:00:00 2001 From: tim b Date: Wed, 22 Sep 2010 21:23:20 -0700 Subject: initial shit version of new search --- static/tests/space.packing/search.js | 218 +++++++++++++++++++++++++++++++++++ 1 file changed, 218 insertions(+) create mode 100755 static/tests/space.packing/search.js (limited to 'static/tests/space.packing/search.js') diff --git a/static/tests/space.packing/search.js b/static/tests/space.packing/search.js new file mode 100755 index 0000000..5d03a3c --- /dev/null +++ b/static/tests/space.packing/search.js @@ -0,0 +1,218 @@ +// just a general object to hold global timers... +Timers = { + +} + + +var ImageCache = function(config){ + + config = config || {"token": Math.random(), 'timerName': "search"} + this.timerName = config.timerName + clearTimeout(Timers[config.timerName]) + this.urls = ('urls' in config) ? config.urls : [] + this.loadingImages = [] + this.loadedImages = [] + this.displayedImages = [] + this.loadAtATime = 10 + this.token = config.token + this.loadWatcher = makeImageCacheWatcher(this) + this.loadWatcher() +} + + +function makeImageCacheWatcher(imageCache){ return function(){ + + var newLoadingImages = [] + +// console.log("hi " + imageCache.token) + + imageCache.loadingImages.forEach(function(img){ + if (img.complete) { + if (img.height == 0 || img.width == 0 || img.height == NaN || img.width == NaN) { + // call server... dump.fm/cmd/thisImageIsFuckingBrokenY'all?img=foo + } else { + imageCache.loadedImages.push(img) +// console.log("loaded +" + img.src) + } + } else newLoadingImages.push(img) + }) + + delete imageCache['loadingImages']; + imageCache.loadingImages = newLoadingImages + + while (imageCache.loadingImages.length < imageCache.loadAtATime && imageCache.urls.length) { + var img = new Image() + img.src = imageCache.urls.shift() + img.animated = (parseUri(img.src)["file"].toLowerCase().substr(-3) == "gif") ? true : false; + imageCache.loadingImages.push(img) + } + + Timers[imageCache.timerName] = setTimeout(makeImageCacheWatcher(imageCache), 500) + +}} + + +/* +imagecache.add() +imagecache.add() +imagecache.add() + +periodically... if (loaded)... (display) + +pause & start +*/ + + +/* + search adds a script to the page... the script will call either Search.searchResult() or Search.searchError() + todo: clean this up. remove duplicated function names etc +*/ + +var Search = { + + 'term': "", + 'images': [], + 'tokens': [], + 'imageCache': new ImageCache(), + + 'init': function(){ + $("#search-query").val("search dump.fm") + $("#search-query").focus(function(){ + if ($("#search-query").val() == 'search dump.fm') + $("#search-query").val("") + }) + $("#search-query").blur(function(){ + if ($("#search-query").val().trim() == '') + $("#search-query").val("search dump.fm") + }) + $("#search-query").keydown(ifEnter(Search.doSearch)) + $("#search-results-images a").live("mouseup", Search.click) + }, + + 'addScript': function(term) { + $("#search-script").remove() + $("head").append("") + }, + 'setContent': function(x){ + $("#search-results-images").html(x) + }, + 'setMessage': function(x){ + $("#search-controls").css("display", "block") + $("#search-control-text").html(x) + }, + 'searchError': function(error){ + Search.setContent("") + $('#search-control-previous').css("visibility", "hidden") + $('#search-control-next').css("visibility", "hidden") + Search.setMessage(error) + }, + + 'doSearch': function(){ + term = $("#search-query").val().trim().toLowerCase() + var rawTokens = term.split(" ") + Search.tokens = [] + rawTokens.forEach(function(t){ if (t.length > 2) Search.tokens.push(t) }) + if (Search.tokens.length == 0) { + Search.setMessage("search query too small") + } else { + Search.setMessage("searching for '"+Search.tokens.join(" and ")+"'") + Search.addScript(Search.tokens.join("+")) + } + }, + + 'renderPage': function(num){ + $("#search-results-images").css("display", "block") + $("#search-controls").css("display", "block") + if (Search.images.length > 0) + Search.setMessage("results for '"+Search.tokens.join(" and ")+"' (page " + (num + 1) + ")"); + var contentString = '' + var start = num * Search.imagesPerPage + var imageCounter = 0 + for(var i = start; i < Search.images.length; i++){ + if(imageCounter > Search.imagesPerPage) break; + contentString += '' + imageCounter += 1 + } + contentString += '

' + if(num > 0) { + $('#search-control-previous').attr("href", 'javascript:Search.renderPage('+(num-1)+')') + $('#search-control-previous').css("visibility", "visible") + } else { + $('#search-control-previous').attr("href", 'javascript:void()') + $('#search-control-previous').css("visibility", "hidden") + } + + if (Search.images.length > start + imageCounter) { + $('#search-control-next').attr("href", 'javascript:Search.renderPage('+(num+1)+')') + $('#search-control-next').css("visibility", "visible") + } else { + $('#search-control-next').attr("href", 'javascript:void()') + $('#search-control-next').css("visibility", "hidden") + } + Search.setContent(contentString) + }, + + 'click': function(e){ + if (e.which == 1) // left click + if (Search.addToChatBoxIfPossible(this)) + window.open(this.href) + else if (e.which == 2) // middle click + window.open(this.href) + }, + + 'addToChatBoxIfPossible': function(img){ + var chatBoxExists = $("#msgInput").length + if (chatBoxExists) { + var chatText = $("#msgInput").val() + if (chatText.length && chatText[chatText.length - 1] != " ") + chatText += " " + chatText += $(img).attr("href") + " " + $("#msgInput").val(chatText) + $("#msgInput").focus().val($("#msgInput").val()) //http://stackoverflow.com/questions/1056359/ + return false + } else return true + }, + + 'searchResult': function(results){ + Search.images = [] + if(results === null || results.length == 0) { + Search.setContent("") + Search.setMessage("no results found") + } else { + results.forEach(function(r){ Search.images.push(r.url) }) + Search.display() + } + }, + 'display': function(){ + //console.log(Search.images) + Search.imageCache = new ImageCache({"urls": Search.images, "token": Math.random(), 'timerName': "search"}) + }, + 'close': function(){ + Search.setContent("") + $('#search-control-previous').css("visibility", "hidden") + $('#search-control-next').css("visibility", "hidden") + $("#search-results-images").css("display", "none") + $("#search-controls").css("display", "none") + } + + +} + +$('#content a').live('hover', function(event) { + if (event.type == 'mouseover') { + var img = Search.imageCache.displayedImages[this.index] + if (img.animated) { + img.width = img.adjWidth + img.height = img.adjHeight + $(this).addClass("animating") + $(this).append(img) + } + } else { + var img = Search.imageCache.displayedImages[this.index] + if (img.animated) { + $(this).removeClass("animating") + this.removeChild(img) + } + } +}); + \ No newline at end of file -- cgit v1.2.3-70-g09d2