up1/static/js/textpaste.js

75 lines
2.1 KiB
JavaScript
Raw Normal View History

2015-06-11 11:55:38 +02:00
upload.modules.addmodule({
name: 'textpaste',
init: function () {
$(document).on('submit', '#textview', this.save.bind(this))
$(document).on('click', '#retbtn', this.closethis.bind(this))
$(document).on('keydown', this.keypress.bind(this))
2015-06-11 11:55:38 +02:00
},
keypress: function(e) {
2015-06-11 19:09:03 +02:00
if (!this.current || !this.current.is(':visible')) {
2015-06-11 11:55:38 +02:00
return
}
if (!(e.which == 83 && (e.ctrlKey || e.metaKey))) {
2015-06-11 11:55:38 +02:00
return
}
2015-06-10 17:45:02 +02:00
2015-06-11 12:09:19 +02:00
this.save()
e.preventDefault()
2015-06-11 11:55:38 +02:00
},
save: function(e) {
2015-06-11 20:47:09 +02:00
e ? e.preventDefault() : undefined
var blob = new Blob([this.current.find('textarea').val()],
2015-06-11 11:55:38 +02:00
{
2015-06-11 20:47:09 +02:00
type: this.current.find('#create_mime').val()
2015-06-11 11:55:38 +02:00
}
2015-06-11 20:47:09 +02:00
)
blob.name = this.current.find('#create_filename').val()
upload.route.setroute(upload.home)
upload.home.doupload(blob)
2015-06-11 11:55:38 +02:00
},
2015-06-11 19:09:03 +02:00
cleanup: function() {
2015-06-11 11:55:38 +02:00
delete this['closeback']
delete this['current']
2015-06-11 19:09:03 +02:00
},
closethis: function() {
var closeback = this.closeback
this.current.remove()
this.cleanup()
2015-06-11 11:55:38 +02:00
closeback()
},
render: function(view, filename, data, mime, closeback) {
var main = $('<form>').prop('id', 'textview').prop('autocomplete', 'off')
main.appendTo(view)
this.closeback = closeback
this.current = main
2015-06-11 23:08:54 +02:00
main.append($('<div>').addClass('topbar').append($('<div>').addClass('viewswitcher').append(
2015-06-11 11:55:38 +02:00
$('<button>').prop('type', 'submit').text('Save').addClass('btn')
).append(
$('<a>').prop('id', 'retbtn').text('Return').addClass('btn')
2015-06-11 23:08:54 +02:00
)))
2015-06-11 11:55:38 +02:00
var filenamefield = $('<input>').prop('id', 'create_filename').val(filename)
var mimefield = $('<input>').prop('hidden', true).prop('id', 'create_mime').val(mime)
main.append(filenamefield).append(mimefield)
var area = $('<textarea>')
2015-06-24 05:30:36 +02:00
var text = $('<div>').prop('id', 'create_text').addClass('previewtext')
2015-06-11 11:55:38 +02:00
.append($('<div>').prop('id', 'create_linenos').append("&gt;"))
.append(area)
main.append(text)
2015-06-10 17:45:02 +02:00
2015-06-11 11:55:38 +02:00
area.val(data).focus()[0].setSelectionRange(0, 0)
area.scrollTop(0)
2015-06-10 08:21:00 +02:00
}
2015-06-11 00:36:29 +02:00
})