/
checklist_ui.js
42 lines (34 loc) · 1.22 KB
/
checklist_ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
Discourse.PostView.reopen({
createChecklistUI: function($post) {
if (!this.post.can_edit) { return };
var boxes = $post.find(".chcklst-box"),
view = this;
boxes.each(function(idx, val) {
$(val).click(function(ev) {
var elem = $(ev.currentTarget),
new_value = elem.hasClass("checked") ? "[ ]": "[*]",
poller = Discourse.Post.load(view.post.get("id"));
elem.after('<i class="fa fa-spinner fa-spin"></i>');
elem.hide();
poller.then(function(result) {
var nth = -1, // make the first run go to index = 0
new_raw = result.raw.replace(/\[([\ \_\-\x\*]?)\]/g, function(match, args, offset) {
nth += 1;
return nth == idx ? new_value : match;
});
view.post.setProperties({
raw: new_raw,
editReason: "change checkmark"
});
view.post.save(function(result) {
view.post.updateFromPost(result);
});
});
});
});
// confirm the feature is enabled by showing the click-ability
boxes.css({"cursor": "pointer"});
}.on('postViewInserted'),
destroyChecklistUI: function() {
}.on('willClearRender')
});