2014-06-03 03:56:40 -04:00
|
|
|
formatLink = (str) ->
|
|
|
|
"![" + str.alt + "](" + str.url + ")"
|
|
|
|
|
2014-05-23 04:22:00 -04:00
|
|
|
$(document).ready ->
|
|
|
|
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>"
|
2014-10-01 18:21:29 -04:00
|
|
|
iconPicture = "<i class=\"fa fa-picture-o div-dropzone-icon\"></i>"
|
|
|
|
iconSpinner = "<i class=\"fa fa-spinner fa-spin div-dropzone-icon\"></i>"
|
2014-05-23 04:22:00 -04:00
|
|
|
btnAlert = "<button type=\"button\"" + alertAttr + ">×</button>"
|
|
|
|
project_image_path_upload = window.project_image_path_upload or null
|
|
|
|
|
2014-09-30 11:07:21 -04:00
|
|
|
$("textarea.markdown-area").wrap "<div class=\"div-dropzone\"></div>"
|
|
|
|
|
2014-05-23 04:22:00 -04:00
|
|
|
$(".div-dropzone").parent().addClass "div-dropzone-wrapper"
|
2014-06-03 03:56:40 -04:00
|
|
|
|
2014-05-23 04:22:00 -04:00
|
|
|
$(".div-dropzone").append divHover
|
|
|
|
$(".div-dropzone-hover").append iconPicture
|
2014-09-30 11:07:21 -04:00
|
|
|
$(".div-dropzone").append divSpinner
|
2014-05-23 04:22:00 -04:00
|
|
|
$(".div-dropzone-spinner").append iconSpinner
|
2014-07-01 23:37:20 -04:00
|
|
|
$(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0
|
|
|
|
"display": "none"
|
2014-05-23 04:22:00 -04:00
|
|
|
|
|
|
|
dropzone = $(".div-dropzone").dropzone(
|
|
|
|
url: project_image_path_upload
|
|
|
|
dictDefaultMessage: ""
|
2014-09-30 11:07:21 -04:00
|
|
|
clickable: true
|
2014-05-23 04:22:00 -04:00
|
|
|
paramName: "markdown_img"
|
|
|
|
maxFilesize: 10
|
|
|
|
uploadMultiple: false
|
|
|
|
acceptedFiles: "image/jpg,image/jpeg,image/gif,image/png"
|
2014-09-30 11:07:21 -04:00
|
|
|
headers:
|
2014-05-23 04:22:00 -04:00
|
|
|
"X-CSRF-Token": $("meta[name=\"csrf-token\"]").attr("content")
|
|
|
|
|
|
|
|
previewContainer: false
|
|
|
|
|
|
|
|
processing: ->
|
2014-06-03 03:56:40 -04:00
|
|
|
$(".div-dropzone-alert").alert "close"
|
2014-05-23 04:22:00 -04:00
|
|
|
|
|
|
|
dragover: ->
|
|
|
|
$(".div-dropzone > textarea").addClass "div-dropzone-focus"
|
|
|
|
$(".div-dropzone-hover").css "opacity", 0.7
|
|
|
|
return
|
|
|
|
|
|
|
|
dragleave: ->
|
|
|
|
$(".div-dropzone > textarea").removeClass "div-dropzone-focus"
|
|
|
|
$(".div-dropzone-hover").css "opacity", 0
|
|
|
|
return
|
|
|
|
|
|
|
|
drop: ->
|
|
|
|
$(".div-dropzone > textarea").removeClass "div-dropzone-focus"
|
|
|
|
$(".div-dropzone-hover").css "opacity", 0
|
|
|
|
$(".div-dropzone > textarea").focus()
|
|
|
|
return
|
|
|
|
|
|
|
|
success: (header, response) ->
|
2014-06-03 03:56:40 -04:00
|
|
|
child = $(dropzone[0]).children("textarea")
|
|
|
|
$(child).val $(child).val() + formatLink(response.link) + "\n"
|
2014-05-23 04:22:00 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
error: (temp, errorMessage) ->
|
2014-06-03 03:56:40 -04:00
|
|
|
checkIfMsgExists = $(".error-alert").children().length
|
|
|
|
if checkIfMsgExists is 0
|
|
|
|
$(".error-alert").append divAlert
|
|
|
|
$(".div-dropzone-alert").append btnAlert + errorMessage
|
2014-05-23 04:22:00 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
sending: ->
|
2014-07-01 23:37:20 -04:00
|
|
|
$(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0.7
|
|
|
|
"display": "inherit"
|
2014-05-23 04:22:00 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
complete: ->
|
|
|
|
$(".dz-preview").remove()
|
|
|
|
$(".markdown-area").trigger "input"
|
2014-07-01 23:37:20 -04:00
|
|
|
$(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0
|
|
|
|
"display": "none"
|
2014-05-23 04:22:00 -04:00
|
|
|
return
|
|
|
|
)
|
|
|
|
|
2014-06-04 11:56:02 -04:00
|
|
|
child = $(dropzone[0]).children("textarea")
|
|
|
|
|
|
|
|
formatLink = (str) ->
|
|
|
|
"![" + str.alt + "](" + str.url + ")"
|
|
|
|
|
|
|
|
handlePaste = (e) ->
|
|
|
|
e.preventDefault()
|
|
|
|
my_event = e.originalEvent
|
2014-09-30 11:07:21 -04:00
|
|
|
|
2014-06-04 11:56:02 -04:00
|
|
|
if my_event.clipboardData and my_event.clipboardData.items
|
|
|
|
processItem(my_event)
|
|
|
|
|
|
|
|
processItem = (e) ->
|
|
|
|
image = isImage(e)
|
|
|
|
if image
|
|
|
|
filename = getFilename(e) or "image.png"
|
|
|
|
text = "{{" + filename + "}}"
|
|
|
|
pasteText(text)
|
|
|
|
uploadFile image.getAsFile(), filename
|
|
|
|
|
|
|
|
else
|
|
|
|
text = e.clipboardData.getData("text/plain")
|
|
|
|
pasteText(text)
|
|
|
|
|
|
|
|
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
|
2014-09-30 11:07:21 -04:00
|
|
|
|
2014-06-04 11:56:02 -04:00
|
|
|
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
|
|
|
|
$(".markdown-area").trigger "input"
|
|
|
|
|
2014-09-30 11:07:21 -04:00
|
|
|
getFilename = (e) ->
|
2014-06-04 11:56:02 -04:00
|
|
|
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")
|
2014-09-30 11:07:21 -04:00
|
|
|
|
2014-06-04 11:56:02 -04:00
|
|
|
value = value.split("\r")
|
|
|
|
value.first()
|
|
|
|
|
|
|
|
uploadFile = (item, filename) ->
|
|
|
|
formData = new FormData()
|
|
|
|
formData.append "markdown_img", item, filename
|
|
|
|
$.ajax
|
|
|
|
url: project_image_path_upload
|
|
|
|
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))
|
2014-09-30 11:07:21 -04:00
|
|
|
|
2014-06-04 11:56:02 -04:00
|
|
|
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) ->
|
2014-07-01 23:37:20 -04:00
|
|
|
$(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0.7
|
|
|
|
"display": "inherit"
|
2014-06-04 11:56:02 -04:00
|
|
|
|
|
|
|
closeSpinner = ->
|
2014-07-01 23:37:20 -04:00
|
|
|
$(".div-dropzone-spinner").css
|
|
|
|
"opacity": 0
|
|
|
|
"display": "none"
|
2014-06-04 11:56:02 -04:00
|
|
|
|
|
|
|
showError = (message) ->
|
|
|
|
checkIfMsgExists = $(".error-alert").children().length
|
|
|
|
if checkIfMsgExists is 0
|
|
|
|
$(".error-alert").append divAlert
|
|
|
|
$(".div-dropzone-alert").append btnAlert + message
|
|
|
|
|
|
|
|
closeAlertMessage = ->
|
|
|
|
$(".div-dropzone-alert").alert "close"
|
|
|
|
|
2014-05-23 04:22:00 -04:00
|
|
|
$(".markdown-selector").click (e) ->
|
|
|
|
e.preventDefault()
|
2014-10-01 11:30:40 -04:00
|
|
|
$(@).closest('.gfm-form').find('.div-dropzone').click()
|
2014-05-23 04:22:00 -04:00
|
|
|
return
|
|
|
|
|
2014-06-21 05:18:57 -04:00
|
|
|
return
|