-
Notifications
You must be signed in to change notification settings - Fork 329
/
main.js
114 lines (100 loc) · 3 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
;(function($, window, document, undefined) {
window.method = null;
$(document).ready(function() {
var input = $('#input');
var output = $('#output');
var checkbox = $('#auto-update');
var dropzone = $('#droppable-zone');
var option = $('[data-option]');
var execute = function() {
try {
output.val(method(input.val(), option.val()));
} catch(e) {
output.val(e);
}
}
function autoUpdate() {
if(!checkbox[0].checked) {
return;
}
execute();
}
if(checkbox.length > 0) {
input.bind('input propertychange', autoUpdate);
option.bind('input propertychange', autoUpdate);
checkbox.click(autoUpdate);
}
if(dropzone.length > 0) {
var dropzonetext = $('#droppable-zone-text');
$(document.body).bind('dragover drop', function(e) {
e.preventDefault();
return false;
});
if(!window.FileReader) {
dropzonetext.text('Your browser dose not support.');
$('input').attr('disabled', true);
return;
}
dropzone.bind('dragover', function() {
dropzone.addClass('hover');
});
dropzone.bind('dragleave', function() {
dropzone.removeClass('hover');
});
dropzone.bind('drop', function(e) {
dropzone.removeClass('hover');
file = e.originalEvent.dataTransfer.files[0];
dropzonetext.text(file.name);
autoUpdate();
});
input.bind('change', function() {
file = input[0].files[0];
dropzonetext.text(file.name);
autoUpdate();
});
var file;
execute = function() {
reader = new FileReader();
var value = option.val();
if (method.update) {
var batch = 1024 * 1024 * 2;
var start = 0;
var total = file.size;
var current = method;
reader.onload = function (event) {
try {
current = current.update(event.target.result, value);
asyncUpdate();
} catch(e) {
output.val(e);
}
};
var asyncUpdate = function () {
if (start < total) {
output.val('hashing...' + (start / total * 100).toFixed(2) + '%');
var end = Math.min(start + batch, total);
reader.readAsArrayBuffer(file.slice(start, end));
start = end;
} else {
output.val(current.hex());
}
};
asyncUpdate();
} else {
output.val('hashing...');
reader.onload = function (event) {
try {
output.val(method(event.target.result, value));
} catch (e) {
output.val(e);
}
};
reader.readAsArrayBuffer(file);
}
};
}
$('#execute').click(execute);
var parts = location.pathname.split('/');
$('a[href="' + parts[parts.length - 1] + '"]').addClass('active');
});
})(jQuery, window, document);