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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
|
{% load ndf_tags %}
{% load i18n %}
<div id="" class="">
<br/>
<div onclick="createDiscussion()" id="create-discussion" data-id="{{ node.pk }}" class="button" style="margin-bottom:0;">
{% if all_replies %}
Initiate New Discussion Thread
{% else %}
Initiate discussion on {{ node.name }}
{% endif %}
</div>
<div id="replies-area">
<!-- populating replies -->
{% for each_reply in all_replies %}
<div style="margin-left:calc({{each_reply.level}} * 48px); {% if each_reply.level == 1 %}margin-top:25px;{% endif %}" class="disc-replies" data-reply-id="{{ each_reply.oid }}" data-priornode-id="{{ each_reply.prior_node }}">
<div class="row">
<div class="small-3 columns">
<a class="text-center" href="/home/{{each_reply.userid}}/dashboard">
<span class="align-center"><i class="fi-torso"></i></span>
<label>{{ each_reply.username }}</label>
</a>
</div>
<div class="small-9 columns">
{% spaceless %}
{{each_reply.HTMLcontent | safe }}
{% endspaceless %}
</div>
</div>
<div class="row">
<div class="small-8 columns"> <small>{{ each_reply.last_update }}</small> </div>
<div class="reply-btn small-2 columns" data-userid="{{each_reply.userid}}" onclick="deleteReply($(this))">{% trans "delete" %}</div>
<div class='reply-btn text-right small-2 columns' data-id='{{ each_reply.oid }}' onclick='openOrgEditor($(this))' data-org-content='{{ each_reply.ORGcontent }}'>
{% trans "reply" %}
</div>
</div>
</div>
{% endfor %}
<!-- END of populating replies -->
</div>
<!-- orgitdown container -->
<div id="org" style="" class="reveal-modal small" data-reveal>
<!-- Reply : -->
<textarea id="orgitdownreps" name="content_org" placeholder="Enter your reply text here."></textarea>
<input data-id="{{ node.pk }}" type="button" value="Post" class="button expand reply-button" onclick="addReply($(this))">
<input type="hidden" id="prior-node" style="visibility:hidden" value="">
<!-- <input type="hidden" id="thd" style="visibility:hidden" value=""> -->
</div>
<!-- end of orgit down container -->
</div>
<script type="text/javascript">
// for discussion tab
// storing ndf_tags return values in JS variables
{% user_access_policy groupid request.user as user_access %}
var user_access = "{{ user_access }}"; // "allow" or "disallow"
{% edit_policy groupid node request.user as status %}
var edit_policy = "{{ status }}"; // "allow" or "disallow"
var is_user_authenticated = {{ request.user.is_authenticated|lower }};
// if( user_access == "allow" && edit_policy == "allow" )
// {
// $("#orgitdownreps").orgitdown(mySettings);
// }
{% if request.user.is_authenticated %}
$("#orgitdownreps").orgitdown(mySettings);
{% endif %}
function createDiscussion()
{
// if( user_access == "allow" && edit_policy == "allow" )
if( is_user_authenticated )
{
{% if request.user.is_authenticated %}
// ajax to create thread for first time.
$.ajax({
url: "{% url 'create_discussion' group_id node.pk %}",
success: function(data){
data = JSON.parse(data);
if( (data[0] == "thread-created") || (data[0] == "Thread-exist") )
{
// $("#create-discussion").hide();
// changing method to behave same as sub-reply button
$("#create-discussion").attr("onclick", "openOrgEditor($(this))");
$("#prior-node").val("{{ node.pk }}") // _id
// $("input[data-id='{{ node.pk }}']").val("{{ node.pk }}") // _id
$("#create-discussion").trigger("click");
}
// else if( data[0] == "Thread-exist" ){
// // alert(data)
// $("#create-discussion").hide();
// $("#prior-node").val("{{ node.pk }}") // _id
// // changing method to behave same as sub-reply button
// $("#create-discussion").attr("onclick", "openOrgEditor($(this))");
// $("create-discussion").trigger("click");
// }
else{ alert(data) }
}//,
// complete: function(){
// $("#prior-node").val("") // removing _id
// }
});
// --- END ajax and create thread for first time.
{% endif %}
}
else
{
// alert("To start discussion you need to login and be the member of this group.");
alert("Login to start discussion");
}
}
// --- END of createDiscussion()
// function called by "Add Reply" and "Reply" buttons to open editor
function openOrgEditor(sourceObj)
{
// if( user_access == "allow" && edit_policy == "allow" )
if( is_user_authenticated )
{
var sourceObjDataId = $(sourceObj).attr("data-id"); // prior-node _id
$("#prior-node").val(sourceObjDataId);
// tempOrgObj = $("#org").clone(true, true);
// console.log($(sourceObj));
// $("#org").detach();
// if( sourceObjDataId == "{{ node.pk }}")
// {
// $('div[data-id="'+ sourceObjDataId +'"]').after(tempOrgObj);
// }
// else
// {
// $tempPrior = $('div[data-id="'+ sourceObjDataId +'"]').parent();
// calcMargLeft = $tempPrior.css("margin-left");
// calcMargLeft = (parseFloat(calcMargLeft, 10) + 48) + "px";
// $('div[data-id="'+ sourceObjDataId +'"]').parent().after(tempOrgObj);
// }
// $("#org").fadeIn();
// if( sourceObjDataId != "{{ node.pk }}")
// {
$('#org').foundation('reveal', 'open');
// }
}
else
{
// alert("To start discussion you need to login and be the member of this group.");
alert("Login to start discussion.");
}
}
function updateDiscussionTabText()
{
var totalReplies = $("#replies-area div.disc-replies:visible").length;
if(totalReplies == 0)
{
$(".view-discussion span").fadeOut().fadeIn().html("Begin Discussion");
$("#create-discussion").text("Initiate discussion on " + $("h1 span.node[itemprop]").text().trim());
}
else
{
$(".view-discussion span").fadeOut().fadeIn().html("Join Discussion ("+totalReplies+")");
}
}
function createReplyHTML(replyStyle, data)
{
var htmlReply = "<div style='"+ replyStyle +"' data-reply-id='"+ data[1] +"' data-priornode-id='"+ data[2] +"' class='disc-replies'>"
+ "<div class='row'><div class='small-3 columns'><a href='/home/"+ data[5] +"/dashboard' class='text-center'>"
+ "<span class='align-center'><i class='fi-torso'></i></span>"
+ "<label>" + data[6] + "</label></a></div>"
+ "<div class='small-9 columns'>"+ data[3] +"</div></div>"
+ "<div class='row'><div class='small-8 columns'><small>"+ data[7] + "</small></div>"
+ "<div class='reply-btn small-2 columns' data-userid='"+ data[5] +"' onclick='deleteReply($(this))'>delete</div>"
+ "<div data-org-content='"
+ data[4] + "' onclick='openOrgEditor($(this))' data-id='"
+ data[1] + "' class='reply-btn text-right small-2 columns'> reply </div></div></div>"
return htmlReply;
}
function addReply(sourceObj)
{
$('#org').foundation('reveal', 'close');
var priorNodeId = $("#prior-node").val(),
replyContent = $("#orgitdownreps").val().trim();
// If reply is non empty - make ajax to save reply.
if( (replyContent.length > 0) && (priorNodeId.length == 24) )
{
$.ajax({
type: "POST",
url: "{% url 'discussion_reply' group_id node.pk %}",
data: {
"csrfmiddlewaretoken": "{{ csrf_token }}",
"prior_node_id": priorNodeId,
"reply_text_content": replyContent
},
success: function(data){
data = JSON.parse(data);
if( data[0] == "reply_saved" )
{
$("#org").fadeOut(); // hide editor
$("#orgitdownreps").val(""); // make textarea empty
if(priorNodeId == "{{ node.pk }}") // main reply
{
// change the text of Start discussion and make it visible
$("#create-discussion").html("Initiate New Discussion Thread");
// $("#create-discussion").fadeIn();
//sample format of data = [0:"status_info", 1:"reply_id", 2:"prior_node", 3:"html_content", 4:"org_content", 5:"user_id", 6:"user_name", 7:"created_at" ]
// add reply text content after #replies-area
$("#replies-area").prepend(createReplyHTML('margin-top:20px;', data)).fadeIn(8000);
}
else // sub-reply
{
$tempPrior = $('div[data-id="'+ priorNodeId +'"]').parent().parent();
calcMargLeft = $tempPrior.css("margin-left");
calcMargLeft = (parseFloat(calcMargLeft, 10) + 48) + "px";
// add reply text content as sub-reply
$($tempPrior).after(createReplyHTML('margin-left:'+calcMargLeft, data)).fadeIn(8000);
}
// updating tab-text
updateDiscussionTabText();
}
else if( data[0] == "no_content" ){
alert("Please provide the reply content.")
}
else { alert(data) }
}
});// --- END of ajax to save reply
}
else // content is not provided
{
alert("Please provide the reply content.")
} // --- END of IF-ELSE
} // --- END of addReply()
function deleteReply(sourceObj)
{
var userid = $(sourceObj).attr("data-userid");
if( (user_access == "allow" && edit_policy == "allow") || (userid == "{{request.user.id}}") )
{
var reply_id = $(sourceObj).closest("div.disc-replies").attr("data-reply-id");
var nodesToDeleteArr = getChildReplies(reply_id, []);
nodesToDeleteArr.push(reply_id); // adding current node's _id
var deleteConfirm;
if(nodesToDeleteArr.length > 1)
{
deleteConfirm = confirm("Along with this reply, total of: " + nodesToDeleteArr.length + " replies would get deleted.\nClick 'OK' to delete. Else click on 'Cancel'");
}
else
{
deleteConfirm = confirm("Are you sure to delete this reply ?\nClick 'OK' to delete.");
}
if(deleteConfirm)
{
$.ajax({
type: 'POST',
url: "{% url 'discussion_delete_reply' group_id %}",
data: {
"csrfmiddlewaretoken": "{{ csrf_token }}",
"nodes_to_delete": JSON.stringify(nodesToDeleteArr)
},
success: function(data){
data = typeof(data) == "string" ? JSON.parse(data): data;
data.forEach(function(reply){
$("[data-reply-id="+ reply +"]").detach().fadeOut();
})
// updating tab-text
updateDiscussionTabText();
}
})
//end of ajax
}
}
else
{
alert("To delete this reply, you need to be login and author of this reply.");
}
}
// recursive function to get child replies
function getChildReplies(oid, replyArr)
{
var tempReplyArr = $("[data-priornode-id="+oid+"]");
// console.log(tempReplyArr)
if (tempReplyArr.length > 0)
{
tempReplyArr.each(function(){
var tempId = $(this).attr("data-reply-id")
// console.log(replyArr);
replyArr.push(tempId);
getChildReplies(tempId, replyArr);
})
}
return replyArr;
}
// --- END of discussion code ----
</script>
|