Skip to content

Commit

Permalink
[showcase][m]: implemented handling dropped files and opening showcas…
Browse files Browse the repository at this point in the history
…e page - refs #6
  • Loading branch information
anuveyatsu committed Nov 21, 2017
1 parent c478a70 commit afa2148
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
14 changes: 14 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ const { version } = require('../package')
const windowList = require('./utils/frames/list')
const toggleWindow = require('./utils/frames/toggle')
const updater = require('./updates')
const showcase = require('./utils/showcase')
const notify = require('./notify')
const handleException = require('./utils/exception')
const { error: showError } = require('./dialogs')

// Load the app instance from electron
const { app } = electron
Expand All @@ -42,6 +44,17 @@ app.on('window-all-closed', () => {
}
})

const filesDropped = async (event, files) => {
event.preventDefault()

if (process.env.CONNECTION === 'offline') {
showError("You're offline")
return
}

await showcase(files)
}

// Chrome Command Line Switches
app.commandLine.appendSwitch('disable-renderer-backgrounding')

Expand Down Expand Up @@ -117,6 +130,7 @@ app.on('ready', async () => {
}

// Define major event listeners for tray
tray.on('drop-files', filesDropped)
tray.on('click', toggleActivity)
tray.on('double-click', toggleActivity)

Expand Down
84 changes: 84 additions & 0 deletions main/utils/showcase.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Native
const path = require('path')

// Packages
const electron = require('electron')
const ejse = require('ejs-electron')
const fs = require('fs-extra')
const {Dataset, File} = require('data.js')
const hri = require('human-readable-ids').hri
const { resolve: resolvePath } = require('app-root-path')

// Utils
const { error: showError } = require('../dialogs')
const toggleWindow = require('./frames/toggle')


const prepareDatasetFromFile = async filePath => {
const pathParts = path.parse(filePath)
const file = await File.load(pathParts.base, {basePath: pathParts.dir})

// List of formats that are known as tabular
const knownTabularFormats = ['csv', 'tsv', 'dsv']
if (knownTabularFormats.includes(file.descriptor.format)) {
await file.addSchema()
}

let dpName, dpTitle
dpName = file.descriptor.name.replace(/\s+/g, '-').toLowerCase()
// Add human readable id so that this packge does not conflict with other
// packages (name is coming from the file name which could just be
// data.csv)
dpName += '-' + hri.random()

// Make unslugifies version for title:
dpTitle = dpName.replace(/-+/g, ' ')
dpTitle = dpTitle.charAt(0).toUpperCase() + dpTitle.slice(1)

const metadata = {
name: dpName,
title: dpTitle,
resources: []
}
const dataset = await Dataset.load(metadata)
dataset.addResource(file)
return dataset
}


module.exports = async (files) => {
if (files.length === 1) {
const path_ = files[0]
if (fs.lstatSync(path_).isFile()) {
const pathParts = path.parse(path_)
if (pathParts.base === 'datapackage.json') {
showError('Sorry, currently you can drop in a single file. Data Package support is coming!')
}
const dataset = await prepareDatasetFromFile(path_)
ejse.data('dataset', dataset.descriptor)
ejse.data('dpId', JSON.stringify(dataset.descriptor).replace(/\\/g, '\\\\').replace(/\'/g, "\\'"))
const win = new electron.BrowserWindow({
width: 800,
height: 700,
title: 'Welcome to DataHub',
resizable: false,
center: true,
frame: false,
show: false,
fullscreenable: false,
maximizable: false,
titleBarStyle: 'hidden-inset',
webPreferences: {
backgroundThrottling: false,
devTools: true
}
})
win.loadURL('file://' + resolvePath('./main/pages/showcase.ejs'))
toggleWindow(null, win)
} else {
showError('Sorry, currently you can drop in a single file. Directory support is coming!')
}
} else if (files.length > 1) {
showError('Sorry, currently you can drop in a single file. Multiple files support is coming!')
}
}
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@
"dependencies": {
"app-root-path": "^2.0.1",
"child-process-promise": "^2.2.1",
"data.js": "^0.9.10",
"ejs-electron": "^2.0.1",
"electron-is-dev": "^0.3.0",
"electron-updater": "^2.16.1",
"fix-path": "^2.1.0",
"fs-extra": "^4.0.2",
"global-packages": "^1.0.2",
"human-readable-ids": "^1.0.3",
"just-compare": "^1.2.1",
"mkdirp": "^0.5.1",
"ms": "^2.0.0",
Expand Down

0 comments on commit afa2148

Please sign in to comment.