@ResourcebrowserUploader = do -> dropZoneSelector: '.editing-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, promise) -> file = queue.pop() if file? @_createResource(file) .done (obj) => @_updateProgress(file, '100%') createdObjs.push(obj) @_processQueue(queue, createdObjs, promise) else return promise.resolve(createdObjs) _addProgressWrapper: () -> itemsElement = $('.editing-resourcebrowser-items').empty() $('
') .addClass('editing-resourcebrowser-loading') .appendTo itemsElement $('
') .addClass('editing-resourcebrowser-progress-wrapper') .appendTo itemsElement _addProgress: (file) -> progressBar = $('
') .addClass('editing-resourcebrowser-progress-bar') .css('width', '10%') progress = $('
') .addClass('editing-resourcebrowser-progress') .html(progressBar) fileName = $('

') .html(file.name) $('
') .addClass('editing-resourcebrowser-progress-file') .append(fileName) .append(progress) .prependTo $('.editing-resourcebrowser-progress-wrapper') file['progressBar'] = progressBar _updateProgress: (file, percent) -> file.progressBar.css('width', percent) _onDrop: (event) -> dataTransfer = event.originalEvent.dataTransfer unless dataTransfer? return files = dataTransfer.files if files.length == 0 return @onUploadStart(queue) @_addProgressWrapper() promise = $.Deferred() .done (data) => @onUploadSuccess(data) queue = for file in files @_addProgress(file) file @_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}" scrivito.create_obj blob: file _path: path _obj_class: @_objClassForMimeType(file.type) init: (@modal) -> @_initializeBindings() # Hook for 3rd parties when the upload starts. onUploadStart: (files) -> # Hook for 3rd parties when the upload fails. onUploadFailure: (error) -> # Hook for 3rd parties when the upload was successful. onUploadSuccess: (objs) ->