Skip to content

Commit 2efaed6

Browse files
committed
Merge pull request #9 from TeskeVirtualSystem/Loader
Added new loader for JPAK1
2 parents 5cf8b2e + 384e4e7 commit 2efaed6

7 files changed

Lines changed: 1368 additions & 2 deletions

File tree

Gruntfile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ module.exports = function(grunt) {
2929

3030
concat: {
3131
dist: {
32-
src: ["jssrc/*.js" ],
32+
src: ["3rdparty/q/q.js", "jssrc/*.js" ],
3333
dest: "dist/jpak.js"
3434
}
3535
},

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"package"
1313
],
1414
"dependencies" : {
15-
15+
"q": "1.0.1"
1616
},
1717
"license": "MIT",
1818
"ignore": [

jssrc/alpha.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3232
**/
3333

3434
var JPAK = {
35+
Constants: {
36+
verbosity: 3 // 0 - Error, 1 - Warning, 2 - Info, 3 - Debug
37+
},
3538
Generics: {},
3639
Loader: {},
3740
Classes: {},
@@ -62,6 +65,20 @@ var JPAK = {
6265
};
6366
}
6467

68+
69+
/**
70+
* Clean all deletedValue from array
71+
*/
72+
Array.prototype.clean = function(deleteValue) {
73+
for (var i = 0; i < this.length; i++) {
74+
if (this[i] === deleteValue) {
75+
this.splice(i, 1);
76+
i--;
77+
}
78+
}
79+
return this;
80+
};
81+
6582
/*
6683
* Extends the Uint8Array to be able to be converted to a string
6784
*/
@@ -134,5 +151,117 @@ var JPAK = {
134151
this.fromObject(JSON.parse(json));
135152
};
136153

154+
JPAK.Constants.Base64_Encoding = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
155+
156+
/**
157+
* Returns a Base64 String from an ArrayBuffer
158+
* Modified version from https://gist.github.com/jonleighton/958841
159+
*/
160+
JPAK.Tools.ArrayBufferToBase64 = function(arrayBuffer) {
161+
var base64 = '';
162+
163+
var bytes = new Uint8Array(arrayBuffer);
164+
var byteLength = bytes.byteLength;
165+
var byteRemainder = byteLength % 3;
166+
var mainLength = byteLength - byteRemainder;
167+
168+
var a, b, c, d;
169+
var chunk;
170+
171+
// Main loop deals with bytes in chunks of 3
172+
for (var i = 0; i < mainLength; i = i + 3) {
173+
// Combine the three bytes into a single integer
174+
chunk = (bytes[i] << 16) | (bytes[i + 1] << 8) | bytes[i + 2];
175+
176+
// Use bitmasks to extract 6-bit segments from the triplet
177+
a = (chunk & 16515072) >> 18; // 16515072 = (2^6 - 1) << 18
178+
b = (chunk & 258048) >> 12; // 258048 = (2^6 - 1) << 12
179+
c = (chunk & 4032) >> 6; // 4032 = (2^6 - 1) << 6
180+
d = chunk & 63; // 63 = 2^6 - 1
181+
182+
// Convert the raw binary segments to the appropriate ASCII encoding
183+
base64 += JPAK.Constants.Base64_Encoding[a] + JPAK.Constants.Base64_Encoding[b] + JPAK.Constants.Base64_Encoding[c] + JPAK.Constants.Base64_Encoding[d];
184+
}
185+
186+
// Deal with the remaining bytes and padding
187+
if (byteRemainder === 1) {
188+
chunk = bytes[mainLength];
189+
190+
a = (chunk & 252) >> 2; // 252 = (2^6 - 1) << 2
191+
192+
// Set the 4 least significant bits to zero
193+
b = (chunk & 3) << 4; // 3 = 2^2 - 1
194+
195+
base64 += JPAK.Constants.Base64_Encoding[a] + JPAK.Constants.Base64_Encoding[b] + '==';
196+
} else if (byteRemainder === 2) {
197+
chunk = (bytes[mainLength] << 8) | bytes[mainLength + 1];
198+
199+
a = (chunk & 64512) >> 10; // 64512 = (2^6 - 1) << 10
200+
b = (chunk & 1008) >> 4; // 1008 = (2^6 - 1) << 4
201+
202+
// Set the 2 least significant bits to zero
203+
c = (chunk & 15) << 2; // 15 = 2^4 - 1
204+
205+
base64 += JPAK.Constants.Base64_Encoding[a] + JPAK.Constants.Base64_Encoding[b] + JPAK.Constants.Base64_Encoding[c] + '=';
206+
}
207+
208+
return base64;
209+
};
210+
211+
JPAK.Tools.debug = function() {
212+
if (JPAK.Constants.verbosity >= 3) {
213+
[].splice.call(arguments, 0, 0, "(JPAK Debug)");
214+
if (console.debug)
215+
console.debug.apply(console, arguments);
216+
else
217+
console.log.apply(console, arguments);
218+
}
219+
};
220+
221+
JPAK.Tools.error = function() {
222+
if (JPAK.Constants.verbosity >= 0) {
223+
[].splice.call(arguments, 0, 0, "(JPAK Error)");
224+
if (console.error)
225+
console.error.apply(console, arguments);
226+
else
227+
console.log.apply(console, arguments);
228+
}
229+
};
230+
231+
JPAK.Tools.warning = function() {
232+
if (JPAK.Constants.verbosity >= 1) {
233+
[].splice.call(arguments, 0, 0, "(JPAK Warning)");
234+
console.log.apply(console, arguments);
235+
}
236+
};
237+
238+
JPAK.Tools.info = function() {
239+
if (JPAK.Constants.verbosity >= 2) {
240+
[].splice.call(arguments, 0, 0, "(JPAK Info)");
241+
if (console.info)
242+
console.info.apply(console, arguments);
243+
else
244+
console.log.apply(console, arguments);
245+
}
246+
};
247+
248+
JPAK.Tools.d = JPAK.Tools.debug;
249+
JPAK.Tools.e = JPAK.Tools.error;
250+
JPAK.Tools.w = JPAK.Tools.warning;
251+
JPAK.Tools.i = JPAK.Tools.info;
252+
JPAK.Tools.l = JPAK.Tools.info;
253+
JPAK.Tools.log = JPAK.Tools.info;
254+
255+
JPAK.Constants.MAGIC_TYPE = {
256+
"JPAK1": 0,
257+
"JMS1": 1,
258+
"JDS1": 2
259+
};
260+
261+
JPAK.Constants.REVERSE_MAGIC_TYPE = {
262+
0: "JPAK1",
263+
1: "JMS1",
264+
2: "JDS1"
265+
};
137266

138267
})();

jssrc/data-loader.js

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/**
2+
_ ____ _ _ __ ____ ___
3+
| | _ \ / \ | |/ / __ _|___ \ / _ \
4+
_ | | |_) / _ \ | ' / \ \ / / __) || | | |
5+
| |_| | __/ ___ \| . \ \ V / / __/ | |_| |
6+
\___/|_| /_/ \_\_|\_\ \_/ |_____(_)___/
7+
8+
Multiuse Javascript Package
9+
https://github.com/TeskeVirtualSystem/jpak
10+
11+
The MIT License (MIT)
12+
13+
Copyright (c) 2013 Lucas Teske
14+
15+
Permission is hereby granted, free of charge, to any person obtaining a copy of
16+
this software and associated documentation files (the "Software"), to deal in
17+
the Software without restriction, including without limitation the rights to
18+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
19+
the Software, and to permit persons to whom the Software is furnished to do so,
20+
subject to the following conditions:
21+
22+
The above copyright notice and this permission notice shall be included in all
23+
copies or substantial portions of the Software.
24+
25+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
27+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
28+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
29+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31+
32+
**/
33+
34+
(function() {
35+
36+
var inNode = (typeof module !== 'undefined' && typeof module.exports !== 'undefined');
37+
38+
if (!inNode) {
39+
var DataLoader = function(parameters) {
40+
var _this = this;
41+
this.xhr = new XMLHttpRequest();
42+
this.method = parameters.method || "GET";
43+
this.url = parameters.url;
44+
this.responseType = parameters.responseType || "arraybuffer";
45+
this.partial = parameters.partial || false;
46+
this.partialFrom = parameters.partialFrom || 0;
47+
this.partialTo = parameters.partialTo || 0;
48+
this.fetchSize = parameters.fetchSize || false;
49+
50+
this.callbacks = {
51+
"load" : [],
52+
"error" : [],
53+
"progress": []
54+
};
55+
56+
this.xhr.onprogress = function(e) {
57+
if (e.lengthComputable && _this.onprogress !== undefined) {
58+
var percentComplete = (( (e.loaded / e.total)*10000 ) >> 0)/100; // Rounded percent to two decimal
59+
_this._reportProgress({"loaded":e.loaded,"total":e.total,"percent": percentComplete});
60+
}
61+
};
62+
63+
this.xhr.onload = function(e) {
64+
if (this.status >= 200 && this.status < 300) {
65+
if (_this.fetchSize)
66+
_this._reportLoad(parseInt(this.getResponseHeader("Content-Length")));
67+
else
68+
_this._reportLoad(this.response);
69+
70+
} else
71+
_this._reportError({"text":"Error loading file! HTTP Status Code: "+this.status,"errorcode": this.status});
72+
};
73+
74+
this.xhr.onreadystatechange = function(e) {
75+
if (this.readyState === 4 && (this.status < 200 || this.status >= 300)) {
76+
JPAK.Tools.e("Error loading url "+_this.url+" ("+this.status+"): "+this.statusText);
77+
_this._reportError({"text": this.statusText, "errorcode": this.status});
78+
}
79+
};
80+
};
81+
82+
DataLoader.prototype._reportProgress = function(progress) {
83+
for (var cb in this.callbacks.progress)
84+
this.callbacks.progress[cb](progress);
85+
this.def.notify(progress);
86+
};
87+
88+
DataLoader.prototype._reportError = function(error) {
89+
for (var cb in this.callbacks.error)
90+
this.callbacks.error[cb](error);
91+
this.def.reject(error);
92+
};
93+
94+
DataLoader.prototype._reportLoad = function(data) {
95+
for (var cb in this.callbacks.load)
96+
this.callbacks.load[cb](data);
97+
this.def.resolve(data);
98+
};
99+
100+
DataLoader.prototype.start = function() {
101+
if (this.fetchSize) {
102+
this.method = "HEAD";
103+
this.partial = false;
104+
}
105+
this.xhr.open(this.method, this.url, true);
106+
this.xhr.responseType = this.responseType;
107+
if (this.partial)
108+
this.xhr.setRequestHeader("Range", "bytes="+this.partialFrom+"-"+this.partialTo);
109+
this.def = Q.defer();
110+
this.xhr.send();
111+
return this.def.promise;
112+
};
113+
114+
DataLoader.prototype.on = function(event, cb) {
115+
if (event in this.callbacks)
116+
this.callbacks[event].push(function(data) {
117+
cb(data);
118+
});
119+
};
120+
121+
JPAK.Tools.DataLoader = DataLoader;
122+
}
123+
124+
})();

0 commit comments

Comments
 (0)