1// Copyright (C) 2018 The Android Open Source Project 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15import {Actions} from '../common/actions'; 16import {globals} from './globals'; 17 18let lastDragTarget: EventTarget|null = null; 19 20export function installFileDropHandler() { 21 window.ondragenter = (evt: DragEvent) => { 22 evt.preventDefault(); 23 evt.stopPropagation(); 24 lastDragTarget = evt.target; 25 if (dragEventHasFiles(evt)) { 26 document.body.classList.add('filedrag'); 27 } 28 }; 29 30 window.ondragleave = (evt: DragEvent) => { 31 evt.preventDefault(); 32 evt.stopPropagation(); 33 if (evt.target === lastDragTarget) { 34 document.body.classList.remove('filedrag'); 35 } 36 }; 37 38 window.ondrop = (evt: DragEvent) => { 39 evt.preventDefault(); 40 evt.stopPropagation(); 41 document.body.classList.remove('filedrag'); 42 if (evt.dataTransfer && dragEventHasFiles(evt)) { 43 const file = evt.dataTransfer.files[0]; 44 if (file) { 45 globals.frontendLocalState.localOnlyMode = false; 46 globals.dispatch(Actions.openTraceFromFile({file})); 47 } 48 } 49 evt.preventDefault(); 50 }; 51 52 window.ondragover = (evt: DragEvent) => { 53 evt.preventDefault(); 54 evt.stopPropagation(); 55 }; 56} 57 58function dragEventHasFiles(event: DragEvent): boolean { 59 if (event.dataTransfer && event.dataTransfer.types) { 60 for (const type of event.dataTransfer.types) { 61 if (type === 'Files') return true; 62 } 63 } 64 return false; 65} 66