summaryrefslogtreecommitdiff
path: root/demos/quickstart/protected/controls/Comments/comments.js
blob: 75cbd4826d09965bec35848e4006c53935ef26d8 (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
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
var currentCommentID;

function show_comment_list()
{
	$('comment-list').show();
	$('add-comment').hide();
	$('show-comment-link').addClassName("active");
	$('add-comment-link').removeClassName("active");
	$('all-comments-link').removeClassName("active");
	show_comments_in_list(currentCommentID);
}

function show_all_comments()
{
	$('comment-list').show();
	$('add-comment').hide();
	$('show-comment-link').removeClassName("active");
	$('add-comment-link').removeClassName("active");
	$('all-comments-link').addClassName("active");
	show_comments_in_list();
}


function show_add_comment()
{
	$('comment-list').hide();
	$('add-comment').show();
	$('show-comment-link').removeClassName("active");
	$('add-comment-link').addClassName("active");
	$('all-comments-link').removeClassName("active");
}

function hide_add_comment()
{
	$('user-comments').hide();
	$('modal-background').hide();
	$(content_textare_id).value = '';
	$(currentCommentID).style.zIndex = 0;
}

function show_comments_in_list(id)
{
	var list = $('comment-list');
	var count=0;
	for(var i=0, k=list.childNodes.length; i < k; i++)
	{
		var node = list.childNodes[i];
		if(node.nodeType == 1) //an element node
		{
			if(typeof(id) == "undefined" || node.className.indexOf(id) >= 0)
			{
				node.style.display="block"
				count++;
			}
			else
				node.style.display="none";
		}
	}
	return count;
}

function show_comments(block)
{
	var id = block.id
	currentCommentID = id;
	$(hidden_block_id).value = id;
	var commentBlock = $('user-comments');
	var pos = Position.cumulativeOffset(block);
	var top = pos[1] + block.offsetHeight;
	commentBlock.style.top = top+"px";
	commentBlock.style.width = (block.offsetWidth-22)+"px";

	commentBlock.show();

	var count = show_comments_in_list(id);

	$('comment-list').show();
	if(count > 0)
		show_comment_list();
	else
		show_add_comment();

	var background = $('modal-background');
	background.style.top="0px";
	background.style.left="0px";
	background.style.opacity="0.5";
	background.style.width = document.body.offsetWidth+"px";
	background.style.height = document.body.offsetHeight+"px";
	background.show();
	block.style.zIndex = 100;
	block.style.paddingRight="9px";
	block.style.marginRight="-9px";

}

function show_block(block)
{
	while(block && block.className.indexOf("block-content") < 0)
		block = block.parentNode;
	if(block && block.className.indexOf("block-content") >= 0)
	{
		block.addClassName('block-hover');
		var tag = $('tag-'+block.id);
//		if(tag.className.indexOf("tag-shown")<=0)
			tag.firstChild.style.visibility="visible"
	}
}

function hide_block(block)
{
	while(block && block.className.indexOf("block-hover") < 0)
		block = block.parentNode;
	if(block && block.className.indexOf("block-hover") >= 0)
	{
		block.removeClassName('block-hover');
		var tag = $('tag-'+block.id);
		if(tag.className.indexOf("tag-shown")<=0)
			tag.firstChild.style.visibility="hidden"
	}
}

function add_comment_tag(el)
{
	var dim = Element.getDimensions(el);
	var comments = get_comment_count(el.id);
	var style = "height:"+(dim.height > 35 ? dim.height : 35)+"px;";
	var cssClass = dim.height ? "block-comment-tag" : "block-comment-tag-ie";
	var title = "View "+comments+" comments"
	var innerStyle="";
	if(comments <= 0)
	{
		innerStyle = " visibility:hidden;";
		comments = "add";
		title = "Add new comment";
	}
	else
		cssClass += " tag-shown";
	var id = "tag-"+el.id;
	var tag = "<div id='"+id+"' class='"+cssClass+"' style='"+style+"'><div style='"+innerStyle+"' title='"+title+"'>"+comments+"</div>&nbsp;</div>";
	new Insertion.Before(el, tag);
	var tag_div = $(id);
	Event.observe(tag_div, "mouseover", function(e){ if(typeof(show_block)!="undefined") show_block(el); });
	Event.observe(tag_div, "mouseout", function(e){ if(typeof(hide_block) !="undefined") hide_block(el); });
	Event.observe(tag_div, "click", function(e) { if(typeof(show_comments) !="undefined") show_comments(el); Event.stop(e); });
}

function increment_count_tag(id)
{
	var tag = $('tag-'+id);
	if(tag && tag.firstChild)
	{
		if(tag.className.indexOf("tag-shown") >= 0)
		{
			var count = Number(tag.firstChild.innerHTML);
			tag.firstChild.innerHTML = (++count)+"";
			tag.firstChild.style.visibility="visible";
		}
		else
		{
			tag.firstChild.innerHTML = "1";
			tag.addClassName("tag-shown");
			tag.firstChild.style.visibility="visible";
		}
	}
}

function get_comment_count(id)
{
	return comment_count[id] ? comment_count[id] : 0;
}

//initialize the comment js

if(!Prado.Browser.ie) //not IE 4,5,6
{
	Event.OnLoad(function()
	{
		var userComments = $('user-comments');
		userComments.style.position="absolute";
		userComments.style.marginRight="80px";
		var commentList = $('comment-list');
		commentList.style.height="320px";
		$('add-comment').style.height="320px";
		commentList.style.overflow="auto";
		$('show-comment-link').style.display="";
		$('to-top').hide();
		$('close-comments').show();
		$('all-comments-link').show();
		userComments.hide();
		$('comments-header').hide();

		$$('#comment-list .source-link').each(function(el){ el.hide(); });

		$$('#content .block-content').each(function(el)
		{
			Event.observe(el, 'mouseover', function(e){ if(typeof(show_block)!="undefined") show_block(Event.element(e)); });
			Event.observe(el, 'mouseout', function(e){ if(typeof(hide_block)!="undefined") hide_block(Event.element(e)); });
			add_comment_tag(el);
		});

		Event.observe($('show-comment-link'), "click", function(e) { show_comment_list(); Event.stop(e); });
		Event.observe($('add-comment-link'), "click", function(e) { show_add_comment();	Event.stop(e); });
		Event.observe($('all-comments-link'), "click", function(e) { show_all_comments();	Event.stop(e); });
		Event.observe($('close-comments'), "click", function(e) { hide_add_comment(); Event.stop(e); });

	});
}