Skip to content

Commit

Permalink
Streamline dialog helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
inukshuk committed Jul 25, 2017
1 parent b614444 commit 4af7e79
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/actions/wizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Database } = require('../common/db')
const { ipcRenderer: ipc } = require('electron')
const { fail, saveProject } = require('../dialog')
const { fail, save } = require('../dialog')
const { PROJECT, WIZARD } = require('../constants')
const { create } = require('../models/project')

Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = {
save(payload) {
return async (dispatch) => {
try {
const file = await saveProject({ defaultPath: payload })
const file = await save.project({ defaultPath: payload })
dispatch(module.exports.project.update({ file }))

} catch (error) {
Expand Down
4 changes: 2 additions & 2 deletions src/commands/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const { basename } = require('path')
const { warn, verbose } = require('../common/log')
const { all, call, put, select } = require('redux-saga/effects')
const { Command } = require('./command')
const { prompt, openImages, fail } = require('../dialog')
const { prompt, open, fail } = require('../dialog')
const { Image } = require('../image')
const { imagePath } = require('../common/cache')
const { text } = require('../value')
Expand Down Expand Up @@ -56,7 +56,7 @@ class Import extends Command {

if (!files) {
this.isInteractive = true
files = yield call(openImages)
files = yield call(open.images)
}

if (!files) return
Expand Down
24 changes: 9 additions & 15 deletions src/commands/ontology.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,7 @@ const mod = require('../models')
const sanitize = require('sanitize-filename')
const { join } = require('path')
const { keys } = Object

const {
openTemplates,
openVocabs,
exportTemplate,
fail
} = require('../dialog')
const dialog = require('../dialog')


class Import extends Command {
Expand All @@ -31,7 +25,7 @@ class Import extends Command {
let { files, isProtected } = this.action.payload

if (!files) {
files = yield call(openVocabs)
files = yield call(dialog.open.vocab)
this.init = performance.now()
}

Expand Down Expand Up @@ -62,7 +56,7 @@ class Import extends Command {
} catch (error) {
warn(`Failed to import "${id}": ${error.message}`)
verbose(error.stack)
fail(error, this.action.type)
dialog.fail(error, this.action.type)
}
}
})
Expand All @@ -71,7 +65,7 @@ class Import extends Command {
} catch (error) {
warn(`Failed to import "${file}": ${error.message}`)
verbose(error.stack)
fail(error, this.action.type)
dialog.fail(error, this.action.type)
}
}

Expand Down Expand Up @@ -215,7 +209,7 @@ class TemplateImport extends Command {
let { files, isProtected } = payload

if (!files) {
files = yield call(openTemplates)
files = yield call(dialog.open.templates)
this.init = performance.now()
}

Expand Down Expand Up @@ -251,7 +245,7 @@ class TemplateImport extends Command {
warn(`Failed to import "${file}": ${error.message}`)
verbose(error.stack)

fail(error, this.action.type)
dialog.fail(error, this.action.type)
}
}

Expand All @@ -278,7 +272,7 @@ class TemplateExport extends Command {

if (!path) {
this.isInteractive = true
path = yield call(exportTemplate, {
path = yield call(dialog.save.template, {
defaultPath: join(
ARGS.documents,
data.name ? sanitize(`${data.name}.ttp`) : ''
Expand All @@ -294,7 +288,7 @@ class TemplateExport extends Command {
warn(`Failed to export template ${id} to ${path}: ${error.message}`)
verbose(error.stack)

fail(error, this.action.type)
dialog.fail(error, this.action.type)
}
}
}
Expand All @@ -318,7 +312,7 @@ class TemplateCreate extends Command {
warn(`Failed to create template "${id}": ${error.message}`)
verbose(error.stack)

fail(error, this.action.type)
dialog.fail(error, this.action.type)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/commands/photo.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { all, call, put, select } = require('redux-saga/effects')
const { Command } = require('./command')
const { openImages } = require('../dialog')
const { open } = require('../dialog')
const mod = require('../models')
const act = require('../actions')
const { PHOTO } = require('../constants')
Expand All @@ -29,7 +29,7 @@ class Create extends Command {
}

if (!files) {
files = yield call(openImages)
files = yield call(open.images)
this.init = performance.now()
}

Expand Down
112 changes: 52 additions & 60 deletions src/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function onClosed(_, { id, payload, error }) {
}
}

function open(type, options = {}) {
function show(type, options = {}) {
return new Promise((resolve, reject) => {
const id = seq.next().value

Expand All @@ -51,7 +51,7 @@ function open(type, options = {}) {
}

function notify(options) {
return open('message-box', {
return show('message-box', {
type: 'none', buttons: ['OK'], ...options
})
}
Expand All @@ -73,7 +73,7 @@ async function prompt(message, {
isChecked,
...options
} = {}, prefix = 'dialog.prompt.') {
const { response, checked } = await open('message-box', {
const { response, checked } = await show('message-box', {
type: 'question',
buttons: buttons.map(id => t(id, prefix)),
message: t(message, prefix),
Expand All @@ -92,73 +92,65 @@ async function prompt(message, {
}

function save(options) {
return open('save', options)
return show('save', options)
}

function file(options) {
return open('file', options)
function open(options) {
return show('file', options)
}

function openImages(options) {
return open('file', {
filters: [
{ name: t('dialog.filter.images'), extensions: ['jpg', 'jpeg'] }
],
properties: ['openFile', 'multiSelections'],
...options
})
}
open.images = (options) => open({
filters: [{
name: t('dialog.filter.images'),
extensions: ['jpg', 'jpeg']
}],
properties: ['openFile', 'multiSelections'],
...options
})

open.vocab = (options) => open({
filters: [{
name: t('dialog.filter.rdf'),
extensions: ['n3', 'ttl']
}],
properties: ['openFile', 'multiSelections'],
...options
})

open.templates = (options) => open({
filters: [{
name: t('dialog.filter.templates'),
extensions: ['ttp']
}],
properties: ['openFile', 'multiSelections'],
...options
})

function openVocabs(options) {
return open('file', {
filters: [
{ name: t('dialog.filter.rdf'), extensions: ['n3', 'ttl'] }
],
properties: ['openFile', 'multiSelections'],
...options
})
}

function openTemplates(options) {
return open('file', {
filters: [
{ name: t('dialog.filter.templates'), extensions: ['ttp'] }
],
properties: ['openFile', 'multiSelections'],
...options
})
}
save.project = (options) => save({
filters: [{
name: t('dialog.filter.projects'),
extensions: ['tpy']
}],
...options
})

function saveProject(options) {
return open('save', {
filters: [
{ name: t('dialog.filter.projects'), extensions: ['tpy'] }
],
...options
})
}

function exportTemplate(options) {
return open('save', {
filters: [
{ name: t('dialog.filter.templates'), extensions: ['ttp'] }
],
...options
})
}
save.template = (options) => save({
filters: [{
name: t('dialog.filter.templates'),
extensions: ['ttp']
}],
...options
})

module.exports = {
start,
stop,
open,
notify,
exportTemplate,
fail,
file,
openImages,
openTemplates,
openVocabs,
notify,
open,
prompt,
save,
saveProject,
prompt
show,
start,
stop
}

0 comments on commit 4af7e79

Please sign in to comment.