home.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. upload.load.need('js/download.js', function() { return upload.download })
  2. upload.load.need('js/textpaste.js', function() { return upload.textpaste })
  3. upload.load.need('js/loadencryption.js', function() { return window.crypt })
  4. upload.load.need('js/updown.js', function() { return upload.updown })
  5. upload.load.need('js/stringview.js', function() { return upload.stringview })
  6. upload.modules.addmodule({
  7. name: 'home',
  8. // Dear santa, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
  9. template: '\
  10. <div class="topbar">\
  11. <div class="viewswitcher">\
  12. <a id="newpaste" class="btn">New Paste</a>\
  13. </div>\
  14. </div>\
  15. <div id="btnarea">\
  16. <div class="right"\
  17. ><a class="btn" id="helpmebtn" >Help</a\
  18. ><a class="dbtn" id="donatebtn" target="_blank" href="#">Donate</a\
  19. ></div>\
  20. </div>\
  21. <div id="helpmearea" class="help">"Upload" : click or drop to upload a file to the Safe Network<br><br>\
  22. "New Paste" : click to create a new text paste<br><br>\
  23. "Help" : hover to display this help<br><br>\
  24. "Donate" : hover to display bitcoin donation adress qrcode , click to send a donation\
  25. </div>\
  26. <div id="helppastearea" class="help">&nbspEdit , paste or drag text in this window<br><br><br>\
  27. "Save" : click to upload the text the Safe Network<br><br>\
  28. "Return" : click to return to the main upload window<br><br>\
  29. "Help" : hover to display this help<br><br>\
  30. "Donate" : hover to display bitcoin donation adress qrcode , click to send a donation to support SafeShare development\
  31. </div>\
  32. <div id="donatearea" class="donate"><img src="donate.png">\
  33. </div>\
  34. <div class="hidden downloading" id="uploading">\
  35. <h1>Uploading</h1>\
  36. Please wait while your file is transferred to the Safe Network...<br><br><img src="wait.png" >\
  37. </div>\
  38. <div class="contentarea" id="uploadview">\
  39. <div class="centerview">\
  40. <div id="pastearea" class="boxarea">\
  41. <h1>Upload</h1>\
  42. </div>\
  43. <div class="hidden boxarea" id="uploadprogress">\
  44. <h1 id="progresstype"></h1>\
  45. <h1 id="progressamount"></h1>\
  46. <div id="progressamountbg"></div>\
  47. </div>\
  48. <div class="hidden" id="uploadfinish">\
  49. <h1><a href="" id="finallink">Link</a></h1>\
  50. </div>\
  51. <form>\
  52. <input type="file" id="filepicker" class="hidden" />\
  53. </form>\
  54. </div>\
  55. </div>',
  56. init: function () {
  57. upload.modules.setdefault(this)
  58. $(document).on('change', '#filepicker', this.pickerchange.bind(this))
  59. $(document).on('click', '#pastearea', this.pickfile.bind(this))
  60. $(document).on('dragover', '#pastearea', this.dragover.bind(this))
  61. $(document).on('dragleave', '#pastearea', this.dragleave.bind(this))
  62. $(document).on('drop', '#pastearea', this.drop.bind(this))
  63. $(document).on('click', '#newpaste', this.newpaste.bind(this))
  64. $(document).on('click', this.triggerfocuspaste.bind(this))
  65. $(document).on('mouseover', '#helpmebtn', this.helpme.bind(this))
  66. $(document).on('mouseout', '#helpmebtn', this.helpme.bind(this))
  67. $(document).on('mouseover', '#donatebtn', this.donate.bind(this))
  68. $(document).on('mouseout', '#donatebtn', this.donate.bind(this))
  69. this.initpastecatcher()
  70. $(document).on('paste', this.pasted.bind(this))
  71. },
  72. dragleave: function (e) {
  73. e.preventDefault()
  74. e.stopPropagation()
  75. this._.pastearea.removeClass('dragover')
  76. },
  77. drop: function (e) {
  78. e.preventDefault()
  79. this._.pastearea.removeClass('dragover')
  80. if (e.dataTransfer.files.length > 0) {
  81. this.doupload(e.dataTransfer.files[0])
  82. }
  83. },
  84. dragover: function (e) {
  85. e.preventDefault()
  86. this._.pastearea.addClass('dragover')
  87. },
  88. pickfile: function(e) {
  89. this._.filepicker.click()
  90. },
  91. pickerchange: function(e) {
  92. if (e.target.files.length > 0) {
  93. this.doupload(e.target.files[0])
  94. $(e.target).parents('form')[0].reset()
  95. }
  96. },
  97. route: function (route, content) {
  98. if (content && content != 'noref') {
  99. return upload.download
  100. }
  101. return this
  102. },
  103. render: function (view) {
  104. view.html(this.template)
  105. this._ = {}
  106. this._.view = view
  107. this._.filepicker = view.find('#filepicker')
  108. this._.pastearea = view.find('#pastearea')
  109. this._.uploadarea = view.find('#uploadview')
  110. this._.uploading = view.find('#uploading')
  111. this._.newpaste = view.find('#newpaste')
  112. this._.progress = {}
  113. this._.progress.main = view.find('#uploadprogress')
  114. this._.progress.type = view.find('#progresstype')
  115. this._.progress.amount = view.find('#progressamount')
  116. this._.progress.bg = view.find('#progressamountbg')
  117. this._.helpme = view.find('#helpmearea')
  118. this._.helppaste = view.find('#helppastearea')
  119. this._.helpmebtn = view.find('#helpmebtn')
  120. this._.donatebtn = view.find('#donatebtn')
  121. this._.donatebtn.prop('href', upload.config.donate_key )
  122. this._.donatearea = view.find('#donatearea')
  123. this._.returnarea = view.find('#retbtn')
  124. $('#footer').hide()
  125. $('#waiting').hide()
  126. $('#helpmearea').hide()
  127. $('#helppastearea').hide()
  128. $('#donatearea').hide()
  129. },
  130. initroute: function () {
  131. this.focuspaste()
  132. },
  133. unrender: function() {
  134. delete this['_']
  135. },
  136. initpastecatcher: function () {
  137. this.pastecatcher = $('<pre>').prop('id', 'pastecatcher')
  138. this.pastecatcher.prop('contenteditable', true)
  139. $('body').append(this.pastecatcher)
  140. },
  141. focuspaste: function () {
  142. setTimeout(function () {
  143. this.pastecatcher.focus()
  144. }, 100)
  145. },
  146. triggerfocuspaste: function(e) {
  147. if (e.which != 1) {
  148. return
  149. }
  150. if (e.target == document.body && this._ && !this._.pastearea.hasClass('hidden')) {
  151. e.preventDefault()
  152. this.focuspaste()
  153. }
  154. },
  155. progress: function(e) {
  156. if (e.eventsource = 'encrypt') {
  157. this._.progress.type.text('Uploading')
  158. } else {
  159. this._.progress.type.text('Encrypting')
  160. }
  161. var percent = (e.loaded / e.total) * 100
  162. this._.progress.bg.css('width', percent + '%')
  163. this._.progress.amount.text(Math.floor(percent) + '%')
  164. },
  165. doupload: function (blob) {
  166. this._.pastearea.addClass('hidden')
  167. this._.uploadarea.addClass('hidden')
  168. this._.helpmebtn.addClass('hidden')
  169. //this._.progress.main.removeClass('hidden')
  170. //this._.progress.type.text('Uploading')
  171. //this._.progress.bg.css('width', 0)
  172. this._.uploading.removeClass('hidden')
  173. this._.newpaste.addClass('hidden')
  174. upload.updown.upload(blob, this.progress.bind(this), this.uploaded.bind(this))
  175. },
  176. closepaste: function() {
  177. this._.pastearea.removeClass('hidden')
  178. this._.view.find('#uploadview').show()
  179. this._.view.find('.viewswitcher').show()
  180. },
  181. dopasteupload: function (data) {
  182. this._.pastearea.addClass('hidden')
  183. this._.view.find('#uploadview').hide()
  184. this._.view.find('.viewswitcher').hide()
  185. upload.textpaste.render(this._.view, 'Pasted text.txt', data, 'text/plain', this.closepaste.bind(this))
  186. },
  187. uploaded: function (data, response) {
  188. console.log ('File Uploaded');
  189. upload.download.delkeys[data.ident] = data.ident;
  190. try {
  191. localStorage.setItem('delete-' + data.ident, data.ident)
  192. } catch (e) {
  193. console.log(e)
  194. }
  195. if (window.location.hash == '#noref') {
  196. history.replaceState(undefined, undefined, '#' + data.seed)
  197. upload.route.setroute(upload.download, undefined, data.seed)
  198. } else {
  199. window.location = '#' + data.seed
  200. }
  201. },
  202. helpme: function() {
  203. //console.log ( 'helpme' );
  204. if( $("#retbtn").is(":visible") == false ) { // in upload
  205. if( $("#helpmearea").is(":visible") == false ) {
  206. this._.helpme.show();this._.uploadarea.hide();}
  207. else {
  208. this._.helpme.hide();this._.uploadarea.show();}
  209. } else { // in paste
  210. if( $("#helppastearea").is(":visible") == false ) {
  211. this._.helppaste.show();this._.uploadarea.hide();}
  212. else {
  213. this._.helppaste.hide();}
  214. }
  215. },
  216. donate: function() {
  217. //console.log ( 'donate' );
  218. if( $("#donatearea").is(":visible") == false )
  219. { this._.donatearea.show();this._.uploadarea.hide();this._.uploading.hide();} else
  220. { this._.donatearea.hide(); if( $("#retbtn").is(":visible") == false ) { this._.uploadarea.show(); }; //
  221. if( $("#helpmebtn").is(":visible") == false ) { this._.uploading.show(); }; // in uploading
  222. }
  223. },
  224. newpaste: function() {
  225. this.dopasteupload('')
  226. },
  227. pasted: function (e) {
  228. if (!this._ || this._.pastearea.hasClass('hidden')) {
  229. return
  230. }
  231. var items = e.clipboardData.items
  232. var text = e.clipboardData.getData('text/plain')
  233. if (text) {
  234. e.preventDefault()
  235. this.dopasteupload(text)
  236. } else if (typeof items == 'undefined') {
  237. self = this
  238. setTimeout(function () {
  239. if (self.pastecatcher.find('img').length) {
  240. var src = self.pastecatcher.find('img').prop('src')
  241. if (src.startsWith('data:')) {
  242. self.doupload(dataURItoBlob(src))
  243. } else {
  244. // TODO: Firefox
  245. }
  246. }
  247. }, 0)
  248. } else if (items.length >= 1) {
  249. e.preventDefault()
  250. for (var i = 0; i < items.length; i++) {
  251. var blob = items[i].getAsFile()
  252. if (blob) {
  253. this.doupload(blob)
  254. break
  255. }
  256. }
  257. }
  258. },
  259. })