refactoring

This commit is contained in:
Milos Stojanovic
2020-02-20 15:29:36 +01:00
parent ba8eadc826
commit a04e31770a
3 changed files with 21 additions and 16 deletions

View File

@@ -122,6 +122,18 @@ const funcs = {
getDownloadLink(path) { getDownloadLink(path) {
return Vue.config.baseURL+'/download&path='+encodeURIComponent(Base64.encode(path)) return Vue.config.baseURL+'/download&path='+encodeURIComponent(Base64.encode(path))
}, },
hasPreview(name) {
return this.isText(name) || this.isImage(name)
},
isText(name) {
return this.hasExtension(name, ['.txt', '.html', '.css', '.js', '.ts', '.php'])
},
isImage(name) {
return this.hasExtension(name, ['.jpg', '.jpeg', '.gif', '.png'])
},
hasExtension(name, exts) {
return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$', 'i')).test(name)
},
} }
} }

View File

@@ -113,7 +113,7 @@
<b-dropdown-item v-if="props.row.type == 'file' && can('download')" aria-role="listitem" @click="download(props.row)"> <b-dropdown-item v-if="props.row.type == 'file' && can('download')" aria-role="listitem" @click="download(props.row)">
<b-icon icon="download" size="is-small" /> {{ lang('Download') }} <b-icon icon="download" size="is-small" /> {{ lang('Download') }}
</b-dropdown-item> </b-dropdown-item>
<b-dropdown-item v-if="props.row.type == 'file' && can(['read', 'download'])" aria-role="listitem" @click="preview(props.row)"> <b-dropdown-item v-if="props.row.type == 'file' && can(['download']) && hasPreview(props.row.path)" aria-role="listitem" @click="preview(props.row)">
<b-icon icon="file-alt" size="is-small" /> {{ lang('View') }} <b-icon icon="file-alt" size="is-small" /> {{ lang('View') }}
</b-dropdown-item> </b-dropdown-item>
<b-dropdown-item v-if="can('write')" aria-role="listitem" @click="copy($event, props.row)"> <b-dropdown-item v-if="can('write')" aria-role="listitem" @click="copy($event, props.row)">
@@ -252,8 +252,10 @@ export default {
itemClick(item) { itemClick(item) {
if (item.type == 'dir' || item.type == 'back') { if (item.type == 'dir' || item.type == 'back') {
this.goTo(item.path) this.goTo(item.path)
} else if (this.can(['read', 'download'])) { } else if (this.can(['download']) && this.hasPreview(item.path)) {
this.preview(item) this.preview(item)
} else if (this.can(['download'])) {
this.download(item)
} }
}, },
selectDir() { selectDir() {

View File

@@ -7,13 +7,13 @@
</p> </p>
</header> </header>
<section class="modal-card-body preview"> <section class="modal-card-body preview">
<textarea v-if="isText()" v-model="content" class="textarea" name="content" rows="20" /> <textarea v-if="isText(item.path)" v-model="content" class="textarea" name="content" rows="20" />
<div v-if="isImage()" class="image"> <div v-if="isImage(item.path)" class="image">
<img :src="content"> <img :src="content">
</div> </div>
</section> </section>
<footer class="modal-card-foot"> <footer class="modal-card-foot">
<button v-if="isText() && can(['write'])" class="button" type="button" @click="saveFile()"> <button v-if="isText(item.path) && can(['write'])" class="button" type="button" @click="saveFile()">
{{ lang('Save') }} {{ lang('Save') }}
</button> </button>
<button class="button" type="button" @click="$parent.close()"> <button class="button" type="button" @click="$parent.close()">
@@ -35,7 +35,7 @@ export default {
} }
}, },
mounted() { mounted() {
if (this.isText()) { if (this.isText(this.item.path)) {
api.downloadItem({ api.downloadItem({
path: this.item.path, path: this.item.path,
}) })
@@ -43,20 +43,11 @@ export default {
this.content = res this.content = res
}) })
.catch(error => this.handleError(error)) .catch(error => this.handleError(error))
} else if (this.isImage()) { } else if (this.isImage(this.item.path)) {
this.content =this.getDownloadLink(this.item.path) this.content =this.getDownloadLink(this.item.path)
} }
}, },
methods: { methods: {
isText() {
return this.hasExtension(['.txt', '.html', '.css', '.js', '.ts', '.php'])
},
isImage() {
return this.hasExtension(['.jpg', '.jpeg', '.gif', '.png'])
},
hasExtension(exts) {
return (new RegExp('(' + exts.join('|').replace(/\./g, '\\.') + ')$', 'i')).test(this.item.path)
},
saveFile() { saveFile() {
api.saveContent({ api.saveContent({
name: this.item.name, name: this.item.name,