1<!-- Copyright (C) 2019 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-->
15<template>
16    <md-card style="min-width: 50em">
17      <md-card-header>
18        <div class="md-title">Open files</div>
19      </md-card-header>
20      <md-card-content>
21        <md-list>
22          <md-list-item v-for="file in dataFiles" v-bind:key="file.filename">
23            <md-icon>{{file.type.icon}}</md-icon>
24            <span class="md-list-item-text">{{file.filename}} ({{file.type.name}})</span>
25            <md-button class="md-icon-button md-accent" @click="onRemoveFile(file.type.name)">
26              <md-icon>close</md-icon>
27            </md-button>
28          </md-list-item>
29        </md-list>
30        <div>
31          <md-checkbox v-model="store.displayDefaults">Show default properties
32            <md-tooltip md-direction="bottom">
33              If checked, shows the value of all properties.
34              Otherwise, hides all properties whose value is the default for its data type.
35            </md-tooltip>
36          </md-checkbox>
37        </div>
38        <div class="md-layout">
39          <div class="md-layout-item md-small-size-100">
40            <md-select v-model="fileType" id="file-type" placeholder="File type">
41              <md-option value="auto">Detect type</md-option>
42              <md-option :value="k" v-for="(v,k) in FILE_TYPES" v-bind:key="v.name">{{v.name}}</md-option>
43            </md-select>
44          </div>
45        </div>
46        <div class="md-layout md-gutter">
47          <input type="file" @change="onLoadFile" id="upload-file" v-show="false" />
48          <label class="md-button md-accent md-raised md-theme-default" for="upload-file">Add File</label>
49          <md-button v-if="dataReady" @click="onSubmit" class="md-button md-primary md-raised md-theme-default">Submit</md-button>
50        </div>
51      </md-card-content>
52    </md-card>
53</template>
54<script>
55import { detectAndDecode, FILE_TYPES, DATA_TYPES } from './decode.js'
56
57export default {
58  name: 'datainput',
59  data() {
60    return {
61      FILE_TYPES,
62      fileType: "auto",
63      dataFiles: {},
64    }
65  },
66  props: ['store'],
67  methods: {
68    onLoadFile(e) {
69      var type = this.fileType;
70      var files = event.target.files || event.dataTransfer.files;
71      var file = files[0];
72      if (!file) {
73        // No file selected.
74        return;
75      }
76      this.$emit('statusChange', file.name + " (loading)");
77
78      var reader = new FileReader();
79      reader.onload = (e) => {
80        var buffer = new Uint8Array(e.target.result);
81        try {
82          if (FILE_TYPES[type]) {
83            var filetype = FILE_TYPES[type];
84            var data = filetype.decoder(buffer, filetype, file.name, this.store);
85          } else {
86            var [filetype, data] = detectAndDecode(buffer, file.name, this.store);
87          }
88        } catch (ex) {
89          this.$emit('statusChange', this.filename + ': ' + ex);
90          return;
91        } finally {
92          event.target.value = ''
93        }
94
95        this.$set(this.dataFiles, filetype.dataType.name, data);
96        this.$emit('statusChange', null);
97      }
98      reader.readAsArrayBuffer(files[0]);
99    },
100    onRemoveFile(typeName) {
101      this.$delete(this.dataFiles, typeName);
102    },
103    onSubmit() {
104      this.$emit('dataReady', Object.keys(this.dataFiles).map(key => this.dataFiles[key]));
105    }
106  },
107  computed: {
108    dataReady: function() { return Object.keys(this.dataFiles).length > 0 }
109  }
110}
111
112</script>
113