return if !scrivito? scrivito.content_browser._uploader = do -> dropZoneSelector: '.scrivito-content-browser-items' dropOverCssClass: 'uploader-drag-over' _fallbackUploadMimeTypeMapping: -> '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() _uploadMimeTypeMapping: -> mapping = scrivito.content_browser.filter_defaults.upload return unless mapping? if Object.keys(mapping).length == 0 return @_fallbackUploadMimeTypeMapping() else return mapping _objClassForMimeType: (mimeType) -> for mime, objClass of @_uploadMimeTypeMapping() 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-content-browser-items').empty() $('
') .addClass('scrivito-content-browser-loading') .appendTo itemsElement $('') .addClass('scrivito-content-browser-progress-wrapper') .appendTo itemsElement _addProgress: (file) -> progressBar = $('') .addClass('scrivito-content-browser-progress-bar') .css('width', '10%') progress = $('') .addClass('scrivito-content-browser-progress') .html(progressBar) fileName = $('') .html(file.name) error = $('') .addClass('scrivito-content-browser-error') $('') .addClass('scrivito-content-browser-progress-file') .append(fileName) .append(error) .append(progress) .prependTo $('.scrivito-content-browser-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() onUploadStart: (files) -> onUploadFailure: (errors) -> onUploadSuccess: (objs) ->