summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/blogs/ArchiveView.js
blob: 785717a8c64bd2235934b3aee3d6c7465982b6b6 (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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
var ArchiveView = ScrollableView.extend({
  
  el: "#archive",
  menu_template: $("#archive .menu .template").html(),
  row_template: $("#archive .scroll .template").html(),
  
  events: {
    "click .item": "pick",
    "mousedown .row": "mousedown",
    "touchstart .row": "touchstart",
    "mousemove .row": "mousemove",
    "touchmove .row": "touchmove",
    "mouseup .row": "mouseup",
    "touchend .row": "touchend",
  },
  
  initialize: function(){
    this.$menu_items = this.$(".menu .items")
    this.$content = this.$(".content")
    this.$loader = this.$(".loader")
    this.scroller = new IScroll('#archive .scroll', app.iscroll_options)
    this.$subtitle = this.$('.subtitle')
    this.subtitle_html = this.$subtitle.html()

  },

  back: function(){
    this.$el.addClass("menu")
    app.header.set_back(false)
    this.$subtitle.html( this.subtitle_html )
  },

  pick: function(e){
    this.$el.removeClass("menu")
    app.header.set_back(true)   
    var index = $(e.currentTarget).data("index")
    this.$subtitle.html( $(e.currentTarget).text() )
    this.populateDecade(index)
  },
  
  show: function(){
    this.deferScrollToTop()
    app.footer.hide()
    this.back()
    document.body.className = "archive"
  },

  populate: function(data){
    if (this.loaded) { return }
    this.loaded = true
    this.data = data
    this.$loader.hide()
    this.$content.empty()
    
    // id title images[ uri label code caption ]
    this.data.forEach(function(row, index){

      var t = this.menu_template.replace(/{{title}}/, row.title)
      var $t = $(t)
      $t.data("title", row.title)
      $t.data("index", index)
      this.$menu_items.append($t)
    }.bind(this))
    
    this.back()
    this.deferScrollToTop()

    this.populateDecade(0, 3)
  },
  
  populateDecade: function(index, count){
    this.$content.empty()

    var loader = new Loader()

    var row = this.data[index]
    
    count = count || row.images.length
    
    row.images.forEach(function(cell, i){
      var $t = $("<div>")
      $t.addClass("row").addClass("loading")
      var t = this.row_template.replace(/{{image}}/, cell.uri)
                               .replace(/{{label}}/, cell.label)
                               .replace(/{{code}}/, cell.code)
                               .replace(/{{caption}}/, cell.caption)
      $t.html(t)
      $t.data("flipped", false)
      this.$content.append($t)
      
      var item = $t[0]
      var aa = this.build_aa_item( item )
      aa.q = 0
      this.render( aa, 0 )
      aa.flipped = true
      this.fix_z_index( aa )
      
      var $text = $t.find(".text")
      if ( ($text.height() % 2) == 1) {
        $text.css("margin-bottom", "1px")
      }
      
      loader.preloadImage(cell.uri, function(){
        aa.flipped = false
        this.fix_z_index( aa )
        $t.removeClass('loading')
      }.bind(this))
    }.bind(this))
  },

//  ['transformProp'] = "translateZ(0) translateX(-50%) translateY(-50%) ";
// .image, .text

  touchstart: function(e){
    app.archive.row = e.currentTarget
    app.archive.mousedown(e.touches[0])
  },
  touchmove: function(e){
    app.archive.mousemove(e.touches[0])
  },
  touchend: function(e){
    app.archive.mouseup()
  },

  row: null,
  image: null,
  text: null,
  flipped: false,
  q: 0,
  
  mousedown: function(e){
    var aa = app.archive.item = app.archive.build_aa_item( app.archive.row || e.currentTarget )
    aa.mouse_x = e.pageX
    aa.mouse_y = e.pageY
  },
  
  build_aa_item: function(el){
    var aa = {}
    aa.row = el
    aa.flipped = $(aa.row).data('flipped')
    aa.image = $(aa.row).find(".image")[0]
    aa.text = $(aa.row).find(".text")[0]
    aa.q = 0
    return aa
  },
  
  mousemove: function(e){
    if (! app.archive.item) return
    aa = app.archive.item
    var dx = ( aa.mouse_x - e.pageX ) / window.innerWidth
    var dy = ( aa.mouse_y - e.pageY ) / window.innerWidth
    
    var gray, opacity, q
    
    dx = Math.abs(dx)
    dx *= 2
    q = clamp( dx, 0, 1 )
    
    this.render(aa, q)
    
    aa.q = q 
/*
    aa.row.style['transformProp'] = [
      "translateZ(0)",
      "translateX(-50%)",
      "translateY(-50%)",
      "rotateY(" + dx + "deg)",
    ].join(" ")
*/
  },

  render: function(aa, q){
    if ( aa.flipped ) {
      gray = Math.round( (1-q) * 100 )
      opacity = lerp(q, 0.2, 1)
      text_opacity = lerp(q, 1, 0.3)
//       console.log("<", gray, opacity)
    }
    else {
      gray = Math.round( q * 100 )
      opacity = lerp(q, 1, 0.2)
      text_opacity = lerp(q, 0.3, 1)
//       console.log(">", gray, opacity)
    }
    aa.image.style.WebkitFilter = "grayscale(" + gray + "%)"
    aa.image.style.opacity = opacity
    aa.text.style.opacity = text_opacity
  },
  
  margin: 0.3,
  
  mouseup: function(e){
    aa = app.archive.item
    app.archive.row = null
    app.archive.item = null
    var was_flipped = aa.flipped
    var flipped = aa.flipped ? (aa.q < app.archive.margin) : (aa.q > app.archive.margin)
    var dest = was_flipped == flipped ? 0 : 1
    $(aa.row).data('flipped', flipped)

    oktween.add({
      obj: {q: aa.q},
      to: {q: dest},
      duration: 200 * Math.abs(aa.q-dest),
      update: function(o, dt){
        app.archive.render(aa, o.q)
      },
    })

    this.fix_z_index(aa)
  },

  fix_z_index: function (aa) {
    if ( aa.flipped ) {
    console.log(aa.q)
      z = aa.q > app.archive.margin ? 2 : 1
      zz = aa.q > app.archive.margin ? 1 : 2
    }
    else {
      z = aa.q < app.archive.margin ? 2 : 1
      zz = aa.q < app.archive.margin ? 1 : 2
    }
    aa.image.style.zIndex = z
    aa.text.style.zIndex = zz

  },

})