Move to zepto, remove a bunch of inline includes

This commit is contained in:
Andre D 2015-08-04 20:48:46 -04:00
parent 635ac4a097
commit 70ded10886
10 changed files with 240 additions and 193 deletions

View file

@ -347,18 +347,9 @@
height: 100%;
}
</style>
<script src="/static/deps/jquery-2.1.4.min.js"></script>
<script src="/static/js/shims.js"></script>
<script src="/static/js/loadencryption.js"></script>
<script src="/static/js/main.js"></script>
<script src="/static/js/home.js"></script>
<script src="/static/js/updown.js"></script>
<script src="/static/js/download.js"></script>
<script src="/static/js/textpaste.js"></script>
<script src="/config.js"></script>
<script src="/static/js/dragresize.js"></script>
<script src="/static/js/base/main.js"></script>
<script>
$(window).unload(function () { $(window).unbind('unload') })
window.onunload = function () {}
</script>
<link rel="stylesheet" href="/static/deps/hybrid.min.css">
<script src="/static/deps/highlight.min.js"></script>

File diff suppressed because one or more lines are too long

2
static/deps/zepto.min.js vendored Normal file

File diff suppressed because one or more lines are too long

135
static/js/base/main.js Normal file
View file

@ -0,0 +1,135 @@
(function(window) {
"use strict";
window.upload = {}
}(window));
(function(upload) {
upload.load = {
loaded: 0,
doneloaded: function() {
this.loaded -= 1
if (this.loaded <= 0) {
this.cb()
}
},
load: function(filename, test, onload) {
if (test && test()) {
return false
}
var head = document.getElementsByTagName('head')[0]
var script = document.createElement('script')
script.src = '/static/' + filename
script.async = true
script.onload = onload
head.appendChild(script)
return true
},
needsome: function() {
this.loaded += 1
return this
},
done: function(callback) {
this.loaded -= 1
this.cb = callback
return this
},
then: function(callback) {
this.deferred.then(callback)
return this
},
need: function(filename, test) {
this.loaded += 1
if(!this.load(filename, test, this.doneloaded.bind(this))) {
this.loaded -= 1
}
return this
}
}
upload.modules = {
modules: [],
addmodule: function (module) {
this.modules.unshift(module)
upload[module.name] = module
},
initmodule: function (module) {
module.init()
},
setdefault: function (module) {
this.default = module
},
init: function () {
this.modules.forEach(this.initmodule.bind(this))
}
}
upload.modules.addmodule({
name: 'contactlink',
init: function() {
$('#contact').prop('href', upload.config.contact_link)
}
})
upload.modules.addmodule({
name: 'route',
init: function () {
window.addEventListener('hashchange', this.hashchange.bind(this))
this.hashchange()
},
setroute: function (module, routeroot, route) {
view = $('.modulecontent.modulearea')
if (!this.currentmodule || this.currentmodule != module) {
// TODO: better
if (this.currentmodule) {
this.currentmodule.unrender()
}
this.currentmodule = module
view.id = 'module_' + module.name
module.render(view)
}
module.initroute(route, routeroot)
},
tryroute: function (route) {
var isroot = route.startsWith('/')
var normalroute = isroot ? route.substring(1) : route
var route = normalroute.substr(normalroute.indexOf('/') + 1)
var routeroot = normalroute.substr(0, normalroute.indexOf('/'))
var chosenmodule
if (!normalroute) {
chosenmodule = upload.modules.default
} else {
upload.modules.modules.every(function (module) {
if (!module.route) {
return true
}
if (module.route(routeroot, route)) {
chosenmodule = module
return false
}
return true
})
}
if (!chosenmodule) {
chosenmodule = upload.modules.default
}
setTimeout(this.setroute.bind(this, chosenmodule, routeroot, route), 0)
},
hashchange: function () {
this.tryroute(window.location.hash.substring(1))
}
})
}(window.upload));
(function () {
upload.load.needsome().need('../config.js').need('js/shims.js').need('deps/zepto.min.js').done(function() {
upload.load.needsome().need('js/home.js', function() {return upload.home}).done(function() {
if (typeof upload.config != 'undefined') {
upload.modules.init()
} else {
alert("Please configure with config.js (see config.js.example)")
}
})
})
}(upload))

View file

@ -1,3 +1,5 @@
upload.load.need('js/dragresize.js', function() { return window.dragresize })
upload.modules.addmodule({
name: 'download',
delkeys: {},

View file

@ -1,4 +1,6 @@
$(function () {
window.dragresize = true
var dragging
var lastx

View file

@ -1,3 +1,8 @@
upload.load.need('js/download.js', function() { return upload.download })
upload.load.need('js/textpaste.js', function() { return upload.textpaste })
upload.load.need('js/loadencryption.js', function() { return window.crypt })
upload.load.need('js/updown.js', function() { return upload.updown })
upload.modules.addmodule({
name: 'home',
// Dear santa, https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/template_strings
@ -103,7 +108,7 @@ upload.modules.addmodule({
return
}
if (e.target == document.body && this._ && this._.pastearea.is(':visible')) {
if (e.target == document.body && this._ && this._.pastearea.hasClass('hidden')) {
e.preventDefault()
this.focuspaste()
}
@ -155,7 +160,7 @@ upload.modules.addmodule({
this.dopasteupload('')
},
pasted: function (e) {
if (!this._ || !this._.pastearea.is(':visible')) {
if (!this._ || !this._.pastearea.hasClass('visible')) {
return
}

View file

@ -1,55 +1,54 @@
var crypt = {}
window.crypt = {}
$(function () {
var crypto = window.crypto || window.msCrypto;
var crypto = window.crypto || window.msCrypto;
function getEntropy() {
function getEntropy() {
var entropy = new Uint32Array(256)
crypto.getRandomValues(entropy)
return entropy
}
}
function getSeed() {
function getSeed() {
var seed = new Uint8Array(16)
crypto.getRandomValues(seed)
return seed
}
}
var worker = new Worker("/static/js/encryption.js")
var worker = new Worker("/static/js/encryption.js")
var promises = {}
var promises = {}
function str2ab(str) {
function str2ab(str) {
var buf = new ArrayBuffer(str.length * 2);
var bufView = new DataView(buf);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView.setUint16(i * 2, str.charCodeAt(i), false)
}
return buf;
}
}
worker.onmessage = function (e) {
worker.onmessage = function (e) {
if (e.data.type == 'progress') {
promises[e.data.id].notify(e.data)
} else {
promises[e.data.id].resolve(e.data)
delete promises[e.data.id]
}
}
}
var counter = 0
var counter = 0
function getpromise() {
function getpromise() {
var promise = $.Deferred()
var promiseid = counter
counter += 1
promise.id = promiseid
promises[promiseid] = promise;
return promise
}
}
crypt.encrypt = function (file, name) {
crypt.encrypt = function (file, name) {
var extension = file.type.split('/')
@ -78,10 +77,10 @@ $(function () {
fr.readAsArrayBuffer(blob)
return promise
}
}
crypt.ident = function (seed) {
crypt.ident = function (seed) {
var promise = getpromise()
worker.postMessage({
@ -91,10 +90,10 @@ $(function () {
})
return promise
}
}
crypt.decrypt = function (file, seed) {
crypt.decrypt = function (file, seed) {
var promise = getpromise()
var fr = new FileReader()
@ -111,5 +110,4 @@ $(function () {
fr.readAsArrayBuffer(file)
return promise
}
})
}

View file

@ -1,84 +0,0 @@
upload = {}
// Configure in config.js
upload.config = {}
upload.modules = {
modules: [],
addmodule: function (module) {
this.modules.unshift(module)
upload[module.name] = module
},
initmodule: function (module) {
module.init()
},
setdefault: function (module) {
this.default = module
},
init: function () {
this.modules.forEach(this.initmodule.bind(this))
}
}
upload.modules.addmodule({
name: 'contactlink',
init: function() {
$('#contact').prop('href', upload.config.contact_link)
}
})
upload.modules.addmodule({
name: 'route',
init: function () {
$(window).on('hashchange', this.hashchange.bind(this))
this.hashchange()
},
setroute: function (module, routeroot, route) {
view = $('.modulecontent.modulearea')
if (!this.currentmodule || this.currentmodule != module) {
// TODO: better
if (this.currentmodule) {
this.currentmodule.unrender()
}
this.currentmodule = module
view.prop('id', 'module_' + module.name)
module.render(view)
}
module.initroute(route, routeroot)
},
tryroute: function (route) {
var isroot = route.startsWith('/')
var normalroute = isroot ? route.substring(1) : route
var route = normalroute.substr(normalroute.indexOf('/') + 1)
var routeroot = normalroute.substr(0, normalroute.indexOf('/'))
var chosenmodule
if (!normalroute) {
chosenmodule = upload.modules.default
} else {
upload.modules.modules.every(function (module) {
if (!module.route) {
return true
}
if (module.route(routeroot, route)) {
chosenmodule = module
return false
}
return true
})
}
if (!chosenmodule) {
chosenmodule = upload.modules.default
}
setTimeout(this.setroute.bind(this, chosenmodule, routeroot, route), 0)
},
hashchange: function () {
this.tryroute(window.location.hash.substring(1))
}
})
$(function () {
if (typeof upload.config.api_key != 'undefined') {
upload.modules.init()
} else {
alert("Please configure with config.js (see config.js.example) - No API key")
}
})

View file

@ -6,7 +6,7 @@ upload.modules.addmodule({
$(document).on('keydown', this.keypress.bind(this))
},
keypress: function(e) {
if (!this.current || !this.current.is(':visible')) {
if (!this.current) {
return
}