[CSS] +fix page header and title color
[lhc/web/www.git] / www / plugins / crayons / js / jquery.html5uploader.js
1 (function ($) {
2
3 $.fn.html5Uploader = function (options) {
4
5 var crlf = '\r\n';
6 var boundary = "iloveigloo";
7 var dashes = "--";
8
9 var settings = {
10 "name": "uploadedFile",
11 "postUrl": "Upload.aspx",
12 "onClientAbort": null,
13 "onClientError": null,
14 "onClientLoad": null,
15 "onClientLoadEnd": null,
16 "onClientLoadStart": null,
17 "onClientProgress": null,
18 "onServerAbort": null,
19 "onServerError": null,
20 "onServerLoad": null,
21 "onServerLoadStart": null,
22 "onServerProgress": null,
23 "onServerReadyStateChange": null,
24 "onSuccess": null
25 };
26
27 if (options) {
28 $.extend(settings, options);
29 }
30
31 return this.each(function (options) {
32 var $this = $(this);
33 if ($this.is("[type='file']")) {
34 $this
35 .bind("change", function () {
36 var files = this.files;
37 for (var i = 0; i < files.length; i++) {
38 fileHandler(files[i]);
39 }
40 });
41 } else {
42 $this
43 .bind("dragenter dragover", function () {
44 $(this).addClass("hover");
45 return false;
46 })
47 .bind("dragleave", function () {
48 $(this).removeClass("hover");
49 return false;
50 })
51 .bind("drop", function (e) {
52 $(this).removeClass("hover");
53 var files = e.originalEvent.dataTransfer.files;
54 for (var i = 0; i < files.length; i++) {
55 fileHandler(files[i]);
56 }
57 return false;
58 });
59 }
60 });
61
62 function fileHandler(file) {
63 var fileReader = new FileReader();
64 fileReader.onabort = function (e) {
65 if (settings.onClientAbort) {
66 settings.onClientAbort(e, file);
67 }
68 };
69 fileReader.onerror = function (e) {
70 if (settings.onClientError) {
71 settings.onClientError(e, file);
72 }
73 };
74 fileReader.onload = function (e) {
75 if (settings.onClientLoad) {
76 settings.onClientLoad(e, file);
77 }
78 };
79 fileReader.onloadend = function (e) {
80 if (settings.onClientLoadEnd) {
81 settings.onClientLoadEnd(e, file);
82 }
83 };
84 fileReader.onloadstart = function (e) {
85 if (settings.onClientLoadStart) {
86 settings.onClientLoadStart(e, file);
87 }
88 };
89 fileReader.onprogress = function (e) {
90 if (settings.onClientProgress) {
91 settings.onClientProgress(e, file);
92 }
93 };
94 fileReader.readAsDataURL(file);
95
96 var xmlHttpRequest = new XMLHttpRequest();
97 xmlHttpRequest.upload.onabort = function (e) {
98 if (settings.onServerAbort) {
99 settings.onServerAbort(e, file);
100 }
101 };
102 xmlHttpRequest.upload.onerror = function (e) {
103 if (settings.onServerError) {
104 settings.onServerError(e, file);
105 }
106 };
107 xmlHttpRequest.upload.onload = function (e) {
108 if (settings.onServerLoad) {
109 settings.onServerLoad(e, file);
110 }
111 };
112 xmlHttpRequest.upload.onloadstart = function (e) {
113 if (settings.onServerLoadStart) {
114 settings.onServerLoadStart(e, file);
115 }
116 };
117 xmlHttpRequest.upload.onprogress = function (e) {
118 if (settings.onServerProgress) {
119 settings.onServerProgress(e, file);
120 }
121 };
122 xmlHttpRequest.onreadystatechange = function (e) {
123 if (settings.onServerReadyStateChange) {
124 settings.onServerReadyStateChange(e, file, xmlHttpRequest.readyState);
125 }
126 if (settings.onSuccess && xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
127 settings.onSuccess(e, file, xmlHttpRequest.responseText);
128 }
129 };
130 xmlHttpRequest.open("POST", settings.postUrl, true);
131
132 if (file.getAsBinary) { // Firefox
133
134 var data = dashes + boundary + crlf +
135 "Content-Disposition: form-data;" +
136 "name=\"" + settings.name + "\";" +
137 "filename=\"" + unescape(encodeURIComponent(file.name)) + "\"" + crlf +
138 "Content-Type: application/octet-stream" + crlf + crlf +
139 file.getAsBinary() + crlf +
140 dashes + boundary + dashes;
141
142 xmlHttpRequest.setRequestHeader("Content-Type", "multipart/form-data;boundary=" + boundary);
143 xmlHttpRequest.sendAsBinary(data);
144
145 } else if (window.FormData) { // Chrome
146
147 var formData = new FormData();
148 formData.append(settings.name, file);
149
150 xmlHttpRequest.send(formData);
151
152 }
153 }
154
155 };
156
157 })(jQuery);