-
Notifications
You must be signed in to change notification settings - Fork 0
/
design.js
328 lines (264 loc) · 7.91 KB
/
design.js
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/**
* Vega
*/
var Design = Design || {
keyboardshortcuts: function() {
// Reset
Cargo.Core.KeyboardShortcut.Remove("J");
Cargo.Core.KeyboardShortcut.Remove("K");
Cargo.Core.KeyboardShortcut.Remove("I");
Cargo.Core.KeyboardShortcut.Remove("X");
// Next
Cargo.Core.KeyboardShortcut.Add("J", 74, function() {
Design.nextProject();
});
// Prev
Cargo.Core.KeyboardShortcut.Add("K", 75, function() {
Design.prevProject();
});
// Next
Cargo.Core.KeyboardShortcut.Add("Down", 74, function() {
Design.nextProject();
});
// Prev
Cargo.Core.KeyboardShortcut.Add("Up", 75, function() {
Design.prevProject();
});
// Index
Cargo.Core.KeyboardShortcut.Add("I", 73, function() {
window.location = Cargo.Helper.GetBaseUrl(true, true);
});
Cargo.Core.KeyboardShortcut.Add("X", 88, function() {
window.location = Cargo.Helper.GetBaseUrl(true, true);
});
},
nextProject: function() {
var pid = $(".project.active").parent().next().attr("data-id") || $(".project_container").first().attr("data-id");
Design.Project.scrollToProject(pid);
},
prevProject: function() {
var pid = $(".project.active").parent().prev().attr("data-id") || $(".project_container").last().attr("data-id");
Design.Project.scrollToProject(pid);
}
};
$(function() {
Cargo.Core.ReplaceLoadingAnims.init();
Design.keyboardshortcuts();
});
/**
* Project methods
*/
Design.Project = {
"$container": null,
"$thumbcontainer": null,
data: {
"thumb_height" : 0,
"thumb_click" : false,
"thumb_hover" : false,
"scrollY" : 0,
"ticking" : null,
"last_project" : null,
"position_offset": 0,
"project_height" : 0,
"window_height" : 0
},
positions: [],
init: function() {
// Cache some elements
Design.Project.$container = $(window);
Design.Project.$thumbcontainer = $(".thumbnails_container");
// Set some data
Design.Project.data.thumb_height = $(".project_thumb:first").outerHeight();
// Set scroll event
Design.Project.$container
.bind("scroll", Design.Project.onScroll);
// Thumbnail scroll
Design.Project.$thumbcontainer
.bind("mouseenter", function() {
$("body").addClass("disable_scroll");
Design.Project.data.thumb_hover = true;
})
.bind("mouseleave", function() {
$("body").removeClass("disable_scroll");
Design.Project.data.thumb_hover = false;
});
},
paginateProjects: function() {
// Reset and set data
Design.Project.positions = [];
Design.Project.data.window_height = $(window).height();
Design.Project.data.position_offset = Math.floor((Design.Project.data.window_height - $(".thumb_block").height()) / Design.Project.data.thumb_height) / 2;
// Store the projects
$(".project").each(function() {
// Add the positions to an array
Design.Project.positions.push($(this).offset().top);
// Minimum height
$(this).css({
"min-height": Design.Project.data.window_height / 2 + 10 + "px"
});
});
// Scroll if we're not over thumbs
if (!Design.Project.data.thumb_hover) {
Design.Project.onScroll();
}
},
onScroll: function(e) {
Design.Project.scrollY = Design.Project.$container.scrollTop() + (Design.Project.data.window_height / 2);
Design.Project.requestTick();
if (e) {
e.stopPropagation();
}
},
requestTick: function() {
if (!Design.Project.ticking) {
requestAnimationFrame(Design.Project.scroll);
}
Design.Project.ticking = true;
},
scroll: function() {
// Update data
Design.Project.ticking = false;
for (var i = Design.Project.positions.length - 1; i >= 0; i--) {
var offset = Design.Project.positions[i];
if (Design.Project.scrollY > offset && Design.Project.scrollY != Design.Project.data.window_height / 2) {
// Active classes
if (i != Design.Project.data.last_project) {
// Reset old
if (Design.Project.data.last_project) {
Design.Project.data.last_project.removeClass("active");
}
// Update
Design.Project.data.last_project = $(".project").eq(i);
Design.Project.data.project_height = Design.Project.data.last_project.outerHeight();
// Set new
Design.Project.data.last_project.addClass("active");
// Ignore if clicked
if (!Design.Project.data.thumb_click) {
$(".project_thumb.active").removeClass("active");
$(".project_thumb").eq(i).addClass("active");
}
}
var position = Design.Project.scrollY - offset;
position = position / Design.Project.data.project_height;
var top = (Design.Project.data.thumb_height * (i - Design.Project.data.position_offset));
top += (position * Design.Project.data.thumb_height);
top = Math.floor(top);
// Scroll it
if (!Design.Project.data.thumb_click) {
if(Cargo.Helper.isIOS()){
Design.Project.$thumbcontainer.animate({
scrollTop: top
});
} else {
Design.Project.$thumbcontainer.scrollTop(top);
}
}
break;
}
};
},
formatThumbnails: function() {
$(".project_thumb").each(function() {
// Center the title
var offset = ($(this).height() - $(this).find(".thumb_title").height()) / 2;
$(this).find(".thumb_title").css({
paddingTop: offset + "px"
});
// Center the image
var thumb_width = $(this).find(".thumb_image img").width(),
thumb_container_width = $(this).find(".thumb_image").width();
// Margin
if (thumb_width > thumb_container_width) {
var margin = (thumb_width - thumb_container_width) / 2
$(this).find(".thumb_image img").css("margin-left", "-" + margin + "px");
}
$(this).addClass("formatted");
});
},
clickThumbnail: function() {
var click_timeout;
$("#project_thumbnails").on("click dblclick", ".project_thumb", function(e) {
if (e.metaKey || e.ctrlKey) {
var purl = Cargo.Helper.GetPurlFromPid($(this).attr("data-id"));
window.open(Cargo.Helper.MakeDirectLinkFromPurl(purl));
}
// Scroll to the project
if (!$(this).hasClass("active")) {
Design.Project.scrollToProject($(this).attr("data-id"));
}
// Active
$(".project_thumb").removeClass("active");
$(this).addClass("active");
$("#project_thumbnails").mouseleave();
setTimeout(function() {
$("#project_thumbnails").mouseenter();
}, 10);
});
},
scrollToProject: function(pid) {
var current_position = Design.Project.$container.scrollTop(),
new_position = $(".project_container[data-id='" + pid + "']").position().top + 1;
// Direction
if (current_position > new_position) {
current_position = new_position + 50;
} else {
current_position = new_position - 50;
}
// Browser
var $scroll_target = $("body, html");
if (typeof InstallTrigger !== "undefined") {
$scroll_target = $("html");
}
// scroll to project
$scroll_target.scrollTop(current_position).animate({
scrollTop: new_position
}, 100);
}
};
$(function() {
Design.Project.formatThumbnails();
Design.Project.clickThumbnail();
Design.Project.init();
// Safari retina scroll bug fix
if (typeof InstallTrigger !== "undefined") {
$(".thumbnails_container").addClass("scrollfix");
}
setTimeout(function() {
Design.Project.paginateProjects();
}, 100);
var resizer;
$(window).resize(function(event) {
// Debounce
clearTimeout(resizer);
resizer = setTimeout(function() {
Design.Project.paginateProjects();
}, 100);
});
});
/**
* Events
*/
Cargo.Event.on("slideshow_thumbnails_toggle", function(el, obj) {
setTimeout(function() {
Design.Project.paginateProjects();
}, 100);
});
Cargo.Event.on("element_resizer_init", function(plugin) {
plugin.setOptions({
adjustElementsToWindowHeight: false,
centerElements: false
});
});
Cargo.Event.on("pagination_complete", function(new_page) {
Design.Project.formatThumbnails();
setTimeout(function() {
Cargo.Plugins.elementResizer.refresh();
Design.Project.paginateProjects();
}, 100);
});
Cargo.Event.on("inspector_unload", function() {
Design.Project.formatThumbnails();
});
Cargo.Event.on("fullscreen_destroy_hotkeys", function() {
Design.keyboardshortcuts();
});