summaryrefslogtreecommitdiff
path: root/StoneIsland/www/js/lib/products/GalleryView.js
blob: 417b14c37cafe2c70a889fe5430bb740205cfb5c (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
var GalleryView = View.extend({

  el: "#gallery",
  template: $("#gallery .template").html(),
  
  events: {
//     "click .left": "prev",
//     "click .right": "next",
//     "touchstart .gallery": "touchstart",
//     "touchmove .gallery": "touchmove",
//     "touchend .gallery": "touchend",
  },
  
  initialize: function(){
    this.$(".template").remove()
  },
  
  reset: function(){
    this.gallery && this.gallery.destroy()
    this.$el.empty()
  },
  
  populate: function(code, image_ids){
    var valid_styles = {}
    var large_styles = {}
    image_ids.forEach(function(id){
      if (id.indexOf("_") == -1) return
      var partz = id.split("_")
      var size = parseInt(partz[0]), style = partz[1]

      if (! large_styles[style] || large_styles[style] < size) {
        large_styles[style] = size
      }
      if (size > 13) return;
      if (! valid_styles[style] || valid_styles[style] < size) {
        valid_styles[style] = size
      }
    })
    this.large_styles = large_styles

    Object.keys(valid_styles).sort(sort_image_styles).forEach(function(style){
      var id = valid_styles[style] + "_" + style
      var t = this.template
        .replace(/{{image}}/, sdk.image(code, id))
        .replace(/{{alt}}/, YOOX_IMAGE_STYLE_LABELS[style] || "Alternate view")
      this.$el.append(t)
    }.bind(this))

    var gallery = this.gallery = new Flickity( "#gallery", {
      selector: '.item',
      cellAlign: 'center',
      autoPlay: false,
      freeScroll: false,
      wrapAround: true,
      imagesLoaded: true,
      prevNextButtons: false,
      pageDots: false,
      contain: true,
      draggable: true,
    })
    if (app.accessible) {
      gallery.select(1, true, true)
    }

    var last_style = 'f'
    this.gallery.on('select', function(){
      this.updateLabel()
    }.bind(this))

    this.gallery.on('staticClick', this.static_click.bind(this))

    if (accessibility.voiceOver) {
      $("#product .gallery-target").click(this.static_click.bind(this))
      $("#product .gallery-target").attr('aria-label', "Image: Front view")
    } else {
      $("#product .gallery-target").remove()
    }
  },
  static_click: function(e){
    var currentImage = this.gallery.selectedElement.style.backgroundImage.replace(/url\(\"?/,"").replace(/\"?\)/,"")
    var partz = currentImage.split("_")
    var head = partz[0]
    var size = partz[1]
    var tail = partz[2]
    var end_partz = (tail || 'f').split(/\./)
    var style = end_partz[0]
    var largest_size = this.large_styles[style]
    var title = YOOX_IMAGE_STYLE_LABELS[style] || "Alternate view"
    var hiresImage = [head, largest_size, tail].join("_")
    // console.log(partz, style)
    app.fullscreenViewer.show(currentImage, hiresImage, title)
  },

  animating: false,
  previous: function(e){
    this.gallery.previous()
    this.updateLabel()
  },
  next: function(e){
    this.gallery.next()
    this.updateLabel()
  },
  updateLabel: function(){
    var currentImage = this.gallery.selectedElement.style.backgroundImage.replace(/url\(\"?/,"").replace(/\"?\)/,"")
    var partz = currentImage.split("_")
    var head = partz[0]
    var size = partz[1]
    var tail = partz[2]
    var end_partz = tail.split(/\./)
    var style = end_partz[0]
    if (style === this.last_style) {
      style = 'z'
    }
    this.last_style = style
    var label = YOOX_IMAGE_STYLE_LABELS[style] || "Image: Alternate view"
    console.log("> new style: " + style)
    $("#product .gallery-target").attr('aria-label', label)
  },

  touchstart: function(e){
  },
  touchmove: function(e){
  },
  touchend: function(e){
  },

})

var YOOX_IMAGE_STYLE_ORDER = "ZZZ f r d e a b c g l".split(" ")
var YOOX_IMAGE_STYLE_LABELS = {
  f: 'Image: Front view',
  r: 'Image: Rear view',
}
function sort_image_styles (b,a){ return (YOOX_IMAGE_STYLE_ORDER.indexOf(b)) - (YOOX_IMAGE_STYLE_ORDER.indexOf(a)) }