diff options
author | Mike Crute <mcrute@gmail.com> | 2015-05-18 20:22:56 -0700 |
---|---|---|
committer | Mike Crute <mcrute@gmail.com> | 2015-05-18 20:22:56 -0700 |
commit | bbca1d6b3875df82af3b2c11b57efef2fb8e319d (patch) | |
tree | ddf5dfb59c0619124ad9d140e8ff6e25bf6f8f8c /static/dnd-upload.js | |
download | moin_mcrute_theme-bbca1d6b3875df82af3b2c11b57efef2fb8e319d.tar.bz2 moin_mcrute_theme-bbca1d6b3875df82af3b2c11b57efef2fb8e319d.tar.xz moin_mcrute_theme-bbca1d6b3875df82af3b2c11b57efef2fb8e319d.zip |
Diffstat (limited to 'static/dnd-upload.js')
-rw-r--r-- | static/dnd-upload.js | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/static/dnd-upload.js b/static/dnd-upload.js new file mode 100644 index 0000000..2623cc2 --- /dev/null +++ b/static/dnd-upload.js | |||
@@ -0,0 +1,173 @@ | |||
1 | function template(text, data) { | ||
2 | var render; | ||
3 | var escaper = /\\|'|\r|\n|\t|\u2028|\u2029/g; | ||
4 | var settings = { | ||
5 | evaluate : /<%([\s\S]+?)%>/g, | ||
6 | interpolate : /<%=([\s\S]+?)%>/g | ||
7 | }; | ||
8 | |||
9 | // Combine delimiters into one regular expression via alternation. | ||
10 | var matcher = new RegExp([ | ||
11 | (settings.interpolate || noMatch).source, | ||
12 | (settings.evaluate || noMatch).source | ||
13 | ].join('|') + '|$', 'g'); | ||
14 | |||
15 | // Compile the template source, escaping string literals appropriately. | ||
16 | var index = 0; | ||
17 | var source = "__p+='"; | ||
18 | text.replace(matcher, function(match, interpolate, evaluate, offset) { | ||
19 | source += text.slice(index, offset) | ||
20 | .replace(escaper, function(match) { return '\\' + escapes[match]; }); | ||
21 | |||
22 | if (interpolate) { | ||
23 | source += "'+\n((__t=(" + interpolate + "))==null?'':__t)+\n'"; | ||
24 | } | ||
25 | if (evaluate) { | ||
26 | source += "';\n" + evaluate + "\n__p+='"; | ||
27 | } | ||
28 | index = offset + match.length; | ||
29 | return match; | ||
30 | }); | ||
31 | source += "';\n"; | ||
32 | |||
33 | source = "var __t,__p='',__j=Array.prototype.join," + | ||
34 | "print=function(){__p+=__j.call(arguments,'');};\n" + | ||
35 | source + "return __p;\n"; | ||
36 | |||
37 | try { | ||
38 | render = new Function('obj', source); | ||
39 | } catch (e) { | ||
40 | e.source = source; | ||
41 | throw e; | ||
42 | } | ||
43 | |||
44 | if (data) return render(data); | ||
45 | var template = function(data) { | ||
46 | return render.call(this, data); | ||
47 | }; | ||
48 | |||
49 | // Provide the compiled function source as a convenience for precompilation. | ||
50 | template.source = 'function(obj){\n' + source + '}'; | ||
51 | |||
52 | return template; | ||
53 | } | ||
54 | |||
55 | var tests = { | ||
56 | filereader: typeof FileReader != 'undefined', | ||
57 | dnd: 'draggable' in document.createElement('span'), | ||
58 | formdata: !!window.FormData, | ||
59 | progress: "upload" in new XMLHttpRequest | ||
60 | }; | ||
61 | |||
62 | var UploadTemplate = template( | ||
63 | '<div>' + | ||
64 | '<label class="dnd-overwrite">Overwrite files ' + | ||
65 | '<input type="checkbox" name="overwrite" id="overwrite-check" checked />' + | ||
66 | '</label>' + | ||
67 | '<div id="dnd-upload"></div>' + | ||
68 | '</div>' | ||
69 | ); | ||
70 | |||
71 | var oldOnReady = document.onreadystatechange; | ||
72 | document.onreadystatechange = function() { | ||
73 | if (document.readyState !== "complete") { | ||
74 | return; | ||
75 | } | ||
76 | |||
77 | if (typeof oldOnReady === "function") { | ||
78 | oldOnReady(); | ||
79 | } | ||
80 | |||
81 | if (document.title.indexOf("Attachments for ") == -1) { | ||
82 | return; | ||
83 | } | ||
84 | |||
85 | for (key in tests) { | ||
86 | if (tests[key] !== true) { | ||
87 | return; | ||
88 | } | ||
89 | } | ||
90 | |||
91 | document.getElementsByTagName("h2")[0].insertAdjacentHTML('afterend', UploadTemplate()); | ||
92 | var holder = document.getElementById('dnd-upload'); | ||
93 | |||
94 | holder.ondragover = function () { | ||
95 | this.className = 'hover'; | ||
96 | return false; | ||
97 | }; | ||
98 | |||
99 | holder.ondragend = function () { | ||
100 | this.className = ''; | ||
101 | return false; | ||
102 | }; | ||
103 | |||
104 | holder.ondrop = function (e) { | ||
105 | this.className = ''; | ||
106 | e.preventDefault(); | ||
107 | readfiles(e.dataTransfer.files); | ||
108 | } | ||
109 | }; | ||
110 | |||
111 | function createElement(tag, attributes, innerHTML) { | ||
112 | var element = document.createElement(tag); | ||
113 | |||
114 | for (key in attributes) { | ||
115 | element.setAttribute(key, attributes[key]); | ||
116 | } | ||
117 | |||
118 | if (typeof innerHTML !== "undefined") { | ||
119 | element.innerHTML = innerHTML; | ||
120 | } | ||
121 | |||
122 | return element; | ||
123 | }; | ||
124 | |||
125 | function postFile(file) { | ||
126 | var size = file.size; | ||
127 | var units = ['G', 'M', 'K', 'B']; | ||
128 | var xhr = new XMLHttpRequest(); | ||
129 | var formData = new FormData(); | ||
130 | |||
131 | formData.append('ticket', document.getElementsByName("ticket")[0].value); | ||
132 | formData.append('action', 'AttachFile'); | ||
133 | formData.append('do', 'upload'); | ||
134 | formData.append('file', file); | ||
135 | |||
136 | if (document.getElementById("overwrite-check").checked) { | ||
137 | formData.append('overwrite', '1'); | ||
138 | } | ||
139 | |||
140 | while (size >= 1024) { | ||
141 | size = size / 1024 | 0; | ||
142 | units.pop(); | ||
143 | } | ||
144 | |||
145 | var progress = createElement('progress', { min: 0, max: 100, value: 0 }); | ||
146 | var progContainer = createElement('div', { "class": "progressbar" }); | ||
147 | var sizeSpan = createElement("span", { "class": "size" }, size + units[units.length - 1]); | ||
148 | var item = createElement('li'); | ||
149 | |||
150 | progContainer.appendChild(progress); | ||
151 | progContainer.appendChild(sizeSpan); | ||
152 | |||
153 | item.appendChild(createElement('span', { "class": "filename" }, file.name)); | ||
154 | item.appendChild(progContainer); | ||
155 | |||
156 | document.getElementById("dnd-upload").appendChild(item); | ||
157 | |||
158 | xhr.open('POST', window.location.pathname); | ||
159 | xhr.onload = function() { progress.value = 100; }; | ||
160 | |||
161 | xhr.upload.onprogress = function (event) { | ||
162 | if (!event.lengthComputable) { return; } | ||
163 | progress.value = (event.loaded / event.total * 100 | 0); | ||
164 | } | ||
165 | |||
166 | xhr.send(formData); | ||
167 | } | ||
168 | |||
169 | function readfiles(files) { | ||
170 | for (var i = 0; i < files.length; i++) { | ||
171 | postFile(files[i]); | ||
172 | } | ||
173 | } | ||