/
portlet.js
executable file
·161 lines (140 loc) · 5.65 KB
/
portlet.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
(function($){
$.fn.imagesLoaded = function(callback){
var elems = this.filter('img'),
len = elems.length;
elems.bind('load',function(){
if (--len <= 0){ callback.call(elems,this); }
}).each(function(){
// cached images don't fire load sometimes, so we reset src.
if (this.complete || this.complete === undefined){
var src = this.src;
// webkit hack from http://groups.google.com/group/jquery-dev/browse_thread/thread/eee6ab7b2da50e1f
this.src = '#';
this.src = src;
}
});
};
function set_controls_position(container){
var width = container.find('img').width();
var left = (width/2) - 43;
container.find('div.gallery-portlet-controls').css("left", left);
}
function get_image(link){
var linkele = link[0];
if(linkele.active != undefined && linkele.active){
return;
}
linkele.active = true;
var container = link.parents('dl.applied-portlet-gallery');
var controls = link.parents('div.gallery-portlet-controls');
var portlet_item = link.parents('dd.portletItem');
var next = controls.find('span.next a');
var prev = controls.find('span.prev a');
var img = container.find('img');
$.ajax({
url : '@@get-image-for-gallery-portlet',
data : link.attr('href').split('?')[1],
type : 'GET',
success : function(data, results){
eval("var json="+data);
//create new image now so it'll be done loading faster...
var newimg = document.createElement('img');
newimg.src = json.src;
newimg.width = img.width();
if(img.attr('height') !== undefined){
newimg.height = img.height();
}
newimg = $(newimg);
newimg.css('display', 'none');
portlet_item.css('height', img.height());
img.fadeOut(1000, function(){
img.replaceWith(newimg);
newimg.imagesLoaded(function(){
portlet_item.animate({ height : $(this).height() }, 500, 'linear');
$(this).fadeIn(1000, function(){
linkele.active = false;
});
}, newimg)
var linked = newimg.parent()
linked.attr('href', json['image-link']);
linked.attr('title', json['title']);
linked.attr('alt', json['description']);
next.attr('href', next.attr('href').split('?')[0] + '?' + json['next-url']);
prev.attr('href', prev.attr('href').split('?')[0] + '?' + json['prev-url']);
set_controls_position(container);
});
}
});
}
function get_timeout_ele(portlet){
if(portlet.hasClass('portletItem')){
portlet = portlet.parent();
}
return portlet.find("input.timeout_id");
}
function get_timeout_id(portlet){
var timeout_id = get_timeout_ele(portlet);
if(timeout_id.size() == 0){
return 0;
}else{
return parseInt(timeout_id.attr('value'));
}
}
function set_timeout_id(portlet, val){
var timeout_id = get_timeout_ele(portlet);
if(timeout_id.size() == 0){
portlet.append('<input type="hidden" name="timeout_id" class="timeout_id" value="' + val + '" />');
}else{
timeout_id.attr('value', val);
}
}
function perform_play(portlet){
portlet.find('span.next a').trigger('click');
set_timeout_id(portlet, setTimeout(function(){perform_play(portlet);}, 5000));
}
function play(portlet){
portlet.find('span.play-pause').addClass('timed');
set_timeout_id(portlet, setTimeout(function(){perform_play(portlet);}, 5000));
}
function pause(portlet){
clearTimeout(get_timeout_id(portlet));
portlet.find('span.play-pause').removeClass('timed');
}
$(document).ready(function(){
$('dl.portletGallery span.next a,dl.portletGallery span.prev a').click(function(){
get_image($(this));
return false;
});
$('dl.portletGallery span.play-pause').css({'display':'inline'});
var portlets = $('dl.portletGallery');
portlets.addClass('applied-portlet-gallery');
portlets.each(function(){
var portlet = $(this);
set_controls_position(portlet);
if(portlet.hasClass('timed')){
play(portlet);
}else{
pause(portlet);
}
});
$('dl.portletGallery span.play-pause a').click(function(){
var portlet = $(this).parent().parent().parent();
if(portlet.find('span.play-pause').hasClass('timed')){
pause(portlet);
}else{
play(portlet);
}
return false;
});
$('dl.portletGallery').hover(
function(){
var controls = $(this).find('div.gallery-portlet-controls:not(.hide)');
controls.fadeIn();
},
function(){
var controls = $(this).find('div.gallery-portlet-controls:not(.hide)');
controls.fadeOut();
}
);
});
})(jQuery);