-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.js
122 lines (105 loc) · 2.88 KB
/
model.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
115
116
117
118
119
120
121
122
afSlingshot.Model = function(config, data, multi) {
this._dep = new Deps.Dependency();
var self = this;
self._uploads = {};
self._initConfig(config, multi);
if (data) {
_.each(_.uniq(_.keys(data)),function(filename) {
var fileDirectives = _.filter(data, function(d) {
return d[0].filename === filename;
});
self._uploads[ filename ] = new afSlingshot.FileRecord(
fileDirectives[0],
self._config
);
});
}
};
// This takes settings and makes some inferences, so we don't have to do it later
afSlingshot.Model.prototype._initConfig = function(config, multi) {
config = _.extend({},config);
// if multi is undefined default it to true;
multi = (multi === undefined) ? true : multi;
if (multi) {
_.defaults(config, {
replaceOnChange: false
});
}else {
_.defaults(config, {
replaceOnChange: true
});
}
this._config = config;
};
afSlingshot.Model.prototype.add = function(fsFiles) {
var self = this;
// Iterate through all directives and all files and upload them
var files = {};
_.each(fsFiles, function(fsFile) {
files[ fsFile.name ] = new afSlingshot.FileRecord( fsFile, self._config, true );
});
if (this._config.replaceOnChange) {
_.each( this._uploads,function( u ){
self.remove( u.name );
});
this._uploads = files;
}else {
this._uploads = _.extend(files, this._uploads);
}
this._dep.changed();
};
/*
* Return progress of file upload.
*/
afSlingshot.Model.prototype.progress = function progress(name) {
this._dep.depend();
var self = this;
var min;
if (!name) {
// If name wasn't defined, recurse and find min of al uploads
min = _.min(_.map(self._uploads, function(v, k) {
return v.progress();
}));
return min;
}else if (self._uploads[name]) {
return self._uploads[name].progress() * 100;
}
};
afSlingshot.Model.prototype.remove = function(name) {
if( this._config.onRemove ){
this._config.onRemove( this._uploads[ name ] );
}
delete this._uploads[ name ];
this._dep.changed();
};
afSlingshot.Model.prototype.downloadAll = function() {
this._dep.depend();
var self = this;
_.each(this.files(),function(f) {
f.download();
});
};
afSlingshot.Model.prototype.file = function(filename) {
this._dep.depend();
return this._uploads[ filename ];
};
afSlingshot.Model.prototype.files = function() {
this._dep.depend();
var files = _.map(this._uploads, function(v, k) {
return v;
});
return files;
};
afSlingshot.Model.prototype.data = function() {
this._dep.depend();
var data = [];
_.each(this._uploads, function(v) {
data = data.concat(v.getData());
});
return data;
};
afSlingshot.Model.prototype.allowedFileTypes = function() {
var firstDirective = this._config.directives[0];
var restriction = Slingshot.getRestrictions( firstDirective.name );
return restriction.allowedFileTypes;
};