Skip to content

Commit

Permalink
keyboard shortcut to sprout chains [#79 state:committed]
Browse files Browse the repository at this point in the history
  • Loading branch information
novakps committed Oct 13, 2010
1 parent d4a0620 commit 57f0992
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 106 deletions.
134 changes: 89 additions & 45 deletions kemia/controller/plugins/bond_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,34 @@ kemia.controller.plugins.BondEdit.prototype.execCommandInternal = function(
this.bond_type = arguments[1];
};

kemia.controller.plugins.BondEdit.SHORTCUTS = [ '1', '2', '3'];
kemia.controller.plugins.BondEdit.SHORTCUTS = [ {
id : '1',
key : '1'
}, {
id : '2',
key : '2'
}, {
id : '3',
key : '3'
}, {
id : '4',
key : '4'
}, {
id : '5',
key : '5'
}, {
id : '6',
key : '6'
}, {
id : '7',
key : '7'
}, {
id : '8',
key : '8'
}, {
id : '9',
key : '9'
} ];

kemia.controller.plugins.BondEdit.prototype.getKeyboardShortcuts = function() {
return kemia.controller.plugins.BondEdit.SHORTCUTS;
Expand Down Expand Up @@ -94,31 +121,48 @@ kemia.controller.plugins.BondEdit.prototype.logger = goog.debug.Logger
// }

kemia.controller.plugins.BondEdit.prototype.handleKeyboardShortcut = function(e) {

var id = e.identifier;
var shortcut = goog.array.find(kemia.controller.plugins.BondEdit.SHORTCUTS,
function(obj) {
return obj.id == e.identifier
});
if (shortcut) {
this.bond_type = shortcut;
return true;
// this.logger.info('handleKeyboardShortcut');
if(this.bond_type) {
var id = e.identifier;
var shortcut = goog.array.find(kemia.controller.plugins.BondEdit.SHORTCUTS,
function(obj) {
return obj.id == e.identifier
});
if (shortcut) {
// this.logger.info('getSelected ' + this.editorObject.getSelected().toString());
goog.array.forEach(this.editorObject.getSelected(), function(target){
// this.logger.info('target ' + target.toString());
var repeat = parseInt(shortcut.id);
var attachment_atom = target;
if (target instanceof kemia.model.Atom) {
this.editorObject.dispatchBeforeChange();
for(var i=0;i<repeat;i++){
attachment_atom = this.addBondToAtom(attachment_atom).otherAtom(attachment_atom);
}
this.editorObject.setModelsSilently(this.editorObject.getModels());
this.editorObject.dispatchChange();
return true;
}
return true;
}, this);
}
}

}

kemia.controller.plugins.BondEdit.prototype.handleMouseMove = function(e) {

if (this.bond_type) {
var target = this.editorObject.findTarget(e);
this.editorObject.clearSelected();
this.editorObject.getOriginalElement().style.cursor = 'default';
if (e.currentTarget.highlightGroup) {
e.currentTarget.highlightGroup.clear();
}
e.currentTarget.highlightGroup = undefined;

if (target instanceof kemia.model.Atom) {
this.editorObject.getOriginalElement().style.cursor = 'pointer';
this.editorObject.addSelected(target);
this.editorObject.getOriginalElement().style.cursor = 'pointer';
if (!e.currentTarget.highlightGroup) {
e.currentTarget.highlightGroup = this.highlightAtom(target);
} else {
Expand All @@ -127,7 +171,8 @@ kemia.controller.plugins.BondEdit.prototype.handleMouseMove = function(e) {
}
return true;
} else if (target instanceof kemia.model.Bond) {
this.editorObject.getOriginalElement().style.cursor = 'pointer';
this.editorObject.addSelected(target);
this.editorObject.getOriginalElement().style.cursor = 'pointer';
if (!e.currentTarget.highlightGroup) {
e.currentTarget.highlightGroup = this.highlightBond(target);
} else {
Expand All @@ -143,47 +188,46 @@ kemia.controller.plugins.BondEdit.prototype.handleMouseMove = function(e) {
kemia.controller.plugins.BondEdit.prototype.handleMouseDown = function(e) {

if (this.bond_type) {
var target = this.editorObject.findTarget(e);
goog.array.forEach(this.editorObject.getSelected(), function(target){

if (target instanceof kemia.model.Atom) {
this.editorObject.dispatchBeforeChange();
this.addBondToAtom(target);
this.editorObject.setModelsSilently(this.editorObject.getModels());
this.editorObject.dispatchChange();
return true;
}
if (target instanceof kemia.model.Bond) {
if (this.bond_type) {
if (target instanceof kemia.model.Atom) {
this.editorObject.dispatchBeforeChange();
this.replaceBond(target);
this.addBondToAtom(target);
this.editorObject.setModelsSilently(this.editorObject.getModels());
this.editorObject.dispatchChange();
return true;
} else {
if (target._last_click) {
if ((goog.now() - target._last_click) < 1000) {
this.editorObject.dispatchBeforeChange();
this.toggleBondType(target);
this.editorObject.setModelsSilently(this.editorObject
.getModels());
this.editorObject.dispatchChange();
return true;
}
if (target instanceof kemia.model.Bond) {
if (this.bond_type) {
this.editorObject.dispatchBeforeChange();
this.replaceBond(target);
this.editorObject.setModelsSilently(this.editorObject.getModels());
this.editorObject.dispatchChange();
return true;
} else {
if (target._last_click) {
if ((goog.now() - target._last_click) < 1000) {
this.editorObject.dispatchBeforeChange();
this.toggleBondType(target);
this.editorObject.setModelsSilently(this.editorObject
.getModels());
this.editorObject.dispatchChange();
return true;
}
}
target._last_click = goog.now();
}
target._last_click = goog.now();
}
}
if (target == undefined && this.bond_type) {
this.editorObject.dispatchBeforeChange();
this.createMolecule(kemia.controller.ReactionEditor
.getMouseCoords(e));
this.editorObject.setModelsSilently(this.editorObject.getModels());
this.editorObject.dispatchChange();
return true;
}

if (target == undefined && this.bond_type) {
this.editorObject.dispatchBeforeChange();
this.createMolecule(kemia.controller.ReactionEditor
.getMouseCoords(e));
this.editorObject.setModelsSilently(this.editorObject.getModels());
this.editorObject.dispatchChange();
return true;
}
}, this);
}

};

kemia.controller.plugins.BondEdit.prototype.createMolecule = function(pos) {
Expand Down
28 changes: 14 additions & 14 deletions kemia/controller/plugins/undoredo.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,20 @@ kemia.controller.plugins.UndoRedo.prototype.getKeyboardShortcuts = function() {
}

kemia.controller.plugins.UndoRedo.prototype.handleKeyboardShortcut = function(e) {
try {
var id = e.identifier;
var shortcut = goog.array.find(
kemia.controller.plugins.UndoRedo.SHORTCUTS, function(obj) {
return obj.id == e.identifier
});
if (shortcut.id == 'undo') {

this.undo();
return true;
}
} catch (e) {
this.logger.info(e);
}
// try {
// var id = e.identifier;
// var shortcut = goog.array.find(
// kemia.controller.plugins.UndoRedo.SHORTCUTS, function(obj) {
// return obj.id == e.identifier
// });
// if (shortcut.id == 'undo') {
//
// this.undo();
// return true;
// }
// } catch (e) {
// this.logger.info(e);
// }
}

/**
Expand Down
107 changes: 60 additions & 47 deletions kemia/controller/reaction_editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ goog.require('kemia.controller.Plugin');
goog.require('kemia.model.NeighborList');
goog.require('goog.ui.Prompt');
goog.require('goog.debug.Console');
// goog.require('goog.ui.KeyboardShortcutHandler');
goog.require('goog.ui.KeyboardShortcutHandler');

/**
* A graphical editor for reactions
Expand Down Expand Up @@ -85,7 +85,7 @@ kemia.controller.ReactionEditor = function(element, opt_config) {
* @protected
*/
this.eventRegister = new goog.events.EventHandler(this);
// this.shortcutHandler = new goog.ui.KeyboardShortcutHandler(document);
this.shortcutHandler = new goog.ui.KeyboardShortcutHandler(document);

// Wrappers around this editor, to be disposed when the editor is disposed.
this.wrappers_ = [];
Expand Down Expand Up @@ -117,6 +117,18 @@ kemia.controller.ReactionEditor.setActiveEditorId = function(editorId) {
kemia.controller.ReactionEditor.activeEditorId_ = editorId;
};

kemia.controller.ReactionEditor.prototype.clearSelected = function() {
this.selected.length = 0;
}

kemia.controller.ReactionEditor.prototype.getSelected = function(){
return this.selected;
}

kemia.controller.ReactionEditor.prototype.addSelected = function(obj){
this.selected.push(obj);
}

/**
* @return {goog.dom.DomHelper?} The dom helper for the editable node.
*/
Expand Down Expand Up @@ -288,45 +300,45 @@ kemia.controller.ReactionEditor.prototype.handleChange = function() {
// * e The browser event.
// * @private
// */
// kemia.controller.ReactionEditor.prototype.handleKeyDown_ = function(e) {
// this.logger.info('handleKeyDown_');
// if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
// if (!this.handleBeforeChangeKeyEvent_(e)) {
// return;
// }
// }
//
// if (!this.invokeShortCircuitingOp_(kemia.controller.Plugin.Op.KEYDOWN, e)
// && goog.editor.BrowserFeature.USES_KEYDOWN) {
// this.handleKeyboardShortcut_(e);
// }
// };
//
//kemia.controller.ReactionEditor.prototype.handleKeyDown_ = function(e) {
// this.logger.info('handleKeyDown_');
// if (!goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
// if (!this.handleBeforeChangeKeyEvent_(e)) {
// return;
// }
// }

// if (!this.invokeShortCircuitingOp_(kemia.controller.Plugin.Op.KEYDOWN, e)
// && goog.editor.BrowserFeature.USES_KEYDOWN) {
// this.handleKeyboardShortcut_(e);
// }
//};

// /**
// * Handles keypress on the field.
// *
// * @param {goog.events.BrowserEvent}
// * e The browser event.
// * @private
// */
// kemia.controller.ReactionEditor.prototype.handleKeyPress_ = function(e) {
// this.logger.info('handleKeyPress_');
// if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
// if (!this.handleBeforeChangeKeyEvent_(e)) {
// return;
// }
// } else {
// // In IE only keys that generate output trigger keypress
// // In Mozilla charCode is set for keys generating content.
// this.gotGeneratingKey_ = true;
// this.dispatchBeforeChange();
// }
//
// if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYPRESS, e)
// && !goog.editor.BrowserFeature.USES_KEYDOWN) {
// this.handleKeyboardShortcut_(e);
// }
// };
// * Handles keypress on the field.
// *
// * @param {goog.events.BrowserEvent}
// * e The browser event.
// * @private
// */
//kemia.controller.ReactionEditor.prototype.handleKeyPress_ = function(e) {
// this.logger.info('handleKeyPress_');
// if (goog.editor.BrowserFeature.USE_MUTATION_EVENTS) {
// if (!this.handleBeforeChangeKeyEvent_(e)) {
// return;
// }
// } else {
// // In IE only keys that generate output trigger keypress
// // In Mozilla charCode is set for keys generating content.
// this.gotGeneratingKey_ = true;
// this.dispatchBeforeChange();
// }

// if (!this.invokeShortCircuitingOp_(goog.editor.Plugin.Op.KEYPRESS, e)
// && !goog.editor.BrowserFeature.USES_KEYDOWN) {
// this.handleKeyboardShortcut_(e);
// }
//};

// /**
// * Handles keyup on the editor.
Expand Down Expand Up @@ -501,6 +513,7 @@ kemia.controller.ReactionEditor.prototype.handleMouseUp_ = function(e) {
// };

kemia.controller.ReactionEditor.prototype.handleKeyboardShortcut_ = function(e) {
// this.logger.info('handelKeyboardShortcut_ ' + e.identifier);
this.invokeShortCircuitingOp_(kemia.controller.Plugin.Op.SHORTCUT, e);
}

Expand Down Expand Up @@ -933,18 +946,18 @@ kemia.controller.ReactionEditor.prototype.addListener = function(type,
*/
kemia.controller.ReactionEditor.prototype.setupChangeListeners_ = function() {

// this.addListener(goog.events.EventType.KEYDOWN, this.handleKeyDown_);
// this.addListener(goog.events.EventType.KEYPRESS, this.handleKeyPress_);
// this.addListener(goog.events.EventType.KEYUP, this.handleKeyUp_);
this.addListener(goog.events.EventType.KEYDOWN, this.handleKeyDown_);
this.addListener(goog.events.EventType.KEYPRESS, this.handleKeyPress_);
this.addListener(goog.events.EventType.KEYUP, this.handleKeyUp_);
this.addListener(goog.events.EventType.MOUSEDOWN, this.handleMouseDown_);
this.addListener(goog.events.EventType.MOUSEMOVE, this.handleMouseMove_);
this.addListener(goog.events.EventType.MOUSEUP, this.handleMouseUp_);
// this.addListener(goog.events.EventType.DBLCLICK, this.handleDblclick_);
this.addListener('paste', this.handlePaste_);

// goog.events.listen(this.shortcutHandler,
// goog.ui.KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED,
// this.handleKeyboardShortcut_, undefined, this);
goog.events.listen(this.shortcutHandler,
goog.ui.KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED,
this.handleKeyboardShortcut_, undefined, this);

};
/*
Expand All @@ -958,7 +971,7 @@ kemia.controller.ReactionEditor.prototype.setupChangeListeners_ = function() {
* goog.ui.KeyboardShortcutHandler.Modifiers.SHIFT, CONTROL, ALT, or META.
*/
kemia.controller.ReactionEditor.prototype.registerShortcut = function(id, key) {
// this.shortcutHandler.registerShortcut(id, key);
this.shortcutHandler.registerShortcut(id, key);
};

/**
Expand Down

0 comments on commit 57f0992

Please sign in to comment.