up1/static/js/textpaste.js

74 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))
2015-06-11 19:09:03 +02:00
$(document).on('keypress', 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
}
2015-06-11 19:09:03 +02:00
if (!(e.which == 115 && (e.ctrlKey || e.metaKey)) && !(e.which == 19)) {
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()
2015-06-11 11:55:38 +02:00
event.preventDefault()
},
save: function(e) {
e ? e.preventDefault() : undefined
upload.route.setroute(upload.home)
2015-06-11 12:09:19 +02:00
upload.home.doupload(new File([this.current.find('textarea').val()],
this.current.find('#create_filename').val(),
2015-06-11 11:55:38 +02:00
{
2015-06-11 12:09:19 +02:00
type: this.current.find('#create_mime').val()
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
main.append($('<div>').addClass('viewswitcher').append(
$('<button>').prop('type', 'submit').text('Save').addClass('btn')
).append(
$('<a>').prop('id', 'retbtn').text('Return').addClass('btn')
))
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>')
var text = $('<div>').prop('id', 'create_text').addClass('previewtext preview')
.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
})