Skip to content
This repository was archived by the owner on Nov 1, 2017. It is now read-only.

Commit 2987bb4

Browse files
committed
Add JS/CSS assets, optional railtie
1 parent 618722f commit 2987bb4

3 files changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Task list checkboxes are rendered as disabled by default
2+
# because rendered user content is cached without regard
3+
# for the viewer. Enables the checkboxes and applies the
4+
# correct list style, if the viewer is able to edit the comment.
5+
enableTaskList = (comment) ->
6+
if comment.find('.js-comment-edit-button').length > 0
7+
comment.addClass('context-loader-container')
8+
comment.find('.js-comment-body').addClass('context-loader-overlay')
9+
comment.
10+
find('.task-list-item').addClass('enabled').
11+
find('.task-list-item-checkbox').attr('disabled', null)
12+
comment.closest('.context-loader-container').find('.context-loader:first').
13+
removeClass 'is-context-loading'
14+
15+
disableTaskList = (comment) ->
16+
comment.
17+
find('.task-list-item').removeClass('enabled').
18+
find('.task-list-item-checkbox').attr('disabled', 'disabled')
19+
comment.closest('.context-loader-container').find('.context-loader:first').
20+
addClass 'is-context-loading'
21+
22+
# Submit updates to task list items asynchronously.
23+
# Successful updates won't require re-rendering to represent reality.
24+
updateTaskListItem = (item) ->
25+
comment = item.parents('.js-comment')
26+
form = comment.find('form.js-comment-update')
27+
body = comment.find('.js-comment-body')
28+
data =
29+
item: item.attr('data-item-index')
30+
checked: if item.prop('checked') then 1 else 0
31+
body_version: body.attr('data-body-version')
32+
33+
disableTaskList(comment)
34+
35+
$.ajax
36+
type: 'post'
37+
url: form.attr('action')+'/task'
38+
data: data
39+
dataType: 'json'
40+
success: (data) ->
41+
console.log 'success', data
42+
if data.stale
43+
window.location.href = window.location.href
44+
else
45+
body.attr('data-body-version', data.new_body_version)
46+
comment.find('.form-content .js-comment-field').val(data.new_body)
47+
error: (e) ->
48+
console.log 'error', e
49+
complete: ->
50+
enableTaskList(comment)
51+
52+
# When the task list item checkbox is updated, submit the change
53+
$(document).on 'change', 'input.task-list-item-checkbox[type=checkbox]', ->
54+
updateTaskListItem $(this)
55+
56+
$.pageUpdate ->
57+
$('.js-comment').each ->
58+
enableTaskList($(this))
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Requires use of `.markdown-body` to override default markdown list styles
2+
.markdown-body .task-list {
3+
list-style-type: none;
4+
padding-left: 10px;
5+
}
6+
7+
.task-list-item {
8+
padding-left: 20px;
9+
}
10+
.task-list-item label {
11+
font-weight: normal;
12+
}
13+
.task-list-item.enabled label {
14+
cursor: pointer;
15+
}
16+
.task-list-item + .task-list-item {
17+
margin-top: 3px;
18+
}
19+
.task-list-item-checkbox {
20+
float: left;
21+
margin-left: -20px;
22+
margin-top: 4px;
23+
}

lib/task_list/railtie.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class TaskList
2+
3+
def self.root_path
4+
@root_path ||= Pathname.new(File.expand_path("../../../", __FILE__))
5+
end
6+
7+
def self.asset_paths
8+
@paths ||= Dir[root_path.join("app/assets/*")]
9+
end
10+
11+
if defined? ::Rails::Railtie
12+
class Railtie < ::Rails::Railtie
13+
initializer "task-lists" do |app|
14+
TaskList.paths.each do |path|
15+
app.config.assets.paths << path
16+
end
17+
end
18+
end
19+
end
20+
end

0 commit comments

Comments
 (0)