summaryrefslogtreecommitdiff
path: root/static/tests/space.packing/search.js
blob: 61fca03d6e9b6000f6e041fddebbd2390da3b213 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// 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("<scr"+"ipt src='http://dump.fm/cmd/ghettosearch/"+term+"' id='search-script'></sc"+"ript>")
  },
  '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("+"))
    }
  },

  '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(){
    $("#content").html("")
    initSpaceFill()
    //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)
      }
    }
});