-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknowledgebuilding.js
277 lines (221 loc) · 7.61 KB
/
knowledgebuilding.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
/* Javascript magic */
(function($) {
/* Add tiny expansion to the jQuery to add content match selector */
$.expr[':'].contentIs = function(el, idx, meta) { return $(el).text() === meta[3]; };
var cur_sort = "";
var sort_order = -1;
var threadTitles = {};
$(function() {
/* Check whether KB is enabled; if not, do nothing */
$('#knbu-type-selector').change(function (e) { GetKnbuInfo(e); });
if(!$('body').hasClass('knbu-map-full')) {
$('#submit-reply.knbu-submit').click(function(e) {
var form = $(this).closest('form');
var ok = true;
var error = '';
if(!form.find('#knbu-type-selector').first().val() || form.find('#knbu-type-selector').first().val() == form.find('#knbu-type-selector').find('option').first().val()) {
ok = false;
error += 'Knowledge type field is required!\n';
}
if(form.find('textarea[name="comment"]').first().val().length == 0) {
ok = false;
error += 'Comment field is required!\n';
}
if(form.find('#title').hasClass('required-field') && form.find('#title').val().length == 0) {
ok = false;
error += 'Main idea field is required!\n';
}
if(!ok) {
e.preventDefault();
alert(error);
}
});
}
function GetKnbuInfo(e) {
var url = $('#admin-ajax-url').val();
var post_ID = $('#post-id').val();
$.post(url, {
knbu_ID: $(e.target).val(),
post_ID: post_ID,
action: 'knbu_provide_knbu_info'
}, function(response) {
response = JSON.parse(response);
var form = $(e.target).closest('.knbu-form');
var textarea = form.find('textarea[name="comment"]');
if(textarea.val().length == 0 || textarea.val() == textarea.data('phrases')) {
textarea.val(response.phrases);
textarea.data('phrases', response.phrases);
}
form.find('.knbu-checklist').hide(200, function() { $(this).html('<p>'+response.description + '</p><p>'+response.checklist+'</p>').show(200); });
});
}
if ($("#knbu-exists").length == 0) { return; }
$('.comment-reply-link').click(function(e) {
e.preventDefault();
var comment_body = $(this).closest('.comment');
if(comment_body.find('#respond').length > 0)
comment_body.find('#respond').toggle(200);
else {
$('#comment_parent').val(comment_body.data('id'));
$('#respond').hide().insertAfter(this).show(200);
}
if($(this).text() == 'Reply') {
$('.comment-reply-link').text('Reply');
$(this).text('Close');
}
else
$(this).text('Reply');
});
$("#comment_sorter li").click(function(e) {
e.preventDefault();
var $target = $(e.target);
var $list = $(".commentlist");
var $coms = $list.find(".comment");
var coms = $coms.get();
var type = $target.data('type');
if(type != 'as thread') {
$list.removeClass('thread-sorted');
}
if ( cur_sort == type )
sort_order = sort_order * -1;
else
sort_order = 1;
if(sort_order > 0) {
$('.sort-up').css({ opacity: 0.2 });
$('.sort-down').css({ opacity: 0.8 });
}
else {
$('.sort-up').css({ opacity: 0.8 });
$('.sort-down').css({ opacity: 0.2 });
}
$('.thread-title').hide();
if(type != 'on-a-map') {
$('#map-frame').hide();
$('#comment-frame').show();
}
switch(type) {
case 'on-a-map':
$('#map-frame').show();
$('#comment-frame').hide();
break;
case 'as-thread':
if(!$list.hasClass('thread-sorted'))
$list.addClass('thread-sorted');
coms.sort(function(a,b) {
var ca = parseInt($(a).data('comment-index'));
var cb = parseInt($(b).data('comment-index'));
return (ca < cb) ? -sort_order: (ca > cb) ? sort_order : 0;
});
break;
case 'by-date':
if(!threadTitles.date) {
threadTitles.date = [];
for(var com in coms) {
var date = $(coms[com]).data('date');
if(!threadTitles.date[date])
{ threadTitles.date[date] = date; }
}
}
coms.sort(function(a,b) {
var ca = $(a).data('stamp');
var cb = $(b).data('stamp');
return (ca < cb) ? -sort_order: (ca > cb) ? sort_order : 0;
});
break;
case 'by-person':
if(!threadTitles.authors) {
threadTitles.authors = [];
for(var com in coms) {
var title = $(coms[com]).find('.message-username').first();
if(!threadTitles.authors[title.text()])
{ threadTitles.authors[title.text()] = title.text(); }
}
}
coms.sort(function(a,b) {
var ca = $(a).find(".message-username").text().toLowerCase();
var cb = $(b).find(".message-username").text().toLowerCase();
return (ca < cb) ? -sort_order: (ca > cb) ? sort_order : 0;
});
break;
case 'by-knowledge-type':
if(!threadTitles.knowledgeTypes) {
threadTitles.knowledgeTypes = [];
for(var com in coms) {
var title = $(coms[com]).find('.message-type').first();
if(!threadTitles.knowledgeTypes[title.data('type')])
{ threadTitles.knowledgeTypes[title.data('type')] = title.text(); }
}
}
coms.sort(function(a,b) {
var ca = $(a).data('type');
var cb = $(b).data('type');
return (ca < cb) ? -sort_order: (ca > cb) ? sort_order : 0;
});
break;
}
$.each(coms,function(idx,itm) { $("#comment-frame").append(itm); });
if(type == 'by-knowledge-type') {
$('.thread-title-knbu').show();
if($('.thread-title-knbu').length == 0) {
for(var key in threadTitles.knowledgeTypes) {
var name = threadTitles.knowledgeTypes[key];
if(!key || key.length == 0)
name = 'Unspecified';
var $title = $('<h3>');
$title.text(name);
$title.attr('data-type', key);
$title.addClass('thread-title thread-title-knbu');
$title.insertBefore($('.message-type[data-type="'+key+'"]').first().closest('.knbu-comment'));
}
}
else {
$('.thread-title-knbu').each(function() {
$(this).insertBefore($('.message-type[data-type="'+$(this).data('type')+'"]').first().closest('.knbu-comment'));
});
}
}
else if(type == 'by-person') {
$('.thread-title-author').show();
if($('.thread-title-author').length == 0) {
for(var key in threadTitles.authors) {
var name = threadTitles.authors[key];
if(!key || key.length == 0)
name = 'Unspecified';
var $title = $('<h3>');
$title.text(name);
$title.addClass('thread-title thread-title-author');
$title.insertBefore($('.message-username:contains("'+key+'")').first().closest('.knbu-comment'));
}
}
else {
$('.thread-title-author').each(function() {
$(this).insertBefore($('.message-username:contentIs("'+$(this).html()+'")').first().closest('.knbu-comment'));
});
}
}
else if(type == 'by-date') {
$('.thread-title-date').show();
if($('.thread-title-date').length == 0) {
for(var key in threadTitles.date) {
var name = threadTitles.date[key];
if(!key || key.length == 0)
name = 'Unspecified';
var $title = $('<h3>');
$title.text(name);
$title.addClass('thread-title thread-title-date');
$title.insertBefore($('.message-date:contains("'+key+'")').first().closest('.knbu-comment'));
}
}
else {
$('.thread-title-date').each(function() {
$(this).insertBefore($('.message-date:contains("'+$(this).html()+'")').first().closest('.knbu-comment'));
});
}
}
$("#comment_sorter li").removeClass('selected');
$target.addClass('selected');
cur_sort = type;
//console.timeEnd('total');
});
});
})(jQuery);