forked from draeton/stitches
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.js
93 lines (81 loc) · 2.83 KB
/
file.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
// ## Stitches.File
//
// [http://draeton.github.com/stitches](http://draeton.github.com/stitches)
//
// Copyright 2011, Matthew Cobbs
// Licensed under the MIT license.
//
/*global jQuery, Stitches */
(function (window, Stitches, $) {
"use strict";
// ## Stitches.File namespace
//
// Holds all File procesing methods
Stitches.File = (function () {
/* shortcut */
var S = window.Stitches;
/* track files to read */
var readQueue = [];
return {
// ### queueFiles
//
// Loops through `files`; adds an image to the `readQueue`
//
// @param {FileList} files From a drop event
queueFiles: function (files) {
$.each(files, function (i, file) {
if (/jpeg|png|gif/.test(file.type)) {
readQueue.push(file);
S.pub("file.queue.done", file);
}
});
},
// ### queueIcons
//
// Read in a file from the `readQueue`. Starts up a new `FileReader`
// to read in the image as data and create a new `Icon`
queueIcons: function () {
var file, reader;
file = readQueue.shift();
if (file) {
try {
reader = new FileReader();
reader.onloadend = function (e) {
/* create an icon and add to the icon queue */
var icon = new S.Icon(file.name, e.target.result);
S.iconQueue.push(icon);
/* notify */
S.pub("file.icon.done", icon);
};
reader.readAsDataURL(file);
} catch (e) {
S.pub("page.error", e);
}
}
},
// ### unqueueIcon
//
// Removes an icon from the queue
//
// @param {Icon} icon
unqueueIcon: function (icon) {
/* remove the icon from the queue */
S.iconQueue = $.grep(S.iconQueue, function (item) {
return item !== icon;
});
S.Icon.clearNameCache(icon.name);
/* notify */
S.pub("file.remove.done", icon);
},
// ### unqueueAllIcons
//
// Clear all icons from the queue
unqueueAllIcons: function () {
$.each(S.iconQueue, function (i, icon) {
S.File.unqueueIcon(icon);
});
S.Icon.clearNameCache();
}
};
})();
})(window, Stitches, jQuery);