summaryrefslogtreecommitdiffstats
path: root/askbot/templates/question.html
blob: ca2f40226003ba02b5636bfec375a6cda9e5add7 (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
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
{% extends "two_column_body.html" %}
<!-- question.html -->
{% block title %}{% spaceless %}{{ question.get_question_title()|escape }}{% endspaceless %}{% endblock %}
{% block meta_description %}
        <meta name="description" content="{{question.summary|striptags|escape}}" />
{% endblock %}
{% block keywords %}{{thread.tagname_meta_generator()}}{% endblock %}
{% block forestyle %}
    <link rel="canonical" href="{{ base_url }}{{question.get_absolute_url()}}" />
    <link rel="stylesheet" type="text/css" href="{{'/js/wmd/wmd.css'|media}}" />
{% endblock %}
{% block forejs %}
    <script type="text/javascript">
        /*<![CDATA[*/
        //below is pure cross-browser javascript, no jQuery
        askbot['data']['userIsThreadModerator'] = {% if user_is_thread_moderator %}true{% else %}false{% endif %};
        askbot['data']['oldestAnswerId'] = {% if oldest_answer_id %}{{ oldest_answer_id }}{% else %}-1{% endif %};
        (function(){

            var hasClass = function(node, selector) {
                var classes = (" " + node.className + " ").split(' ');
                for (var i = 0; i < classes.length; i++) {
                    if (classes[i] === selector) {
                        return true;
                    }
                }
                return false;
            }

            var findChildrenByClassName = function(node, className) {
                var nodes = [];
                var walk = function(node) {
                    if (hasClass(node, className)) {
                        nodes.push(node);
                    }
                    if (node.childNodes) {
                        for (var i=0; i < node.childNodes.length; i++) {
                            walk(node.childNodes[i]);
                        }
                    }
                };
                walk(node);
                return nodes;
            };

            var getTextContent = function(node) {
                var text = node.innerText || node.textContent;
                if (text === undefined) {
                    return '';
                } else {
                    return text;
                }
            };

            var hasAttribute = function(node, attrName) {
                if (node.hasAttribute) {
                    return node.hasAttribute(attrName);
                } else {
                    return (!(!(node.getAttribute(attrName))));
                }
            };

            var findChildByAttribute = function(node, attrName, attrVal) {
                var children = node.childNodes;
                for (var i = 0; i < children.length; i++) {
                    var child = children[i];
                    if (child.getAttribute(attrName) === attrVal) {
                        return child;
                    }
                };
                return null;
            };

            var postIsComment = function(postId) {
                if (document.getElementById('comment-' + postId)) {
                    return true;
                }
                return false;
            };

            var removeNode = function(node) {
                node.parentNode.removeChild(node);
            };

            var trim = function(text) {
                return text.replace(/^\s+|\s+$/g, '');
            };

            var data = askbot['data'];
            var isAuthorOfPost = function(post_id) {
                return (data['user_posts'] && data['user_posts'][post_id]);
            };

            if (data['userIsAuthenticated']){
                var votes = {};
                {% for post_id in user_votes %}
                    votes['{{post_id}}'] = {{user_votes[post_id]}};
                {% endfor %}
                data['user_votes'] = votes;
                var posts = {};
                {% for post_id in user_post_id_list %}
                    posts['{{post_id}}'] = 1;
                {% endfor %}
                data['user_posts'] = posts;
            }

            function render_vote_buttons(post_type, post_id){
                var upvote_btn = document.getElementById(
                    post_type + '-img-upvote-' + post_id
                );
                var downvote_btn = document.getElementById(
                    post_type + '-img-downvote-' + post_id
                );
                if (data['userIsAuthenticated']){
                    if (post_id in data['user_votes']){
                        var vote = data['user_votes'][post_id];
                        if (vote == -1){
                            var btn = downvote_btn;
                        } else if (vote == 1){
                            var btn = upvote_btn;
                        } else {
                            return;
                        }
                        if (post_type == 'comment'){
                            btn.className = btn.className + ' upvoted';
                        } else {
                            btn.className = btn.className + ' on';
                        }
                    }
                }
            }

            function hide_convert_answer_links(post_id){
                var id1 = 'post-' + post_id + '-convert';//for repost as Q comment
                var repostAsQuestionComment = document.getElementById(id1);
                var id2 = 'post-' + post_id + '-repost-as-comment-under-previous-answer';
                var repostAsPrevAnsComment = document.getElementById(id2);
                var extraOptsList = repostAsQuestionComment.parentNode;
                var extraOpts = extraOptsList.parentNode;

                var isAuthenticated = data['userIsAuthenticated'];
                var isMod = data['userIsAdminOrMod'];
                if (isAuthenticated && (isMod || isAuthorOfPost(post_id))) {
                    //still may need to hide because answer may be too long
                    var answer_id = 'post-id-' + post_id;
                    var answer_container = document.getElementById(answer_id);
                    var answerBody = findChildrenByClassName(answer_container, 'post-body')[0];
                    //todo: this is not reliable
                    var answerBodyNodes = answerBody.childNodes;
                    var answerElement = answerBodyNodes[answerBodyNodes.length - 1];
                    if (trim(getTextContent(answerElement)).length > askbot['data']['maxCommentLength']) {
                        repostAsQuestionComment.parentNode.removeChild(repostAsQuestionComment);
                        repostAsPrevAnsComment.parentNode.removeChild(repostAsPrevAnsComment);
                    } else if (parseInt(post_id) === data['oldestAnswerId']) {
                        repostAsPrevAnsComment.parentNode.removeChild(repostAsPrevAnsComment);
                    }
                } else {
                    repostAsQuestionComment.parentNode.removeChild(repostAsQuestionComment);
                    repostAsPrevAnsComment.parentNode.removeChild(repostAsPrevAnsComment);
                }

                //if the whole control is empty - remove it
                if (extraOptsList.getElementsByTagName('li').length === 0) {
                    extraOpts.parentNode.removeChild(extraOpts);
                }
            }

            function hidePublishAnswerLink(postId) {
                if (data['userIsThreadModerator'] === false) {
                    //hide publish/unpublish answer links
                    var answerId = 'post-' + postId + '-publish';
                    var pubBtn = document.getElementById(answerId);
                    pubBtn.parentNode.removeChild(pubBtn);
                }
            }

            function render_post_controls(post_id){
                if (data['userIsAdminOrMod']){
                    return;//all remaining functions stay on
                }
                if (data['user_posts'] && post_id in data['user_posts']){
                    //todo: remove edit button from older comments
                    return;
                }
                var deleteBtn = document.getElementById('post-' + post_id + '-delete');
                var controls = deleteBtn.parentNode;
                if (//maybe remove "delete" button
                    data['userReputation'] < 
                    {{settings.MIN_REP_TO_DELETE_OTHERS_COMMENTS}}
                ) {
                    removeNode(deleteBtn);
                }
                var flags = findChildrenByClassName(controls, 'question-flag');
                if (flags.length > 0) {
                    removeNode(flags[0]);
                }
                var closeBtn = findChildrenByClassName(controls, 'question-close');
                if (
                    closeBtn.length === 1 &&
                    data['userReputation'] <
                    {{ settings.MIN_REP_TO_CLOSE_OTHERS_QUESTIONS }}
                ) {
                    removeNode(closeBtn[0]);
                }
                var repLow = (data['userReputation'] < {{settings.MIN_REP_TO_EDIT_OTHERS_POSTS}});
                if (//maybe remove "edit" button
                    repLow || postIsComment(post_id)//only authors edit comments
                ){
                    var edit_btn = document.getElementById(
                        'post-' + post_id + '-edit'
                    )
                    edit_btn.parentNode.removeChild(edit_btn);
                }
                if (//maybe remove retag button
                    data['userReputation'] <
                    {{settings.MIN_REP_TO_RETAG_OTHERS_QUESTIONS}}
                ){
                    var retagBtn = document.getElementById('retag');
                    if (retagBtn) {
                        retagBtn.parentNode.removeChild(retagBtn);
                    }
                }
            }
            function render_add_comment_button(post_id, extra_comment_count){
                if (extra_comment_count > 0){
                    var text = "{% trans %}see more comments{% endtrans%}";
                } else {
                    var text = "{% trans %}add a comment{% endtrans %}";
                }
                var add_comment_btn = document.getElementById('add-comment-to-post-' + post_id);
                add_comment_btn.innerHTML = text;
            }

            function render_add_answer_button(){
                var add_answer_btn = document.getElementById('add-answer-btn');
                if (askbot['data']['userIsAuthenticated']){
                    if (askbot['data']['userId'] == {{question.author_id}}){
                        add_answer_btn.className += ' answer-own-question';
                        add_answer_btn.setAttribute(
                            'value',
                            '{% trans %}Answer Your Own Question{% endtrans %}'
                        )
                    } else {
                        add_answer_btn.setAttribute(
                           'value',
                            '{% trans %}Post Your Answer{% endtrans %}'
                        )
                    }
                } else {
                    add_answer_btn.setAttribute(
                        'value',
                        '{% trans %}Login/Signup to Post{% endtrans %}'
                    );
                }
            }

            function hide_convert_links() {
                var isAuthenticated = data['userIsAuthenticated'];
                var isMod = data['userIsAdminOrMod'];
                if (isAuthenticated && isMod) {
                    return;
                }
                var convertForms = findChildrenByClassName(document, 'convert-comment');
                for (var i = 0; i < convertForms.length; i++) {
                    //get comment id
                    var form = convertForms[i];
                    var idInput = findChildByAttribute(form, 'name', 'comment_id');
                    var commentId = idInput.getAttribute('value');
                    if (! isAuthorOfPost(commentId) ) {
                        form.setAttribute('style', 'display:none;');
                    }
                }
            }
            
            askbot['functions'] = askbot['functions'] || {};
            askbot['functions']['renderPostVoteButtons'] = render_vote_buttons;
            askbot['functions']['renderPostControls'] = render_post_controls;
            askbot['functions']['renderAddCommentButton'] = render_add_comment_button;
            askbot['functions']['renderAddAnswerButton'] = render_add_answer_button;
            askbot['functions']['hideConvertLinks'] = hide_convert_links;
            askbot['functions']['hideConvertAnswerLinks'] = hide_convert_answer_links;
            askbot['functions']['hidePublishAnswerLink'] = hidePublishAnswerLink;
        })();
        /*]]>*/
    </script>
{% endblock %}
{% block content %}
    {% if 'QUESTION_PAGE_TOP_BANNER'|show_block_to(request.user) %}
        <div class="banner">{{ settings.QUESTION_PAGE_TOP_BANNER|safe }}</div>
    {% endif %}
    {% if is_cacheable %}
        {% cache long_time "thread-content-html" thread.id %}
            {% include "question/content.html" %}
        {% endcache %}
    {% else %}
        {% include "question/content.html" %}
    {% endif %}
{% endblock %}
{% block sidebar %}
    {% include "question/sidebar.html" %}
{% endblock %}
{% block endjs %}
    <script type='text/javascript'>
        {# not compressable #}
        {% if settings.ENABLE_MATHJAX or settings.MARKUP_CODE_FRIENDLY %}
        var codeFriendlyMarkdown = true;
        {% else %}
        var codeFriendlyMarkdown = false;
        {% endif %}
        var maxCommentLength = {{settings.MAX_COMMENT_LENGTH}};
        askbot['urls']['postComments'] = '{% url post_comments %}';
        askbot['urls']['editComment'] = '{% url edit_comment %}';
        askbot['urls']['deleteComment'] = '{% url delete_comment %}';
        askbot['urls']['convertComment'] = '{% url comment_to_answer %}';
        askbot['urls']['getComment'] = '{% url get_comment %}';
        askbot['urls']['saveDraftAnswer'] = '{% url save_draft_answer %}';
        askbot['urls']['vote_url'] = '{% url vote %}'
        askbot['urls']['user_signin'] = '{{ settings.LOGIN_URL }}';
        askbot['urls']['swap_question_with_answer'] = '{% url swap_question_with_answer %}';
        askbot['urls']['upvote_comment'] = '{% url upvote_comment %}';
        askbot['urls']['delete_post'] = '{% url delete_post %}';
        askbot['urls']['get_html_template'] = '{% url get_html_template %}';
        askbot['urls']['getGroupsList'] = '{% url get_groups_list %}';
        askbot['urls']['publishAnswer'] = '{% url publish_answer %}';
        askbot['data']['userIsThreadModerator'] = {{ user_is_thread_moderator|as_js_bool }};
        askbot['data']['questionAuthorId'] = {{ question.author_id }};
        askbot['data']['threadIsClosed'] = {{ thread.closed|as_js_bool }};
        askbot['data']['answersSortTab'] = '{{ tab_id }}';
        askbot['data']['questionId'] = {{ question.id }};
        askbot['data']['threadSlug'] = '{{ thread.title|slugify }}';
        askbot['messages']['addComment'] = '{% trans %}add a comment{% endtrans %}';
        askbot['settings']['saveCommentOnEnter'] = {{ settings.SAVE_COMMENT_ON_ENTER|as_js_bool }};
        askbot['settings']['tagSource'] = '{{ settings.TAG_SOURCE }}';
        askbot['settings']['enableSharingGoogle'] = {{ settings.ENABLE_SHARING_GOOGLE|as_js_bool }};
        askbot['settings']['enableEmailAlerts'] = {{ settings.ENABLE_EMAIL_ALERTS|as_js_bool }};
    </script>
    {% include "meta/editor_data.html" %}
    {% compress js %}
    {% include "question/javascript.html" %}
    {% if settings.TAG_SOURCE == 'category-tree' %}
        {% include "meta/category_tree_js.html" %}
    {% endif %}
    {% include "question/custom_javascript.html" ignore missing %}
    {% endcompress %}
    {#
    <script type="text/javascript">
        var messages = askbot['messages'];
        messages['upvote_question'] = gettext(
            'I like this question (click again to cancel)'
        );
        messages['upvote_answer'] = gettext(
            'I like this answer (click again to cancel)'
        );
        messages['downvote_question'] = gettext(
            "I don't like this question (click again to cancel)"
        );
        messages['downvote_answer'] = gettext(
            "I don't like this answer (click again to cancel)"
        );
    </script>
    #}
{% endblock %}