forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReply.coffee
More file actions
180 lines (160 loc) · 5.53 KB
/
Copy pathReply.coffee
File metadata and controls
180 lines (160 loc) · 5.53 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
define [
'Backbone'
'underscore'
'i18n!discussions.reply'
'jquery'
'compiled/models/Entry'
'str/htmlEscape'
'jst/discussions/_reply_attachment'
'compiled/fn/preventDefault'
'compiled/views/editor/KeyboardShortcuts'
'str/stripTags'
'jsx/shared/rce/RichContentEditor'
'jquery.instructure_forms'
], (Backbone, _, I18n, $, Entry, htmlEscape, replyAttachmentTemplate,
preventDefault, KeyboardShortcuts, stripTags, RichContentEditor) ->
RichContentEditor.preloadRemoteModule()
class Reply
##
# Creates a new reply to an Entry
#
# @param {view} an EntryView instance
constructor: (@view, @options={}) ->
@el = @view.$ '.discussion-reply-action:first'
# works for threaded discussion topic and entries
@discussionEntry = @el.closest '.discussion_entry'
# required for non-threaded reply area at bottom of an entry block
@discussionEntry = @el.closest '.entry' if @discussionEntry.length == 0
@form = @discussionEntry.find('form.discussion-reply-form:first').submit preventDefault @submit
@textArea = @getEditingElement()
@form.find('.cancel_button').click @hide
@form.on 'click', '.toggle-wrapper a', (e) =>
e.preventDefault()
RichContentEditor.callOnRCE(@textArea, 'toggle')
# hide the clicked link, and show the other toggle link.
# todo: replace .andSelf with .addBack when JQuery is upgraded.
$(e.currentTarget).siblings('a').andSelf().toggle()
@form.delegate '.alert .close', 'click', preventDefault @hideNotification
@editing = false
_.defer(@attachKeyboardShortcuts)
attachKeyboardShortcuts: =>
@view.$('.toggle-wrapper').first().before((new KeyboardShortcuts()).render().$el)
##
# Shows or hides the TinyMCE editor for a reply
#
# @api public
toggle: ->
if not @editing
@edit()
else
@hide()
##
# Shows the TinyMCE editor for a reply
#
# @api public
edit: ->
@form.addClass 'replying'
@discussionEntry.addClass 'replying'
RichContentEditor.initSidebar()
RichContentEditor.loadNewEditor(@textArea, {
focus: true,
manageParent: true,
tinyOptions: {
width: '100%'
}
})
@editing = true
@trigger 'edit', this
##
# Hides the TinyMCE editor
#
# @api public
hide: =>
@content = RichContentEditor.callOnRCE(@textArea, 'get_code')
RichContentEditor.destroyRCE(@textArea)
@form.removeClass 'replying'
@discussionEntry.removeClass 'replying'
@textArea.val @content
@editing = false
@trigger 'hide', this
@discussionEntry.find('.discussion-reply-action').focus()
hideNotification: =>
@view.model.set 'notification', ''
##
# Submit handler for the reply form. Creates a new Entry and saves it
# to the server.
#
# @api private
submit: =>
@hide()
RichContentEditor.callOnRCE(@textArea, 'set_code', '')
@view.model.set 'notification', "<div class='alert alert-info'>#{htmlEscape I18n.t 'saving_reply', 'Saving reply...'}</div>"
entry = new Entry @getModelAttributes()
entry.save null,
success: @onPostReplySuccess
error: @onPostReplyError
multipart: entry.get('attachment')
proxyAttachment: true
@removeAttachments()
##
# Get the jQueryEl element on the discussion entry to edit.
#
# @api private
getEditingElement: ->
@view.$('.reply-textarea:first')
##
# Computes the model's attributes before saving it to the server
#
# @api private
getModelAttributes: ->
now = new Date().getTime()
# TODO: remove this summary, server should send it in create response and no further
# work is required
summary: stripTags(@content)
message: @content
parent_id: if @options.topLevel then null else @view.model.get 'id'
user_id: ENV.current_user_id
created_at: now
updated_at: now
attachment: @form.find('input[type=file]')[0]
new: true
##
# Callback when the model is succesfully saved
#
# @api private
onPostReplySuccess: (entry, response) =>
if response.errors
@hideNotification()
@textArea.val entry.get('message')
@edit()
@form.formErrors(response)
else
@view.model.set 'notification', ''
@trigger 'save', entry
@textArea.val ''
##
# Callback when the model fails to save
#
# @api private
onPostReplyError: (entry) =>
@view.model.set 'notification', "<div class='alert alert-info'>#{I18n.t "*An error occurred*, please post your reply again later", wrapper: '<strong>$1</strong>'}</div>"
@textArea.val entry.get('message')
@edit()
##
# Adds an attachment
addAttachment: ($el) ->
@form.find('ul.discussion-reply-attachments').append(replyAttachmentTemplate())
@form.find('ul.discussion-reply-attachments input').focus()
@form.find('a.discussion-reply-add-attachment').hide() # TODO: when the data model allows it, tweak this to support multiple in the UI
##
# Removes an attachment
removeAttachment: ($el) ->
$el.closest('ul.discussion-reply-attachments li').remove()
@form.find('a.discussion-reply-add-attachment').show().focus()
##
# Removes all attachments
removeAttachments: ->
@form.find('ul.discussion-reply-attachments').empty()
@form.find('a.discussion-reply-add-attachment').show().focus()
_.extend Reply.prototype, Backbone.Events
Reply