summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/commit/image-file.js
blob: e893491b19bb47c0504f88b59a95c8fe36c5ef89 (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
(function() {
  this.ImageFile = (function() {
    var prepareFrames;

    // Width where images must fits in, for 2-up this gets divided by 2
    ImageFile.availWidth = 900;

    ImageFile.viewModes = ['two-up', 'swipe'];

    function ImageFile(file) {
      this.file = file;
      this.requestImageInfo($('.two-up.view .frame.deleted img', this.file), (function(_this) {
        // Determine if old and new file has same dimensions, if not show 'two-up' view
        return function(deletedWidth, deletedHeight) {
          return _this.requestImageInfo($('.two-up.view .frame.added img', _this.file), function(width, height) {
            if (width === deletedWidth && height === deletedHeight) {
              return _this.initViewModes();
            } else {
              return _this.initView('two-up');
            }
          });
        };
      })(this));
    }

    ImageFile.prototype.initViewModes = function() {
      var viewMode;
      viewMode = ImageFile.viewModes[0];
      $('.view-modes', this.file).removeClass('hide');
      $('.view-modes-menu', this.file).on('click', 'li', (function(_this) {
        return function(event) {
          if (!$(event.currentTarget).hasClass('active')) {
            return _this.activateViewMode(event.currentTarget.className);
          }
        };
      })(this));
      return this.activateViewMode(viewMode);
    };

    ImageFile.prototype.activateViewMode = function(viewMode) {
      $('.view-modes-menu li', this.file).removeClass('active').filter("." + viewMode).addClass('active');
      return $(".view:visible:not(." + viewMode + ")", this.file).fadeOut(200, (function(_this) {
        return function() {
          $(".view." + viewMode, _this.file).fadeIn(200);
          return _this.initView(viewMode);
        };
      })(this));
    };

    ImageFile.prototype.initView = function(viewMode) {
      return this.views[viewMode].call(this);
    };

    prepareFrames = function(view) {
      var maxHeight, maxWidth;
      maxWidth = 0;
      maxHeight = 0;
      $('.frame', view).each((function(_this) {
        return function(index, frame) {
          var height, width;
          width = $(frame).width();
          height = $(frame).height();
          maxWidth = width > maxWidth ? width : maxWidth;
          return maxHeight = height > maxHeight ? height : maxHeight;
        };
      })(this)).css({
        width: maxWidth,
        height: maxHeight
      });
      return [maxWidth, maxHeight];
    };

    ImageFile.prototype.views = {
      'two-up': function() {
        return $('.two-up.view .wrap', this.file).each((function(_this) {
          return function(index, wrap) {
            $('img', wrap).each(function() {
              var currentWidth;
              currentWidth = $(this).width();
              if (currentWidth > ImageFile.availWidth / 2) {
                return $(this).width(ImageFile.availWidth / 2);
              }
            });
            return _this.requestImageInfo($('img', wrap), function(width, height) {
              $('.image-info .meta-width', wrap).text(width + "px");
              $('.image-info .meta-height', wrap).text(height + "px");
              return $('.image-info', wrap).removeClass('hide');
            });
          };
        })(this));
      },
      'swipe': function() {
        var maxHeight, maxWidth;
        maxWidth = 0;
        maxHeight = 0;
        return $('.swipe.view', this.file).each((function(_this) {
          return function(index, view) {
            var ref;
            ref = prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1];
            $('.swipe-frame', view).css({
              width: maxWidth + 16,
              height: maxHeight + 28
            });
            $('.swipe-wrap', view).css({
              width: maxWidth + 1,
              height: maxHeight + 2
            });
            return $('.swipe-bar', view).css({
              left: 0
            }).draggable({
              axis: 'x',
              containment: 'parent',
              drag: function(event) {
                return $('.swipe-wrap', view).width((maxWidth + 1) - $(this).position().left);
              },
              stop: function(event) {
                return $('.swipe-wrap', view).width((maxWidth + 1) - $(this).position().left);
              }
            });
          };
        })(this));
      },
      'onion-skin': function() {
        var dragTrackWidth, maxHeight, maxWidth;
        maxWidth = 0;
        maxHeight = 0;
        dragTrackWidth = $('.drag-track', this.file).width() - $('.dragger', this.file).width();
        return $('.onion-skin.view', this.file).each((function(_this) {
          return function(index, view) {
            var ref;
            ref = prepareFrames(view), maxWidth = ref[0], maxHeight = ref[1];
            $('.onion-skin-frame', view).css({
              width: maxWidth + 16,
              height: maxHeight + 28
            });
            $('.swipe-wrap', view).css({
              width: maxWidth + 1,
              height: maxHeight + 2
            });
            return $('.dragger', view).css({
              left: dragTrackWidth
            }).draggable({
              axis: 'x',
              containment: 'parent',
              drag: function(event) {
                return $('.frame.added', view).css('opacity', $(this).position().left / dragTrackWidth);
              },
              stop: function(event) {
                return $('.frame.added', view).css('opacity', $(this).position().left / dragTrackWidth);
              }
            });
          };
        })(this));
      }
    };

    ImageFile.prototype.requestImageInfo = function(img, callback) {
      var domImg;
      domImg = img.get(0);
      if (domImg) {
        if (domImg.complete) {
          return callback.call(this, domImg.naturalWidth, domImg.naturalHeight);
        } else {
          return img.on('load', (function(_this) {
            return function() {
              return callback.call(_this, domImg.naturalWidth, domImg.naturalHeight);
            };
          })(this));
        }
      }
    };

    return ImageFile;

  })();

}).call(this);