forked from GetmeUK/ContentTools
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-uploader.coffee
More file actions
127 lines (92 loc) · 3.44 KB
/
Copy pathimage-uploader.coffee
File metadata and controls
127 lines (92 loc) · 3.44 KB
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
class ImageUploader
# A dummy image uploader to allow the image dialog to be tested in the
# sandbox environment.
@imagePath: 'image.png'
@imageSize: [600, 174]
constructor: (dialog) ->
# Initialize the dialog to support image uploads
@_dialog = dialog
# Listen to key events from the dialog and assign handlers to each
@_dialog.addEventListener 'cancel', () =>
@_onCancel()
@_dialog.addEventListener 'imageuploader.cancelupload', () =>
@_onCancelUpload()
@_dialog.addEventListener 'imageuploader.clear', () =>
@_onClear()
@_dialog.addEventListener 'imageuploader.fileready', (ev) =>
@_onFileReady(ev.detail().file)
@_dialog.addEventListener 'imageuploader.mount', () =>
@_onMount()
@_dialog.addEventListener 'imageuploader.rotateccw', () =>
@_onRotateCCW()
@_dialog.addEventListener 'imageuploader.rotatecw', () =>
@_onRotateCW()
@_dialog.addEventListener 'imageuploader.save', () =>
@_onSave()
@_dialog.addEventListener 'imageuploader.unmount', () =>
@_onUnmount()
# Event handlers
_onCancel: () ->
# Handle the user cancelling the dialog
_onCancelUpload: () ->
# Handle an upload being cancelled
# Stop the upload
clearTimeout(@_uploadingTimeout)
# Set the dialog to empty
@_dialog.state('empty')
_onClear: () ->
# Handle the current image being cleared
@_dialog.clear()
_onFileReady: (file) ->
# Handle a file being selected by the user
console.log file
# Set the dialog state to uploading
@_dialog.progress(0)
@_dialog.state('uploading')
# Simulate uploading the specified file
upload = () =>
progress = @_dialog.progress()
progress += 1
if progress <= 100
@_dialog.progress(progress)
@_uploadingTimeout = setTimeout(upload, 25)
else
@_dialog.populate(
ImageUploader.imagePath,
ImageUploader.imageSize
)
@_uploadingTimeout = setTimeout(upload, 25)
_onMount: () ->
# Handle the dialog being mounted on the UI
_onRotateCCW: () ->
# Handle a request by the user to rotate the image counter-clockwise
# Simulate rotating the image
@_dialog.busy(true)
clearBusy = () =>
@_dialog.busy(false)
setTimeout(clearBusy, 1500)
_onRotateCW: () ->
# Handle a request by the user to rotate the image clockwise
# Simulate rotating the image
@_dialog.busy(true)
clearBusy = () =>
@_dialog.busy(false)
setTimeout(clearBusy, 1500)
_onSave: () ->
# Handle the user saving the image
# Simulate processing the image
@_dialog.busy(true)
clearBusy = () =>
@_dialog.busy(false)
@_dialog.save(
ImageUploader.imagePath,
ImageUploader.imageSize
{alt: 'Example of bad variable names'}
)
setTimeout(clearBusy, 1500)
_onUnmount: () ->
# Handle the dialog being unmounted from the UI
# Class methods
@createImageUploader: (dialog) ->
return new ImageUploader(dialog)
window.ImageUploader = ImageUploader