|
3 | 3 | #= provides tasklist:enabled |
4 | 4 | #= provides tasklist:disabled |
5 | 5 | #= provides tasklist:change |
| 6 | +#= provides tasklist:changed |
6 | 7 | # |
| 8 | +#= require crema/element/fire |
7 | 9 | #= require crema/events/pageupdate |
8 | 10 | # |
9 | 11 | # Enables Task List update behavior. |
|
26 | 28 | # |
27 | 29 | # ### Specification |
28 | 30 | # |
29 | | -# TaskLists MUST be contained in a `div.js-task-list-container`. |
| 31 | +# TaskLists MUST be contained in a `(div).js-task-list-container`. |
30 | 32 | # |
31 | 33 | # TaskList Items SHOULD be an a list (`UL`/`OL`) element. |
32 | 34 | # |
|
37 | 39 | # `value` attribute is the source (Markdown) to be udpated. The source MUST |
38 | 40 | # follow the syntax guidelines. |
39 | 41 | # |
40 | | -# TaskList updates trigger `tasklist:change` events. |
| 42 | +# TaskList updates trigger `tasklist:change` events. If the change is |
| 43 | +# successful, `tasklist:changed` is fired. The change can be canceled. |
41 | 44 | # |
42 | | -# jQuery is required and the `$.pageUpdate` behavior MUST be available. |
| 45 | +# jQuery and crema are required. |
43 | 46 | # |
44 | | -# ### Events |
| 47 | +# ### Methods |
45 | 48 | # |
46 | | -# When the TaskList field has been changed, a `tasklist:change` event is fired. |
| 49 | +# `.taskList('enable')` or `.taskList()` |
| 50 | +# |
| 51 | +# Enables TaskList updates for the container. |
| 52 | +# |
| 53 | +# `.taskList('disable')` |
| 54 | +# |
| 55 | +# Disables TaskList updates for the container. |
| 56 | +# |
| 57 | +## ### Events |
| 58 | +# |
| 59 | +# `tasklist:enabled` |
| 60 | +# |
| 61 | +# Fired when the TaskList is enabled. |
| 62 | +# |
| 63 | +# * **Synchronicity** Sync |
| 64 | +# * **Bubbles** Yes |
| 65 | +# * **Cancelable** No |
| 66 | +# * **Target** `.js-task-list-container` |
| 67 | +# |
| 68 | +# `tasklist:disabled` |
| 69 | +# |
| 70 | +# Fired when the TaskList is disabled. |
| 71 | +# |
| 72 | +# * **Synchronicity** Sync |
| 73 | +# * **Bubbles** Yes |
| 74 | +# * **Cancelable** No |
| 75 | +# * **Target** `.js-task-list-container` |
| 76 | +# |
| 77 | +# `tasklist:change` |
| 78 | +# |
| 79 | +# Fired before the TaskList item change takes affect. |
| 80 | +# |
| 81 | +# * **Synchronicity** Sync |
| 82 | +# * **Bubbles** Yes |
| 83 | +# * **Cancelable** Yes |
| 84 | +# * **Target** `.js-task-list-field` |
| 85 | +# |
| 86 | +# `tasklist:changed` |
| 87 | +# |
| 88 | +# Fired once the TaskList item change has taken affect. |
| 89 | +# |
| 90 | +# * **Synchronicity** Sync |
| 91 | +# * **Bubbles** Yes |
| 92 | +# * **Cancelable** No |
| 93 | +# * **Target** `.js-task-list-field` |
47 | 94 | # |
48 | 95 | # ### NOTE |
49 | 96 | # |
50 | 97 | # Task list checkboxes are rendered as disabled by default because rendered |
51 | 98 | # user content is cached without regard for the viewer. We enable checkboxes |
52 | 99 | # on `pageUpdate` if the container has a `(textarea).js-task-list-field`. |
| 100 | +# |
| 101 | +# To automatically enable TaskLists, add the `js-task-list-enable` class to the |
| 102 | +# `js-task-list-container`. |
53 | 103 |
|
54 | 104 | incomplete = "[ ]" |
55 | 105 | complete = "[x]" |
@@ -111,44 +161,60 @@ updateTaskListItem = (source, itemIndex, checked) -> |
111 | 161 | line |
112 | 162 | result.join("\n") |
113 | 163 |
|
114 | | -# Enables task list items to trigger updates. |
| 164 | +# Updates the $field value to reflect the state of $item. |
| 165 | +# Triggers the `tasklist:change` event before the value has changed, and fires |
| 166 | +# a `tasklist:changed` event once the value has changed. |
| 167 | +updateTaskList = ($item) -> |
| 168 | + $container = $item.closest '.js-task-list-container' |
| 169 | + $field = $container.find '.js-task-list-field' |
| 170 | + index = 1 + $container.find('.task-list-item-checkbox').index($item) |
| 171 | + checked = $item.prop 'checked' |
| 172 | + |
| 173 | + $field.fire 'tasklist:change', [index, checked], -> |
| 174 | + $field.val updateTaskListItem($field.val(), index, checked) |
| 175 | + $field.trigger 'change' |
| 176 | + $field.fire 'tasklist:changed', [index, checked] |
| 177 | + |
| 178 | +# When the task list item checkbox is updated, submit the change |
| 179 | +$(document).on 'change', '.task-list-item-checkbox', -> |
| 180 | + updateTaskList $(this) |
| 181 | + |
| 182 | +# Enables TaskList item changes. |
115 | 183 | enableTaskList = ($container) -> |
116 | 184 | if $container.find('.js-task-list-field').length > 0 |
117 | 185 | $container. |
118 | 186 | find('.task-list-item').addClass('enabled'). |
119 | 187 | find('.task-list-item-checkbox').attr('disabled', null) |
120 | | - $container.trigger 'tasklist:enabled' |
| 188 | + $container.addClass('is-task-list-enabled'). |
| 189 | + trigger 'tasklist:enabled' |
121 | 190 |
|
| 191 | +# Enables a collection of TaskList containers. |
| 192 | +enableTaskLists = ($containers) -> |
| 193 | + for container in $containers |
| 194 | + enableTaskList $(container) |
| 195 | + |
| 196 | +# Disable TaskList item changes. |
122 | 197 | disableTaskList = ($container) -> |
123 | 198 | $container. |
124 | 199 | find('.task-list-item').removeClass('enabled'). |
125 | 200 | find('.task-list-item-checkbox').attr('disabled', 'disabled') |
126 | | - $container.trigger 'tasklist:disabled' |
127 | | - |
128 | | -# Updates the $field value to reflect the state of $item. |
129 | | -# Triggers the `tasklist:change` event when the value has changed. |
130 | | -updateTaskList = ($item) -> |
131 | | - $container = $item.closest '.js-task-list-container' |
132 | | - $field = $container.find '.js-task-list-field' |
133 | | - index = 1 + $container.find('.task-list-item-checkbox').index($item) |
134 | | - checked = $item.prop 'checked' |
135 | | - |
136 | | - disableTaskList $container |
| 201 | + $container.removeClass('is-task-list-enabled'). |
| 202 | + trigger 'tasklist:disabled' |
137 | 203 |
|
138 | | - $field.val updateTaskListItem($field.val(), index, checked) |
139 | | - $field.trigger 'change' |
140 | | - $field.trigger 'tasklist:change', [index, checked] |
| 204 | +# Disables a collection of TaskList containers. |
| 205 | +disableTaskLists = ($containers) -> |
| 206 | + for container in $containers |
| 207 | + disableTaskList $(container) |
141 | 208 |
|
142 | | -# When the task list item checkbox is updated, submit the change |
143 | | -$(document).on 'change', '.task-list-item-checkbox', -> |
144 | | - updateTaskList $(this) |
| 209 | +$.fn.taskList = (method) -> |
| 210 | + $container = $(this).closest('.js-task-list-container') |
145 | 211 |
|
146 | | -$(document).on 'tasklist:disable', '.js-task-list-container', (event) -> |
147 | | - disableTaskList $(this) |
| 212 | + methods = |
| 213 | + enable: enableTaskLists |
| 214 | + disable: disableTaskLists |
148 | 215 |
|
149 | | -$(document).on 'tasklist:enable', '.js-task-list-container', (event) -> |
150 | | - enableTaskList $(this) |
| 216 | + methods[method || 'enable']($container) |
151 | 217 |
|
| 218 | +# When the page is updated, enable new TaskList containers. |
152 | 219 | $.pageUpdate -> |
153 | | - $('.js-task-list-container').each -> |
154 | | - enableTaskList $(this) |
| 220 | + $('.js-task-list-container.js-task-list-enable').taskList() |
0 commit comments