Port code into backbone/rivets land.
Showing
7 changed files
with
312 additions
and
0 deletions
.gitignore
0 → 100644
Gruntfile.js
0 → 100644
1 | /* global module */ | ||
2 | |||
3 | module.exports = function (grunt) { | ||
4 | /* global require */ | ||
5 | 'use strict'; | ||
6 | |||
7 | var config = {}; | ||
8 | config.base = 'src'; | ||
9 | config.jshint = { | ||
10 | options: { | ||
11 | }, | ||
12 | browserOptions: { | ||
13 | }, | ||
14 | }; | ||
15 | config.jscs = { | ||
16 | options: { | ||
17 | validateIndentation: 4, | ||
18 | reporter: 'console', | ||
19 | maxErrors: -1, | ||
20 | }, | ||
21 | }; | ||
22 | config.bower = { | ||
23 | directory: 'lib/bower', | ||
24 | }; | ||
25 | config.jasmine = { | ||
26 | withCoverage: true, | ||
27 | }; | ||
28 | var montyPython = require('grunt-monty-python')(grunt); | ||
29 | montyPython.createConfig(config); | ||
30 | }; |
bower.json
0 → 100644
1 | { | ||
2 | "name": "frontend-uploads", | ||
3 | "version": "2016.07.06-0", | ||
4 | "authors": [ | ||
5 | "Adam Heath <doogie@brainfood.com>" | ||
6 | ], | ||
7 | "main": [ | ||
8 | "src/scripts/Upload.js" | ||
9 | ], | ||
10 | "private": true, | ||
11 | "ignore": [ | ||
12 | "**/.*", | ||
13 | "node_modules", | ||
14 | "src/lib", | ||
15 | "src/scripts/*.spec.js", | ||
16 | "src/scripts/config.js", | ||
17 | "src/scripts/main.js" | ||
18 | ], | ||
19 | "dependencies": { | ||
20 | "requirejs": "", | ||
21 | "jquery": "", | ||
22 | "underscore": "", | ||
23 | "backbone": "", | ||
24 | "rivets": "", | ||
25 | "flow": "" | ||
26 | } | ||
27 | } |
package.json
0 → 100644
1 | { | ||
2 | "name": "frontend-uploads", | ||
3 | "version": "2016.07.06-0", | ||
4 | "main": [ | ||
5 | "src/scripts/Upload.js" | ||
6 | ], | ||
7 | "dependencies": { | ||
8 | }, | ||
9 | "devDependencies": { | ||
10 | "bower-requirejs": "~0.9.2", | ||
11 | "grunt": "~0", | ||
12 | "grunt-monty-python": "git+ssh://git@gitlab.brainfood.com:brainfood/grunt-monty-python.git" | ||
13 | }, | ||
14 | "engines": { | ||
15 | "node": ">=0.8.0" | ||
16 | } | ||
17 | } | ||
18 |
src/scripts/Upload.js
0 → 100644
1 | define(function(require) { | ||
2 | 'use strict'; | ||
3 | |||
4 | var $ = require('jquery'); | ||
5 | var Backbone = require('backbone'); | ||
6 | var Flow = require('flow'); | ||
7 | |||
8 | var Status = Backbone.Model.extend({ | ||
9 | defaults: function() { | ||
10 | return { | ||
11 | result: null, | ||
12 | resultData: null, | ||
13 | sent: null, | ||
14 | completion: null, | ||
15 | total: null, | ||
16 | }; | ||
17 | }, | ||
18 | }); | ||
19 | var Upload = Backbone.Model.extend({ | ||
20 | defaults: function() { | ||
21 | return { | ||
22 | name: null, | ||
23 | status: new Status(), | ||
24 | date: null, | ||
25 | size: null, | ||
26 | }; | ||
27 | }, | ||
28 | initialize: function() { | ||
29 | Upload.__super__.initialize.apply(this, arguments); | ||
30 | _.each(['retry', 'pause', 'resume', 'cancel'], function(methodName) { | ||
31 | this[methodName] = _.bind(function(e) { | ||
32 | if (e) { | ||
33 | e.preventDefault(); | ||
34 | } | ||
35 | var flowFile = this.flowFile; | ||
36 | if (flowFile) { | ||
37 | flowFile[methodName].call(flowFile); | ||
38 | } | ||
39 | if (flowFile.paused) { | ||
40 | this.get('status').set({ | ||
41 | result: 'paused', | ||
42 | resultData: null, | ||
43 | }); | ||
44 | } | ||
45 | return false; | ||
46 | }, this); | ||
47 | }, this); | ||
48 | }, | ||
49 | parse: function(data, options) { | ||
50 | data = _.extend({}, data); | ||
51 | var status = data.status; | ||
52 | if (!(status instanceof Backbone.Model)) { | ||
53 | data.status = new Status(status); | ||
54 | } | ||
55 | return data; | ||
56 | }, | ||
57 | }); | ||
58 | var Uploads = Backbone.Collection.extend({ | ||
59 | model: Upload, | ||
60 | initialize: function initialize(data, options) { | ||
61 | Uploads.__super__.initialize.apply(this, arguments); | ||
62 | }, | ||
63 | }); | ||
64 | |||
65 | function makeFallbackFlowEventHandler(eventName) { | ||
66 | return function() { | ||
67 | console.log('flow.' + eventName, arguments); | ||
68 | }; | ||
69 | } | ||
70 | |||
71 | _.extend(Upload, { | ||
72 | Uploads: Uploads, | ||
73 | Status: Status, | ||
74 | flowEventHandlers: { | ||
75 | fileAdded: function(flowFile) { | ||
76 | var model = flowFile._model; | ||
77 | if (!model) { | ||
78 | model = flowFile._model = new Upload({ | ||
79 | name: flowFile.name, | ||
80 | size: flowFile.size, | ||
81 | startDate: new Date(), | ||
82 | }); | ||
83 | model.flowFile = flowFile; | ||
84 | this.model.get('collection').add(model); | ||
85 | } | ||
86 | return true; | ||
87 | }, | ||
88 | filesAdded: function() { | ||
89 | return true; | ||
90 | }, | ||
91 | filesSubmitted: function() { | ||
92 | this.flow.upload(); | ||
93 | }, | ||
94 | fileProgress: function(flowFile) { | ||
95 | var model = flowFile._model; | ||
96 | if (model) { | ||
97 | model.get('status').set({ | ||
98 | result: 'uploading', | ||
99 | resultData: null, | ||
100 | completion: Math.floor(flowFile.progress() * 100), | ||
101 | size: flowFile.sizeUploaded(), | ||
102 | }); | ||
103 | } | ||
104 | }, | ||
105 | fileSuccess: function(flowFile, message, chunk) { | ||
106 | var model = flowFile._model; | ||
107 | if (model) { | ||
108 | model.get('status').set({ | ||
109 | result: 'success', | ||
110 | resultData: message, | ||
111 | }); | ||
112 | } | ||
113 | }, | ||
114 | fileError: function(flowFile, message, chunk) { | ||
115 | var model = flowFile._model; | ||
116 | if (model) { | ||
117 | model.get('status').set({ | ||
118 | result: 'error', | ||
119 | resultData: message, | ||
120 | }); | ||
121 | } | ||
122 | }, | ||
123 | fileRemoved: function(flowFile) { | ||
124 | var model = flowFile._model; | ||
125 | if (model) { | ||
126 | this.model.get('collection').remove(model); | ||
127 | } | ||
128 | }, | ||
129 | }, | ||
130 | rivetsComponent: { | ||
131 | static: [ | ||
132 | 'accept', | ||
133 | 'allowDuplicateUploads', | ||
134 | 'maxChunkRetries', | ||
135 | 'maxFilesize', | ||
136 | 'prioritizeFirstAndLastChunk', | ||
137 | 'simultaneousUploads', | ||
138 | ], | ||
139 | template: function() { | ||
140 | return this.el.innerHTML; | ||
141 | }, | ||
142 | initialize: function initialize(el, locals) { | ||
143 | var instance = {el: el}; | ||
144 | var model = instance.model = new Backbone.Model(locals); | ||
145 | _.each(this.observers, function(observer, key) { | ||
146 | sightglass(observer.obj, observer.keypath, function() { | ||
147 | model.set(key, observer.value()); | ||
148 | }, observer.options); | ||
149 | }); | ||
150 | var flow = instance.flow = new Flow({ | ||
151 | target: _.bind(function(flowFile, flowChunk, isTest) { | ||
152 | var model = this.model; | ||
153 | var target = model.get('target'); | ||
154 | var sessionId = model.get('sessionId'); | ||
155 | if (target) { | ||
156 | if (target.indexOf('?') !== -1) { | ||
157 | target += '&'; | ||
158 | } else { | ||
159 | target += '?'; | ||
160 | } | ||
161 | } else { | ||
162 | target = '?'; | ||
163 | } | ||
164 | return target + 'sessionId=' + sessionId; | ||
165 | }, instance), | ||
166 | allowDuplicateUploads: locals.allowDuplicateUploads, | ||
167 | maxChunkRetries: locals.maxChunkRetries, | ||
168 | maxFilesize: locals.maxFilesize, | ||
169 | prioritizeFirstAndLastChunk: locals.prioritizeFirstAndLastChunk, | ||
170 | simultaneousUploads: locals.simultaneousUploads, | ||
171 | }); | ||
172 | _.each([ | ||
173 | 'fileSuccess', | ||
174 | 'fileProgress', | ||
175 | 'fileAdded', | ||
176 | 'filesAdded', | ||
177 | 'filesSubmitted', | ||
178 | 'fileRemoved', | ||
179 | 'fileRetry', | ||
180 | 'fileError', | ||
181 | //'uploadStart', | ||
182 | //'complete', | ||
183 | //'progress', | ||
184 | //'error', | ||
185 | ], function(eventName) { | ||
186 | var eventHandler = Upload.flowEventHandlers[eventName] || makeFallbackFlowEventHandler(eventName); | ||
187 | flow.on(eventName, _.bind(eventHandler, instance)); | ||
188 | }); | ||
189 | flow.assignBrowse(el, true, false, { | ||
190 | accept: model.get('accept'), | ||
191 | }); | ||
192 | flow.assignDrop(el); | ||
193 | instance.view = rivets.bind($(el).contents(), this.view.models); | ||
194 | return {}; | ||
195 | }, | ||
196 | }, | ||
197 | }); | ||
198 | |||
199 | return Upload; | ||
200 | }); |
src/scripts/config.js
0 → 100644
1 | /* global require:true */ | ||
2 | var require; | ||
3 | require = (function() { | ||
4 | 'use strict'; | ||
5 | |||
6 | var require = { | ||
7 | baseUrl: 'scripts', | ||
8 | shim: { | ||
9 | backbone: { | ||
10 | deps: ['underscore'], | ||
11 | exports: 'Backbone', | ||
12 | }, | ||
13 | underscore: { | ||
14 | exports: '_', | ||
15 | }, | ||
16 | }, | ||
17 | paths: { | ||
18 | flow: '../lib/bower/underscore/underscore', | ||
19 | jquery: '../lib/bower/jquery/dist/jquery', | ||
20 | underscore: '../lib/bower/underscore/underscore', | ||
21 | backbone: '../lib/bower/backbone/backbone', | ||
22 | }, | ||
23 | }; | ||
24 | |||
25 | return require; | ||
26 | })(); |
src/scripts/main.js
0 → 100644
-
Please register or sign in to post a comment