{{extend "forums_layout.html"}}

{{block title}}{{super}} - {{=forum.name}} - {{=topic.subject}}{{end}}

{{block forum_nav}}
    <ul class="breadcrumb">
      <li>
        <a href="{{=url_for('plugs.forums.views.ForumView.list')}}" class="home" title="首页">论坛首页</a> <span class="divider">/</span>
      </li>
      <li>
        <a href="/forum/{{=forum.id}}">{{=forum.name}}</a> <span class="divider">/</span>
      </li>
      <li class="active">
        <a href="/forum/{{=forum.id}}/{{=topic.id}}">{{=topic.subject}}</a>
      </li>
    </ul>
{{end}}

{{block content_main}}
{{use "jquery"}}
{{use "jqutils", ajaxForm=True}}
{{use "xheditor"}}
{{use "jqimgzoom"}}
{{use "totop"}}

{{def make_sub_posts(parent):}}
    {{if len(sub_posts.get(parent, [])) > 0:}}
    <ul id="sub_posts_{{=parent}}">
        {{for post in sub_posts[parent]:}}
            <div class="box">
                <li class="post" id="post_{{=post.id}}">
                <a name="post_{{=post.id}}"></a>
                <div class="post_body">
                    <div class="user_image">
                        <img title="{{=post.username}}" src="{{=post.userimage}}">
                    </div>
                    <div class="body">
                        <div class="header">
                            由 <span class="user">{{<< post.posted_by}}</span> 回复于 
                            <span class="date">{{= post.created_on}}</span>
                        </div>
                        <div class="post_content" data-content="{{=post.id}}">
                        {{<< post.content}}
                        {{<< post.updated}}
                        </div>
                        
                        {{make_sub_posts(post.id)}}
                        
                    </div>
                    <div class="footer">           
                        <span class="actions" style="display:none">
                            {{<< post.actions}}
                        </span>
                    </div>
                </div>
                <div class="clear"></div>
                </li>
            </div>
        {{pass}}
    </ul><!-- ul -->
    {{pass}}
{{pass}}

<a name="a_topic"></a>
<div class="topic">
    <h2>{{=topic.subject}}</h2>
    <div class="topic_msg">
        共有{{=topic.num_replies}}篇评论
        分类: <span>{{=topic.get_display_value('topic_type')}}</span>
    </div>
</div>
<ul id="posts" class="unstyled">
    {{for post in posts:}}
    <a name="{{=post.id}}"></a>
    <li class="post" id="post_{{=post.id}}">
        <div class="post_body first">
            <div class="user_image">
                <img title="{{=post.username}}" src="{{=post.userimage}}">
            </div>
            <div class="header">
                {{=post.floor}}楼 由 <span class="user">{{<< post.posted_by}}</span> 发表于 
                <span class="date">{{=post.created_on}}</span>
            </div>
            <div class="body">
            <div class="post_content" data-content="{{=post.id}}">
            {{<< post.content}}
            {{<< post.updated}}
            </div>
            
            {{make_sub_posts(post.id)}}
            
            </div>
            <div class="footer">
                <span class="actions" style="display:none">
                    {{<<post.actions}}
                </span>
            </div>
        </div>
        <div class="clear"></div>
    </li>
    {{pass}}
</ul>
{{<< functions.get_var('FORUM/post_footer')}}

<div class="pagination pagination-centered">{{<<pagination}}</div>
{{include "inc_syntaxhighlighter.html"}}
{{if topic.enable_comment:}}
    {{if request.user:}}
        {{if not topic.closed:}}
            <div class="replay">
                <h3>发表评论</h3>
                <form action="/forum/{{=forum.id}}/{{=topic.id}}/new_post" method="POST" id="postform">
                    <div class="type-text">
                        <textarea name="content" class="field" cols="75" rows="15"></textarea>
                    </div>
                    <input type="hidden" name="slug" value="{{=slug}}"></input>
                    <div class="line">
                        <input type="checkbox" name="reply_email" id="field_reply_email">有回复时邮件通知我</input>
                    </div>
                    <div class="line">
                        <button type="submit" class="btn btn-primary">发表评论</button>
                    </div>
                </form>
            </div>
        {{else:}}
            <div class="replay">
                <div class="alert alert-warn">此主题已经被关闭，无法继续评论！</div>
            </div>
        {{pass}}
    {{else:}}
        <div class="replay">
            <div class="alert alert-warn">你尚未登录，无法评论！</div>
        </div>
    {{pass}}
{{else:}}
    <div class="replay">
        <div class="alert alert-warn">此主题被设置为禁止回复！</div>
    </div>
{{pass}}

{{use "jqdropdown"}}
<div class="dropdown dropdown-tip" id="div_move_topic" style="display:none;">
    <div class="dropdown-panel">
        Loading...
    </div>
</div>

<style>
#div_move_topic form {margin-bottom:0;]
</style>

{{include "inc_xheditor_plugins.html"}}
{{include "inc_xheditor_draw_plugins.html"}}
{{include "inc_xheditor_mp3upload_plugins.html"}}
{{include "inc_paginate.html"}}
{{use "fancybox"}}
{{use "poshytip"}}
{{use "colorpicker"}}
{{use "inlineplayer"}}
<script type="text/javascript">
    var bind_action = function(el, action){
        el.click(function(event){
            event.preventDefault();
            var ele = $(this);
            var post_id = ele.attr('rel')
            $.ajax({
                type: "POST",
                data: {post_id:post_id, action:action},
                url: "{{=url_for('plugs.forums.views.ForumView.post_actions')}}",
                success: function(data){
                    show_message(data.msg);
                    if(data.txt){
                        ele.html(data.txt);
                    }
                    //添加删除的处理，动态切換内容
                    if (action == 'delete'){
                        $.get('/forum/get_post_content/'+post_id).success(function(data){
                            $('div[data-content='+post_id+']').html(data.data);
                        });
                    }
                    if (action == 'enable_comment'){
                        window.location.reload();
                    }
                }
            });
        });
    }
    
    function success(data){
        window.location.href = '{{=request.path}}'+'?page='+data.page+'#'+data.id;
        window.location.reload();
    }
    
    $(function(){
        var l=location.toString();
        if(l.indexOf("#")>0){
            var did = l.substr(l.indexOf("#")+1);
            var cur = $('a[name='+did+']');
            if(cur.length>0){
                $("html,body").animate({
                 scrollTop: cur.offset().top - 60
                }, 500);
            }
        }
        
        $('textarea').xheditor({
            tools:'Cut,Copy,Paste,Pastetext,|,Blocktag,Fontface,FontSize,Bold,Italic,Underline,Strikethrough,FontColor,BackColor,SelectAll,Removeformat,|,Align,List,Outdent,Indent,|,Link,Unlink,Img,Hr,Emot,Table,Code,Quote,Draw,Mp3upload,|,About',
            skin:'vista',
            upLinkUrl:"!/forum/upload_file?forum_id={{=forum.id}}&slug={{=slug}}",
            upFlashUrl:"!/upload_file?forum_id={{=forum.id}}&slug={{=slug}}",
            upFlashExt:"swf",
            upImgUrl:"!/forum/upload_image?forum_id={{=forum.id}}&slug={{=slug}}",
            plugins:$.extend(plugins, mp3upload_plugins),
            plugins:$.extend(plugins, draw_plugins),
            localUrlTest:/^https?:\/\/[^\/]*?(xxx\.com)\//i,
            remoteImgSaveUrl:'/forum/paste_image?forum_id={{=forum.id}}&slug={{=slug}}'
        });
        //初始化SyntaxHighlighter
        //SyntaxHighlighter.defaults['highlight'] = [5, 10, 15];
        SyntaxHighlighter.defaults['toolbar'] = true;
        SyntaxHighlighter.config.strings.print = '打印';
        do_syntax();
        
        //处理回复表格提交
        var el = $('#postform');
        var result_process = create_result_process(el, 
            {success:success});
        
        var options = { 
            success: result_process,  // post-submit callback 
            dataType: 'json'
        }; 
        
        el.ajaxForm(options);
        
        //处理贴子action
        $('li.post').hover(
            function(){
                $(this).find('span.actions').slideDown();
            },
            function(){
                $(this).find('span.actions').slideUp();
            }
        );
        
        $.each(['close_thread', 'hidden', 'delete', 'top', 'essence', 'homepage', 'email', 'enable_comment'], function(index, value){
                bind_action($('li.post').find('a.'+value), value);
            }); 
        
        //处理回到顶部显示
        $('.content').UItoTop({ easingType: 'easeOutQuart', text:'<i class="icon-arrow-up"></i>回到顶部' });
        
        $('.post_body img').imgZoom({loadingImg:"{{=url_for_static('jqimgzoom/css/imgzoom-loading.gif')}}"});
        
        //处理贴子移动
        $('a.move_topic').dropdown('attach', '#div_move_topic');
        $('#div_move_topic').on('show', function(e, d){
            $.get('/forum/get_managed_forums?topic_id={{=topic.id}}').success(function(data){
                $('#div_move_topic').find('.dropdown-panel').html(data);
            });
        });
    });
</script>
{{end}}
