diff options
Diffstat (limited to 'static/js/src/search.js')
| -rw-r--r-- | static/js/src/search.js | 230 |
1 files changed, 230 insertions, 0 deletions
diff --git a/static/js/src/search.js b/static/js/src/search.js new file mode 100644 index 0000000..d854e34 --- /dev/null +++ b/static/js/src/search.js @@ -0,0 +1,230 @@ + +var Search = { + + 'term': "", + 'images': [], + 'tokens': [], + 'closed': true, + + 'initFullpage': function(){ + Search.type = "fullpage" + Search.init() + Search.initSpaceFill = Search.initSpaceFillFullpage + }, + + 'initInpage': function(){ + Search.type = "inpage" + Search.init() + Search.initSpaceFill = Search.initSpaceFillInpage + }, + + 'init': function(){ + ImgCache.config("search", {"onImgsLoaded": Search.imgsLoaded}) + + $('#search-results-images a').live('hover', Search.resultsHover) + + var input = Search.$input = $("#search-query") + var label = "search dump.fm" + Search.$container = $("#search-results-images") + + input.val(label) + input.focus(function(){ + if (input.val() == label) input.val("") + }) + input.blur(function(){ + if (input.val().trim() == '') input.val(label) + }) + input.keydown(ifEnter(Search.doSearch)) + $("#search-results-images a").live("mouseup", Search.click) + }, + + 'initSpaceFillFullpage': function() { + SpaceFill.init({ + "container": "#search-results-images", + "width": $(document).width(), + "height": $(document).height(), + "type": "columns", + "spacing": "justify", + "minMargin": 16, + "columnWidth": 250 + }) + }, + + 'initSpaceFillInpage': function() { + SpaceFill.init({ + "container": "#search-results-images", + "width": $(document).width() * 0.93, + "height": $(document).height(), + "type": "columns", + "spacing": "justify", + "minMargin": 8, + "columnWidth": 120 + }) + }, + + "resultsHover": function(e){ + if (e.type == 'mouseover') { + var img = ImgCache.imgs[this.href] + if (img.animated) { + img.width = img.adjWidth + img.height = img.adjHeight + $(this).addClass("animating") + $(this).append(img) + } + } else { + var img = ImgCache.imgs[this.href] + if (img.animated) { + $(this).removeClass("animating") + this.removeChild(img) + } + } + }, + + "imgsLoaded": function(imgs){ + //if (ColumnFill.isSpaceFilled()) return; + + if (Search.closed) return; + + if (Search.$container[0].style.display != "block") { + Search.$container.css("display", "block") + $("#userList").css("display", "none") + Search.setMessage("results for '"+Search.tokens.join(" and ")+"'") + } + + for (var url in imgs){ + var img = imgs[url] + if (isImgBroken(img)) continue; + var width = img.width + var height = img.height + + var maxWidth = SpaceFill.config.columnWidth + var maxHeight = Math.floor(SpaceFill.config.columnWidth * 1.2) + + if (width > maxWidth) { + scaleFactor = maxWidth / width + width = maxWidth + height = Math.floor(height * scaleFactor) + } else if (height > maxHeight) { + scaleFactor = maxHeight / height + height = maxHeight + width = Math.floor(width * scaleFactor) + } + img.adjWidth = width + img.adjHeight = height + + var c = document.createElement("canvas") + c.width = width + c.height = height + var ctx = c.getContext('2d'); + ctx.drawImage(img, 0, 0, c.width, c.height) + + var a = document.createElement("a") + a.onclick = falseFunc + a.href = img.src + a.style.width = width + "px" + a.style.height = height + "px" + + a.appendChild(c) + + SpaceFill.add(a) + } + }, + + 'setContent': function(x){ + $("#search-results-images").html(x) + }, + + 'setMessage': function(x){ + $("#search-controls").css("display", "block") + $("#search-message").html(x) + }, + + 'searchError': function(error){ + Search.setContent("") + Search.setMessage(error) + }, + + 'doSearch': function(){ + Search.closed = false + 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.doAjax(Search.tokens.join("+")) + } + }, + + 'doAjax': function(term) { + if (Domain == "http://dump.fm") { + $.ajax({ + "dataType": "json", + "url": "/cmd/search/" + term, + "success": Search.results, + "error": Search.error, + "timeout": 20000, + }) + } else { // search main site via jsonp + $("#search-script").remove() + $("head").append("<s"+"cript src='http://dump.fm/cmd/search/"+term+"?callback=Search.results' id='search-script'></s"+"cript>") + } + }, + + '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 + }, + + 'results': function(results){ + Search.resultsClear() + if(results === null || results.length == 0) { + Search.setMessage("no results found") + } else { + Search.initSpaceFill() + var urls = [] + results.forEach(function(r){ + var url = r.url + if (url.charAt(0) == '/') + url = 'http://dump.fm/images' + url + else + url = 'http://' + url + urls.push(url) + }) + ImgCache.add("search", urls) + } + }, + + 'resultsClear': function(){ + $("#search-results-images").html("") + //ImgCache.pause("search") + ImgCache.clear("search") + }, + + 'close': function(){ + Search.resultsClear() + Search.closed = true + Search.$container.css("display", "none") + $("#search-controls").css("display", "none") + $("#userList").css("display", "block") + } + +} |
