Skip to content

Commit

Permalink
feat(app): create new diagram when opening empty file
Browse files Browse the repository at this point in the history
Closes #636
  • Loading branch information
philippfromme committed Apr 18, 2018
1 parent 28fcba6 commit 4c4a7d5
Show file tree
Hide file tree
Showing 17 changed files with 186 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All notable changes to the [Camunda Modeler](https://github.com/camunda/camunda-

___Note:__ Yet to be released changes appear here._

* `FEAT`: add ability to create a new diagram when opening an empty file ([#636](https://github.com/camunda/camunda-modeler/issues/636))

## ...

Expand Down
17 changes: 17 additions & 0 deletions app/lib/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,23 @@ Dialog.prototype.getDialogOptions = function(type, opts) {
'Do you want to save the file as.. ?'
].join('\n')
};
},
emptyFile: function(options) {
var type = options.fileType.toUpperCase();

return {
type: 'question',
title: [
'Empty ',
type,
' file'
].join(''),
buttons: [
{ id: 'cancel', label: 'Cancel' },
{ id: 'create', label: 'Create' }
],
message: 'The ' + type + ' file is empty. Would you like to create a new diagram?'
};
}
};

Expand Down
8 changes: 1 addition & 7 deletions app/lib/file-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,6 @@ FileSystem.prototype.open = function(filePath, callback) {
forEach(filenames, function(filename, idx) {
var file = self._openFile(filename);

if (!file.contents) {
callback(file);

return false;
}

files.push(file);
});

Expand All @@ -70,7 +64,7 @@ FileSystem.prototype._openFile = function(filePath, callback) {
};

FileSystem.prototype.getFilePath = function(diagramFile) {
return diagramFile.path !== '[unsaved]' ? diagramFile.path : null;
return diagramFile.path !== '' ? diagramFile.path : null;
};

FileSystem.prototype.saveAs = function(diagramFile, callback) {
Expand Down
4 changes: 4 additions & 0 deletions app/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ renderer.on('dialog:content-changed', function(done) {
dialog.showDialog('contentChanged', done);
});

renderer.on('dialog:empty-file', function(fileType, done) {
dialog.showDialog('emptyFile', { fileType: fileType }, done);
});

renderer.on('deploy', function(data, done) {
var workspaceConfig = config.get('workspace', { endpoints: [] });

Expand Down
24 changes: 24 additions & 0 deletions app/test/spec/dialog-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,31 @@ describe('Dialog', function() {
});


it('should show empty file dialog', function(done) {

// given
electronDialog.setResponse(1); // 'Create' button

// when
dialog.showDialog('emptyFile', {
fileType: 'bpmn'
}, function(err, result) {
var dialogArgs = getDialogArgs(electronDialog.showMessageBox);

expect(result).to.equal('create');

expect(electronDialog.showMessageBox).to.have.been.called;

expect(dialogArgs.title).to.equal('Empty BPMN file');
expect(dialogArgs.message).to.equal('The BPMN file is empty. Would you like to create a new diagram?');

done();
});
});


it('should show message dialog -> close', function(done) {

// given
var messageBoxArg;

Expand Down
22 changes: 20 additions & 2 deletions client/lib/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,20 @@ App.prototype.openFiles = function(files) {
var dialog = this.dialog;

series(files, (file, done) => {

if (file.contents || !file.contents.length) {
dialog.openEmptyFile(file.fileType, (err, answer) => {
if (answer === 'create') {
var tabProvider = this._findTabProvider(file.fileType);

done(null, tabProvider.createNewFile({
name: file.name,
path: file.path
}));
}
});
}

var type = parseFileType(file);

if (!type) {
Expand Down Expand Up @@ -578,6 +592,7 @@ App.prototype.openFiles = function(files) {
done(null, assign({}, file, { fileType: type }));
}
}

}, (err, diagramFiles) => {
if (err) {
return debug('open-diagram canceled: %s', err);
Expand All @@ -596,7 +611,6 @@ App.prototype.openFiles = function(files) {
* Open a new tab based on a file chosen by the user.
*/
App.prototype.openDiagram = function() {

var dialog = this.dialog;

var cwd = getFilePath(this.activeTab);
Expand Down Expand Up @@ -1039,7 +1053,7 @@ App.prototype.saveTab = function(tab, options, done) {

debug('exported %s \n%s', tab.id, file.contents);

var saveAs = isUnsaved(file) || options && options.saveAs;
var saveAs = !file.path || options && options.saveAs;

this.saveFile(file, saveAs, updateTab);
});
Expand Down Expand Up @@ -1080,6 +1094,8 @@ App.prototype.saveFile = function(file, saveAs, done) {
}

if (!saveAs) {
file.isUnsaved = false;

return fileSystem.writeFile(assign({}, file), handleFileError);
}

Expand All @@ -1097,6 +1113,8 @@ App.prototype.saveFile = function(file, saveAs, done) {

debug('save file %s as %s', file.name, suggestedFile.path);

file.isUnsaved = false;

fileSystem.writeFile(assign({}, file, suggestedFile), handleFileError);
});
};
Expand Down
13 changes: 7 additions & 6 deletions client/lib/app/tabs/bpmn/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var debug = require('debug')('bpmn-provider');

var ensureOpts = require('util/ensure-opts');

var isUnsaved = require('util/file/is-unsaved');

var initialXML = require('./initial.bpmn');

var BpmnTab = require('./bpmn-tab');
Expand All @@ -26,7 +24,9 @@ function BpmnProvider(options) {

var createdFiles = 0;

this.createNewFile = function() {
this.createNewFile = function(attrs) {
attrs = attrs ||{};

// increment counter
createdFiles++;

Expand All @@ -37,10 +37,11 @@ function BpmnProvider(options) {

return {
fileType: 'bpmn',
name: 'diagram_' + createdFiles + '.bpmn',
path: isUnsaved.PATH,
name: attrs.name || 'diagram_' + createdFiles + '.bpmn',
path: attrs.path || '',
contents: xml,
isInitial: true
isInitial: true,
isUnsaved: true
};
};

Expand Down
13 changes: 7 additions & 6 deletions client/lib/app/tabs/cmmn/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var debug = require('debug')('cmmn-provider');

var ensureOpts = require('util/ensure-opts');

var isUnsaved = require('util/file/is-unsaved');

var initialXML = require('./initial.cmmn');

var CmmnTab = require('./cmmn-tab');
Expand All @@ -26,7 +24,9 @@ function CmmnProvider(options) {

var createdFiles = 0;

this.createNewFile = function() {
this.createNewFile = function(attrs) {
attrs = attrs ||{};

// increment counter
createdFiles++;

Expand All @@ -37,9 +37,10 @@ function CmmnProvider(options) {

return {
fileType: 'cmmn',
name: 'diagram_' + createdFiles + '.cmmn',
path: isUnsaved.PATH,
contents: xml
name: attrs.name || 'diagram_' + createdFiles + '.cmmn',
path: attrs.path || '',
contents: xml,
isUnsaved: true
};
};

Expand Down
10 changes: 4 additions & 6 deletions client/lib/app/tabs/dmn/provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ var debug = require('debug')('dmn-provider');

var ensureOpts = require('util/ensure-opts');

var isUnsaved = require('util/file/is-unsaved');

var tableXML = require('./table.dmn'),
diagramXML = require('./diagram.dmn');

Expand All @@ -27,7 +25,6 @@ function DmnProvider(options) {
var createdFiles = 0;

this.createNewFile = function(attrs) {

attrs = attrs || {};

var xml;
Expand All @@ -45,9 +42,10 @@ function DmnProvider(options) {

return {
fileType: 'dmn',
name: 'diagram_' + createdFiles + '.dmn',
path: isUnsaved.PATH,
contents: xml
name: attrs.name || 'diagram_' + createdFiles + '.dmn',
path: attrs.path || '',
contents: xml,
isUnsaved: true
};
};

Expand Down
13 changes: 12 additions & 1 deletion client/lib/external/base-dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,21 @@ function Dialog(events) {
*/
this.openError = function(err, done) {
debug('---> Dialog.openError: ', err);
// TODO: implement

// TODO(nikku): implement
done(null);
};

/**
* Display 'empty file' dialog and callback with (type).
*
* @param {String} type
* @param {Function} done
*/
this.openEmptyFile = function(type, done) {
this._open('dialog:empty-file', type, done);
};

/**
* Open a 'close' dialog and callback with (err, file).
*
Expand Down
6 changes: 1 addition & 5 deletions client/lib/util/file/is-unsaved.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
'use strict';

var defaultPath = '[unsaved]';

function isUnsaved(file) {
return !file || file.path === defaultPath;
return !file || !!file.isUnsaved;
}

isUnsaved.PATH = defaultPath;

module.exports = isUnsaved;
9 changes: 9 additions & 0 deletions client/test/helper/mock/dialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ function Dialog(events) {
this.namespaceResponse = null;
this.reimportWarningResponse = null;
this.contentChangedResponse = null;
this.emptyFileResponse = null;

this.setResponse = function(type, fileOrError) {
this[type + 'Response'] = fileOrError;
Expand Down Expand Up @@ -138,6 +139,14 @@ function Dialog(events) {
}
};

this.openEmptyFile = function(type, done) {
if (this.emptyFileResponse instanceof Error) {
done(this.emptyFileResponse);
} else {
done(null, this.emptyFileResponse);
}
};

/**
* Open a 'name' dialog and callback with (err, answer).
*
Expand Down
5 changes: 3 additions & 2 deletions client/test/helper/mock/file-system.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ function FileSystem() {
* @param {Function} done
*/
this.writeFile = function(file, done) {

if (this.writeFileResponse) {

if (this.writeFileResponse instanceof Error) {
Expand All @@ -59,10 +58,12 @@ function FileSystem() {

// make sure the file has a well defined
// path after save (expected behavior...)
if (file.path === '[unsaved]') {
if (file.path === '') {
throw new Error('incorrect file path');
}

file.isUnsaved = false;

done(null, assign({}, file));
};

Expand Down
Loading

0 comments on commit 4c4a7d5

Please sign in to comment.