This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Cup.j
1407 lines (1118 loc) · 43 KB
/
Cup.j
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Cup.j
* Cup
*
* Created by Aparajita Fishman on February 3, 2013.
* Copyright 2013, Filmworkers Club. All rights reserved.
*/
@import <Foundation/CPDictionary.j>
@import <Foundation/CPNumberFormatter.j>
@import <Foundation/CPRunLoop.j>
@import <Foundation/CPTimer.j>
@import <AppKit/CPAlert.j>
@import <AppKit/CPArrayController.j>
@import <AppKit/CPCompatibility.j>
@import <AppKit/CPPlatform.j>
@import <AppKit/CPPlatformWindow.j>
@import <AppKit/CPTableView.j>
@global jQuery
@typedef jQueryEvent
CupFileStatusPending = 0;
CupFileStatusUploading = 1;
CupFileStatusComplete = 2;
/*
These constants are bit flags passed to the cup:didFilterFile:because:
delegate method, indicating why the file was rejected.
*/
CupFilteredName = 1 << 0;
CupFilteredSize = 1 << 1;
var FileStatuses = [];
var baseWidgetId = @"Cup_input",
delegateFilter = 1 << 0,
delegateWillAdd = 1 << 1,
delegateAdd = 1 << 2,
delegateSubmit = 1 << 3,
delegateSend = 1 << 4,
delegateSucceed = 1 << 5,
delegateFail = 1 << 6,
delegateComplete = 1 << 7,
delegateStop = 1 << 8,
delegateFileProgress = 1 << 9,
delegateProgress = 1 << 10,
delegateStart = 1 << 11,
delegateStop = 1 << 12,
delegateChange = 1 << 13,
delegatePaste = 1 << 14,
delegateDrop = 1 << 15,
delegateDrag = 1 << 16,
delegateChunkWillSend = 1 << 17,
delegateChunkSucceed = 1 << 18,
delegateChunkFail = 1 << 19,
delegateChunkComplete = 1 << 20,
delegateStartQueue = 1 << 21,
delegateClearQueue = 1 << 22,
delegateStopQueue = 1 << 23;
var CupDefaultProgressInterval = 100;
/*!
@class Cup
A wrapper for jQuery File Upload. The main configuration options
are available as accessor methods in this class. If other options
need to be set, use the options and setOptions: methods.
The full set of callbacks supported by jQuery File Upload are provided as delegate methods.
See the CupDelegate class for more info.
This class exposes many KVO compliant properties, outlets and actions that are useful when
creating interfaces that use this class. If you plan to create an interface using Cup
in Xcode, you will get the most out of it by doing the following:
- In the controller class that will use Cup, create a Cup outlet.
- In Xcode, edit the xib that will contain the Cup interface.
- Add an NSObject to the xib and set its class to Cup.
- Connect the Cup instance to your controller's Cup outlet.
- Add an NSArrayController to the xib.
- Connect the queueController outlet of the Cup object to the array controller.
Once you have done this, you can bind directly to properties in the Cup object
and to the arrangedObjects and selection of the array controller.
----------
Properties
----------
Where noted, the properties in this class mirror the options in jQuery File Upload, which are
documented here: https://github.com/blueimp/jQuery File Upload/wiki/Options. Except where
noted, the properties are read-write, and where the type is suitable, can be used with bindings.
Most of the read-write properties can be set in Xcode:
- Select the Cup object.
- Select the Identity Inspector.
- In the User Defined Runtime Attributes pane, click + to add an attribute.
- Set the Key Path to the property's name.
- Set the Type to the property's type or the parameter type its setter takes.
- Set the value to whatever you want.
For example, you can set the URL of the upload server by adding this User Defined
Runtime Attribute:
Key Path: URL
Type: String
Value: http://myserver.com/upload
Name Description
--------------------- -----------------------------------------------------------------------------
uploading A BOOL set to YES during uploading. (read-only)
indeterminate A BOOL that indicates whether the total size of the upload queue is known.
This affects what is reported in the progress callbacks. This will be YES
if the browser does not support the File API. (read-only)
progress A dictionary which contains info on the overall progress of the upload.
The dictionary contains the following items:
uploadedBytes Number of bytes uploaded so far.
total Total number of bytes to be uploaded. If indeterminate
is YES, this is zero.
percentComplete Integer percentage of the total (0-100) uploaded so far.
If indeterminate is YES, this is zero.
bitrate The overall bitrate of the upload so far.
As usual, you can bind to items within the dictionary.
(read-only)
URL A string representing the URL to which files will be uploaded.
jQuery File Upload option: url.
sequential A BOOL indicating whether multiple uploads will be performed sequentially
or concurrently. NO by default. jQuery File Upload option: sequentialUploads.
maxChunkSize If non-zero, uploads will be chunked. This is definitely preferable
if you plan on supporting large files. jQuery File Upload option: maxChunkSize.
maxConcurrentUploads An int which limits the number of concurrent uploads when sequential is NO.
jQuery File Upload option: limitConcurrentUploads.
progressInterval The minimum time interval in milliseconds to calculate and trigger progress events.
jQuery File Upload option: progressInterval.
filenameFilter A string regular expression suitable for use with the Javascript RegExp constructor.
When adding files to the queue, filenames that do not match regex are rejected.
Setting this property updates the filenameFilterRegex property.
filenameFilterRegex A Javascript regular expression. When adding files, filenames that do not match
are rejected. Setting this property updates the filenameFilter property.
allowedExtensions This write-only property should be a space-delimited string (e.g. "jpg png gif")
with one or more filename extensions (with or without dots). Setting this property
constructs a RegExp which allows filenames that end with the given extensions and
sets the filenameFilter and filenameFilterRegex properties accordingly.
maxFileSize An int representing the maximum size of a file that can be added to the queue.
Only supported on browsers that support the File API.
autoUpload A BOOL that indicates whether files added to the queue should immediately
start uploading. Defaults to NO.
removeCompletedFiles A BOOL that indicates whether files that have successfully uploaded should be
removed from the queue. Defaults to NO.
currentEvent The most recent jQuery event (NOT Cappuccino event) which triggered a method.
Usually this is of no interest, but if for some reason delegates want it,
they can retrieve it through this property. (read-only)
currentData When a jQuery File Upload callback is triggered (which eventually calls a Cup
delegate method), in most cases a data object is passed that reflects the current state.
The most relevant fields within that object are copied to the Cappuccino state, so usually
you will have no need for this. Delegates may use this method to retrieve the data passed
from the most recent callback. (read-only)
queue The array of CupFile objects used to represent the queue. In most cases
you should consider this read-only and manipulate the queue through its array controller.
fileClass The class of the objects stored in the queue. May be set either with a class or a string
class name, which allows you to set the class in Xcode either through User Defined Runtime
Attributes or bindings. This is useful if you want to add custom properties or methods to
the file objects. Must be CupFile or a subclass thereof.
-------
Outlets
-------
dropTarget Files can be added to the queue by dragging and dropping.
By default the entire browser window is the drop target for files.
You can connect this outlet to any view (including Cappuccino windows)
to specify the drop target.
delegate Cup communicates with its delegate extensively. You can
connect this outlet to the object that acts as the delegate.
queueController The array controller used to manage the upload queue.
-------
Actions
-------
addFiles: Presents an open file dialog to add one or more files to the upload queue.
start: Starts all files in the upload queue.
stop: Stops all files in the upload queue.
clearQueue: Clears all files from the upload queue. If an upload is in progress, does nothing.
*/
@implementation Cup : CPObject
{
// jQuery File Upload options
JSObject fileUploadOptions;
CPString URL @accessors;
CPString redirectURL @accessors;
BOOL sequential @accessors;
int maxChunkSize @accessors;
int maxConcurrentUploads @accessors;
int progressInterval @accessors;
@outlet CPView dropTarget @accessors(readonly);
JSObject jQueryDropTarget;
CPString filenameFilter @accessors;
RegExp filenameFilterRegex @accessors;
int maxFileSize @accessors;
BOOL autoUpload @accessors;
BOOL removeCompletedFiles @accessors;
jQueryEvent currentEvent @accessors(readonly);
JSObject currentData @accessors(readonly);
BOOL uploading @accessors;
BOOL indeterminate @accessors;
CPMutableDictionary progress @accessors;
@outlet id delegate @accessors(readonly);
int delegateImplementsFlags;
Class fileClass @accessors;
CPString widgetId;
JSObject callbacks;
CPMutableArray queue @accessors(readonly);
@outlet CPArrayController queueController @accessors(readonly);
}
+ (BOOL)automaticallyNotifiesObserversForKey:(CPString)key
{
if (key === @"filenameFilter" || key === @"filenameFilterRegex")
return NO;
else
return [super automaticallyNotifiesObserversForKey:key];
}
/*!
Returns the current version of the framework as a string.
*/
+ (CPString)versionString
{
var bundle = [CPBundle bundleForClass:[self class]];
return [bundle objectForInfoDictionaryKey:@"CPBundleVersion"];
}
#pragma mark Initialization
/*!
Initializes and returns a Cup object which uploads to the given URL.
*/
- (id)initWithURL:(CPString)aURL
{
self = [self init];
if (self)
[self setURL:aURL];
return self;
}
/*!
The designated initializer.
*/
- (id)init
{
self = [super init];
if (self)
[self _init];
return self;
}
#pragma mark Attributes
/*!
Returns a copy of the options passed to jQuery File Upload.
To set the options from your copy, use setOptions:.
*/
- (JSObject)options
{
return cloneOptions([self makeOptions]);
}
/*!
Sets the options to a copy of the passed in options.
Callbacks and dropZone are ignored. Options that are mirrored in this class
will be set in a KVO compliant way.
*/
- (void)setOptions:(JSObject)options
{
fileUploadOptions = cloneOptions(options);
[self setURL:options["url"] || @""];
[self setSequential:options["sequential"] || NO];
[self setMaxChunkSize:options["maxChunkSize"] || 0];
[self setMaxConcurrentUploads:options["limitConcurrentUploads"] || 0];
[self setProgressInterval:options["progressInterval"] || CupDefaultProgressInterval];
}
/*!
Sets the view that will be the drop target for files dragged into
the browser. Pass [CPPlatformWindow primaryPlatformWindow] to make
the entire window the drop target. Pass nil to disable drag and drop.
*/
- (void)setDropTarget:(CPView)target
{
dropTarget = target;
if (dropTarget === [CPPlatformWindow primaryPlatformWindow])
jQueryDropTarget = jQuery(document);
else if (!dropTarget)
jQueryDropTarget = nil;
else
jQueryDropTarget = jQuery(dropTarget._DOMElement);
// If drag and drop is enabled, disable the browser's default drag and drop action
jQuery(document)[dropTarget ? "bind" : "unbind"]('drop dragover', function(e)
{
e.preventDefault();
});
}
/*!
Sets the delegate. For information on delegate methods, see the CupDelegate class.
*/
- (void)setDelegate:(id)aDelegate
{
if (aDelegate === delegate)
return;
delegateImplementsFlags = 0;
delegate = aDelegate;
if (!delegate)
return;
if ([delegate respondsToSelector:@selector(cup:didFilterFile:because:)])
delegateImplementsFlags |= delegateFilter;
if ([delegate respondsToSelector:@selector(cup:willAddFile:)])
delegateImplementsFlags |= delegateWillAdd;
if ([delegate respondsToSelector:@selector(cup:didAddFile:)])
delegateImplementsFlags |= delegateAdd;
if ([delegate respondsToSelector:@selector(cupDidStart:)])
delegateImplementsFlags |= delegateStart;
if ([delegate respondsToSelector:@selector(cup:willSubmitFile:)])
delegateImplementsFlags |= delegateSubmit;
if ([delegate respondsToSelector:@selector(cup:willSendFile:)])
delegateImplementsFlags |= delegateSend;
if ([delegate respondsToSelector:@selector(cup:chunkWillSendForFile:)])
delegateImplementsFlags |= delegateChunkWillSend;
if ([delegate respondsToSelector:@selector(cup:chunkDidSucceedForFile:)])
delegateImplementsFlags |= delegateChunkSucceed;
if ([delegate respondsToSelector:@selector(cup:chunkDidFailForFile:)])
delegateImplementsFlags |= delegateChunkFail;
if ([delegate respondsToSelector:@selector(cup:chunkDidCompleteForFile:)])
delegateImplementsFlags |= delegateChunkComplete;
if ([delegate respondsToSelector:@selector(cup:uploadForFile:didProgress:)])
delegateImplementsFlags |= delegateFileProgress;
if ([delegate respondsToSelector:@selector(cup:uploadsDidProgress:)])
delegateImplementsFlags |= delegateProgress;
if ([delegate respondsToSelector:@selector(cup:uploadDidSucceedForFile:)])
delegateImplementsFlags |= delegateSucceed;
if ([delegate respondsToSelector:@selector(cup:uploadDidFailForFile:)])
delegateImplementsFlags |= delegateFail;
if ([delegate respondsToSelector:@selector(cup:uploadDidCompleteForFile:)])
delegateImplementsFlags |= delegateComplete;
if ([delegate respondsToSelector:@selector(cup:uploadWasStoppedForFile:)])
delegateImplementsFlags |= delegateStop;
if ([delegate respondsToSelector:@selector(cupDidStop:)])
delegateImplementsFlags |= delegateStop;
if ([delegate respondsToSelector:@selector(cup:fileInputDidSelectFiles:)])
delegateImplementsFlags |= delegateChange;
if ([delegate respondsToSelector:@selector(cupDidStartQueue:)])
delegateImplementsFlags |= delegateStartQueue;
if ([delegate respondsToSelector:@selector(cupDidClearQueue:)])
delegateImplementsFlags |= delegateClearQueue;
if ([delegate respondsToSelector:@selector(cupDidStopQueue:)])
delegateImplementsFlags |= delegateStopQueue;
if ([delegate respondsToSelector:@selector(cup:didPasteFiles:)])
delegateImplementsFlags |= delegatePaste;
if ([delegate respondsToSelector:@selector(cup:didDropFiles:)])
delegateImplementsFlags |= delegateDrop;
if ([delegate respondsToSelector:@selector(cup:wasDraggedOverWithEvent:)])
delegateImplementsFlags |= delegateDrag;
}
/*!
Sets the class for the objects stored in the upload queue.
The class must be CupFile or a subclass thereof.
@param aClass Either a class object or a string name of a class
*/
- (void)setFileClass:(Class)aClass
{
if ([aClass isKindOfClass:[CPString class]])
aClass = CPClassFromString(aClass);
if ([aClass isKindOfClass:[CupFile class]])
{
fileClass = aClass;
[[self queueController] setObjectClass:fileClass];
}
else
CPLog.warn("%s: %s the file class must be a subclass of CupFile.", [self className], [aClass className]);
}
/*!
Sets the filter used to validate filenames that are being added to the queue.
The string is passed to `new RegExp()`, so no delimiters should be included in the string.
*/
- (void)setFilenameFilter:(CPString)aFilter
{
[self _setFilenameFilter:aFilter caseSensitive:YES];
}
/*!
Sets the filter used to validate filenames that are being added to the queue.
The string is passed to `new RegExp()`, so no delimiters should be included in the string.
The filenameFilterRegex property stays in sync with this property.
*/
- (void)setFilenameFilter:(CPString)aFilter caseSensitive:(BOOL)caseSensitive
{
[self _setFilenameFilter:aFilter caseSensitive:caseSensitive];
}
/*!
Sets the filter regex used to validate filenames that are being added to the queue.
The filenameFilter property stays in sync with this property.
*/
- (void)setFilenameFilterRegex:(RegExp)regex
{
if ((filenameFilterRegex || "").toString() === (regex || "").toString())
return;
[self willChangeValueForKey:@"filenameFilterRegex"];
[self willChangeValueForKey:@"filenameFilter"];
filenameFilterRegex = regex;
if (regex)
{
// RegExp.toString() includes leading/trailing "/" and possible flags, remove those
filenameFilter = regex.toString().replace(/^\/(.*)\/\w*$/, "$1");
}
else
filenameFilter = @"";
[self didChangeValueForKey:@"filenameFilter"];
[self didChangeValueForKey:@"filenameFilterRegex"];
}
/*!
Sets the list of allowed filename extensions (with or without dots) when adding files.
This is just a convenience method that generates a filename filter regex.
Any existing filename filter will be replaced.
@param extensions May be either an array of extensions or a whitespace-delimited list
in a single string.
*/
- (void)setAllowedExtensions:(id)extensions
{
var filter = @"";
if (extensions)
{
if ([extensions isKindOfClass:[CPString class]])
extensions = extensions.split(/\s+/);
[extensions enumerateObjectsUsingBlock:function(extension)
{
extension = extension.replace(/^\./, "");
}];
filter = [CPString stringWithFormat:@"^.+\\.(%@)$", extensions.join("|")];
}
[self setFilenameFilter:filter caseSensitive:NO];
}
/*!
Returns the array controller for the queue, instantiating it (and the queue) if necessary
and setting its content to the queue array.
*/
- (CPArrayController)queueController
{
if (!queueController)
{
if (queue === nil)
queue = [];
queueController = [[CPArrayController alloc] initWithContent:queue];
[queueController setObjectClass:[fileClass class]];
[queueController addObserver:self forKeyPath:@"content" options:0 context:nil];
}
return queueController;
}
#pragma mark Actions
/*!
Add files via a file chooser dialog.
Can be used as an action method.
*/
- (@action)addFiles:(id)sender
{
jQuery("#" + widgetId)[0].click();
}
/*!
Upload all of the files in the queue.
Can be used as an action method.
*/
- (@action)start:(id)sender
{
[self fileUpload:@"option", [self makeOptions]];
if (!URL)
{
CPLog.error("%s: The URL has not been set.", [self className]);
return;
}
[queue makeObjectsPerformSelector:@selector(submit)];
if (delegateImplementsFlags & delegateStartQueue)
[delegate cupDidStartQueue:self];
}
/*!
Stop all uploads. Can be used as an action method.
*/
- (@action)stop:(id)sender
{
if (delegateImplementsFlags & delegateStopQueue)
[delegate cupDidStopQueue:self];
[queue makeObjectsPerformSelector:@selector(stop)];
[self setUploading:NO];
}
/*!
Clears the queue of files to be uploaded.
If an upload is in progress, nothing happens.
Can be used as an action method.
*/
- (@action)clearQueue:(id)sender
{
if (uploading)
return;
[queue removeAllObjects];
[[self queueController] setContent:queue];
[self resetProgress];
if (delegateImplementsFlags & delegateClearQueue)
[delegate cupDidClearQueue:self];
}
#pragma mark Methods
/*!
Returns the file in the queue with the given UID, or nil if none match.
*/
- (CupFile)fileWithUID:(CPString)aUID
{
var file = [queue objectAtIndex:[queue indexOfObjectPassingTest:function(file)
{
return [file UID] === aUID;
}]];
return file;
}
#pragma mark Overrides
- (void)awakeFromCib
{
[queueController setContent:queue];
[queueController addObserver:self forKeyPath:@"content" options:0 context:nil];
}
- (void)observeValueForKeyPath:(CPString)aKeyPath ofObject:(id)anObject change:(CPDictionary)changeDict context:(JSObject)context
{
if (aKeyPath === @"content")
{
// If a file is added or removed from the content, reset the overall progress to zero.
[self resetProgress];
}
}
#pragma mark Delegate (private)
/// @cond IGNORE
- (void)addFile:(JSFile)file
{
var filterFlags = [self validateFile:file],
canAdd = filterFlags === 0,
cupFile = [[fileClass alloc] initWithCup:self file:file data:currentData];
if (canAdd)
{
if (delegateImplementsFlags & delegateWillAdd)
canAdd = [delegate cup:self willAddFile:cupFile];
}
else if (delegateImplementsFlags & delegateFilter)
[delegate cup:self didFilterFile:cupFile because:filterFlags];
else
[self fileWasRejected:cupFile because:filterFlags];
if (canAdd)
{
[[self queueController] addObject:cupFile];
if (delegateImplementsFlags & delegateAdd)
[delegate cup:self didAddFile:cupFile];
if (autoUpload)
{
[self fileUpload:@"option", [self makeOptions]];
[cupFile submit];
}
}
}
- (void)uploadDidStart
{
[[self queueController] setSelectionIndexes:[CPIndexSet indexSet]];
[self setUploading:YES];
if (delegateImplementsFlags & delegateStart)
[delegate cupDidStart:self];
}
- (BOOL)submitFile:(CupFile)file
{
if (!URL)
{
CPLog.error("%s: The URL has not been set.", [self className]);
return NO;
}
var canSubmit = YES;
if (delegateImplementsFlags & delegateSubmit)
canSubmit = [delegate cup:self willSubmitFile:file];
return canSubmit;
}
- (BOOL)willSendFile:(CupFile)file
{
var canSend = YES;
if (delegateImplementsFlags & delegateSend)
canSend = [delegate cup:self willSendFile:file];
if (canSend)
[file start];
return canSend;
}
- (BOOL)chunkWillSendForFile:(CupFile)file
{
if (delegateImplementsFlags & delegateChunkWillSend)
return [delegate cup:self willSendChunkForFile:file];
return YES;
}
- (void)chunkDidSucceedForFile:(CupFile)file
{
[file setUploadedBytes:currentData.loaded];
if (delegateImplementsFlags & delegateChunkSucceed)
[delegate cup:self chunkDidSucceedForFile:file];
}
- (void)chunkDidFailForFile:(CupFile)file
{
if (delegateImplementsFlags & delegateChunkFail)
[delegate cup:self chunkDidFailForFile:file];
}
- (void)chunkDidCompleteForFile:(CupFile)file
{
if (delegateImplementsFlags & delegateChunkComplete)
[delegate cup:self chunkDidCompleteForFile:file];
}
- (void)uploadForFile:(CupFile)file didProgress:(JSObject)fileProgress
{
if (fileProgress.uploadedBytes)
[file setUploadedBytes:fileProgress.uploadedBytes];
[file setBitrate:fileProgress.bitrate];
if (delegateImplementsFlags & delegateFileProgress)
[delegate cup:self uploadForFile:file didProgress:fileProgress];
}
- (void)uploadsDidProgress:(JSObject)overallProgress
{
[self updateProgressWithUploadedBytes:overallProgress.uploadedBytes
total:overallProgress.total
percentComplete:overallProgress.uploadedBytes / overallProgress.total * 100
bitrate:overallProgress.bitrate];
if (delegateImplementsFlags & delegateProgress)
[delegate cup:self uploadsDidProgress:overallProgress];
}
- (void)uploadDidSucceedForFile:(CupFile)file
{
[file setStatus:CupFileStatusComplete];
// If a file upload is chunked and had one or more chunks,
// the final progress was already set and when we get here
// the loaded and bitrate values are zero, so ignore them.
if (currentData.loaded)
[file setUploadedBytes:currentData.loaded];
if (currentData.bitrate)
[file setBitrate:currentData.bitrate];
if (delegateImplementsFlags & delegateSucceed)
[delegate cup:self uploadDidSucceedForFile:file];
}
- (void)uploadDidFailForFile:(CupFile)file
{
[file setStatus:CupFileStatusPending];
if (delegateImplementsFlags & delegateFail)
[delegate cup:self uploadDidFailForFile:file];
}
- (void)uploadDidCompleteForFile:(CupFile)file
{
[file setUploading:NO];
if (delegateImplementsFlags & delegateComplete)
[delegate cup:self uploadDidCompleteForFile:file];
}
- (void)uploadWasStoppedForFile:(CupFile)file
{
if (delegateImplementsFlags & delegateStop)
[delegate cup:self uploadWasStoppedForFile:file];
}
- (void)uploadDidStop
{
[self setUploading:NO];
if (delegateImplementsFlags & delegateStop)
[delegate cupDidStop:self];
// Remove complete files
if (removeCompletedFiles)
{
var indexes = [queue indexesOfObjectsPassingTest:function(file)
{
return [file status] === CupFileStatusComplete;
}];
[queue removeObjectsAtIndexes:indexes];
[[self queueController] setContent:queue];
}
}
- (void)fileInputDidSelectFiles:(CPArray)files
{
if (delegateImplementsFlags & delegateChange)
[delegate cup:self fileInputDidSelectFiles:files];
}
- (void)filesWerePasted:(CPArray)files
{
if (delegateImplementsFlags & delegatePaste)
[delegate cup:self didPasteFiles:files];
}
- (void)filesWereDropped:(CPArray)files
{
if (delegateImplementsFlags & delegateDrop)
[delegate cup:self didDropFiles:files];
}
- (void)filesWereDraggedOverWithEvent:(jQueryEvent)anEvent
{
if (delegateImplementsFlags & delegateDrag)
[delegate cup:self wasDraggedOverWithEvent:anEvent];
}
#pragma mark Private helpers
- (void)_init
{
[self makeFileInput];
fileUploadOptions = {};
delegateImplementsFlags = 0;
fileClass = [CupFile class];
[self queueController]; // instantiates queue and controller
URL = URL || @"";
redirectURL = @"";
sequential = NO;
maxConcurrentUploads = 0;
maxChunkSize = 0;
progressInterval = CupDefaultProgressInterval;
progress = [CPMutableDictionary dictionary];
dropTarget = [CPPlatformWindow primaryPlatformWindow];
jQueryDropTarget = jQuery(document);
removeCompletedFiles = NO;
[self resetProgress];
[self setUploading:NO];
[self setIndeterminate:!CPFeatureIsCompatible(CPFileAPIFeature)];
// We have to wait till the next time through the run loop
// so the display server has a chance to update the dom.
[CPTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(finishInit) userInfo:nil repeats:NO];
}
- (void)makeFileInput
{
// Get the first unused id
var input = nil;
for (var counter = 1; ; ++counter)
{
widgetId = baseWidgetId + counter;
input = document.getElementById(widgetId);
if (!input)
break;
}
var bodyElement = [CPPlatform mainBodyElement];
input = document.createElement("input");
input.className = "cpdontremove";
input.setAttribute("type", "file");
input.setAttribute("id", widgetId);
input.setAttribute("name", "files[]");
input.setAttribute("multiple", "");
input.style.visibility = "hidden";
bodyElement.appendChild(input);
}
- (void)finishInit
{
jQuery("#" + widgetId).fileupload([self makeOptions]);
}
- (void)setCallbacks:(JSObject)options
{
if (!callbacks)
{
callbacks =
{
add: function(e, data)
{
currentEvent = e;
currentData = data;
[self addFile:data.files[0]];
[self pumpRunLoop];
},
submit: function(e, data)
{
currentEvent = e;
currentData = data;
var canSubmit = [self submitFile:[self fileFromJSFile:data.files[0]]];
[self pumpRunLoop];
return canSubmit;
},
send: function(e, data)
{
currentEvent = e;
currentData = data;
var canSend = [self willSendFile:[self fileFromJSFile:data.files[0]]];
[self pumpRunLoop];
return canSend;
},
done: function(e, data)
{
currentEvent = e;
currentData = data;
[self uploadDidSucceedForFile:[self fileFromJSFile:data.files[0]]];
[self pumpRunLoop];
},
fail: function(e, data)
{
currentEvent = e;
currentData = data;
[self uploadDidFailForFile:[self fileFromJSFile:data.files[0]]];
[self pumpRunLoop];
},
always: function(e, data)
{
currentEvent = e;
currentData = data;
[self uploadDidCompleteForFile:[self fileFromJSFile:data.files[0]]];
[self pumpRunLoop];
},
progress: function(e, data)
{
currentEvent = e;
currentData = data;
var fileProgress = {
uploadedBytes: data.loaded,
total: data.total,
bitrate: data.bitrate
};
[self uploadForFile:[self fileFromJSFile:data.files[0]] didProgress:fileProgress];
[self pumpRunLoop];
},
progressall: function(e, data)
{
currentEvent = e;
currentData = data;
var overallProgress = {
uploadedBytes: data.loaded,
total: data.total,
bitrate: data.bitrate
};
[self uploadsDidProgress:overallProgress];
[self pumpRunLoop];
},
start: function(e)