summaryrefslogtreecommitdiffstats
path: root/public_html/data/js/thumbnail-view.js
blob: eb9ee33cece3bd8ab82e0f32625c3e725aa822f8 (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
(function () {
'use strict';
define(['jquery', 'underscore', 'multipaste', 'jquery.colorbox'], function ($, _, Multipaste) {
	var ui = {
		thumbnailLinks: '.upload_thumbnails a',
		formButtons: '#submit_form button[type=submit]',
		submitForm: '#submit_form',
		markedThumbnails: '.upload_thumbnails .marked',
		colorbox: '.colorbox',
		thumbnails: '.upload_thumbnails',
		toggleSelectModeButton: '#toggle_select_mode',
	};

	var PrivateFunctions = {
		inSelectMode: false,

		setupEvents: function () {
			$(ui.toggleSelectModeButton).on('click', _.bind(this.toggleSelectMode, this));
			$(ui.thumbnailLinks).on('click', _.bind(this.thumbnailClick, this));
			$(window).resize(_.bind(this.onResize, this));
		},

		browserHandlesImageOrientation: function () {
			var testImg = $('<img>');
			$('body').append(testImg);
			var style = window.getComputedStyle(testImg.get(0));
			var result = style.getPropertyValue('image-orientation')
			console.log('Browser default image-orientation: ', result)
			testImg.remove();
			return result == 'from-image';
		},

		setupColorbox: function () {
			var browserHandlesImageOrientation = PrivateFunctions.browserHandlesImageOrientation();

			$(ui.colorbox).colorbox({
				transistion: "none",
				speed: 0,
				initialWidth: "100%",
				initialHeight: "100%",
				photo: true,
				retinaImage: true,
				maxHeight: "100%",
				maxWidth: "100%",
				current: 'Image {current} of {total}. Use h/l or right/left arrow keys or these buttons:',
				next: '<span class="glyphicon glyphicon-chevron-right"></span>',
				previous: '<span class="glyphicon glyphicon-chevron-left"></span>',
				close: '<span class="glyphicon glyphicon-remove"></span>',
				loop: false,
				orientation: function() {
					if (browserHandlesImageOrientation) {
						return 1;
					} else {
						return $(this).data('orientation');
					}
				},
			});
		},

		removeColorbox: function () {
			$.colorbox.remove();
		},

		setupPopovers: function () {
			$(ui.thumbnailLinks).popover({
				trigger: 'hover',
				placement: 'auto bottom',
				html: true
			});
		},

		toggleSelectMode: function () {
			if (this.inSelectMode) {
				$(ui.formButtons).hide();
				$(ui.submitForm).find('input').remove();
				$(ui.markedThumbnails).removeClass('marked');
				this.setupColorbox();
			} else {
				$(ui.formButtons).show();
				this.removeColorbox();
			}
			this.inSelectMode = !this.inSelectMode;
		},

		submitInput: function (id) {
			return $('<input>').attr({
				type: 'hidden',
				name: 'ids[' + id + ']',
				value: id,
				id: 'submit_' +id
			});
		},

		thumbnailClick: function (event) {
			if (!this.inSelectMode) { return; }
			event.preventDefault();
			var id = $(event.target).closest('a').data('id');

			var submitInput = $(ui.submitForm).find('input#submit_' + id);

			if (submitInput.length === 0) {
				$(ui.submitForm).append(this.submitInput(id));
			} else {
				submitInput.remove();
			}
			$(event.target).closest('a').toggleClass('marked');
		},

		needMultipleLines: function (thumbnailContainer) {
			var containerWidth, thumbsWidth, thumbs, thumbsCount;
			containerWidth = thumbnailContainer.parent().width();
			thumbs = thumbnailContainer.find('a');
			thumbsCount = thumbs.length;
			thumbsWidth = thumbs.outerWidth(true) * thumbsCount;

			return containerWidth < thumbsWidth;
		},

		thumbnailsWidth: function (thumbnailContainer) {
			var containerWidth, thumbs, thumbWidth;
			containerWidth = thumbnailContainer.parent().width();
			thumbs = thumbnailContainer.find('a');
			thumbWidth = thumbs.outerWidth(true);
			return containerWidth - (containerWidth % thumbWidth);
		},

		onResize: function () {
			_.each($(ui.thumbnails), function (thumbnailContainer) {
				thumbnailContainer = $(thumbnailContainer);
				var margin = this.needMultipleLines(thumbnailContainer) ? 'auto' : '0';
				thumbnailContainer.css('margin-left', margin);
				thumbnailContainer.width(this.thumbnailsWidth(thumbnailContainer));
			}, this);
		}
	};

	var ThumbnailView = {
		initialize: function () {
			PrivateFunctions.setupEvents();
			PrivateFunctions.onResize();
			PrivateFunctions.setupColorbox();
			PrivateFunctions.setupPopovers();
		}
	};

	return ThumbnailView;
});
})();