JS VisibleTo module

This commit is contained in:
Rei
2014-01-16 18:33:30 +08:00
parent 096f01cf36
commit cc25b80874
7 changed files with 62 additions and 22 deletions

View File

@@ -1 +1 @@
@Campo = {}
@campo = {}

View File

@@ -1,4 +1,4 @@
@Posts =
@campo.Posts =
init: ->
@bindActions()
@@ -34,4 +34,4 @@
else
votes.text('').removeClass('up down')
@Posts.init()
@campo.Posts.init()

View File

@@ -0,0 +1,11 @@
@campo.VisibleTo =
# data-visible-to="options"
#
# options:
# - user: visible to logined user
check: ->
if !window.campo.currentUser?
$('[data-visible-to~=user]').remove()
$ ->
campo.VisibleTo.check()

View File

@@ -7,6 +7,11 @@ html
= javascript_include_tag "application", "data-turbolinks-track" => true
meta name="viewport" content="width=device-width, initial-scale=1.0"
= csrf_meta_tags
- if logined?
javascript:
campo.currentUser = {
id: #{current_user.id}
};
body
nav.navbar.navbar-campo.navbar-static-top

View File

@@ -10,21 +10,20 @@ li.list-group-item.list-group-item-campo.post id="post_#{post.id}" data-post-id=
= time_ago_in_words post.created_at
article.text
= format_post post.content
- if logined?
.actions.clearfix
- if current_user == post.user
.pull-left
- if post.post_number == 1
a.btn href=edit_topic_path(post.topic)
span.glyphicon.glyphicon-edit
- else
a.btn href=edit_post_path(post) data-remote='true'
span.glyphicon.glyphicon-edit
.pull-right
span.votes class=(post.votes > 0 ? 'up' : 'down')
- if post.votes != 0
= post.votes
a.btn data-post-vote-action="up"
span.glyphicon.glyphicon-thumbs-up
a.btn data-post-vote-action="down"
span.glyphicon.glyphicon-thumbs-down
.actions.clearfix
- if current_user == post.user
.pull-left
- if post.post_number == 1
a.btn href=edit_topic_path(post.topic)
span.glyphicon.glyphicon-edit
- else
a.btn href=edit_post_path(post) data-remote='true'
span.glyphicon.glyphicon-edit
.pull-right
span.votes class=(post.votes > 0 ? 'up' : 'down')
- if post.votes != 0
= post.votes
a.btn data-post-vote-action="up"
span.glyphicon.glyphicon-thumbs-up
a.btn data-post-vote-action="down"
span.glyphicon.glyphicon-thumbs-down

View File

@@ -10,7 +10,7 @@ test 'should update votes', ->
</ul
")
Posts.updateVotes([
campo.Posts.updateVotes([
{ post_id: 1, value: 'up' }
{ post_id: 2, value: 'down' }
])

View File

@@ -0,0 +1,25 @@
module('visible-to')
test 'should keep element for user if logined', ->
$fixture = $('#qunit-fixture')
$fixture.append("
<div data-visible-to='user'></div>
")
campo.currentUser = { id: 1 }
campo.VisibleTo.check()
ok($fixture.find('div').length)
# clear
campo.currentUser = null
test 'should remove element for user if logined', ->
$fixture = $('#qunit-fixture')
$fixture.append("
<div data-visible-to='user'></div>
")
campo.VisibleTo.check()
ok(!$fixture.find('div').length)