@ResourcebrowserUploader = do -> dropZoneSelector: '.scrivito-resourcebrowser-items' dropOverCssClass: 'uploader-drag-over' mimeTypeMapping: 'image/*': 'Image' 'video/*': 'Video' _initializeBindings: -> dropZone = @modal.find(@dropZoneSelector) dropZone.on 'dragover', (event) => $(event.currentTarget).addClass(@dropOverCssClass) event.preventDefault() dropZone.on 'dragleave', (event) => $(event.currentTarget).removeClass(@dropOverCssClass) event.preventDefault() dropZone.on 'drop', (event) => $(event.currentTarget).removeClass(@dropOverCssClass) @_onDrop(event) event.preventDefault() _objClassForMimeType: (mimeType) -> for mime, objClass of @mimeTypeMapping return objClass if mimeType.match(mime) undefined _processQueue: (queue, createdObjs, failedUploads, promise) -> file = queue.pop() if file? @_createResource(file) .done (obj) => @_updateProgress(file, '100%') createdObjs.push(obj) @_processQueue(queue, createdObjs, failedUploads, promise) .fail (args) => @_updateError(file, args.message || "Upload failed. Please check your network connection.") @_updateProgress(file, '0%') failedUploads.push(args) @_processQueue(queue, createdObjs, failedUploads, promise) else return promise.resolve(createdObjs, failedUploads) _addProgressWrapper: () -> itemsElement = $('.scrivito-resourcebrowser-items').empty() $('
') .addClass('scrivito-resourcebrowser-loading') .appendTo itemsElement $('
') .addClass('scrivito-resourcebrowser-progress-wrapper') .appendTo itemsElement _addProgress: (file) -> progressBar = $('
') .addClass('scrivito-resourcebrowser-progress-bar') .css('width', '10%') progress = $('
') .addClass('scrivito-resourcebrowser-progress') .html(progressBar) fileName = $('

') .html(file.name) error = $('

') .addClass('scrivito-resourcebrowser-error') $('
') .addClass('scrivito-resourcebrowser-progress-file') .append(fileName) .append(error) .append(progress) .prependTo $('.scrivito-resourcebrowser-progress-wrapper') file.progressBar = progressBar file.error = error _updateProgress: (file, percent) -> file.progressBar.css('width', percent) _updateError: (file, message) -> file.error.text(message) _onDrop: (event) -> dataTransfer = event.originalEvent.dataTransfer unless dataTransfer? return files = dataTransfer.files if files.length == 0 return @_addProgressWrapper() queue = for file in files @_addProgress(file) file @onUploadStart(queue) promise = $.Deferred() .done (createdObjs, failedUploads) => @onUploadFailure(failedUploads) if failedUploads.length > 0 @onUploadSuccess(createdObjs) if createdObjs.length > 0 @_processQueue(queue, [], [], promise) promise _randomResourceId: -> hex = Math.floor(Math.random() * Math.pow(16, 8)).toString(16) while (hex.length < 8) hex = '0' + hex hex _createResource: (file) -> objName = file.name.replace(/[^a-z0-9_.$\-]/ig, '-') path = "_resources/#{@_randomResourceId()}/#{objName}" objClass = @_objClassForMimeType(file.type) if objClass scrivito.create_obj blob: file _path: path _obj_class: objClass else $.Deferred().reject id: "no_obj_class_for_mime_type" file_name: file.name message: "File type #{file.type} rejected by resource configuration." init: (@modal) -> @_initializeBindings() # Hook for 3rd parties when the upload starts. onUploadStart: (files) -> # Hook for 3rd parties when the upload fails. onUploadFailure: (errors) -> # Hook for 3rd parties when the upload was successful. onUploadSuccess: (objs) ->