This repository has been archived by the owner on Jan 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
danda.js
142 lines (118 loc) · 4.39 KB
/
danda.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
/*
* Danda Javascript Library v0.1
* http://www.dandamoore.com
*
* Copyright 2011, Dustin Moore
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* To receive a copy of the GNU Lesser General Public License see
* <http://www.gnu.org/copyleft/lgpl.html>
*/
var danda = { ui : {} };
danda.ui.ImageGallery = function(_id) {
$.tmpl(
'<div id="${id}_disabled_layer" class="danda-ui-image-gallery-disabled-layer"></div>'
+ '<div id="${id}_pop_up" class="danda-ui-image-gallery-pop-up">'
+ ' <img />'
+ ' <div class="danda-ui-image-gallery-controls">'
+ ' <a class="danda-ui-image-gallery-close" href="#${id}">Close</a></div>'
+ ' </div>'
+ '</div>', { id: _id }).hide().appendTo('body');
var $img_gallery = $('#' + _id);
var _$previous_control = $('<div class="danda-ui-image-gallery-previous-control"></div>').prependTo($img_gallery);
var _$next_control = $('<div class="danda-ui-image-gallery-next-control"></div>').appendTo($img_gallery);
var _$disabled_layer = $('#' + _id + '_disabled_layer');
var _$pop_up = $('#' + _id + '_pop_up');
var _$img_anchors = $img_gallery.find('a');
var _$img = _$pop_up.find('img');
var _pop_up_orig_height = 0;
var _pop_up_orig_width = 0;
var _img_aspect_ratio = 0;
var _index = 0;
$img_gallery.find('a:gt(0)').hide();
_$img_anchors.each(function() {
$(this).data('large-image-url', this.href);
this.href = '#' + _id;
$(this).click(popUp);
});
$img_gallery.children().css('float', 'left');
_$previous_control.addClass('danda-ui-image-gallery-no-control');
_$previous_control.click(previous);
_$next_control.click(next);
_$pop_up.find('.danda-ui-image-gallery-controls .danda-ui-image-gallery-close').click(popDown);
_$img.load(onImgLoad);
function previous() {
if (_index > 0) {
_$img_anchors.eq(_index).hide();
_index--;
_$img_anchors.eq(_index).show();
}
if (_index < _$img_anchors.length - 1)
_$next_control.removeClass('danda-ui-image-gallery-no-control');
if (_index <= 0)
_$previous_control.addClass('danda-ui-image-gallery-no-control');
}
function next() {
if (_index < _$img_anchors.length - 1) {
_$img_anchors.eq(_index).hide();
_index++;
_$img_anchors.eq(_index).show();
}
if (_index >= _$img_anchors.length - 1)
_$next_control.addClass('danda-ui-image-gallery-no-control');
if (_index > 0)
_$previous_control.removeClass('danda-ui-image-gallery-no-control');
}
function popUp() {
_$disabled_layer.show();
_$pop_up.show();
_$img.attr('src', $(this).data('large-image-url'));
$(window).resize(resize);
}
function popDown() {
_$pop_up.hide();
_$disabled_layer.hide();
$(window).unbind('resize');
}
function onImgLoad() {
_pop_up_orig_height = _pop_up_orig_height || _$pop_up.height();
_pop_up_orig_width = _pop_up_orig_width || _$pop_up.width();
_img_aspect_ratio = _img_aspect_ratio || (_$img.height() / _$img.width());
resize();
}
function resize() {
var window_height = $(window).height();
var window_width = $(window).width();
var height_diff = window_height - _pop_up_orig_height;
var width_diff = window_width - _pop_up_orig_width;
var height_offset = height_diff / 2;
var width_offset = width_diff / 2;
if (height_offset < 0 || width_offset < 0) {
if (height_diff < width_diff) {
var new_height = window_height - 35;
var new_width = new_height / _img_aspect_ratio;
_$img.height(new_height)
.width(new_width);
} else {
var new_width = window_width - 10;
var new_height = _img_aspect_ratio * new_width;
_$img.height(new_height)
.width(new_width);
}
height_offset = (window_height - new_height - 35) / 2;
width_offset = (window_width - new_width - 10) / 2;
}
_$pop_up.css('top', height_offset);
_$pop_up.css('left', width_offset);
}
return {};
};