Chapter 20: Some JavaScript Magic (v0.20)
This commit is contained in:
		
							parent
							
								
									8ac72cdc25
								
							
						
					
					
						commit
						98fa01d8bd
					
				| 
						 | 
				
			
			@ -77,6 +77,13 @@ def user(username):
 | 
			
		|||
                           next_url=next_url, prev_url=prev_url)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/user/<username>/popup')
 | 
			
		||||
@login_required
 | 
			
		||||
def user_popup(username):
 | 
			
		||||
    user = User.query.filter_by(username=username).first_or_404()
 | 
			
		||||
    return render_template('user_popup.html', user=user)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@bp.route('/edit_profile', methods=['GET', 'POST'])
 | 
			
		||||
@login_required
 | 
			
		||||
def edit_profile():
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,9 +7,11 @@
 | 
			
		|||
            </td>
 | 
			
		||||
            <td>
 | 
			
		||||
                {% set user_link %}
 | 
			
		||||
                    <a href="{{ url_for('main.user', username=post.author.username) }}">
 | 
			
		||||
                        {{ post.author.username }}
 | 
			
		||||
                    </a>
 | 
			
		||||
                    <span class="user_popup">
 | 
			
		||||
                        <a href="{{ url_for('main.user', username=post.author.username) }}">
 | 
			
		||||
                            {{ post.author.username }}
 | 
			
		||||
                        </a>
 | 
			
		||||
                    </span>
 | 
			
		||||
                {% endset %}
 | 
			
		||||
                {{ _('%(username)s said %(when)s',
 | 
			
		||||
                    username=user_link, when=moment(post.timestamp).fromNow()) }}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -73,5 +73,47 @@
 | 
			
		|||
                $(destElem).text("{{ _('Error: Could not contact server.') }}");
 | 
			
		||||
            });
 | 
			
		||||
        }
 | 
			
		||||
        $(function () {
 | 
			
		||||
            var timer = null;
 | 
			
		||||
            var xhr = null;
 | 
			
		||||
            $('.user_popup').hover(
 | 
			
		||||
                function(event) {
 | 
			
		||||
                    // mouse in event handler
 | 
			
		||||
                    var elem = $(event.currentTarget);
 | 
			
		||||
                    timer = setTimeout(function() {
 | 
			
		||||
                        timer = null;
 | 
			
		||||
                        xhr = $.ajax(
 | 
			
		||||
                            '/user/' + elem.first().text().trim() + '/popup').done(
 | 
			
		||||
                                function(data) {
 | 
			
		||||
                                    xhr = null;
 | 
			
		||||
                                    elem.popover({
 | 
			
		||||
                                        trigger: 'manual',
 | 
			
		||||
                                        html: true,
 | 
			
		||||
                                        animation: false,
 | 
			
		||||
                                        container: elem,
 | 
			
		||||
                                        content: data
 | 
			
		||||
                                    }).popover('show');
 | 
			
		||||
                                    flask_moment_render_all();
 | 
			
		||||
                                }
 | 
			
		||||
                            );
 | 
			
		||||
                    }, 1000);
 | 
			
		||||
                },
 | 
			
		||||
                function(event) {
 | 
			
		||||
                    // mouse out event handler
 | 
			
		||||
                    var elem = $(event.currentTarget);
 | 
			
		||||
                    if (timer) {
 | 
			
		||||
                        clearTimeout(timer);
 | 
			
		||||
                        timer = null;
 | 
			
		||||
                    }
 | 
			
		||||
                    else if (xhr) {
 | 
			
		||||
                        xhr.abort();
 | 
			
		||||
                        xhr = null;
 | 
			
		||||
                    }
 | 
			
		||||
                    else {
 | 
			
		||||
                        elem.popover('destroy');
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
            );
 | 
			
		||||
        });
 | 
			
		||||
    </script>
 | 
			
		||||
{% endblock %}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,22 @@
 | 
			
		|||
<table class="table">
 | 
			
		||||
    <tr>
 | 
			
		||||
        <td width="64" style="border: 0px;"><img src="{{ user.avatar(64) }}"></td>
 | 
			
		||||
        <td style="border: 0px;">
 | 
			
		||||
            <p><a href="{{ url_for('main.user', username=user.username) }}">{{ user.username }}</a></p>
 | 
			
		||||
            <small>
 | 
			
		||||
                {% if user.about_me %}<p>{{ user.about_me }}</p>{% endif %}
 | 
			
		||||
                {% if user.last_seen %}
 | 
			
		||||
                <p>{{ _('Last seen on') }}: {{ moment(user.last_seen).format('lll') }}</p>
 | 
			
		||||
                {% endif %}
 | 
			
		||||
                <p>{{ _('%(count)d followers', count=user.followers.count()) }}, {{ _('%(count)d following', count=user.followed.count()) }}</p>
 | 
			
		||||
                {% if user != current_user %}
 | 
			
		||||
                    {% if not current_user.is_following(user) %}
 | 
			
		||||
                    <a href="{{ url_for('main.follow', username=user.username) }}">{{ _('Follow') }}</a>
 | 
			
		||||
                    {% else %}
 | 
			
		||||
                    <a href="{{ url_for('main.unfollow', username=user.username) }}">{{ _('Unfollow') }}</a>
 | 
			
		||||
                    {% endif %}
 | 
			
		||||
                {% endif %}
 | 
			
		||||
            </small>
 | 
			
		||||
        </td>
 | 
			
		||||
    </tr>
 | 
			
		||||
</table>
 | 
			
		||||
		Loading…
	
		Reference in New Issue