2015-01-15 00:43:31 -05:00
|
|
|
class @DropzoneInput
|
|
|
|
constructor: (form) ->
|
|
|
|
Dropzone.autoDiscover = false
|
|
|
|
alertClass = "alert alert-danger alert-dismissable div-dropzone-alert"
|
|
|
|
alertAttr = "class=\"close\" data-dismiss=\"alert\"" + "aria-hidden=\"true\""
|
|
|
|
divHover = "<div class=\"div-dropzone-hover\"></div>"
|
|
|
|
divSpinner = "<div class=\"div-dropzone-spinner\"></div>"
|
|
|
|
divAlert = "<div class=\"" + alertClass + "\"></div>"
|
2015-02-16 16:09:52 -05:00
|
|
|
iconPaperclip = "<i class=\"fa fa-paperclip div-dropzone-icon\"></i>"
|
2015-01-15 00:43:31 -05:00
|
|
|
iconSpinner = "<i class=\"fa fa-spinner fa-spin div-dropzone-icon\"></i>"
|
2015-08-10 03:18:02 -04:00
|
|
|
uploadProgress = $("<div class=\"div-dropzone-progress\"></div>")
|
2015-01-15 00:43:31 -05:00
|
|
|
btnAlert = "<button type=\"button\"" + alertAttr + ">×</button>"
|
2015-02-16 13:58:40 -05:00
|
|
|
project_uploads_path = window.project_uploads_path or null
|
2015-06-02 08:56:50 -04:00
|
|
|
markdown_preview_path = window.markdown_preview_path or null
|
2015-03-20 08:11:12 -04:00
|
|
|
max_file_size = gon.max_file_size or 10
|
2015-01-15 00:43:31 -05:00
|
|
|
|
|
|
|
form_textarea = $(form).find("textarea.markdown-area")
|
|
|
|
form_textarea.wrap "<div class=\"div-dropzone\"></div>"
|
2015-06-05 06:13:02 -04:00
|
|
|
form_textarea.on 'paste', (event) =>
|
2015-01-30 15:50:00 -05:00
|
|
|
handlePaste(event)
|
2015-06-05 06:13:02 -04:00
|
|
|
form_textarea.on "input", ->
|
2015-06-02 08:56:50 -04:00
|
|
|
hideReferencedUsers()
|
2015-06-05 06:13:02 -04:00
|
|
|
form_textarea.on "blur", ->
|
2015-06-02 08:56:50 -04:00
|
|
|
renderMarkdown()
|
2015-01-15 00:43:31 -05:00
|
|
|
|
|
|
|
form_dropzone = $(form).find('.div-dropzone')
|
|
|
|
form_dropzone.parent().addClass "div-dropzone-wrapper"
|
|
|
|
form_dropzone.append divHover
|
2015-07-10 13:46:40 -04:00
|
|
|
form_dropzone.find(".div-dropzone-hover").append iconPaperclip
|
2015-01-15 00:43:31 -05:00
|
|
|
form_dropzone.append divSpinner
|
2015-07-10 13:46:40 -04:00
|
|
|
form_dropzone.find(".div-dropzone-spinner").append iconSpinner
|
2015-08-10 03:18:02 -04:00
|
|
|
form_dropzone.find(".div-dropzone-spinner").append uploadProgress
|
2015-07-10 13:46:40 -04:00
|
|
|
form_dropzone.find(".div-dropzone-spinner").css
|
2015-01-15 00:43:31 -05:00
|
|
|
"opacity": 0
|
|
|
|
"display": "none"
|
|
|
|
|
|
|
|
# Preview button
|
|
|
|
$(document).off "click", ".js-md-preview-button"
|
|
|
|
$(document).on "click", ".js-md-preview-button", (e) ->
|
|
|
|
###
|
|
|
|
Shows the Markdown preview.
|
|
|
|
|
|
|
|
Lets the server render GFM into Html and displays it.
|
|
|
|
###
|
|
|
|
e.preventDefault()
|
|
|
|
form = $(this).closest("form")
|
|
|
|
# toggle tabs
|
|
|
|
form.find(".js-md-write-button").parent().removeClass "active"
|
|
|
|
form.find(".js-md-preview-button").parent().addClass "active"
|
|
|
|
|
|
|
|
# toggle content
|
|
|
|
form.find(".md-write-holder").hide()
|
|
|
|
form.find(".md-preview-holder").show()
|
|
|
|
|
2015-06-02 08:56:50 -04:00
|
|
|
renderMarkdown()
|
2015-01-15 00:43:31 -05:00
|
|
|
|
|
|
|
# Write button
|
|
|
|
$(document).off "click", ".js-md-write-button"
|
|
|
|
$(document).on "click", ".js-md-write-button", (e) ->
|
|
|
|
###
|
|
|
|
Shows the Markdown textarea.
|
|
|
|
###
|
|
|
|
e.preventDefault()
|
|
|
|
form = $(this).closest("form")
|
|
|
|
# toggle tabs
|
|
|
|
form.find(".js-md-write-button").parent().addClass "active"
|
|
|
|
form.find(".js-md-preview-button").parent().removeClass "active"
|
|
|
|
|
|
|
|
# toggle content
|
|
|
|
form.find(".md-write-holder").show()
|
|
|
|
form.find(".md-preview-holder").hide()
|
|
|
|
|
|
|
|
dropzone = form_dropzone.dropzone(
|
2015-02-16 13:58:40 -05:00
|
|
|
url: project_uploads_path
|
2015-01-15 00:43:31 -05:00
|
|
|
dictDefaultMessage: ""
|
|
|
|
clickable: true
|
2015-02-16 13:58:40 -05:00
|
|
|
paramName: "file"
|
2015-03-20 08:11:12 -04:00
|
|
|
maxFilesize: max_file_size
|
2015-01-15 00:43:31 -05:00
|
|
|
uploadMultiple: false
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
|
|
|
|
|
|
|
|
previewContainer: false
|
|
|
|
|
|
|
|
processing: ->
|
|
|
|
$(".div-dropzone-alert").alert "close"
|
|
|
|
|
|
|
|
dragover: ->
|
|
|
|
form_textarea.addClass "div-dropzone-focus"
|
|
|
|
form.find(".div-dropzone-hover").css "opacity", 0.7
|
|
|
|
return
|
|
|
|
|
|
|
|
dragleave: ->
|
|
|
|
form_textarea.removeClass "div-dropzone-focus"
|
|
|
|
form.find(".div-dropzone-hover").css "opacity", 0
|
|
|
|
return
|
|
|
|
|
|
|
|
drop: ->
|
|
|
|
form_textarea.removeClass "div-dropzone-focus"
|
|
|
|
form.find(".div-dropzone-hover").css "opacity", 0
|
|
|
|
form_textarea.focus()
|
|
|
|
return
|
|
|
|
|
|
|
|
success: (header, response) ->
|
|
|
|
child = $(dropzone[0]).children("textarea")
|
|
|
|
$(child).val $(child).val() + formatLink(response.link) + "\n"
|
|
|
|
return
|
|
|
|
|
|
|
|
error: (temp, errorMessage) ->
|
2015-03-20 08:11:12 -04:00
|
|
|
errorAlert = $(form).find('.error-alert')
|
|
|
|
checkIfMsgExists = errorAlert.children().length
|
2015-01-15 00:43:31 -05:00
|
|
|
if checkIfMsgExists is 0
|
2015-03-20 08:11:12 -04:00
|
|
|
errorAlert.append divAlert
|
2015-01-15 00:43:31 -05:00
|
|
|
$(".div-dropzone-alert").append btnAlert + errorMessage
|
|
|
|
return
|
|
|
|
|
2015-08-10 03:18:02 -04:00
|
|
|
totaluploadprogress: (totalUploadProgress) ->
|
|
|
|
uploadProgress.text Math.round(totalUploadProgress) + "%"
|
|
|
|
return
|
|
|
|
|
2015-01-15 00:43:31 -05:00
|
|
|
sending: ->
|
|
|
|
form_dropzone.find(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0.7
|
|
|
|
"display": "inherit"
|
|
|
|
return
|
|
|
|
|
2015-08-10 03:18:02 -04:00
|
|
|
queuecomplete: ->
|
|
|
|
uploadProgress.text ""
|
2015-01-15 00:43:31 -05:00
|
|
|
$(".dz-preview").remove()
|
|
|
|
$(".markdown-area").trigger "input"
|
|
|
|
$(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0
|
|
|
|
"display": "none"
|
|
|
|
return
|
|
|
|
)
|
|
|
|
|
|
|
|
child = $(dropzone[0]).children("textarea")
|
|
|
|
|
2015-06-02 08:56:50 -04:00
|
|
|
hideReferencedUsers = ->
|
2015-06-05 06:13:02 -04:00
|
|
|
referencedUsers = form.find(".referenced-users")
|
2015-06-02 08:56:50 -04:00
|
|
|
referencedUsers.hide()
|
|
|
|
|
|
|
|
renderReferencedUsers = (users) ->
|
2015-06-05 06:13:02 -04:00
|
|
|
referencedUsers = form.find(".referenced-users")
|
2015-06-02 08:56:50 -04:00
|
|
|
|
|
|
|
if referencedUsers.length
|
|
|
|
if users.length >= 10
|
|
|
|
referencedUsers.show()
|
|
|
|
referencedUsers.find(".js-referenced-users-count").text users.length
|
|
|
|
else
|
|
|
|
referencedUsers.hide()
|
|
|
|
|
|
|
|
renderMarkdown = ->
|
|
|
|
preview = form.find(".js-md-preview")
|
|
|
|
mdText = form.find(".markdown-area").val()
|
|
|
|
if mdText.trim().length is 0
|
|
|
|
preview.text "Nothing to preview."
|
|
|
|
hideReferencedUsers()
|
|
|
|
else
|
|
|
|
preview.text "Loading..."
|
|
|
|
$.ajax(
|
|
|
|
type: "POST",
|
|
|
|
url: markdown_preview_path,
|
|
|
|
data: {
|
|
|
|
text: mdText
|
|
|
|
},
|
|
|
|
dataType: "json"
|
|
|
|
).success (data) ->
|
|
|
|
preview.html data.body
|
2015-09-10 15:29:56 -04:00
|
|
|
preview.syntaxHighlight()
|
2015-06-02 08:56:50 -04:00
|
|
|
|
|
|
|
renderReferencedUsers data.references.users
|
|
|
|
|
2015-02-16 13:58:40 -05:00
|
|
|
formatLink = (link) ->
|
|
|
|
text = "[#{link.alt}](#{link.url})"
|
|
|
|
text = "!#{text}" if link.is_image
|
2015-02-14 10:04:45 -05:00
|
|
|
text
|
2015-01-15 00:43:31 -05:00
|
|
|
|
2015-01-30 15:50:00 -05:00
|
|
|
handlePaste = (event) ->
|
|
|
|
pasteEvent = event.originalEvent
|
|
|
|
if pasteEvent.clipboardData and pasteEvent.clipboardData.items
|
|
|
|
image = isImage(pasteEvent)
|
|
|
|
if image
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
filename = getFilename(pasteEvent) or "image.png"
|
|
|
|
text = "{{" + filename + "}}"
|
|
|
|
pasteText(text)
|
|
|
|
uploadFile image.getAsFile(), filename
|
2015-01-15 00:43:31 -05:00
|
|
|
|
|
|
|
isImage = (data) ->
|
|
|
|
i = 0
|
|
|
|
while i < data.clipboardData.items.length
|
|
|
|
item = data.clipboardData.items[i]
|
|
|
|
if item.type.indexOf("image") isnt -1
|
|
|
|
return item
|
|
|
|
i++
|
|
|
|
return false
|
|
|
|
|
|
|
|
pasteText = (text) ->
|
|
|
|
caretStart = $(child)[0].selectionStart
|
|
|
|
caretEnd = $(child)[0].selectionEnd
|
|
|
|
textEnd = $(child).val().length
|
|
|
|
|
|
|
|
beforeSelection = $(child).val().substring 0, caretStart
|
|
|
|
afterSelection = $(child).val().substring caretEnd, textEnd
|
|
|
|
$(child).val beforeSelection + text + afterSelection
|
|
|
|
form_textarea.trigger "input"
|
|
|
|
|
|
|
|
getFilename = (e) ->
|
|
|
|
if window.clipboardData and window.clipboardData.getData
|
|
|
|
value = window.clipboardData.getData("Text")
|
|
|
|
else if e.clipboardData and e.clipboardData.getData
|
|
|
|
value = e.clipboardData.getData("text/plain")
|
|
|
|
|
|
|
|
value = value.split("\r")
|
|
|
|
value.first()
|
|
|
|
|
|
|
|
uploadFile = (item, filename) ->
|
|
|
|
formData = new FormData()
|
2015-02-16 13:58:40 -05:00
|
|
|
formData.append "file", item, filename
|
2015-01-15 00:43:31 -05:00
|
|
|
$.ajax
|
2015-02-16 13:58:40 -05:00
|
|
|
url: project_uploads_path
|
2015-01-15 00:43:31 -05:00
|
|
|
type: "POST"
|
|
|
|
data: formData
|
|
|
|
dataType: "json"
|
|
|
|
processData: false
|
|
|
|
contentType: false
|
|
|
|
headers:
|
|
|
|
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
|
|
|
|
|
|
|
|
beforeSend: ->
|
|
|
|
showSpinner()
|
|
|
|
closeAlertMessage()
|
|
|
|
|
|
|
|
success: (e, textStatus, response) ->
|
|
|
|
insertToTextArea(filename, formatLink(response.responseJSON.link))
|
|
|
|
|
|
|
|
error: (response) ->
|
|
|
|
showError(response.responseJSON.message)
|
|
|
|
|
|
|
|
complete: ->
|
|
|
|
closeSpinner()
|
|
|
|
|
|
|
|
insertToTextArea = (filename, url) ->
|
|
|
|
$(child).val (index, val) ->
|
|
|
|
val.replace("{{" + filename + "}}", url + "\n")
|
|
|
|
|
|
|
|
appendToTextArea = (url) ->
|
|
|
|
$(child).val (index, val) ->
|
|
|
|
val + url + "\n"
|
|
|
|
|
|
|
|
showSpinner = (e) ->
|
|
|
|
form.find(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0.7
|
|
|
|
"display": "inherit"
|
|
|
|
|
|
|
|
closeSpinner = ->
|
|
|
|
form.find(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0
|
|
|
|
"display": "none"
|
|
|
|
|
|
|
|
showError = (message) ->
|
2015-03-20 08:11:12 -04:00
|
|
|
errorAlert = $(form).find('.error-alert')
|
|
|
|
checkIfMsgExists = errorAlert.children().length
|
2015-01-15 00:43:31 -05:00
|
|
|
if checkIfMsgExists is 0
|
2015-03-20 08:11:12 -04:00
|
|
|
errorAlert.append divAlert
|
2015-01-15 00:43:31 -05:00
|
|
|
$(".div-dropzone-alert").append btnAlert + message
|
|
|
|
|
|
|
|
closeAlertMessage = ->
|
|
|
|
form.find(".div-dropzone-alert").alert "close"
|
|
|
|
|
|
|
|
form.find(".markdown-selector").click (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
$(@).closest('.gfm-form').find('.div-dropzone').click()
|
|
|
|
return
|
|
|
|
|
2015-02-16 13:58:40 -05:00
|
|
|
formatLink: (link) ->
|
|
|
|
text = "[#{link.alt}](#{link.url})"
|
|
|
|
text = "!#{text}" if link.is_image
|
2015-03-20 08:11:12 -04:00
|
|
|
text
|