Skip to content

Commit

Permalink
modified button configuration syntax per feedback from author; added …
Browse files Browse the repository at this point in the history
…tests
  • Loading branch information
dobozysaurus committed Mar 5, 2013
1 parent 46544a7 commit 60894e9
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
@@ -1,3 +1,4 @@
.DS_STORE
.idea/
docs/downloads/*/
docs/downloads/*/
node_modules
22 changes: 15 additions & 7 deletions epiceditor/js/epiceditor.js
Expand Up @@ -51,7 +51,7 @@
}

/**
* Saves the current style state for the styles requested, then applys styles
* Saves the current style state for the styles requested, then applies styles
* to overwrite the existing one. The old styles are returned as an object so
* you can pass it back in when you want to revert back to the old style
* @param {object} el The element to get the styles of
Expand Down Expand Up @@ -328,10 +328,16 @@
, toggleFullscreen: 'Enter Fullscreen'
}
, parser: typeof marked == 'function' ? marked : null
, buttons: { fullscreen: true, preview: true }
}
, defaultStorage;

self.settings = _mergeObjs(true, defaults, opts);

var buttons = self.settings.buttons;
self._fullscreenEnabled = typeof(buttons) === 'object' ? typeof buttons.fullscreen === 'undefined' || buttons.fullscreen : buttons === true;
self._editEnabled = typeof(buttons) === 'object' ? typeof buttons.edit === 'undefined' || buttons.edit : buttons === true;
self._previewEnabled = typeof(buttons) === 'object' ? typeof buttons.preview === 'undefined' || buttons.preview : buttons === true;

if (!(typeof self.settings.parser == 'function' && typeof self.settings.parser('TEST') == 'string')) {
self.settings.parser = function (str) {
Expand Down Expand Up @@ -490,6 +496,8 @@
}

callback = callback || function () {};

var buttons = self.settings.buttons;

// The editor HTML
// TODO: edit-mode class should be dynamically added
Expand All @@ -499,9 +507,9 @@
'<iframe frameborder="0" id="epiceditor-editor-frame"></iframe>' +
'<iframe frameborder="0" id="epiceditor-previewer-frame"></iframe>' +
'<div id="epiceditor-utilbar">' +
'<img width="30" src="' + this.settings.basePath + '/images/preview.png" title="' + this.settings.string.togglePreview + '" class="epiceditor-toggle-btn epiceditor-toggle-preview-btn"> ' +
'<img width="30" src="' + this.settings.basePath + '/images/edit.png" title="' + this.settings.string.toggleEdit + '" class="epiceditor-toggle-btn epiceditor-toggle-edit-btn"> ' +
'<img width="30" src="' + this.settings.basePath + '/images/fullscreen.png" title="' + this.settings.string.toggleFullscreen + '" class="epiceditor-fullscreen-btn">' +
(self._previewEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/preview.png" title="' + this.settings.string.togglePreview + '" class="epiceditor-toggle-btn epiceditor-toggle-preview-btn"> ' : '') +
(self._editEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/edit.png" title="' + this.settings.string.toggleEdit + '" class="epiceditor-toggle-btn epiceditor-toggle-edit-btn"> ' : '') +
(self._fullscreenEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/fullscreen.png" title="' + this.settings.string.toggleFullscreen + '" class="epiceditor-fullscreen-btn">' : '') +
'</div>' +
'</div>'

Expand Down Expand Up @@ -832,15 +840,15 @@
// Check for alt+p and make sure were not in fullscreen - default shortcut to switch to preview
if (isMod === true && e.keyCode == self.settings.shortcut.preview && !self.is('fullscreen')) {
e.preventDefault();
if (self.is('edit')) {
if (self.is('edit') && self._previewEnabled) {
self.preview();
}
else {
else if (self._editEnabled) {
self.edit();
}
}
// Check for alt+f - default shortcut to make editor fullscreen
if (isMod === true && e.keyCode == self.settings.shortcut.fullscreen) {
if (isMod === true && e.keyCode == self.settings.shortcut.fullscreen && self._fullscreenEnabled) {
e.preventDefault();
self._goFullscreen(fsElement);
}
Expand Down
2 changes: 1 addition & 1 deletion epiceditor/js/epiceditor.min.js

Large diffs are not rendered by default.

21 changes: 14 additions & 7 deletions src/editor.js
Expand Up @@ -328,11 +328,16 @@
, toggleFullscreen: 'Enter Fullscreen'
}
, parser: typeof marked == 'function' ? marked : null
, fullscreenEnabled: true
, buttons: { fullscreen: true, preview: true }
}
, defaultStorage;

self.settings = _mergeObjs(true, defaults, opts);

var buttons = self.settings.buttons;
self._fullscreenEnabled = typeof(buttons) === 'object' ? typeof buttons.fullscreen === 'undefined' || buttons.fullscreen : buttons === true;
self._editEnabled = typeof(buttons) === 'object' ? typeof buttons.edit === 'undefined' || buttons.edit : buttons === true;
self._previewEnabled = typeof(buttons) === 'object' ? typeof buttons.preview === 'undefined' || buttons.preview : buttons === true;

if (!(typeof self.settings.parser == 'function' && typeof self.settings.parser('TEST') == 'string')) {
self.settings.parser = function (str) {
Expand Down Expand Up @@ -491,6 +496,8 @@
}

callback = callback || function () {};

var buttons = self.settings.buttons;

// The editor HTML
// TODO: edit-mode class should be dynamically added
Expand All @@ -500,9 +507,9 @@
'<iframe frameborder="0" id="epiceditor-editor-frame"></iframe>' +
'<iframe frameborder="0" id="epiceditor-previewer-frame"></iframe>' +
'<div id="epiceditor-utilbar">' +
'<img width="30" src="' + this.settings.basePath + '/images/preview.png" title="' + this.settings.string.togglePreview + '" class="epiceditor-toggle-btn epiceditor-toggle-preview-btn"> ' +
'<img width="30" src="' + this.settings.basePath + '/images/edit.png" title="' + this.settings.string.toggleEdit + '" class="epiceditor-toggle-btn epiceditor-toggle-edit-btn"> ' +
(self.settings.fullscreenEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/fullscreen.png" title="' + this.settings.string.toggleFullscreen + '" class="epiceditor-fullscreen-btn">' : '') +
(self._previewEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/preview.png" title="' + this.settings.string.togglePreview + '" class="epiceditor-toggle-btn epiceditor-toggle-preview-btn"> ' : '') +
(self._editEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/edit.png" title="' + this.settings.string.toggleEdit + '" class="epiceditor-toggle-btn epiceditor-toggle-edit-btn"> ' : '') +
(self._fullscreenEnabled ? '<img width="30" src="' + this.settings.basePath + '/images/fullscreen.png" title="' + this.settings.string.toggleFullscreen + '" class="epiceditor-fullscreen-btn">' : '') +
'</div>' +
'</div>'

Expand Down Expand Up @@ -833,15 +840,15 @@
// Check for alt+p and make sure were not in fullscreen - default shortcut to switch to preview
if (isMod === true && e.keyCode == self.settings.shortcut.preview && !self.is('fullscreen')) {
e.preventDefault();
if (self.is('edit')) {
if (self.is('edit') && self._previewEnabled) {
self.preview();
}
else {
else if (self._editEnabled) {
self.edit();
}
}
// Check for alt+f - default shortcut to make editor fullscreen
if (isMod === true && e.keyCode == self.settings.shortcut.fullscreen && self.settings.fullscreenEnabled) {
if (isMod === true && e.keyCode == self.settings.shortcut.fullscreen && self._fullscreenEnabled) {
e.preventDefault();
self._goFullscreen(fsElement);
}
Expand Down
59 changes: 59 additions & 0 deletions test/test.options.js
Expand Up @@ -27,6 +27,65 @@ describe('EpicEditor([options])', function () {
removeContainer(id);
done();
});

describe('options.buttons', function () {
it('should have all buttons enabled by default', function () {
editor = new EpicEditor(opts).load();
expect(editor._fullscreenEnabled).to.be(true);
expect(editor._previewEnabled).to.be(true);
expect(editor._editEnabled).to.be(true);
var wrapper = editor.getElement('wrapper');
expect(wrapper.getElementsByClassName('epiceditor-fullscreen-btn').length)
.to.equal(1);
expect(wrapper.getElementsByClassName('epiceditor-toggle-preview-btn').length)
.to.equal(1);
expect(wrapper.getElementsByClassName('epiceditor-toggle-edit-btn').length)
.to.equal(1);
});
it('should disable all buttons if the buttons config is set to false', function () {
opts.buttons = false;
editor = new EpicEditor(opts).load();
expect(editor._fullscreenEnabled).to.be(false);
expect(editor._previewEnabled).to.be(false);
expect(editor._editEnabled).to.be(false);
var wrapper = editor.getElement('wrapper');
expect(wrapper.getElementsByClassName('epiceditor-fullscreen-btn').length)
.to.equal(0);
expect(wrapper.getElementsByClassName('epiceditor-toggle-preview-btn').length)
.to.equal(0);
expect(wrapper.getElementsByClassName('epiceditor-toggle-edit-btn').length)
.to.equal(0);
});
it('should enable all buttons if the buttons config is set to true', function () {
opts.buttons = true;
editor = new EpicEditor(opts).load();
expect(editor._fullscreenEnabled).to.be(true);
expect(editor._previewEnabled).to.be(true);
expect(editor._editEnabled).to.be(true);
var wrapper = editor.getElement('wrapper');
expect(wrapper.getElementsByClassName('epiceditor-fullscreen-btn').length)
.to.equal(1);
expect(wrapper.getElementsByClassName('epiceditor-toggle-preview-btn').length)
.to.equal(1);
expect(wrapper.getElementsByClassName('epiceditor-toggle-edit-btn').length)
.to.equal(1);
});
it('should properly merge configs if none are specified', function () {
// if no specific value for a button is specified, assume that it is true.
opts.buttons = { fullscreen: false };
editor = new EpicEditor(opts).load();
expect(editor._fullscreenEnabled).to.be(false);
expect(editor._previewEnabled).to.be(true);
expect(editor._editEnabled).to.be(true);
var wrapper = editor.getElement('wrapper');
expect(wrapper.getElementsByClassName('epiceditor-fullscreen-btn').length)
.to.equal(0);
expect(wrapper.getElementsByClassName('epiceditor-toggle-preview-btn').length)
.to.equal(1);
expect(wrapper.getElementsByClassName('epiceditor-toggle-edit-btn').length)
.to.equal(1);
});
});

it('should allow the container option to be passed as an element ID string', function () {
opts.container = id;
Expand Down

0 comments on commit 60894e9

Please sign in to comment.